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