1<?php 2 3use \dokuwiki\plugin\bez; 4 5if(!defined('DOKU_INC')) die(); 6 7define('BEZ_NOTIFICATIONS_COOKIE_NAME', 'bez_notifications'); 8 9class action_plugin_bez_default extends action_plugin_bez_base { 10 11 protected $action = ''; 12 protected $params = array(); 13 14 protected $notifications = array(); 15 16 protected $errors = array(); 17 18 public function get_action() { 19 return $this->action; 20 } 21 22 public function get_param($id, $default='') { 23 return (isset($this->params[$id]) ? $this->params[$id] : $default); 24 } 25 26 private function add_notification($value, $header=NULL) { 27 if (isset($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME])) { 28 $notifs = unserialize($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME]); 29 } else { 30 $notifs = array(); 31 } 32 $notifs[] = array('value' => $value, 'header' => $header); 33 setcookie(BEZ_NOTIFICATIONS_COOKIE_NAME, serialize($notifs)); 34 } 35 36 private function flush_notifications() { 37 if (!isset($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME])) { 38 return array(); 39 } 40 $this->notifications = unserialize($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME]); 41 42 //remove cookie 43 setcookie(BEZ_NOTIFICATIONS_COOKIE_NAME, serialize(array())); 44 } 45 46 private function add_error($value, $header=NULL) { 47 $this->errors[] = array('value' => $value, 'header' => $header); 48 } 49 50 /** 51 * Register its handlers with the DokuWiki's event controller 52 */ 53 public function register(Doku_Event_Handler $controller) 54 { 55 $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'setup_id'); 56 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'setup_enviroment'); 57 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'action_act_preprocess'); 58 $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'tpl_act_render'); 59 $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'tpl_pagetools_display'); 60 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'include_dependencies', array()); 61 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call'); 62 63 $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source'); 64 $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications'); 65 $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies'); 66 } 67 68 public function include_dependencies(Doku_Event $event) { 69 // Adding a stylesheet 70 $event->data["link"][] = array ( 71 "type" => "text/css", 72 "rel" => "stylesheet", 73 "href" => DOKU_BASE. 74 "lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.css", 75 ); 76 77 // Adding a JavaScript File 78 $event->data["script"][] = array ( 79 "type" => "text/javascript", 80 "src" => DOKU_BASE. 81 "lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.min.js", 82 "_data" => "", 83 ); 84 85 // Adding a JavaScript File 86 $event->data["script"][] = array ( 87 "type" => "text/javascript", 88 "src" => DOKU_BASE. 89 "lib/plugins/bez/lib/jquery.datepair/datepair.js", 90 "_data" => "", 91 ); 92 93 // Adding a JavaScript File 94 $event->data["script"][] = array ( 95 "type" => "text/javascript", 96 "src" => DOKU_BASE. 97 "lib/plugins/bez/lib/jquery.datepair/jquery.datepair.js", 98 "_data" => "", 99 ); 100 101 $event->data["link"][] = array ( 102 "type" => "text/css", 103 "rel" => "stylesheet", 104 "href" => DOKU_BASE. 105 "lib/plugins/bez/lib/jquery.form-validator/theme-default.min.css", 106 ); 107 108 109 $event->data["script"][] = array ( 110 "type" => "text/javascript", 111 "src" => DOKU_BASE. 112 "lib/plugins/bez/lib/jquery.form-validator/jquery.form-validator.min.js", 113 "_data" => "", 114 ); 115 } 116 117 public function setup_id(Doku_Event $event, $param) { 118 global $INFO, $ID; 119 120 $id = $_GET['id']; 121 $ex = explode(':', $id); 122 123 //check if we process BEZ 124 if ($ex[0] !== 'bez' && $ex[1] !== 'bez') { 125 return; 126 } 127 128 $INFO['id'] = $id; 129 $ID = $id; 130 } 131 132 public function setup_enviroment(Doku_Event $event, $param) { 133 global $ACT, $conf, $ID; 134 135 if ($ACT !== 'show') { 136 return; 137 } 138 139 $ex = explode(':', $ID); 140 141 //check if we process BEZ 142 if ($ex[0] !== 'bez' && $ex[1] !== 'bez') { 143 return; 144 } 145 146 if ($ex[1] === 'bez') { 147 //pl is default language 148 if ($ex[0] == 'pl') { 149 //throw out "pl" and "bez" 150 array_shift($ex); 151 array_shift($ex); 152 153 $url = call_user_func_array(array($this, 'url'), $ex); 154 header("Location: $url"); 155 } 156 $conf['lang'] = array_shift($ex); 157 158 $this->localised = false; 159 } 160 //throw out "bez" 161 array_shift($ex); 162 163 $this->action = array_shift($ex); 164 165 if (count($ex) % 2 !== 0) { 166 throw new Exception('invalid params'); 167 } 168 169 for ($i = 0; $i < count($ex); $i += 2) { 170 $this->params[$ex[$i]] = $ex[$i+1]; 171 } 172 173 $this->setupLocale(); 174 $this->createObjects(); 175 176 if (empty($conf['baseurl'])) { 177 msg($this->getLang('info set baseurl')); 178 } 179 } 180 181 /** 182 * handle ajax requests 183 */ 184 public function _ajax_call(Doku_Event $event, $param) { 185 global $auth; 186 if ($event->data !== 'plugin_bez') { 187 return; 188 } 189 //no other ajax call handlers needed 190 $event->stopPropagation(); 191 $event->preventDefault(); 192 193 } 194 195 public function tpl_pagetools_display(Doku_Event $event, $param) { 196 if ($this->action !== '') { 197 $event->preventDefault(); 198 } 199 } 200 201 protected $prevent_rendering = false; 202 203 public function action_act_preprocess(Doku_Event $event, $param) 204 { 205 global $conf; 206 207 if ($this->action === '') { 208 return; 209 } 210 211 $event->preventDefault(); 212 try { 213 $this->flush_notifications(); 214 215 $ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php"; 216 217 if (file_exists($ctl)) { 218 include $ctl; 219 } 220 } catch(bez\meta\ValidationException $e) { 221 foreach ($e->get_errors() as $field => $error_code) { 222 $lang = $this->getLang($field); 223 if ($lang != '') { 224 $field = $lang; 225 } 226 $this->add_error( 227 $this->getLang('validate_' . $error_code), 228 $field); 229 } 230 231 $this->tpl->set_values($_POST); 232 233 } catch(bez\meta\PermissionDeniedException $e) { 234 dbglog('plugin_bez', $e); 235 header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login'); 236 } catch (\PHPMailer\PHPMailer\Exception $e) { 237 msg($e->getMessage(), -1); 238 } catch(Exception $e) { 239 dbglog('plugin_bez', $e); 240 if ($conf['allowdebug']) { 241 dbg($e); 242 } else { 243 msg($e->getMessage(), -1); 244 } 245 $this->prevent_rendering = true; 246 } 247 } 248 249 public function tpl_act_render($event, $param) 250 { 251 global $conf; 252 253 if ($this->action === '') { 254 return false; 255 } 256 $event->preventDefault(); 257 258 if ($this->prevent_rendering) return; 259 260 try { 261 262 foreach ($this->errors as $error) { 263 echo '<div class="error">'; 264 if ($error['header'] === NULL) { 265 echo $error['value']; 266 } else { 267 echo '<strong>'.$error['header'].'</strong>: '.$error['value']; 268 } 269 echo '</div>'; 270 } 271 272 foreach ($this->notifications as $note) { 273 echo '<div class="info">'; 274 if ($note['header'] === NULL) { 275 echo $note['value']; 276 } else { 277 echo $note['header'].': <strong>'.$note['value'].'</strong>'; 278 } 279 echo '</div>'; 280 } 281 282 $this->bez_tpl_include(str_replace('/', '', $this->get_action())); 283 284 } catch(bez\meta\PermissionDeniedException $e) { 285 dbglog('plugin_bez', $e); 286 } catch(Exception $e) { 287 /*exception*/ 288 dbglog('plugin_bez', $e); 289 if ($conf['allowdebug']) { 290 dbg($e); 291 } 292 } 293 } 294 295 public function add_notifications_source(Doku_Event $event) 296 { 297 $event->data[] = 'bez:problems_coming'; 298 $event->data[] = 'bez:problems_outdated'; 299 $event->data[] = 'bez:tasks_coming'; 300 $event->data[] = 'bez:tasks_outdated'; 301 } 302 303 public function add_notification_cache_dependencies(Doku_Event $event) 304 { 305 if (!preg_grep('/^bez:.*/', $event->data['plugins'])) return; 306 307 /** @var \helper_plugin_bez_db $db_helper */ 308 $db_helper = plugin_load('helper', 'bez_db'); 309 $event->data['dependencies'][] = $db_helper->getDB()->getAdapter()->getDbFile(); 310 } 311 312 public function add_notifications(Doku_Event $event) 313 { 314 if (!preg_grep('/^bez:.*/', $event->data['plugins'])) return; 315 316 $user = $event->data['user']; 317 $this->createObjects(true); 318 319 if (in_array('bez:problems_coming', $event->data['plugins'])) { 320 $threads = $this->get_model()->factory('thread')->get_all(array( 321 'type' => 'issue', 322 'priority' => '1', 323 'coordinator' => $user 324 )); 325 /** @var bez\mdl\Thread $thread */ 326 foreach ($threads as $thread) { 327 $link = '<a href="' . $this->url('thread', 'id', $thread->id) . '">'; 328 $link .= '#' . $thread->id; 329 $link .= '</a>'; 330 331 $full = sprintf($this->getLang('notification problems_coming'), $link); 332 $event->data['notifications'][] = [ 333 'plugin' => 'bez:problems_coming', 334 'id' => 'thread:' . $thread->id, 335 'full' => $full, 336 'brief' => $link, 337 'timestamp' => strtotime($thread->last_activity_date) 338 ]; 339 } 340 } 341 342 if (in_array('bez:problems_outdated', $event->data['plugins'])) { 343 $threads = $this->get_model()->threadFactory->get_all(array( 344 'type' => 'issue', 345 'priority' => '2', 346 'coordinator' => $user 347 )); 348 /** @var bez\mdl\Thread $thread */ 349 foreach ($threads as $thread) { 350 $link = '<a href="' . $this->url('thread', 'id', $thread->id) . '">'; 351 $link .= '#' . $thread->id; 352 $link .= '</a>'; 353 354 $full = sprintf($this->getLang('notification problems_outdated'), $link); 355 $event->data['notifications'][] = [ 356 'plugin' => 'bez:problems_outdated', 357 'id' => 'thread:' . $thread->id, 358 'full' => $full, 359 'brief' => $link, 360 'timestamp' => strtotime($thread->last_activity_date) 361 ]; 362 } 363 } 364 365 if (in_array('bez:tasks_coming', $event->data['plugins'])) { 366 $tasks = $this->get_model()->factory('task')->get_all(array( 367 'priority' => '1', 368 'assignee' => $user 369 )); 370 /** @var bez\mdl\Thread $thread */ 371 foreach ($tasks as $task) { 372 $link = '<a href="' . $this->url('task', 'tid', $task->id) . '">'; 373 $link .= '#z' . $task->id; 374 $link .= '</a>'; 375 376 $full = sprintf($this->getLang('notification tasks_coming'), $link); 377 $event->data['notifications'][] = [ 378 'plugin' => 'bez:tasks_coming', 379 'id' => 'task:' . $task->id, 380 'full' => $full, 381 'brief' => $link, 382 'timestamp' => strtotime($task->plan_date) 383 ]; 384 } 385 } 386 387 if (in_array('bez:tasks_outdated', $event->data['plugins'])) { 388 $tasks = $this->get_model()->factory('task')->get_all(array( 389 'priority' => '2', 390 'assignee' => $user 391 )); 392 /** @var bez\mdl\Thread $thread */ 393 foreach ($tasks as $task) { 394 $link = '<a href="' . $this->url('task', 'tid', $task->id) . '">'; 395 $link .= '#z' . $task->id; 396 $link .= '</a>'; 397 398 $full = sprintf($this->getLang('notification tasks_outdated'), $link); 399 $event->data['notifications'][] = [ 400 'plugin' => 'bez:tasks_outdated', 401 'id' => 'task:' . $task->id, 402 'full' => $full, 403 'brief' => $link, 404 'timestamp' => strtotime($task->plan_date) 405 ]; 406 } 407 } 408 } 409} 410