From 83bba9954aa2b0b19a938b5061c86b70f3dbe507 Mon Sep 17 00:00:00 2001 From: vv221 Date: Fri, 3 Apr 2020 22:37:30 +0200 Subject: [PATCH] On children pages, show links to other children pages and back to the main parent page --- MenuItem.php | 41 ++++++++++++++++++++++++++++++----------- action.php | 34 +++++++++++++++++----------------- lang/en/lang.php | 2 ++ 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/MenuItem.php b/MenuItem.php index 1fd401e..b11abd8 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -10,21 +10,28 @@ class MenuItem extends AbstractItem { * * @param string $type * @param string $label + * @param bool $strip_namespace */ - public function __construct(string $type, string $label = '') { + public function __construct( + string $type, + string $label = '', + bool $strip_namespace = false + ) { $this->type = $type; if ( empty($label) ) { $label = ucfirst($type); } $this->label = $label; parent::__construct(); - $this->setTargetFromType(); + $this->setTargetFromType($strip_namespace); } /** * Set the item target link from its type + * + * @param bool $strip_namespace */ - protected function setTargetFromType() : void { + protected function setTargetFromType(bool $strip_namespace) : void { global $INFO; global $plugin_controller; $language = null; @@ -36,16 +43,28 @@ class MenuItem extends AbstractItem { $translation_plugin =& $plugin_controller->load('helper', 'translation'); $language = $translation_plugin->getLangPart($INFO['id']); } + // Split path to page + $page_path = explode(':', $INFO['id']); // If the top level namespace is a language one, the children namespace should be inserted inside it - if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) { - $this->id = preg_replace( - "/^$language:/", - "$language:".$this->type.':', - $INFO['id'] - ); - } else { - $this->id = $this->type.':'.$INFO['id']; + $is_in_a_lang_namespace = ( $language !== null && $page_path[0] === $language ); + if ( $is_in_a_lang_namespace ) { + array_shift($page_path); } + // Strip the top level namespace if we are already on a child page + if ( $strip_namespace ) { + array_shift($page_path); + } + // Build the link target path + $target_path = []; + if ( $is_in_a_lang_namespace ) { + $target_path[] = $language; + } + // Add namespace of the target child page, unless the special value "_main" has been used + // "_main" is used to generate a link back to the main parent page + if ( $this->type !== '_main' ) { + $target_path[] = $this->type; + } + $this->id = implode(':', array_merge($target_path, $page_path)); $this->params = []; } } diff --git a/action.php b/action.php index 9062349..96de36e 100644 --- a/action.php +++ b/action.php @@ -40,16 +40,20 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { if ( $event->data['view'] !== 'page' ) { return; } - // Only add links if the current page is not included in a reserved namespace + // If the current page is included in a reserved namespace, add a link back to main page $children_types = $this->getConf('children_list'); $top_namespace = $this->getTopLevelNamespace(); - if ( in_array($top_namespace, $children_types) ) { - return; + $is_child_page = in_array($top_namespace, $children_types); + if ( $is_child_page ) { + $main_label = $this->getLang('btn_main'); + $this->addMenuItem($event, '_main', $main_label, $top_namespace); } // Add menu items for each child page foreach ( $children_types as $child_type ) { - $child_label = $this->getLang("btn_$child_type"); - $this->addMenuItem($event, $child_type, $child_label); + if ( $child_type !== $top_namespace ) { + $child_label = $this->getLang("btn_$child_type"); + $this->addMenuItem($event, $child_type, $child_label, $is_child_page); + } } } @@ -59,19 +63,15 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { * @param Doku_Event $event * @param string $type * @param string $name + * @param bool $strip_namespace */ - protected function addMenuItem(Doku_Event $event, string $type, string $name = '') { - $item = $this->generateMenuItem($type, $name); - $event->data['items'][] = $item; - } - - /** - * Generate a new menu item - * - * @param string $type - */ - protected function generateMenuItem(string $type, string $name = '') { - return new MenuItem($type, $name); + protected function addMenuItem( + Doku_Event $event, + string $type, + string $name = '', + bool $strip_namespace = false + ) { + $event->data['items'][] = new MenuItem($type, $name, $strip_namespace); } /** diff --git a/lang/en/lang.php b/lang/en/lang.php index 7be973b..b737008 100644 --- a/lang/en/lang.php +++ b/lang/en/lang.php @@ -1,5 +1,7 @@