2020-04-02 23:15:41 +00:00
|
|
|
<?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
|
2020-04-02 23:44:50 +00:00
|
|
|
* @param string $label
|
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
|
|
|
public function __construct(
|
|
|
|
string $type,
|
|
|
|
string $label = '',
|
|
|
|
bool $strip_namespace = false
|
|
|
|
) {
|
2020-04-05 13:25:56 +00:00
|
|
|
$this->type = "childrenpages_$type";
|
2020-04-03 00:14:16 +00:00
|
|
|
if ( empty($label) ) {
|
|
|
|
$label = ucfirst($type);
|
|
|
|
}
|
2020-04-02 23:44:50 +00:00
|
|
|
$this->label = $label;
|
2020-04-02 23:15:41 +00:00
|
|
|
parent::__construct();
|
2020-04-05 13:25:56 +00:00
|
|
|
$this->setTargetFromType($type, $strip_namespace);
|
2020-04-03 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the item target link from its type
|
2020-04-03 20:37:30 +00:00
|
|
|
*
|
2020-04-05 13:25:56 +00:00
|
|
|
* @param string $type
|
|
|
|
* @param bool $strip_namespace
|
2020-04-03 03:32:33 +00:00
|
|
|
*/
|
2020-04-05 13:25:56 +00:00
|
|
|
protected function setTargetFromType(string $type, bool $strip_namespace) : void {
|
2020-04-03 03:32:33 +00:00
|
|
|
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');
|
2020-04-03 00:46:35 +00:00
|
|
|
$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']);
|
|
|
|
}
|
2020-04-03 20:37:30 +00:00
|
|
|
// Split path to page
|
|
|
|
$page_path = explode(':', $INFO['id']);
|
2020-04-03 20:13:43 +00:00
|
|
|
// If the top level namespace is a language one, the children namespace should be inserted inside it
|
2020-04-03 20:37:30 +00:00
|
|
|
$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
|
2020-04-05 16:24:59 +00:00
|
|
|
if ( $type !== '_main' ) {
|
2020-04-05 13:25:56 +00:00
|
|
|
$target_path[] = $type;
|
2020-04-03 00:46:35 +00:00
|
|
|
}
|
2020-04-03 20:37:30 +00:00
|
|
|
$this->id = implode(':', array_merge($target_path, $page_path));
|
2020-04-02 23:52:49 +00:00
|
|
|
$this->params = [];
|
2020-04-02 23:15:41 +00:00
|
|
|
}
|
2020-04-28 10:52:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2020-04-02 23:15:41 +00:00
|
|
|
}
|