1<?php
2/**
3 * DokuWiki Plugin hidemenus (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Daniel Weisshaar <daniwei-dev@gmx.de>
7 */
8class action_plugin_hidemenus_hidemenus extends \dokuwiki\Extension\ActionPlugin
9{
10
11    /** @inheritDoc */
12    public function register(Doku_Event_Handler $controller)
13    {
14        // for information about sequence number see https://www.dokuwiki.org/devel:action_plugins#register_method
15        $sequence_number = 3999; // react on event as late as possible
16        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'handle_menu_items_assembly', array(), $sequence_number);
17
18    }
19
20    /**
21     * Event handler for MENU_ITEMS_ASSEMBLY AFTER event
22     *
23     * Looks if the user is logged in, otherwise hides the page tools by clearing the item data array.
24     *
25     * @param Doku_Event $event  event object by reference
26     * @param mixed      $param  optional parameter passed when event was registered
27     * @return void
28     */
29    public function handle_menu_items_assembly(Doku_Event $event, $param)
30    {
31        global $INFO;
32
33        if(!empty($INFO['userinfo']))
34        {
35            return; // user is already logged in
36        }
37
38        // check the configuration if hiding is desired
39        $allowPageToolsHiding = $this->getConf('hidePageTools');
40        $allowSiteToolsHiding = $this->getConf('hideSiteTools');
41
42        if(
43            ($allowPageToolsHiding && $event->data['view'] === 'page')
44            || ($allowSiteToolsHiding && $event->data['view'] === 'site')
45        )
46        {
47            $event->data['items'] = []; // remove all existing item entries
48        }
49    }
50
51}
52
53