1<?php
2
3use dokuwiki\plugin\approve\meta\ViewModeEdit;
4use dokuwiki\plugin\approve\meta\ViewModeSiteTools;
5
6/**
7 * Approve Plugin: places a link in usermenue and allows for change between modes
8 * Copied and adapted from userpage plugin
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Michael Kirchner
12 * @author     Blake Martin
13 * @author     Andreas Gohr <andi@splitbrain.org>
14 * @author     Anika Henke <anika@selfthinker.org>
15 */
16
17class action_plugin_approve_viewmode extends DokuWiki_Action_Plugin
18{
19    /** @inheritdoc */
20    function register(Doku_Event_Handler $controller)
21    {
22        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleAct');
23        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addSiteTools');
24        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addPageTools');
25    }
26
27    public function handleAct(Doku_Event $event)
28    {
29        if (!$this->getConf('viewmode')) return;
30        if ($event->data != 'viewmodesitetools' && $event->data != 'viewmodeedit') return;
31        $viewmode = get_doku_pref('approve_viewmode', false);
32        set_doku_pref('approve_viewmode', !$viewmode);  // toggle status
33        $event->data = 'redirect';
34    }
35
36    /**
37     * Add Link for mode change to the site tools
38     *
39     * @param Doku_Event $event
40     * @return bool
41     */
42    public function addSiteTools(Doku_Event $event)
43    {
44        global $INPUT;
45        if (!$this->getConf('viewmode')) return false;
46        if (!$INPUT->server->str('REMOTE_USER')) return false;
47        if ($event->data['view'] != 'user') return false;
48
49        array_splice($event->data['items'], 1, 0, [new ViewModeSiteTools()]);
50
51        return true;
52    }
53
54    public function addPageTools(Doku_Event $event)
55    {
56        global $INPUT;
57        if (!$this->getConf('viewmode')) return false;
58        if (!$INPUT->server->str('REMOTE_USER')) return false;
59        if ($event->data['view'] != 'page') return false;
60
61        $viewmode = get_doku_pref('approve_viewmode', false);
62        if ($viewmode) {
63            array_splice($event->data['items'], 0, 1, [new ViewModeEdit()]);
64        }
65        return true;
66    }
67
68}