dokuwiki-plugin-childrenpages/MenuItem.php

71 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace dokuwiki\plugin\childrenpages;
use dokuwiki\Menu\Item\AbstractItem;
class MenuItem extends AbstractItem {
/**
* Generate a menu item from a passed string
*
* @param string $type
* @param string $label
* @param bool $strip_namespace
*/
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($strip_namespace);
}
/**
* Set the item target link from its type
*
* @param bool $strip_namespace
*/
protected function setTargetFromType(bool $strip_namespace) : void {
global $INFO;
global $plugin_controller;
$language = null;
// If one of the "translate" or "translation" plugins is activated, get the language code for the current page
if ( ! $plugin_controller->isdisabled('translate') ) {
$translate_plugin =& $plugin_controller->load('helper', 'translate');
$language = $translate_plugin->getPageLanguage();
} elseif ( ! $plugin_controller->isdisabled('translation') ) {
$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
$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 = [];
}
}