xref: /dokuwiki/inc/Menu/MobileMenu.php (revision d9156e4d28b707baa0379e1d24e1ce262a0d2c1e)
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    /**
18     * Returns all items grouped by view
19     *
20     * @return AbstractItem[][]
21     */
22    public function getGroupedItems()
23    {
24        $pagemenu = new PageMenu(AbstractItem::CTX_MOBILE);
25        $sitemenu = new SiteMenu(AbstractItem::CTX_MOBILE);
26        $usermenu = new UserMenu(AbstractItem::CTX_MOBILE);
27
28        return [
29            'page' => $pagemenu->getItems(),
30            'site' => $sitemenu->getItems(),
31            'user' => $usermenu->getItems()
32        ];
33    }
34
35    /**
36     * Get all items in a flat array
37     *
38     * This returns the same format as AbstractMenu::getItems()
39     *
40     * @return AbstractItem[]
41     */
42    public function getItems()
43    {
44        $menu = $this->getGroupedItems();
45        return array_merge(...array_values($menu));
46    }
47
48    /**
49     * Print a dropdown menu with all DokuWiki actions
50     *
51     * Note: this will not use any pretty URLs
52     *
53     * @param string $empty empty option label
54     * @param string $button submit button label
55     * @return string
56     */
57    public function getDropdown($empty = '', $button = '&gt;')
58    {
59        global $ID;
60        global $REV;
61        /** @var string[] $lang */
62        global $lang;
63        global $INPUT;
64
65        $html = '<form action="' . script() . '" method="get" accept-charset="utf-8">';
66        $html .= '<div class="no">';
67        $html .= '<input type="hidden" name="id" value="' . $ID . '" />';
68        if ($REV) $html .= '<input type="hidden" name="rev" value="' . $REV . '" />';
69        if ($INPUT->server->str('REMOTE_USER')) {
70            $html .= '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
71        }
72
73        $html .= '<select name="do" class="edit quickselect" title="' . $lang['tools'] . '">';
74        $html .= '<option value="">' . $empty . '</option>';
75
76        foreach ($this->getGroupedItems() as $tools => $items) {
77            if ($items !== []) {
78                $html .= '<optgroup label="' . $lang[$tools . '_tools'] . '">';
79                foreach ($items as $item) {
80                    $params = $item->getParams();
81                    $html .= '<option value="' . $params['do'] . '">';
82                    $html .= hsc($item->getLabel());
83                    $html .= '</option>';
84                }
85                $html .= '</optgroup>';
86            }
87        }
88
89        $html .= '</select>';
90        $html .= '<button type="submit">' . $button . '</button>';
91        $html .= '</div>';
92        $html .= '</form>';
93
94        return $html;
95    }
96
97}
98