xref: /plugin/acknowledge/action.php (revision 35bc8bea4272528a9043098b14e2a1eea0ac4027)
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');
19f09444ffSAndreas 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
27f09444ffSAndreas 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) {
38f09444ffSAndreas 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
43f09444ffSAndreas Gohr        // Remove page assignees here because the syntax might have been removed
44f09444ffSAndreas Gohr        // they are readded on metadata rendering if still there
45f09444ffSAndreas Gohr        $helper->clearPageAssignments($event->data['id']);
46f09444ffSAndreas Gohr
47f09444ffSAndreas Gohr        if ($event->data['changeType'] === DOKU_CHANGE_TYPE_CREATE) {
48f09444ffSAndreas Gohr            // new pages need to have their auto assignments updated based on the existing patterns
49f09444ffSAndreas Gohr            $helper->setAutoAssignees($event->data['id']);
50f09444ffSAndreas 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    /**
67f09444ffSAndreas Gohr     * Handle Migration events
68f09444ffSAndreas Gohr     *
69f09444ffSAndreas Gohr     * @param Doku_Event $event
70f09444ffSAndreas Gohr     * @param $param
71f09444ffSAndreas Gohr     * @return void
72f09444ffSAndreas Gohr     */
73f09444ffSAndreas Gohr    public function handleUpgrade(Doku_Event $event, $param)
74f09444ffSAndreas Gohr    {
75f09444ffSAndreas Gohr        if ($event->data['sqlite']->getAdapter()->getDbname() !== 'acknowledgement') {
76f09444ffSAndreas Gohr            return;
77f09444ffSAndreas Gohr        }
78f09444ffSAndreas Gohr        $to = $event->data['to'];
79f09444ffSAndreas Gohr        if ($to !== 3) return; // only handle upgrade to version 3
80f09444ffSAndreas Gohr
81f09444ffSAndreas Gohr        /** @var helper_plugin_acknowledge $helper */
82f09444ffSAndreas Gohr        $helper = plugin_load('helper', 'acknowledge');
83f09444ffSAndreas Gohr        $helper->updatePageIndex();
84f09444ffSAndreas Gohr    }
85f09444ffSAndreas Gohr
86f09444ffSAndreas 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
103*35bc8beaSAndreas Gohr        // only display for users assigned to the page
104*35bc8beaSAndreas Gohr        if (!$helper->isUserAssigned($id, $user, $USERINFO['grps'])) {
105*35bc8beaSAndreas Gohr            return '';
106*35bc8beaSAndreas Gohr        }
107*35bc8beaSAndreas Gohr
1085773dd37SAnna Dabrowska        if ($ackSubmitted) {
1095773dd37SAnna Dabrowska            $helper->saveAcknowledgement($id, $user);
1105773dd37SAnna Dabrowska        }
1115773dd37SAnna Dabrowska
112ef3ab392SAndreas Gohr        $ack = $helper->hasUserAcknowledged($id, $user);
113209df5deSAndreas Gohr
114209df5deSAndreas Gohr        $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">';
115209df5deSAndreas Gohr        $html .= inlineSVG(__DIR__ . '/admin.svg');
116209df5deSAndreas Gohr        $html .= '</div>';
117209df5deSAndreas Gohr
118ef3ab392SAndreas Gohr        if ($ack) {
119ef3ab392SAndreas Gohr            $html .= '<div>';
120209df5deSAndreas Gohr            $html .= '<h4>';
121209df5deSAndreas Gohr            $html .= $this->getLang('ackOk');
122209df5deSAndreas Gohr            $html .= '</h4>';
123209df5deSAndreas Gohr            $html .= sprintf($this->getLang('ackGranted'), dformat($ack));
124ef3ab392SAndreas Gohr            $html .= '</div>';
125*35bc8beaSAndreas Gohr        } else {
126ef3ab392SAndreas Gohr            $html .= '<div>';
127209df5deSAndreas Gohr            $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>';
128d9a8334dSAnna Dabrowska            $latest = $helper->getLatestUserAcknowledgement($id, $user);
129d9a8334dSAnna Dabrowska            if ($latest) {
130d9a8334dSAnna Dabrowska                $html .= '<a href="'
131d9a8334dSAnna Dabrowska                    . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">'
132d9a8334dSAnna Dabrowska                    . sprintf($this->getLang('ackDiff'), dformat($latest))
133d9a8334dSAnna Dabrowska                    . '</a><br>';
134d9a8334dSAnna Dabrowska            }
135d9a8334dSAnna Dabrowska
136209df5deSAndreas Gohr            $form = new Form(['id' => 'ackForm']);
137209df5deSAndreas Gohr            $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required');
138209df5deSAndreas Gohr            $form->addHTML('<br><button type="submit" name="acksubmit" id="ack-submit">' . $this->getLang('ackButton') . '</button>');
139209df5deSAndreas Gohr
1405773dd37SAnna Dabrowska            $html .= $form->toHTML();
141ef3ab392SAndreas Gohr            $html .= '</div>';
142ef3ab392SAndreas Gohr        }
143ef3ab392SAndreas Gohr
144ef3ab392SAndreas Gohr        return $html;
145ef3ab392SAndreas Gohr    }
1464d6d17d0SAndreas Gohr}
147