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        $html = '';
80
81        $ack = $helper->hasUserAcknowledged($id, $user);
82        if ($ack) {
83
84            $html .= '<div>';
85            $html .= $this->getLang('ackGranted') . sprintf('%s', dformat($ack));
86            $html .= '</div>';
87        } elseif ($helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
88            $form = new Form(['id' => 'ackForm']);
89            $form->addCheckbox('ack')->attr('required', 'required');
90            $form->addLabel($this->getLang('ackText'), 'ack');
91            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">'. $this->getLang('ackButton') .'</button>');
92            $html .= '<div>';
93
94            $html .= $this->getLang('ackRequired') . '<br>';
95
96            $latest = $helper->getLatestUserAcknowledgement($id, $user);
97            if ($latest) {
98                $html .= '<a href="'
99                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
100                    . sprintf($this->getLang('ackDiff'), dformat($latest))
101                    . '</a><br>';
102            }
103
104            $html .= $form->toHTML();
105            $html .= '</div>';
106        }
107
108        return $html;
109    }
110}
111