193b8c351SAndreas Gohr<?php 293b8c351SAndreas Gohr 393b8c351SAndreas Gohrnamespace dokuwiki\Menu; 493b8c351SAndreas Gohr 593b8c351SAndreas Gohruse dokuwiki\Menu\Item\AbstractItem; 693b8c351SAndreas Gohr 7368ce258SAndreas Gohr/** 8368ce258SAndreas Gohr * Class AbstractMenu 9368ce258SAndreas Gohr * 10368ce258SAndreas Gohr * Basic menu functionality. A menu defines a list of AbstractItem that shall be shown. 11368ce258SAndreas Gohr * It contains convenience functions to display the menu in HTML, but template authors can also 12368ce258SAndreas Gohr * just accesst the items via getItems() and create the HTML as however they see fit. 13368ce258SAndreas Gohr */ 1493b8c351SAndreas Gohrabstract class AbstractMenu { 1593b8c351SAndreas Gohr 1693b8c351SAndreas Gohr /** @var string[] list of Item classes to load */ 1793b8c351SAndreas Gohr protected $types = array(); 1893b8c351SAndreas Gohr 1993b8c351SAndreas Gohr /** @var int the context this menu is used in */ 2093b8c351SAndreas Gohr protected $context = AbstractItem::CTX_DESKTOP; 2193b8c351SAndreas Gohr 2293b8c351SAndreas Gohr /** @var string view identifier to be set in the event */ 2393b8c351SAndreas Gohr protected $view = ''; 2493b8c351SAndreas Gohr 2593b8c351SAndreas Gohr /** 2693b8c351SAndreas Gohr * AbstractMenu constructor. 2793b8c351SAndreas Gohr * 2893b8c351SAndreas Gohr * @param int $context the context this menu is used in 2993b8c351SAndreas Gohr */ 3093b8c351SAndreas Gohr public function __construct($context = AbstractItem::CTX_DESKTOP) { 3193b8c351SAndreas Gohr $this->context = $context; 3293b8c351SAndreas Gohr } 3393b8c351SAndreas Gohr 3493b8c351SAndreas Gohr /** 3593b8c351SAndreas Gohr * Get the list of action items in this menu 3693b8c351SAndreas Gohr * 3793b8c351SAndreas Gohr * @return AbstractItem[] 3893b8c351SAndreas Gohr * @triggers MENU_ITEMS_ASSEMBLY 3993b8c351SAndreas Gohr */ 4093b8c351SAndreas Gohr public function getItems() { 4193b8c351SAndreas Gohr $data = array( 4293b8c351SAndreas Gohr 'view' => $this->view, 4393b8c351SAndreas Gohr 'items' => array(), 4493b8c351SAndreas Gohr ); 4593b8c351SAndreas Gohr trigger_event('MENU_ITEMS_ASSEMBLY', $data, array($this, 'loadItems')); 4693b8c351SAndreas Gohr return $data['items']; 4793b8c351SAndreas Gohr } 4893b8c351SAndreas Gohr 4993b8c351SAndreas Gohr /** 5093b8c351SAndreas Gohr * Default action for the MENU_ITEMS_ASSEMBLY event 5193b8c351SAndreas Gohr * 5293b8c351SAndreas Gohr * @see getItems() 5393b8c351SAndreas Gohr * @param array $data The plugin data 5493b8c351SAndreas Gohr */ 5593b8c351SAndreas Gohr public function loadItems(&$data) { 5693b8c351SAndreas Gohr foreach($this->types as $class) { 5793b8c351SAndreas Gohr try { 5893b8c351SAndreas Gohr $class = "\\dokuwiki\\Menu\\Item\\$class"; 5993b8c351SAndreas Gohr /** @var AbstractItem $item */ 6093b8c351SAndreas Gohr $item = new $class(); 6193b8c351SAndreas Gohr if(!$item->visibleInContext($this->context)) continue; 62*e6bd4c92SAndreas Gohr $data['items'][] = $item; 6393b8c351SAndreas Gohr } catch(\RuntimeException $ignored) { 6493b8c351SAndreas Gohr // item not available 6593b8c351SAndreas Gohr } 6693b8c351SAndreas Gohr } 6793b8c351SAndreas Gohr } 6893b8c351SAndreas Gohr 6993b8c351SAndreas Gohr /** 7093b8c351SAndreas Gohr * Generate HTML list items for this menu 7193b8c351SAndreas Gohr * 7293b8c351SAndreas Gohr * This is a convenience method for template authors. If you need more fine control over the 7393b8c351SAndreas Gohr * output, use getItems() and build the HTML yourself 7493b8c351SAndreas Gohr * 7593b8c351SAndreas Gohr * @param string|false $classprefix create a class from type with this prefix, false for no class 762f7a5efdSAndreas Gohr * @param bool $svg add the SVG link 77c2b9771aSAndreas Gohr * @return string 7893b8c351SAndreas Gohr */ 792f7a5efdSAndreas Gohr public function getListItems($classprefix = '', $svg = true) { 8093b8c351SAndreas Gohr $html = ''; 8193b8c351SAndreas Gohr foreach($this->getItems() as $item) { 8293b8c351SAndreas Gohr if($classprefix !== false) { 8393b8c351SAndreas Gohr $class = ' class="' . $classprefix . $item->getType() . '"'; 8493b8c351SAndreas Gohr } else { 8593b8c351SAndreas Gohr $class = ''; 8693b8c351SAndreas Gohr } 8793b8c351SAndreas Gohr 8893b8c351SAndreas Gohr $html .= "<li$class>"; 892f7a5efdSAndreas Gohr $html .= $item->asHtmlLink(false, $svg); 9093b8c351SAndreas Gohr $html .= '</li>'; 9193b8c351SAndreas Gohr } 92c2b9771aSAndreas Gohr return $html; 9393b8c351SAndreas Gohr } 9493b8c351SAndreas Gohr 9593b8c351SAndreas Gohr} 96