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