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