X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=plugins%2Fdemo_plugin%2Fdemo_plugin.php;h=d89765cfcb2ca20bfbf34a4cda8a592669b73524;hb=bcba6bd353161fab456b423e93571ab027d5423c;hp=f5f028e023c68a583ff12c5e7c0837006655492b;hpb=79851b489087f8a3027ecd805255cd13ee6fcf63;p=github%2Fshaarli%2FShaarli.git diff --git a/plugins/demo_plugin/demo_plugin.php b/plugins/demo_plugin/demo_plugin.php index f5f028e0..d89765cf 100644 --- a/plugins/demo_plugin/demo_plugin.php +++ b/plugins/demo_plugin/demo_plugin.php @@ -1,11 +1,14 @@ 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; +} + +function demo_plugin_register_routes(): array +{ + return [ + [ + 'method' => 'GET', + 'route' => '/custom', + 'callable' => 'Shaarli\DemoPlugin\DemoPluginController:index', + ], + ]; +} + /** * Hook render_header. - * Executed on every page redering. + * Executed on every page render. * * Template placeholders: * - buttons_toolbar @@ -29,26 +89,78 @@ function hook_demo_plugin_render_header($data) { // Only execute when linklist is rendered. - if ($data['_PAGE_'] == Router::$PAGE_LINKLIST) { - + if ($data['_PAGE_'] == TemplatePage::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 = [ + 'attr' => [ + '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 = [ + 'attr' => [ + 'method' => 'GET', + 'action' => $data['_BASE_PATH_'] . '/', + 'class' => 'addform', + ], + 'inputs' => [ + [ + 'type' => 'text', + 'name' => 'demo', + 'placeholder' => 'demo', + ] + ] + ]; + $data['fields_toolbar'][] = $form; } // Another button always displayed - $data['buttons_toolbar'][] = '
  • DEMO
  • '; + $button = [ + 'attr' => [ + 'href' => '#', + ], + 'html' => 'Demo', + ]; + $data['buttons_toolbar'][] = $button; return $data; } /** * Hook render_includes. - * Executed on every page redering. + * Executed on every page render. * * Template placeholders: * - css_files @@ -72,7 +184,7 @@ function hook_demo_plugin_render_includes($data) /** * Hook render_footer. - * Executed on every page redering. + * Executed on every page render. * * Template placeholders: * - text @@ -89,8 +201,8 @@ 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.'; + // Footer text + $data['text'][] = '
    ' . demo_plugin_t('Shaarli is now enhanced by the awesome demo_plugin.'); // Free elements at the end of the page. $data['endofpage'][] = '' . @@ -126,8 +238,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 = [ + 'attr' => [ + 'href' => '?up', + 'title' => 'Uppercase!', + ], + 'html' => '←', + ]; if (isset($_GET['up'])) { // Manipulate link data @@ -135,7 +258,22 @@ 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; + + // Action to trigger custom filter hiding bookmarks not containing 'e' letter in their description + $action = [ + 'attr' => [ + 'href' => '?e', + 'title' => 'Hide bookmarks without "e" in their description.', + ], + 'html' => 'e', + 'on' => isset($_GET['e']) + ]; + $data['action_plugin'][] = $action; // link_plugin (for each link) foreach ($data['links'] as &$value) { @@ -163,9 +301,9 @@ function hook_demo_plugin_render_linklist($data) function hook_demo_plugin_render_editlink($data) { // Load HTML into a string - $html = file_get_contents(PluginManager::$PLUGINS_PATH .'/demo_plugin/field.html'); + $html = file_get_contents(PluginManager::$PLUGINS_PATH . '/demo_plugin/field.html'); - // replace value in HTML if it exists in $data + // Replace value in HTML if it exists in $data if (!empty($data['link']['stuff'])) { $html = sprintf($html, $data['link']['stuff']); } else { @@ -191,7 +329,11 @@ function hook_demo_plugin_render_editlink($data) function hook_demo_plugin_render_tools($data) { // field_plugin - $data['tools_plugin'][] = 'tools_plugin'; + $data['tools_plugin'][] = '
    + + Demo Plugin Custom Route + +
    '; return $data; } @@ -212,9 +354,7 @@ function hook_demo_plugin_render_tools($data) */ function hook_demo_plugin_render_picwall($data) { - // plugin_start_zone $data['plugin_start_zone'][] = '
    BEFORE
    '; - // plugin_end_zone $data['plugin_end_zone'][] = '
    AFTER
    '; return $data; @@ -236,9 +376,7 @@ function hook_demo_plugin_render_picwall($data) */ function hook_demo_plugin_render_tagcloud($data) { - // plugin_start_zone $data['plugin_start_zone'][] = '
    BEFORE
    '; - // plugin_end_zone $data['plugin_end_zone'][] = '
    AFTER
    '; return $data; @@ -260,24 +398,18 @@ function hook_demo_plugin_render_tagcloud($data) */ function hook_demo_plugin_render_daily($data) { - // plugin_start_zone $data['plugin_start_zone'][] = '
    BEFORE
    '; - // plugin_end_zone $data['plugin_end_zone'][] = '
    AFTER
    '; // 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; @@ -322,4 +454,78 @@ function hook_demo_plugin_delete_link($data) if (strpos($data['url'], 'youtube.com') !== false) { exit('You can not delete a YouTube link. Don\'t ask.'); } -} \ No newline at end of file +} + +/** + * Execute render_feed hook. + * Called with ATOM and RSS feed. + * + * Special data keys: + * - _PAGE_: current page + * - _LOGGEDIN_: true/false + * + * @param array $data data passed to plugin + * + * @return array altered $data. + */ +function hook_demo_plugin_render_feed($data) +{ + foreach ($data['links'] as &$link) { + if ($data['_PAGE_'] == TemplatePage::FEED_ATOM) { + $link['description'] .= ' - ATOM Feed' ; + } elseif ($data['_PAGE_'] == TemplatePage::FEED_RSS) { + $link['description'] .= ' - RSS Feed'; + } + } + return $data; +} + +/** + * When plugin parameters are saved. + * + * @param array $data $_POST array + * + * @return array Updated $_POST array + */ +function hook_demo_plugin_save_plugin_parameters($data) +{ + // Here we edit the provided value. + // This hook can also be used to generate config files, etc. + if (! empty($data['DEMO_PLUGIN_PARAMETER']) && ! endsWith($data['DEMO_PLUGIN_PARAMETER'], '_SUFFIX')) { + $data['DEMO_PLUGIN_PARAMETER'] .= '_SUFFIX'; + } + + return $data; +} + +/** + * This hook is called when a search is performed, on every search entry. + * It allows to add custom filters, and filter out additional link. + * + * For exemple here, we hide all bookmarks not containing the letter 'e' in their description. + * + * @param Bookmark $bookmark Search entry. Note that this is a Bookmark object, and not a link array. + * It should NOT be altered. + * @param array $context Additional info on the search performed. + * + * @return bool True if the bookmark should be kept in the search result, false to discard it. + */ +function hook_demo_plugin_filter_search_entry(Bookmark $bookmark, array $context): bool +{ + if (isset($_GET['e'])) { + return strpos($bookmark->getDescription(), 'e') !== false; + } + + return true; +} + +/** + * 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.'); + t('This is a parameter dedicated to the demo plugin. It\'ll be suffixed.'); + t('Other demo parameter'); +}