1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\plugin\approve\meta\ViewModeEdit;
7use dokuwiki\plugin\approve\meta\ViewModeSiteTools;
8
9class action_plugin_approve_viewmode extends ActionPlugin
10{
11    /** @inheritdoc */
12    function register(EventHandler $controller)
13    {
14        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAct');
15        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addSiteTools');
16        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addPageTools');
17    }
18
19    public function handleAct(Event $event)
20    {
21        if (!$this->getConf('viewmode')) return;
22        if ($event->data != 'viewmodesitetools' && $event->data != 'viewmodeedit') return;
23        $viewmode = get_doku_pref('approve_viewmode', false);
24        set_doku_pref('approve_viewmode', !$viewmode);  // toggle status
25        $event->data = 'redirect';
26    }
27
28    /**
29     * Add Link for mode change to the site tools
30     *
31     * @param Event $event
32     * @return bool
33     */
34    public function addSiteTools(Event $event)
35    {
36        global $INPUT;
37        if (!$this->getConf('viewmode')) return false;
38        if (!$INPUT->server->str('REMOTE_USER')) return false;
39        if ($event->data['view'] != 'user') return false;
40
41        array_splice($event->data['items'], 1, 0, [new ViewModeSiteTools()]);
42
43        return true;
44    }
45
46    public function addPageTools(Event $event)
47    {
48        global $INPUT;
49        if (!$this->getConf('viewmode')) return false;
50        if (!$INPUT->server->str('REMOTE_USER')) return false;
51        if ($event->data['view'] != 'page') return false;
52
53        $viewmode = get_doku_pref('approve_viewmode', false);
54        if ($viewmode) {
55            array_splice($event->data['items'], 0, 1, [new ViewModeEdit()]);
56        }
57        return true;
58    }
59
60}
61