dokuwiki-plugin-childrenpages/action.php

89 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
2020-04-02 22:50:50 +00:00
* Tabpage plugin: Shows links to children pages in the page menu
*
* @license BSD 2-Clause
* @author Antoine Le Gonidec <vv221.dokuwiki@dotslashplay.it>
*/
// must be run within Dokuwiki
if ( ! defined('DOKU_INC') ) {
die();
}
use dokuwiki\plugin\childrenpages\MenuItem;
2020-04-02 22:50:50 +00:00
class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller
*/
public function register(Doku_Event_Handler $controller) : void {
$controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuItems');
}
/**
* Add new items to the page menu
*
* @param Doku_Event $event
*/
public function addMenuItems(Doku_Event $event) : void {
global $INFO;
// Check that this method has been called in the expected context
if ( $event->name !== 'MENU_ITEMS_ASSEMBLY' ) {
$message = "Tabpage plugin error:";
$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);
}
// Only add content to the page menu
if ( $event->data['view'] !== 'page' ) {
return;
}
// Only add links if the current page is not included in a reserved namespace
$children_types = $this->getConf('children_list');
if ( ! plugin_isdisabled('translate') ) {
// Skip top-level namespace added by "translate" plugin
$translate_plugin =& plugin_load('helper', 'translate');
$language = $translate_plugin->getPageLanguage();
if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) {
$top_namespace = explode(':', $INFO['namespace'])[1];
} else {
$top_namespace = explode(':', $INFO['namespace'])[0];
}
} else {
$top_namespace = explode(':', $INFO['namespace'])[0];
}
if ( in_array($top_namespace, $children_types) ) {
return;
}
// 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);
}
}
/**
* Add a new item to the page menu
*
* @param Doku_Event $event
* @param string $type
* @param string $name
*/
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);
}
}