xref: /plugin/acknowledge/action.php (revision 74126d4b5f888ba407f033d947da1ba9979f565b) !
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     * FIXME do we track ALL pages? or do we check for assignments?
25     *
26     * Store page last modified date
27     * Handle page deletions
28     * Remove assignments on page save, they get readded on rendering if needed
29     *
30     * @param Doku_Event $event
31     * @param $param
32     */
33    public function handlePageSave(Doku_Event $event, $param)
34    {
35        /** @var helper_plugin_acknowledge $helper */
36        $helper = plugin_load('helper', 'acknowledge');
37
38        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_DELETE) {
39            $helper->removePage($event->data['id']);
40        } elseif ($event->data['changeType'] !== DOKU_CHANGE_TYPE_MINOR_EDIT) {
41            $helper->storePageDate($event->data['id'], $event->data['newRevision']);
42        }
43
44        $helper->clearAssignments($event->data['id']);
45    }
46
47    /**
48     * @param Doku_Event $event
49     * @param $param
50     */
51    public function handleAjax(Doku_Event $event, $param)
52    {
53        if ($event->data === 'plugin_acknowledge_assign') {
54            echo $this->html();
55            $event->stopPropagation();
56            $event->preventDefault();
57        }
58    }
59
60    /**
61     * Returns the acknowledgment form/confirmation
62     *
63     * @return string The HTML to display
64     */
65    protected function html()
66    {
67        global $INPUT;
68        global $USERINFO;
69        $id = $INPUT->str('id');
70        $ackSubmitted = $INPUT->str('ack') === 'true';
71        $user = $INPUT->server->str('REMOTE_USER');
72        if ($id === '' || $user === '') return '';
73
74        /** @var helper_plugin_acknowledge $helper */
75        $helper = plugin_load('helper', 'acknowledge');
76
77        if ($ackSubmitted) {
78            $helper->saveAcknowledgement($id, $user);
79        }
80
81        $html = '';
82
83        $ack = $helper->hasUserAcknowledged($id, $user);
84        if ($ack) {
85
86            $html .= '<div>';
87            $html .= $this->getLang('ackGranted') . sprintf('%s', dformat($ack));
88            $html .= '</div>';
89        } elseif ($helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
90            $form = new Form(['id' => 'ackForm']);
91            $form->addCheckbox('ack');
92            $form->addLabel($this->getLang('ackText'), 'ack');
93            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">'. $this->getLang('ackButton') .'</button>');
94            $html .= '<div>';
95            $html .= $this->getLang('ackRequired') . ':<br>';
96            $html .= $form->toHTML();
97            $html .= '</div>';
98        }
99
100        return $html;
101    }
102}
103