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