1<?php
2
3namespace dokuwiki\Menu;
4
5use dokuwiki\Menu\Item\AbstractItem;
6
7/**
8 * Class MobileMenu
9 *
10 * Note: this does not inherit from AbstractMenu because it is not working like the other
11 * menus. This is a meta menu, aggregating the items from the other menus and offering a combined
12 * view. The idea is to use this on mobile devices, thus the context is fixed to CTX_MOBILE
13 */
14class MobileMenu implements MenuInterface
15{
16    /**
17     * Returns all items grouped by view
18     *
19     * @return AbstractItem[][]
20     */
21    public function getGroupedItems()
22    {
23        $pagemenu = new PageMenu(AbstractItem::CTX_MOBILE);
24        $sitemenu = new SiteMenu(AbstractItem::CTX_MOBILE);
25        $usermenu = new UserMenu(AbstractItem::CTX_MOBILE);
26
27        return [
28            'page' => $pagemenu->getItems(),
29            'site' => $sitemenu->getItems(),
30            'user' => $usermenu->getItems()
31        ];
32    }
33
34    /**
35     * Get all items in a flat array
36     *
37     * This returns the same format as AbstractMenu::getItems()
38     *
39     * @return AbstractItem[]
40     */
41    public function getItems()
42    {
43        $menu = $this->getGroupedItems();
44        return array_merge(...array_values($menu));
45    }
46
47    /**
48     * Print a dropdown menu with all DokuWiki actions
49     *
50     * Note: this will not use any pretty URLs
51     *
52     * @param string $empty empty option label
53     * @param string $button submit button label
54     * @return string
55     */
56    public function getDropdown($empty = '', $button = '&gt;')
57    {
58        global $ID;
59        global $REV;
60        /** @var string[] $lang */
61        global $lang;
62        global $INPUT;
63
64        $html = '<form action="' . script() . '" method="get" accept-charset="utf-8">';
65        $html .= '<div class="no">';
66        $html .= '<input type="hidden" name="id" value="' . $ID . '" />';
67        if ($REV) $html .= '<input type="hidden" name="rev" value="' . $REV . '" />';
68        if ($INPUT->server->str('REMOTE_USER')) {
69            $html .= '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
70        }
71
72        $html .= '<select name="do" class="edit quickselect" title="' . $lang['tools'] . '">';
73        $html .= '<option value="">' . $empty . '</option>';
74
75        foreach ($this->getGroupedItems() as $tools => $items) {
76            if ($items !== []) {
77                $html .= '<optgroup label="' . $lang[$tools . '_tools'] . '">';
78                foreach ($items as $item) {
79                    $params = $item->getParams();
80                    $html .= '<option value="' . $params['do'] . '">';
81                    $html .= hsc($item->getLabel());
82                    $html .= '</option>';
83                }
84                $html .= '</optgroup>';
85            }
86        }
87
88        $html .= '</select>';
89        $html .= '<button type="submit">' . $button . '</button>';
90        $html .= '</div>';
91        $html .= '</form>';
92
93        return $html;
94    }
95}
96