xref: /plugin/acknowledge/action.php (revision 45240794bdbd02483d35a247625be40921d89e8d)
1<?php
2
3/**
4 * DokuWiki Plugin acknowledge (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\Extension\ActionPlugin;
11use dokuwiki\Extension\EventHandler;
12use dokuwiki\Extension\Event;
13use dokuwiki\Form\Form;
14
15class action_plugin_acknowledge extends ActionPlugin
16{
17    /** @inheritDoc */
18    public function register(EventHandler $controller)
19    {
20        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handlePageSave');
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxAssign');
22        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxAutocomplete');
23        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'AFTER', $this, 'handleUpgrade');
24    }
25
26    /**
27     * Manage page meta data
28     *
29     * Store page last modified date
30     * Handle page deletions
31     * Handle page creations
32     *
33     * @param Event $event
34     * @param $param
35     */
36    public function handlePageSave(Event $event, $param)
37    {
38        /** @var helper_plugin_acknowledge $helper */
39        $helper = plugin_load('helper', 'acknowledge');
40
41        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_DELETE) {
42            $helper->removePage($event->data['id']); // this cascades to assignments
43        } elseif ($event->data['changeType'] !== DOKU_CHANGE_TYPE_MINOR_EDIT) {
44            $helper->storePageDate($event->data['id'], $event->data['newRevision'], $event->data['newContent']);
45        }
46
47        // Remove page assignees here because the syntax might have been removed
48        // they are readded on metadata rendering if still there
49        $helper->clearPageAssignments($event->data['id']);
50
51        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_CREATE) {
52            // new pages need to have their auto assignments updated based on the existing patterns
53            $helper->setAutoAssignees($event->data['id']);
54        }
55    }
56
57    /**
58     * @param Event $event
59     * @param $param
60     */
61    public function handleAjaxAssign(Event $event, $param)
62    {
63        if ($event->data === 'plugin_acknowledge_assign') {
64            $event->stopPropagation();
65            $event->preventDefault();
66
67            global $INPUT;
68            $id = $INPUT->str('id');
69
70            if (page_exists($id)) {
71                echo $this->html();
72            }
73        }
74    }
75
76    /**
77     * @param Event $event
78     * @return void
79     */
80    public function handleAjaxAutocomplete(Event $event)
81    {
82        if ($event->data === 'plugin_acknowledge_autocomplete') {
83
84            if (!checkSecurityToken()) return;
85
86            global $INPUT;
87
88            $event->stopPropagation();
89            $event->preventDefault();
90
91            /** @var helper_plugin_acknowledge $hlp */
92            $hlp = $this->loadHelper('acknowledge');
93
94            $knownUsers = $hlp->getUsers();
95
96            $search = $INPUT->str('user');
97            $found = array_filter($knownUsers, function ($user) use ($search) {
98                return (strstr(strtolower($user['label']), strtolower($search))) !== false ? $user : null;
99            });
100
101            header('Content-Type: application/json');
102
103            echo json_encode($found);
104        }
105    }
106
107    /**
108     * Handle Migration events
109     *
110     * @param Event $event
111     * @param $param
112     * @return void
113     */
114    public function handleUpgrade(Event $event, $param)
115    {
116        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'acknowledgement') {
117            return;
118        }
119        $to = $event->data['to'];
120        if ($to !== 3) return; // only handle upgrade to version 3
121
122        /** @var helper_plugin_acknowledge $helper */
123        $helper = plugin_load('helper', 'acknowledge');
124        $helper->updatePageIndex();
125    }
126
127    /**
128     * Returns the acknowledgment form/confirmation
129     *
130     * @return string The HTML to display
131     */
132    protected function html()
133    {
134        global $INPUT;
135        global $USERINFO;
136        $id = $INPUT->str('id');
137        $ackSubmitted = $INPUT->bool('ack');
138        $user = $INPUT->server->str('REMOTE_USER');
139        if ($id === '' || $user === '') return '';
140
141        /** @var helper_plugin_acknowledge $helper */
142        $helper = plugin_load('helper', 'acknowledge');
143
144        // only display for users assigned to the page
145        if (!$helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
146            return '';
147        }
148
149        if ($ackSubmitted) {
150            $helper->saveAcknowledgement($id, $user);
151        }
152
153        $ack = $helper->hasUserAcknowledged($id, $user);
154
155        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
156        $html .= inlineSVG(__DIR__ . '/admin.svg');
157        $html .= '</div>';
158
159        if ($ack) {
160            $html .= '<div>';
161            $html .= '<h4>';
162            $html .= $this->getLang('ackOk');
163            $html .= '</h4>';
164            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
165            $html .= '</div>';
166        } else {
167            $html .= '<div>';
168            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
169            $latest = $helper->getLatestUserAcknowledgement($id, $user);
170            if ($latest) {
171                $html .= '<a href="'
172                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
173                    . sprintf($this->getLang('ackDiff'), dformat($latest))
174                    . '</a><br>';
175            }
176
177            $form = new Form(['id' => 'ackForm']);
178            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
179            $form->addHTML(
180                '<br><button type="submit" name="acksubmit" id="ack-submit">'
181                . $this->getLang('ackButton')
182                . '</button>'
183            );
184
185            $html .= $form->toHTML();
186            $html .= '</div>';
187        }
188
189        return $html;
190    }
191}
192