1<?php 2 3namespace dokuwiki\Menu; 4 5use dokuwiki\Menu\Item\AbstractItem; 6 7abstract class AbstractMenu { 8 9 /** @var string[] list of Item classes to load */ 10 protected $types = array(); 11 12 /** @var int the context this menu is used in */ 13 protected $context = AbstractItem::CTX_DESKTOP; 14 15 /** @var string view identifier to be set in the event */ 16 protected $view = ''; 17 18 /** 19 * AbstractMenu constructor. 20 * 21 * @param int $context the context this menu is used in 22 */ 23 public function __construct($context = AbstractItem::CTX_DESKTOP) { 24 $this->context = $context; 25 } 26 27 /** 28 * Get the list of action items in this menu 29 * 30 * @return AbstractItem[] 31 * @triggers MENU_ITEMS_ASSEMBLY 32 */ 33 public function getItems() { 34 $data = array( 35 'view' => $this->view, 36 'items' => array(), 37 ); 38 trigger_event('MENU_ITEMS_ASSEMBLY', $data, array($this, 'loadItems')); 39 return $data['items']; 40 } 41 42 /** 43 * Default action for the MENU_ITEMS_ASSEMBLY event 44 * 45 * @see getItems() 46 * @param array $data The plugin data 47 */ 48 public function loadItems(&$data) { 49 foreach($this->types as $class) { 50 try { 51 $class = "\\dokuwiki\\Menu\\Item\\$class"; 52 /** @var AbstractItem $item */ 53 $item = new $class(); 54 if(!$item->visibleInContext($this->context)) continue; 55 $data['items'][$item->getType()] = $item; 56 } catch(\RuntimeException $ignored) { 57 // item not available 58 } 59 } 60 } 61 62 /** 63 * Generate HTML list items for this menu 64 * 65 * This is a convenience method for template authors. If you need more fine control over the 66 * output, use getItems() and build the HTML yourself 67 * 68 * @param string|false $classprefix create a class from type with this prefix, false for no class 69 * @param bool $svg add the SVG link 70 * @return string 71 */ 72 public function getListItems($classprefix = '', $svg = true) { 73 $html = ''; 74 foreach($this->getItems() as $item) { 75 if($classprefix !== false) { 76 $class = ' class="' . $classprefix . $item->getType() . '"'; 77 } else { 78 $class = ''; 79 } 80 81 $html .= "<li$class>"; 82 $html .= $item->asHtmlLink(false, $svg); 83 $html .= '</li>'; 84 } 85 return $html; 86 } 87 88} 89