xref: /plugin/acknowledge/action.php (revision cfdacc6aa653c54967dcdc5175d6eaf5dd54939e)
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        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'AFTER', $this, 'handleUpgrade');
20    }
21
22    /**
23     * Manage page meta data
24     *
25     * Store page last modified date
26     * Handle page deletions
27     * Handle page creations
28     *
29     * @param Doku_Event $event
30     * @param $param
31     */
32    public function handlePageSave(Doku_Event $event, $param)
33    {
34        /** @var helper_plugin_acknowledge $helper */
35        $helper = plugin_load('helper', 'acknowledge');
36
37        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_DELETE) {
38            $helper->removePage($event->data['id']); // this cascades to assignments
39        } elseif ($event->data['changeType'] !== DOKU_CHANGE_TYPE_MINOR_EDIT) {
40            $helper->storePageDate($event->data['id'], $event->data['newRevision'], $event->data['newContent']);
41        }
42
43        // Remove page assignees here because the syntax might have been removed
44        // they are readded on metadata rendering if still there
45        $helper->clearPageAssignments($event->data['id']);
46
47        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_CREATE) {
48            // new pages need to have their auto assignments updated based on the existing patterns
49            $helper->setAutoAssignees($event->data['id']);
50        }
51    }
52
53    /**
54     * @param Doku_Event $event
55     * @param $param
56     */
57    public function handleAjax(Doku_Event $event, $param)
58    {
59        if ($event->data === 'plugin_acknowledge_assign') {
60            $event->stopPropagation();
61            $event->preventDefault();
62
63            global $INPUT;
64            $id = $INPUT->str('id');
65
66            if (page_exists($id)) {
67                echo $this->html();
68            }
69        }
70    }
71
72    /**
73     * Handle Migration events
74     *
75     * @param Doku_Event $event
76     * @param $param
77     * @return void
78     */
79    public function handleUpgrade(Doku_Event $event, $param)
80    {
81        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'acknowledgement') {
82            return;
83        }
84        $to = $event->data['to'];
85        if ($to !== 3) return; // only handle upgrade to version 3
86
87        /** @var helper_plugin_acknowledge $helper */
88        $helper = plugin_load('helper', 'acknowledge');
89        $helper->updatePageIndex();
90    }
91
92    /**
93     * Returns the acknowledgment form/confirmation
94     *
95     * @return string The HTML to display
96     */
97    protected function html()
98    {
99        global $INPUT;
100        global $USERINFO;
101        $id = $INPUT->str('id');
102        $ackSubmitted = $INPUT->bool('ack');
103        $user = $INPUT->server->str('REMOTE_USER');
104        if ($id === '' || $user === '') return '';
105
106        /** @var helper_plugin_acknowledge $helper */
107        $helper = plugin_load('helper', 'acknowledge');
108
109        // only display for users assigned to the page
110        if (!$helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
111            return '';
112        }
113
114        if ($ackSubmitted) {
115            $helper->saveAcknowledgement($id, $user);
116        }
117
118        $ack = $helper->hasUserAcknowledged($id, $user);
119
120        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
121        $html .= inlineSVG(__DIR__ . '/admin.svg');
122        $html .= '</div>';
123
124        if ($ack) {
125            $html .= '<div>';
126            $html .= '<h4>';
127            $html .= $this->getLang('ackOk');
128            $html .= '</h4>';
129            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
130            $html .= '</div>';
131        } else {
132            $html .= '<div>';
133            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
134            $latest = $helper->getLatestUserAcknowledgement($id, $user);
135            if ($latest) {
136                $html .= '<a href="'
137                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
138                    . sprintf($this->getLang('ackDiff'), dformat($latest))
139                    . '</a><br>';
140            }
141
142            $form = new Form(['id' => 'ackForm']);
143            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
144            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">' . $this->getLang('ackButton') . '</button>');
145
146            $html .= $form->toHTML();
147            $html .= '</div>';
148        }
149
150        return $html;
151    }
152}
153