109 lines
3.4 KiB
PHP
109 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace dokuwiki\plugin\childrenpages;
|
|
|
|
use dokuwiki\Menu\Item\AbstractItem;
|
|
|
|
class MenuItem extends AbstractItem {
|
|
/**
|
|
* @var bool $is_active True if it is a link to the current page
|
|
*/
|
|
public $is_active;
|
|
|
|
/**
|
|
* 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 = "childrenpages_$type";
|
|
if ( empty($label) ) {
|
|
$label = ucfirst($type);
|
|
}
|
|
$this->label = $label;
|
|
parent::__construct();
|
|
$this->setTargetFromType($type, $strip_namespace);
|
|
$this->setIsActive();
|
|
}
|
|
|
|
/**
|
|
* Set the item target link from its type
|
|
*
|
|
* @param string $type
|
|
* @param bool $strip_namespace
|
|
*/
|
|
protected function setTargetFromType(string $type, 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 ( $type !== '_main' ) {
|
|
$target_path[] = $type;
|
|
}
|
|
$this->id = implode(':', array_merge($target_path, $page_path));
|
|
$this->params = [];
|
|
}
|
|
|
|
/**
|
|
* Set the active status of the link
|
|
*/
|
|
protected function setIsActive() : void {
|
|
global $INFO;
|
|
$current_page_id = $INFO['id'];
|
|
$target_page_id = $this->id;
|
|
$this->is_active = ( $target_page_id === $current_page_id );
|
|
}
|
|
|
|
/**
|
|
* Convenience method to get the attributes for constructing an <a> element
|
|
* Parent method is declared in dokuwiki\Menu\Item\AbstractItem
|
|
*
|
|
* @param string|false $classprefix create a class from type with this prefix, false for no class
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getLinkAttributes($classprefix = 'menuitem ') : array {
|
|
$attributes = parent::getLinkAttributes($classprefix);
|
|
// Get already set classes, defaulting to an empty string
|
|
$classes = ( isset($attributes['class']) ) ?
|
|
$attributes['class'] : '';
|
|
// Add an extra class, based on the existence of the target page
|
|
$extra_class = ( page_exists($this->id) ) ?
|
|
'wikilink1' : 'wikilink2';
|
|
$classes = trim("$classes $extra_class");
|
|
// Return the full attributes list, including the updated classes list
|
|
$attributes['class'] = $classes;
|
|
return $attributes;
|
|
}
|
|
}
|