2020-03-30 21:32:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-04-03 21:25:51 +00:00
|
|
|
* Children pages plugin: Shows links to children pages in the page menu
|
2020-03-30 21:32:07 +00:00
|
|
|
*
|
|
|
|
* @license BSD 2-Clause
|
2024-04-24 18:06:28 +00:00
|
|
|
* @author Antoine Le Gonidec <vv221.dokuwiki@vv221.fr>
|
2020-03-30 21:32:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// must be run within Dokuwiki
|
|
|
|
if ( ! defined('DOKU_INC') ) {
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2020-04-02 23:15:41 +00:00
|
|
|
use dokuwiki\plugin\childrenpages\MenuItem;
|
|
|
|
|
2020-04-02 22:50:50 +00:00
|
|
|
class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
|
2020-03-30 21:32:07 +00:00
|
|
|
/**
|
|
|
|
* Registers a callback function for a given event
|
|
|
|
*
|
|
|
|
* @param Doku_Event_Handler $controller
|
|
|
|
*/
|
2020-04-02 22:04:45 +00:00
|
|
|
public function register(Doku_Event_Handler $controller) : void {
|
2020-04-02 23:15:41 +00:00
|
|
|
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuItems');
|
2020-04-02 21:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new items to the page menu
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event
|
|
|
|
*/
|
2020-04-02 23:15:41 +00:00
|
|
|
public function addMenuItems(Doku_Event $event) : void {
|
2020-04-02 21:48:40 +00:00
|
|
|
// Check that this method has been called in the expected context
|
|
|
|
if ( $event->name !== 'MENU_ITEMS_ASSEMBLY' ) {
|
2020-04-03 21:25:51 +00:00
|
|
|
$message = "Children pages plugin error:";
|
2020-04-02 21:48:40 +00:00
|
|
|
$message .= "addMenuItem method should only be called by \"MENU_ITEMS_ASSEMBLY\" event";
|
|
|
|
$message .= ", but it has been called by \"$event->name\".";
|
|
|
|
throw new Exception($message);
|
|
|
|
}
|
2020-04-02 22:04:45 +00:00
|
|
|
// Only add content to the page menu
|
|
|
|
if ( $event->data['view'] !== 'page' ) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-28 11:26:30 +00:00
|
|
|
// Check if all links should be shown, or if the one to the current page shoud be excluded
|
|
|
|
$show_all_links = (bool) $this->getConf('show_link_to_current_page');
|
2020-04-03 20:37:30 +00:00
|
|
|
// If the current page is included in a reserved namespace, add a link back to main page
|
2020-04-03 00:46:35 +00:00
|
|
|
$children_types = $this->getConf('children_list');
|
2020-04-03 03:32:33 +00:00
|
|
|
$top_namespace = $this->getTopLevelNamespace();
|
2020-04-03 20:37:30 +00:00
|
|
|
$is_child_page = in_array($top_namespace, $children_types);
|
2020-04-28 11:26:30 +00:00
|
|
|
if ( $show_all_links || $is_child_page ) {
|
2020-04-03 20:37:30 +00:00
|
|
|
$main_label = $this->getLang('btn_main');
|
2020-05-01 18:20:18 +00:00
|
|
|
$this->addMenuItem($event, '_main', $main_label, $is_child_page);
|
2020-04-03 00:19:57 +00:00
|
|
|
}
|
|
|
|
// Add menu items for each child page
|
2020-04-02 23:44:50 +00:00
|
|
|
foreach ( $children_types as $child_type ) {
|
2020-04-28 11:26:30 +00:00
|
|
|
if ( $show_all_links || $child_type !== $top_namespace ) {
|
2020-04-03 20:37:30 +00:00
|
|
|
$child_label = $this->getLang("btn_$child_type");
|
|
|
|
$this->addMenuItem($event, $child_type, $child_label, $is_child_page);
|
|
|
|
}
|
2020-04-02 23:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new item to the page menu
|
|
|
|
*
|
|
|
|
* @param Doku_Event $event
|
2020-04-02 23:44:50 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param string $name
|
2020-04-03 20:37:30 +00:00
|
|
|
* @param bool $strip_namespace
|
2020-04-02 23:15:41 +00:00
|
|
|
*/
|
2020-04-03 20:37:30 +00:00
|
|
|
protected function addMenuItem(
|
|
|
|
Doku_Event $event,
|
|
|
|
string $type,
|
|
|
|
string $name = '',
|
|
|
|
bool $strip_namespace = false
|
|
|
|
) {
|
|
|
|
$event->data['items'][] = new MenuItem($type, $name, $strip_namespace);
|
2020-03-30 21:32:07 +00:00
|
|
|
}
|
2020-04-03 03:32:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the top level namespace for the current page
|
|
|
|
* Exclude namespace added by the "translate" plugin
|
|
|
|
*/
|
|
|
|
protected function getTopLevelNamespace() : ?string {
|
|
|
|
global $INFO;
|
|
|
|
global $plugin_controller;
|
2020-04-03 20:13:43 +00:00
|
|
|
$language = null;
|
|
|
|
// If one of the "translate" or "translation" plugins is activated, get the language code for the current page
|
2020-04-03 03:32:33 +00:00
|
|
|
if ( ! $plugin_controller->isdisabled('translate') ) {
|
|
|
|
$translate_plugin =& $plugin_controller->load('helper', 'translate');
|
|
|
|
$language = $translate_plugin->getPageLanguage();
|
2020-04-03 20:13:43 +00:00
|
|
|
} elseif ( ! $plugin_controller->isdisabled('translation') ) {
|
|
|
|
$translation_plugin =& $plugin_controller->load('helper', 'translation');
|
|
|
|
$language = $translation_plugin->getLangPart($INFO['id']);
|
|
|
|
}
|
|
|
|
// Skip top-level language namespace if one is in use
|
|
|
|
if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) {
|
|
|
|
return explode(':', $INFO['namespace'])[1];
|
2020-04-03 03:32:33 +00:00
|
|
|
} else {
|
|
|
|
return explode(':', $INFO['namespace'])[0];
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 21:32:07 +00:00
|
|
|
}
|