1<?php
2/**
3 * DokuWiki Plugin acknowledge (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
7 */
8
9use dokuwiki\Form\Form;
10
11class action_plugin_acknowledge extends DokuWiki_Action_Plugin
12{
13
14    /** @inheritDoc */
15    public function register(Doku_Event_Handler $controller)
16    {
17        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handlePageSave');
18        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
19    }
20
21    /**
22     * Manage page meta data
23     *
24     * Store page last modified date
25     * Handle page deletions
26     * Remove assignments on page save, they get readded on rendering if needed
27     *
28     * @param Doku_Event $event
29     * @param $param
30     */
31    public function handlePageSave(Doku_Event $event, $param)
32    {
33        /** @var helper_plugin_acknowledge $helper */
34        $helper = plugin_load('helper', 'acknowledge');
35
36        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_DELETE) {
37            $helper->removePage($event->data['id']);
38        } elseif ($event->data['changeType'] !== DOKU_CHANGE_TYPE_MINOR_EDIT) {
39            $helper->storePageDate($event->data['id'], $event->data['newRevision'], $event->data['newContent']);
40        }
41
42        $helper->clearAssignments($event->data['id']);
43    }
44
45    /**
46     * @param Doku_Event $event
47     * @param $param
48     */
49    public function handleAjax(Doku_Event $event, $param)
50    {
51        if ($event->data === 'plugin_acknowledge_assign') {
52            echo $this->html();
53            $event->stopPropagation();
54            $event->preventDefault();
55        }
56    }
57
58    /**
59     * Returns the acknowledgment form/confirmation
60     *
61     * @return string The HTML to display
62     */
63    protected function html()
64    {
65        global $INPUT;
66        global $USERINFO;
67        $id = $INPUT->str('id');
68        $ackSubmitted = $INPUT->bool('ack');
69        $user = $INPUT->server->str('REMOTE_USER');
70        if ($id === '' || $user === '') return '';
71
72        /** @var helper_plugin_acknowledge $helper */
73        $helper = plugin_load('helper', 'acknowledge');
74
75        if ($ackSubmitted) {
76            $helper->saveAcknowledgement($id, $user);
77        }
78
79        $ack = $helper->hasUserAcknowledged($id, $user);
80
81        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
82        $html .= inlineSVG(__DIR__ . '/admin.svg');
83        $html .= '</div>';
84
85        if ($ack) {
86
87            $html .= '<div>';
88            $html .= '<h4>';
89            $html .= $this->getLang('ackOk');
90            $html .= '</h4>';
91            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
92            $html .= '</div>';
93        } elseif ($helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
94
95            $html .= '<div>';
96            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
97            $latest = $helper->getLatestUserAcknowledgement($id, $user);
98            if ($latest) {
99                $html .= '<a href="'
100                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
101                    . sprintf($this->getLang('ackDiff'), dformat($latest))
102                    . '</a><br>';
103            }
104
105            $form = new Form(['id' => 'ackForm']);
106            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
107            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">' . $this->getLang('ackButton') . '</button>');
108
109            $html .= $form->toHTML();
110            $html .= '</div>';
111        }
112
113        return $html;
114    }
115}
116