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