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