1<?php 2 3/** 4 * DokuWiki Plugin acknowledge (AJAX 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_ajax extends ActionPlugin 16{ 17 /** @inheritDoc */ 18 public function register(EventHandler $controller) 19 { 20 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxAcknowledge'); 21 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxAutocomplete'); 22 } 23 24 /** 25 * @param Event $event 26 * @param $param 27 */ 28 public function handleAjaxAcknowledge(Event $event, $param) 29 { 30 if ($event->data === 'plugin_acknowledge_acknowledge') { 31 $event->stopPropagation(); 32 $event->preventDefault(); 33 34 global $INPUT; 35 $id = $INPUT->str('id'); 36 37 if (page_exists($id)) { 38 echo $this->html(); 39 } 40 } 41 } 42 43 /** 44 * @param Event $event 45 * @return void 46 */ 47 public function handleAjaxAutocomplete(Event $event) 48 { 49 if ($event->data === 'plugin_acknowledge_autocomplete') { 50 if (!checkSecurityToken()) return; 51 52 global $INPUT; 53 54 $event->stopPropagation(); 55 $event->preventDefault(); 56 57 /** @var helper_plugin_acknowledge $hlp */ 58 $hlp = $this->loadHelper('acknowledge'); 59 60 $found = []; 61 62 if ($INPUT->has('user')) { 63 $search = $INPUT->str('user'); 64 $knownUsers = $hlp->getUsers(); 65 $found = array_filter($knownUsers, function ($user) use ($search) { 66 return (strstr(strtolower($user['label']), strtolower($search))) !== false ? $user : null; 67 }); 68 } 69 70 if ($INPUT->has('pg')) { 71 $search = $INPUT->str('pg'); 72 $pages = ft_pageLookup($search, true); 73 $found = array_map(function ($id, $title) { 74 return ['value' => $id, 'label' => $title ?? $id]; 75 }, array_keys($pages), array_values($pages)); 76 } 77 78 header('Content-Type: application/json'); 79 80 echo json_encode($found); 81 } 82 } 83 84 /** 85 * Returns the acknowledgment form/confirmation 86 * 87 * @return string The HTML to display 88 */ 89 protected function html() 90 { 91 global $INPUT; 92 global $USERINFO; 93 $id = $INPUT->str('id'); 94 $ackSubmitted = $INPUT->bool('ack'); 95 $user = $INPUT->server->str('REMOTE_USER'); 96 if ($id === '' || $user === '') return ''; 97 98 /** @var helper_plugin_acknowledge $helper */ 99 $helper = plugin_load('helper', 'acknowledge'); 100 101 // only display for users assigned to the page 102 if (!$helper->isUserAssigned($id, $user, $USERINFO['grps'])) { 103 return ''; 104 } 105 106 // if the approve plugin is active, only show if the page is approved 107 if ($helper->isBlockedByApprove($id)) { 108 return ''; 109 } 110 111 if ($ackSubmitted) { 112 $helper->saveAcknowledgement($id, $user); 113 } 114 115 $ack = $helper->hasUserAcknowledged($id, $user); 116 117 $html = '<div class="' . ($ack ? 'ack' : 'noack') . '">'; 118 $html .= inlineSVG(__DIR__ . '/../admin.svg'); 119 $html .= '</div>'; 120 121 if ($ack) { 122 $html .= '<div>'; 123 $html .= '<h4>'; 124 $html .= $this->getLang('ackOk'); 125 $html .= '</h4>'; 126 $html .= sprintf($this->getLang('ackGranted'), dformat($ack)); 127 $html .= '</div>'; 128 } else { 129 $html .= '<div>'; 130 $html .= '<h4>' . $this->getLang('ackRequired') . '</h4>'; 131 $latest = $helper->getLatestUserAcknowledgement($id, $user); 132 if ($latest) { 133 $html .= '<a href="' 134 . wl($id, ['do' => 'diff', 'at' => $latest], false, '&') . '">' 135 . sprintf($this->getLang('ackDiff'), dformat($latest)) 136 . '</a><br>'; 137 } 138 139 $form = new Form(['id' => 'ackForm']); 140 $form->addCheckbox('ack', $this->getLang('ackText'))->attr('required', 'required'); 141 $form->addHTML( 142 '<br><button type="submit" name="acksubmit" id="ack-submit">' 143 . $this->getLang('ackButton') 144 . '</button>' 145 ); 146 147 $html .= $form->toHTML(); 148 $html .= '</div>'; 149 } 150 151 return $html; 152 } 153} 154