Completed
Push — svgpagetools ( 17ef2a...7c844e )
by Andreas
04:40
created

AbstractMenu::getListItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\Menu;
4
5
use dokuwiki\Menu\Item\AbstractItem;
6
7
abstract 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
     * @return string
70
     */
71
    public function getListItems($classprefix = '') {
72
        $html = '';
73
        foreach($this->getItems() as $item) {
74
            if($classprefix !== false) {
75
                $class = ' class="' . $classprefix . $item->getType() . '"';
76
            } else {
77
                $class = '';
78
            }
79
80
            $html .= "<li$class>";
81
            $html .= $item->asHtmlLink(false);
82
            $html .= '</li>';
83
        }
84
        return $html;
85
    }
86
87
}
88