aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Menu/AbstractMenu.php
blob: 8c01abe088a1a259936927d58340e96575106293 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace dokuwiki\Menu;

use dokuwiki\Menu\Item\AbstractItem;

abstract class AbstractMenu {

    /** @var string[] list of Item classes to load */
    protected $types = array();

    /** @var int the context this menu is used in */
    protected $context = AbstractItem::CTX_DESKTOP;

    /** @var string view identifier to be set in the event */
    protected $view = '';

    /**
     * AbstractMenu constructor.
     *
     * @param int $context the context this menu is used in
     */
    public function __construct($context = AbstractItem::CTX_DESKTOP) {
        $this->context = $context;
    }

    /**
     * Get the list of action items in this menu
     *
     * @return AbstractItem[]
     * @triggers MENU_ITEMS_ASSEMBLY
     */
    public function getItems() {
        $data = array(
            'view' => $this->view,
            'items' => array(),
        );
        trigger_event('MENU_ITEMS_ASSEMBLY', $data, array($this, 'loadItems'));
        return $data['items'];
    }

    /**
     * Default action for the MENU_ITEMS_ASSEMBLY event
     *
     * @see getItems()
     * @param array $data The plugin data
     */
    public function loadItems(&$data) {
        foreach($this->types as $class) {
            try {
                $class = "\\dokuwiki\\Menu\\Item\\$class";
                /** @var AbstractItem $item */
                $item = new $class();
                if(!$item->visibleInContext($this->context)) continue;
                $data['items'][$item->getType()] = $item;
            } catch(\RuntimeException $ignored) {
                // item not available
            }
        }
    }

    /**
     * Generate HTML list items for this menu
     *
     * This is a convenience method for template authors. If you need more fine control over the
     * output, use getItems() and build the HTML yourself
     *
     * @param string|false $classprefix create a class from type with this prefix, false for no class
     * @param bool $svg add the SVG link
     * @return string
     */
    public function getListItems($classprefix = '', $svg = true) {
        $html = '';
        foreach($this->getItems() as $item) {
            if($classprefix !== false) {
                $class = ' class="' . $classprefix . $item->getType() . '"';
            } else {
                $class = '';
            }

            $html .= "<li$class>";
            $html .= $item->asHtmlLink(false, $svg);
            $html .= '</li>';
        }
        return $html;
    }

}