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_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 $conf['lang'] = array_shift($ex); 130 //$this->lang_code = $conf['lang']; 131 $this->localised = false; 132 } 133 //throw out "bez" 134 array_shift($ex); 135 136 $this->action = array_shift($ex); 137 138 if (count($ex) % 2 !== 0) { 139 throw new Exception('invalid params'); 140 } 141 142 for ($i = 0; $i < count($ex); $i += 2) { 143 $this->params[$ex[$i]] = $ex[$i+1]; 144 } 145 146 $this->setupLocale(); 147 $this->createObjects(); 148 } 149 150 /** 151 * handle ajax requests 152 */ 153 public function _ajax_call(Doku_Event $event, $param) { 154 global $auth; 155 if ($event->data !== 'plugin_bez') { 156 return; 157 } 158 //no other ajax call handlers needed 159 $event->stopPropagation(); 160 $event->preventDefault(); 161 162 } 163 164 public function tpl_pagetools_display(Doku_Event $event, $param) { 165 if ($this->action !== '') { 166 $event->preventDefault(); 167 } 168 } 169 170 protected $prevent_rendering = false; 171 172 public function action_act_preprocess(Doku_Event $event, $param) 173 { 174 global $conf; 175 176 if ($this->action === '') { 177 return; 178 } 179 180 $event->preventDefault(); 181 try { 182 $this->flush_notifications(); 183 184 $ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php"; 185 186 if (file_exists($ctl)) { 187 include $ctl; 188 } 189 } catch(bez\meta\ValidationException $e) { 190 foreach ($e->get_errors() as $field => $error_code) { 191 $lang = $this->getLang($field); 192 if ($lang != '') { 193 $field = $lang; 194 } 195 $this->add_error( 196 $this->getLang('validate_' . $error_code), 197 $field); 198 } 199 200 $this->tpl->set_values($_POST); 201 202 } catch(bez\meta\PermissionDeniedException $e) { 203 dbglog('plugin_bez', $e); 204 header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login'); 205 } catch(Exception $e) { 206 dbglog('plugin_bez', $e); 207 if ($conf['allowdebug']) { 208 dbg($e); 209 } else { 210 msg($e->getMessage(), -1); 211 } 212 $this->prevent_rendering = true; 213 } 214 } 215 216 public function tpl_act_render($event, $param) 217 { 218 global $conf; 219 220 if ($this->action === '') { 221 return false; 222 } 223 $event->preventDefault(); 224 225 if ($this->prevent_rendering) return; 226 227 try { 228 229 foreach ($this->errors as $error) { 230 echo '<div class="error">'; 231 if ($error['header'] === NULL) { 232 echo $error['value']; 233 } else { 234 echo '<strong>'.$error['header'].'</strong>: '.$error['value']; 235 } 236 echo '</div>'; 237 } 238 239 foreach ($this->notifications as $note) { 240 echo '<div class="info">'; 241 if ($note['header'] === NULL) { 242 echo $note['value']; 243 } else { 244 echo $note['header'].': <strong>'.$note['value'].'</strong>'; 245 } 246 echo '</div>'; 247 } 248 249 $this->bez_tpl_include(str_replace('/', '', $this->get_action())); 250 251 } catch(bez\meta\PermissionDeniedException $e) { 252 dbglog('plugin_bez', $e); 253 } catch(Exception $e) { 254 /*exception*/ 255 dbglog('plugin_bez', $e); 256 if ($conf['allowdebug']) { 257 dbg($e); 258 } 259 } 260 } 261} 262