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 63 public function include_dependencies(Doku_Event $event) { 64 // Adding a stylesheet 65 $event->data["link"][] = array ( 66 "type" => "text/css", 67 "rel" => "stylesheet", 68 "href" => DOKU_BASE. 69 "lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.css", 70 ); 71 72 // Adding a JavaScript File 73 $event->data["script"][] = array ( 74 "type" => "text/javascript", 75 "src" => DOKU_BASE. 76 "lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.min.js", 77 "_data" => "", 78 ); 79 80 // Adding a JavaScript File 81 $event->data["script"][] = array ( 82 "type" => "text/javascript", 83 "src" => DOKU_BASE. 84 "lib/plugins/bez/lib/jquery.datepair/datepair.js", 85 "_data" => "", 86 ); 87 88 // Adding a JavaScript File 89 $event->data["script"][] = array ( 90 "type" => "text/javascript", 91 "src" => DOKU_BASE. 92 "lib/plugins/bez/lib/jquery.datepair/jquery.datepair.js", 93 "_data" => "", 94 ); 95 96 $event->data["link"][] = array ( 97 "type" => "text/css", 98 "rel" => "stylesheet", 99 "href" => DOKU_BASE. 100 "lib/plugins/bez/lib/jquery.form-validator/theme-default.min.css", 101 ); 102 103 104 $event->data["script"][] = array ( 105 "type" => "text/javascript", 106 "src" => DOKU_BASE. 107 "lib/plugins/bez/lib/jquery.form-validator/jquery.form-validator.min.js", 108 "_data" => "", 109 ); 110 } 111 112 public function setup_enviroment(Doku_Event $event, $param) { 113 global $ACT, $auth, $conf, $INFO; 114 115 if ($ACT !== 'show') { 116 return; 117 } 118 119 $id = $_GET['id']; 120 $ex = explode(':', $id); 121 122 //check if we process BEZ 123 if ($ex[0] !== 'bez' && $ex[1] !== 'bez') { 124 return; 125 } 126 127 128 if ($ex[1] === 'bez') { 129 //pl is default language 130 if ($ex[0] == 'pl') { 131 //throw out "pl" and "bez" 132 array_shift($ex); 133 array_shift($ex); 134 135 $url = call_user_func_array(array($this, 'url'), $ex); 136 header("Location: $url"); 137 } 138 $conf['lang'] = array_shift($ex); 139 140 $this->localised = false; 141 } 142 //throw out "bez" 143 array_shift($ex); 144 145 $this->action = array_shift($ex); 146 147 if (count($ex) % 2 !== 0) { 148 throw new Exception('invalid params'); 149 } 150 151 for ($i = 0; $i < count($ex); $i += 2) { 152 $this->params[$ex[$i]] = $ex[$i+1]; 153 } 154 155 $this->setupLocale(); 156 $this->createObjects(); 157 158 if (empty($conf['baseurl'])) { 159 msg($this->getLang('info set baseurl')); 160 } 161 } 162 163 /** 164 * handle ajax requests 165 */ 166 public function _ajax_call(Doku_Event $event, $param) { 167 global $auth; 168 if ($event->data !== 'plugin_bez') { 169 return; 170 } 171 //no other ajax call handlers needed 172 $event->stopPropagation(); 173 $event->preventDefault(); 174 175 } 176 177 public function tpl_pagetools_display(Doku_Event $event, $param) { 178 if ($this->action !== '') { 179 $event->preventDefault(); 180 } 181 } 182 183 protected $prevent_rendering = false; 184 185 public function action_act_preprocess(Doku_Event $event, $param) 186 { 187 global $conf; 188 189 if ($this->action === '') { 190 return; 191 } 192 193 $event->preventDefault(); 194 try { 195 $this->flush_notifications(); 196 197 $ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php"; 198 199 if (file_exists($ctl)) { 200 include $ctl; 201 } 202 } catch(bez\meta\ValidationException $e) { 203 foreach ($e->get_errors() as $field => $error_code) { 204 $lang = $this->getLang($field); 205 if ($lang != '') { 206 $field = $lang; 207 } 208 $this->add_error( 209 $this->getLang('validate_' . $error_code), 210 $field); 211 } 212 213 $this->tpl->set_values($_POST); 214 215 } catch(bez\meta\PermissionDeniedException $e) { 216 dbglog('plugin_bez', $e); 217 header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login'); 218 } catch(Exception $e) { 219 dbglog('plugin_bez', $e); 220 if ($conf['allowdebug']) { 221 dbg($e); 222 } else { 223 msg($e->getMessage(), -1); 224 } 225 $this->prevent_rendering = true; 226 } 227 } 228 229 public function tpl_act_render($event, $param) 230 { 231 global $conf; 232 233 if ($this->action === '') { 234 return false; 235 } 236 $event->preventDefault(); 237 238 if ($this->prevent_rendering) return; 239 240 try { 241 242 foreach ($this->errors as $error) { 243 echo '<div class="error">'; 244 if ($error['header'] === NULL) { 245 echo $error['value']; 246 } else { 247 echo '<strong>'.$error['header'].'</strong>: '.$error['value']; 248 } 249 echo '</div>'; 250 } 251 252 foreach ($this->notifications as $note) { 253 echo '<div class="info">'; 254 if ($note['header'] === NULL) { 255 echo $note['value']; 256 } else { 257 echo $note['header'].': <strong>'.$note['value'].'</strong>'; 258 } 259 echo '</div>'; 260 } 261 262 $this->bez_tpl_include(str_replace('/', '', $this->get_action())); 263 264 } catch(bez\meta\PermissionDeniedException $e) { 265 dbglog('plugin_bez', $e); 266 } catch(Exception $e) { 267 /*exception*/ 268 dbglog('plugin_bez', $e); 269 if ($conf['allowdebug']) { 270 dbg($e); 271 } 272 } 273 } 274} 275