xref: /plugin/acknowledge/action.php (revision f09444ff1e83ecee2d791c4f9e5f30833ccbf588)
14d6d17d0SAndreas Gohr<?php
24d6d17d0SAndreas Gohr/**
34d6d17d0SAndreas Gohr * DokuWiki Plugin acknowledge (Action Component)
44d6d17d0SAndreas Gohr *
54d6d17d0SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
64d6d17d0SAndreas Gohr * @author  Andreas Gohr, Anna Dabrowska <dokuwiki@cosmocode.de>
74d6d17d0SAndreas Gohr */
84d6d17d0SAndreas Gohr
95773dd37SAnna Dabrowskause dokuwiki\Form\Form;
104d6d17d0SAndreas Gohr
114d6d17d0SAndreas Gohrclass action_plugin_acknowledge extends DokuWiki_Action_Plugin
124d6d17d0SAndreas Gohr{
134d6d17d0SAndreas Gohr
14ef3ab392SAndreas Gohr    /** @inheritDoc */
154d6d17d0SAndreas Gohr    public function register(Doku_Event_Handler $controller)
164d6d17d0SAndreas Gohr    {
17ef3ab392SAndreas Gohr        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handlePageSave');
18ef3ab392SAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
19*f09444ffSAndreas Gohr        $controller->register_hook('PLUGIN_SQLITE_DATABASE_UPGRADE', 'AFTER', $this, 'handleUpgrade');
204d6d17d0SAndreas Gohr    }
214d6d17d0SAndreas Gohr
224d6d17d0SAndreas Gohr    /**
23ef3ab392SAndreas Gohr     * Manage page meta data
244d6d17d0SAndreas Gohr     *
25ef3ab392SAndreas Gohr     * Store page last modified date
26ef3ab392SAndreas Gohr     * Handle page deletions
27*f09444ffSAndreas Gohr     * Handle page creations
284d6d17d0SAndreas Gohr     *
29ef3ab392SAndreas Gohr     * @param Doku_Event $event
30ef3ab392SAndreas Gohr     * @param $param
314d6d17d0SAndreas Gohr     */
32ef3ab392SAndreas Gohr    public function handlePageSave(Doku_Event $event, $param)
334d6d17d0SAndreas Gohr    {
34ef3ab392SAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
35ef3ab392SAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
36ef3ab392SAndreas Gohr
37ef3ab392SAndreas Gohr        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_DELETE) {
38*f09444ffSAndreas Gohr            $helper->removePage($event->data['id']); // this cascades to assignments
39ef3ab392SAndreas Gohr        } elseif ($event->data['changeType'] !== DOKU_CHANGE_TYPE_MINOR_EDIT) {
405dee13f7SAnna Dabrowska            $helper->storePageDate($event->data['id'], $event->data['newRevision'], $event->data['newContent']);
414d6d17d0SAndreas Gohr        }
424d6d17d0SAndreas Gohr
43*f09444ffSAndreas Gohr        // Remove page assignees here because the syntax might have been removed
44*f09444ffSAndreas Gohr        // they are readded on metadata rendering if still there
45*f09444ffSAndreas Gohr        $helper->clearPageAssignments($event->data['id']);
46*f09444ffSAndreas Gohr
47*f09444ffSAndreas Gohr        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_CREATE) {
48*f09444ffSAndreas Gohr            // new pages need to have their auto assignments updated based on the existing patterns
49*f09444ffSAndreas Gohr            $helper->setAutoAssignees($event->data['id']);
50*f09444ffSAndreas Gohr        }
51ef3ab392SAndreas Gohr    }
52ef3ab392SAndreas Gohr
53ef3ab392SAndreas Gohr    /**
54ef3ab392SAndreas Gohr     * @param Doku_Event $event
55ef3ab392SAndreas Gohr     * @param $param
56ef3ab392SAndreas Gohr     */
57ef3ab392SAndreas Gohr    public function handleAjax(Doku_Event $event, $param)
58ef3ab392SAndreas Gohr    {
595773dd37SAnna Dabrowska        if ($event->data === 'plugin_acknowledge_assign') {
60ef3ab392SAndreas Gohr            echo $this->html();
61ef3ab392SAndreas Gohr            $event->stopPropagation();
62ef3ab392SAndreas Gohr            $event->preventDefault();
63ef3ab392SAndreas Gohr        }
64ef3ab392SAndreas Gohr    }
65ef3ab392SAndreas Gohr
66ef3ab392SAndreas Gohr    /**
67*f09444ffSAndreas Gohr     * Handle Migration events
68*f09444ffSAndreas Gohr     *
69*f09444ffSAndreas Gohr     * @param Doku_Event $event
70*f09444ffSAndreas Gohr     * @param $param
71*f09444ffSAndreas Gohr     * @return void
72*f09444ffSAndreas Gohr     */
73*f09444ffSAndreas Gohr    public function handleUpgrade(Doku_Event $event, $param)
74*f09444ffSAndreas Gohr    {
75*f09444ffSAndreas Gohr        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'acknowledgement') {
76*f09444ffSAndreas Gohr             return;
77*f09444ffSAndreas Gohr        }
78*f09444ffSAndreas Gohr        $to = $event->data['to'];
79*f09444ffSAndreas Gohr        if($to !== 3) return; // only handle upgrade to version 3
80*f09444ffSAndreas Gohr
81*f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
82*f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
83*f09444ffSAndreas Gohr        $helper->updatePageIndex();
84*f09444ffSAndreas Gohr    }
85*f09444ffSAndreas Gohr
86*f09444ffSAndreas Gohr    /**
87ef3ab392SAndreas Gohr     * Returns the acknowledgment form/confirmation
88ef3ab392SAndreas Gohr     *
89ef3ab392SAndreas Gohr     * @return string The HTML to display
90ef3ab392SAndreas Gohr     */
91ef3ab392SAndreas Gohr    protected function html()
92ef3ab392SAndreas Gohr    {
93ef3ab392SAndreas Gohr        global $INPUT;
94ef3ab392SAndreas Gohr        global $USERINFO;
95ef3ab392SAndreas Gohr        $id = $INPUT->str('id');
962d63bbe3SAnna Dabrowska        $ackSubmitted = $INPUT->bool('ack');
97ef3ab392SAndreas Gohr        $user = $INPUT->server->str('REMOTE_USER');
98ef3ab392SAndreas Gohr        if ($id === '' || $user === '') return '';
99ef3ab392SAndreas Gohr
100ef3ab392SAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
101ef3ab392SAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
102ef3ab392SAndreas Gohr
1035773dd37SAnna Dabrowska        if ($ackSubmitted) {
1045773dd37SAnna Dabrowska            $helper->saveAcknowledgement($id, $user);
1055773dd37SAnna Dabrowska        }
1065773dd37SAnna Dabrowska
107ef3ab392SAndreas Gohr        $ack = $helper->hasUserAcknowledged($id, $user);
108209df5deSAndreas Gohr
109209df5deSAndreas Gohr        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
110209df5deSAndreas Gohr        $html .= inlineSVG(__DIR__ . '/admin.svg');
111209df5deSAndreas Gohr        $html .= '</div>';
112209df5deSAndreas Gohr
113ef3ab392SAndreas Gohr        if ($ack) {
114ef3ab392SAndreas Gohr
115ef3ab392SAndreas Gohr            $html .= '<div>';
116209df5deSAndreas Gohr            $html .= '<h4>';
117209df5deSAndreas Gohr            $html .= $this->getLang('ackOk');
118209df5deSAndreas Gohr            $html .= '</h4>';
119209df5deSAndreas Gohr            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
120ef3ab392SAndreas Gohr            $html .= '</div>';
121ef3ab392SAndreas Gohr        } elseif ($helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
122209df5deSAndreas Gohr
123ef3ab392SAndreas Gohr            $html .= '<div>';
124209df5deSAndreas Gohr            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
125d9a8334dSAnna Dabrowska            $latest = $helper->getLatestUserAcknowledgement($id, $user);
126d9a8334dSAnna Dabrowska            if ($latest) {
127d9a8334dSAnna Dabrowska                $html .= '<a href="'
128d9a8334dSAnna Dabrowska                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
129d9a8334dSAnna Dabrowska                    . sprintf($this->getLang('ackDiff'), dformat($latest))
130d9a8334dSAnna Dabrowska                    . '</a><br>';
131d9a8334dSAnna Dabrowska            }
132d9a8334dSAnna Dabrowska
133209df5deSAndreas Gohr            $form = new Form(['id' => 'ackForm']);
134209df5deSAndreas Gohr            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
135209df5deSAndreas Gohr            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">' . $this->getLang('ackButton') . '</button>');
136209df5deSAndreas Gohr
1375773dd37SAnna Dabrowska            $html .= $form->toHTML();
138ef3ab392SAndreas Gohr            $html .= '</div>';
139ef3ab392SAndreas Gohr        }
140ef3ab392SAndreas Gohr
141ef3ab392SAndreas Gohr        return $html;
142ef3ab392SAndreas Gohr    }
1434d6d17d0SAndreas Gohr}
144