X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=plugins%2Fdemo_plugin%2Fdemo_plugin.php;h=ca520d15147ce937302eb99d0a15c38c1721e2fb;hb=9d9f6d75b94aab51067bdfbe50b58b66d1194f6d;hp=7335c9d4665a0429549d7fd3977ca961f81e58fa;hpb=209aa96e7e32d810db30d2eb437cef2280c5cad6;p=github%2Fshaarli%2FShaarli.git diff --git a/plugins/demo_plugin/demo_plugin.php b/plugins/demo_plugin/demo_plugin.php index 7335c9d4..ca520d15 100644 --- a/plugins/demo_plugin/demo_plugin.php +++ b/plugins/demo_plugin/demo_plugin.php @@ -14,6 +14,26 @@ * and check user status with _LOGGEDIN_. */ +use Shaarli\Config\ConfigManager; + +/** + * In the footer hook, there is a working example of a translation extension for Shaarli. + * + * The extension must be attached to a new translation domain (i.e. NOT 'shaarli'). + * Use case: any custom theme or non official plugin can use the translation system. + * + * See the documentation for more information. + */ +const EXT_TRANSLATION_DOMAIN = 'demo'; + +/* + * This is not necessary, but it's easier if you don't want Poedit to mix up your translations. + */ +function demo_plugin_t($text, $nText = '', $nb = 1) +{ + return t($text, $nText, $nb, EXT_TRANSLATION_DOMAIN); +} + /** * Initialization function. * It will be called when the plugin is loaded. @@ -27,6 +47,12 @@ function demo_plugin_init($conf) { $conf->get('toto', 'nope'); + if (! $conf->exists('translation.extensions.demo')) { + // Custom translation with the domain 'demo' + $conf->set('translation.extensions.demo', 'plugins/demo_plugin/languages/'); + $conf->write(true); + } + $errors[] = 'This a demo init error.'; return $errors; } @@ -47,18 +73,70 @@ function hook_demo_plugin_render_header($data) { // Only execute when linklist is rendered. if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) { - // If loggedin if ($data['_LOGGEDIN_'] === true) { - // Buttons in toolbar - $data['buttons_toolbar'][] = '
  • DEMO_buttons_toolbar
  • '; + /* + * Links in toolbar: + * A link is an array of its attributes (key="value"), + * and a mandatory `html` key, which contains its value. + */ + $button = array( + 'attr' => array ( + 'href' => '#', + 'class' => 'mybutton', + 'title' => 'hover me', + ), + 'html' => 'DEMO buttons toolbar', + ); + $data['buttons_toolbar'][] = $button; } - // Fields in toolbar - $data['fields_toolbar'][] = 'DEMO_fields_toolbar'; + /* + * Add additional input fields in the tools. + * A field is an array containing: + * [ + * 'form-attribute-1' => 'form attribute 1 value', + * 'form-attribute-2' => 'form attribute 2 value', + * 'inputs' => [ + * [ + * 'input-1-attribute-1 => 'input 1 attribute 1 value', + * 'input-1-attribute-2 => 'input 1 attribute 2 value', + * ], + * [ + * 'input-2-attribute-1 => 'input 2 attribute 1 value', + * ], + * ], + * ] + * This example renders as: + *
    + * + * + *
    + */ + $form = array( + 'attr' => array( + 'method' => 'GET', + 'action' => '?', + 'class' => 'addform', + ), + 'inputs' => array( + array( + 'type' => 'text', + 'name' => 'demo', + 'placeholder' => 'demo', + ) + ) + ); + $data['fields_toolbar'][] = $form; } // Another button always displayed - $data['buttons_toolbar'][] = '
  • DEMO
  • '; + $button = array( + 'attr' => array( + 'href' => '#', + ), + 'html' => 'Demo', + ); + $data['buttons_toolbar'][] = $button; return $data; } @@ -107,7 +185,7 @@ function hook_demo_plugin_render_includes($data) function hook_demo_plugin_render_footer($data) { // footer text - $data['text'][] = 'Shaarli is now enhanced by the awesome demo_plugin.'; + $data['text'][] = '
    '. demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.'); // Free elements at the end of the page. $data['endofpage'][] = '' . @@ -143,8 +221,19 @@ function hook_demo_plugin_render_footer($data) */ function hook_demo_plugin_render_linklist($data) { - // action_plugin - $data['action_plugin'][] = '
    ←
    '; + /* + * Action links (action_plugin): + * A link is an array of its attributes (key="value"), + * and a mandatory `html` key, which contains its value. + * It's also recommended to add key 'on' or 'off' for theme rendering. + */ + $action = array( + 'attr' => array( + 'href' => '?up', + 'title' => 'Uppercase!', + ), + 'html' => '←', + ); if (isset($_GET['up'])) { // Manipulate link data @@ -152,7 +241,11 @@ function hook_demo_plugin_render_linklist($data) $value['description'] = strtoupper($value['description']); $value['title'] = strtoupper($value['title']); } + $action['on'] = true; + } else { + $action['off'] = true; } + $data['action_plugin'][] = $action; // link_plugin (for each link) foreach ($data['links'] as &$value) { @@ -284,17 +377,13 @@ function hook_demo_plugin_render_daily($data) // Manipulate columns data - foreach ($data['cols'] as &$value) { - foreach ($value as &$value2) { - $value2['formatedDescription'] .= ' ಠ_ಠ'; - } + foreach ($data['linksToDisplay'] as &$value) { + $value['formatedDescription'] .= ' ಠ_ಠ'; } // Add plugin content at the end of each link - foreach ($data['cols'] as &$value) { - foreach ($value as &$value2) { - $value2['link_plugin'][] = 'DEMO'; - } + foreach ($data['linksToDisplay'] as &$value) { + $value['link_plugin'][] = 'DEMO'; } return $data; @@ -358,10 +447,18 @@ function hook_demo_plugin_render_feed($data) foreach ($data['links'] as &$link) { if ($data['_PAGE_'] == Router::$PAGE_FEED_ATOM) { $link['description'] .= ' - ATOM Feed' ; - } - elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) { + } elseif ($data['_PAGE_'] == Router::$PAGE_FEED_RSS) { $link['description'] .= ' - RSS Feed'; } } return $data; } + +/** + * This function is never called, but contains translation calls for GNU gettext extraction. + */ +function demo_dummy_translation() +{ + // meta + t('A demo plugin covering all use cases for template designers and plugin developers.'); +}