dokuwiki-plugin-childrenpages/MenuItem.php

94 lines
3 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 = "childrenpages_$type";
if ( empty($label) ) {
$label = ucfirst($type);
}
$this->label = $label;
parent::__construct();
$this->setTargetFromType($type, $strip_namespace);
}
/**
* 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 = [];
}
/**
* 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;
}
}