xref: /plugin/acknowledge/action/ajax.php (revision c38b6ca85313c7f98e013ff1ce06b406d156a85b)
1<?php
2
3/**
4 * DokuWiki Plugin acknowledge (AJAX 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_ajax extends ActionPlugin
16{
17    /** @inheritDoc */
18    public function register(EventHandler $controller)
19    {
20        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxAcknowledge');
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxAutocomplete');
22    }
23
24    /**
25     * @param Event $event
26     * @param $param
27     */
28    public function handleAjaxAcknowledge(Event $event, $param)
29    {
30        if ($event->data === 'plugin_acknowledge_acknowledge') {
31            $event->stopPropagation();
32            $event->preventDefault();
33
34            global $INPUT;
35            $id = $INPUT->str('id');
36
37            if (page_exists($id)) {
38                echo $this->html();
39            }
40        }
41    }
42
43    /**
44     * @param Event $event
45     * @return void
46     */
47    public function handleAjaxAutocomplete(Event $event)
48    {
49        if ($event->data === 'plugin_acknowledge_autocomplete') {
50            if (!checkSecurityToken()) return;
51
52            global $INPUT;
53
54            $event->stopPropagation();
55            $event->preventDefault();
56
57            /** @var helper_plugin_acknowledge $hlp */
58            $hlp = $this->loadHelper('acknowledge');
59
60            $found = [];
61
62            if ($INPUT->has('user')) {
63                $search = $INPUT->str('user');
64                $knownUsers = $hlp->getUsers();
65                $found = array_filter($knownUsers, function ($user) use ($search) {
66                    return (strstr(strtolower($user['label']), strtolower($search))) !== false ? $user : null;
67                });
68            }
69
70            if ($INPUT->has('pg')) {
71                $search = $INPUT->str('pg');
72                $pages = ft_pageLookup($search, true);
73                $found = array_map(function ($id, $title) {
74                    return ['value' => $id, 'label' => $title ?? $id];
75                }, array_keys($pages), array_values($pages));
76            }
77
78            header('Content-Type: application/json');
79
80            echo json_encode($found);
81        }
82    }
83
84    /**
85     * Returns the acknowledgment form/confirmation
86     *
87     * @return string The HTML to display
88     */
89    protected function html()
90    {
91        global $INPUT;
92        global $USERINFO;
93        $id = $INPUT->str('id');
94        $ackSubmitted = $INPUT->bool('ack');
95        $user = $INPUT->server->str('REMOTE_USER');
96        if ($id === '' || $user === '') return '';
97
98        /** @var helper_plugin_acknowledge $helper */
99        $helper = plugin_load('helper', 'acknowledge');
100
101        // only display for users assigned to the page
102        if (!$helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
103            return '';
104        }
105
106        if ($ackSubmitted) {
107            $helper->saveAcknowledgement($id, $user);
108        }
109
110        $ack = $helper->hasUserAcknowledged($id, $user);
111
112        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
113        $html .= inlineSVG(__DIR__ . '/../admin.svg');
114        $html .= '</div>';
115
116        if ($ack) {
117            $html .= '<div>';
118            $html .= '<h4>';
119            $html .= $this->getLang('ackOk');
120            $html .= '</h4>';
121            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
122            $html .= '</div>';
123        } else {
124            $html .= '<div>';
125            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
126            $latest = $helper->getLatestUserAcknowledgement($id, $user);
127            if ($latest) {
128                $html .= '<a href="'
129                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
130                    . sprintf($this->getLang('ackDiff'), dformat($latest))
131                    . '</a><br>';
132            }
133
134            $form = new Form(['id' => 'ackForm']);
135            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
136            $form->addHTML(
137                '<br><button type="submit" name="acksubmit" id="ack-submit">'
138                . $this->getLang('ackButton')
139                . '</button>'
140            );
141
142            $html .= $form->toHTML();
143            $html .= '</div>';
144        }
145
146        return $html;
147    }
148}
149