xref: /plugin/acknowledge/action.php (revision 5a824e2a82dfbbdf08fc3d3aa862100e73996918)
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            echo $this->html();
61            $event->stopPropagation();
62            $event->preventDefault();
63        }
64    }
65
66    /**
67     * Handle Migration events
68     *
69     * @param Doku_Event $event
70     * @param $param
71     * @return void
72     */
73    public function handleUpgrade(Doku_Event $event, $param)
74    {
75        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'acknowledgement') {
76            return;
77        }
78        $to = $event->data['to'];
79        if ($to !== 3) return; // only handle upgrade to version 3
80
81        /** @var helper_plugin_acknowledge $helper */
82        $helper = plugin_load('helper', 'acknowledge');
83        $helper->updatePageIndex();
84    }
85
86    /**
87     * Returns the acknowledgment form/confirmation
88     *
89     * @return string The HTML to display
90     */
91    protected function html()
92    {
93        global $INPUT;
94        global $USERINFO;
95        $id = $INPUT->str('id');
96        $ackSubmitted = $INPUT->bool('ack');
97        $user = $INPUT->server->str('REMOTE_USER');
98        if ($id === '' || $user === '') return '';
99
100        /** @var helper_plugin_acknowledge $helper */
101        $helper = plugin_load('helper', 'acknowledge');
102
103        // only display for users assigned to the page
104        if (!$helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
105            return '';
106        }
107
108        if ($ackSubmitted) {
109            $helper->saveAcknowledgement($id, $user);
110        }
111
112        $ack = $helper->hasUserAcknowledged($id, $user);
113
114        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
115        $html .= inlineSVG(__DIR__ . '/admin.svg');
116        $html .= '</div>';
117
118        if ($ack) {
119            $html .= '<div>';
120            $html .= '<h4>';
121            $html .= $this->getLang('ackOk');
122            $html .= '</h4>';
123            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
124            $html .= '</div>';
125        } else {
126            $html .= '<div>';
127            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
128            $latest = $helper->getLatestUserAcknowledgement($id, $user);
129            if ($latest) {
130                $html .= '<a href="'
131                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
132                    . sprintf($this->getLang('ackDiff'), dformat($latest))
133                    . '</a><br>';
134            }
135
136            $form = new Form(['id' => 'ackForm']);
137            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
138            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">' . $this->getLang('ackButton') . '</button>');
139
140            $html .= $form->toHTML();
141            $html .= '</div>';
142        }
143
144        return $html;
145    }
146}
147