1<?php 2/** 3 * DokuWiki Plugin todo_list (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 */ 7 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11/** 12 * Class syntax_plugin_todo_list 13 */ 14class syntax_plugin_todo_list extends syntax_plugin_todo_todo { 15 16 /** 17 * @return string Syntax mode type 18 */ 19 public function getType() { 20 return 'substition'; 21 } 22 23 /** 24 * @return string Paragraph type 25 */ 26 public function getPType() { 27 return 'block'; 28 } 29 30 /** 31 * @return int Sort order - Low numbers go before high numbers 32 */ 33 public function getSort() { 34 return 250; 35 } 36 37 /** 38 * Connect lookup pattern to lexer. 39 * 40 * @param string $mode Parser mode 41 */ 42 public function connectTo($mode) { 43 $this->Lexer->addSpecialPattern('~~TODOLIST[^~]*~~', $mode, 'plugin_todo_list'); 44 } 45 46 /** 47 * Handle matches of the todolist syntax 48 * 49 * @param string $match The match of the syntax 50 * @param int $state The state of the handler 51 * @param int $pos The position in the document 52 * @param Doku_Handler $handler The handler 53 * @return array Data for the renderer 54 */ 55 public function handle($match, $state, $pos, Doku_Handler &$handler) { 56 57 $options = substr($match, 10, -2); // strip markup 58 $options = explode(' ', $options); 59 $data = array( 60 'header' => $this->getConf("Header"), 61 'completed' => 'all', 62 'assigned' => 'all', 63 'ns' => 'all', 64 'showdate' => $this->getConf("ShowdateList"), 65 'checkbox' => $this->getConf("Checkbox"), 66 'username' => $this->getConf("Username"), 67 ); 68 $allowedvalues = array('yes', 'no'); 69 foreach($options as $option) { 70 @list($key, $value) = explode(':', $option, 2); 71 switch($key) { 72 case 'header': // how should the header be rendered? 73 if(in_array($value, array('id', 'firstheader', 'none'))) { 74 $data['header'] = $value; 75 } 76 break; 77 case 'showdate': 78 if(in_array($value, $allowedvalues)) { 79 $data['showdate'] = ($value == 'yes'); 80 } 81 break; 82 case 'checkbox': // should checkbox be rendered? 83 if(in_array($value, $allowedvalues)) { 84 $data['checkbox'] = ($value == 'yes'); 85 } 86 break; 87 case 'completed': 88 if(in_array($value, $allowedvalues)) { 89 $data['completed'] = ($value == 'yes'); 90 } 91 break; 92 case 'username': // how should the username be rendered? 93 if(in_array($value, array('user', 'real', 'none'))) { 94 $data['username'] = $value; 95 } 96 break; 97 case 'assigned': 98 if(in_array($value, $allowedvalues)) { 99 $data['assigned'] = ($value == 'yes'); 100 break; 101 } 102 //assigned? 103 $data['assigned'] = explode(',', $value); 104 // @date 20140317 le: if check for logged in user, also check for logged in user email address 105 if( in_array( '@@USER@@', $data['assigned'] ) ) { 106 $data['assigned'][] = '@@MAIL@@'; 107 } 108 $data['assigned'] = array_map( array($this,"__todolistTrimUser"), $data['assigned'] ); 109 break; 110 case 'ns': 111 $data['ns'] = $value; 112 break; 113 case 'startbefore': 114 list($data['startbefore'], $data['startignore']) = $this->analyseDate($value); 115 break; 116 case 'startafter': 117 list($data['startafter'], $data['startignore']) = $this->analyseDate($value); 118 break; 119 case 'duebefore': 120 list($data['duebefore'], $data['dueignore']) = $this->analyseDate($value); 121 break; 122 case 'dueafter': 123 list($data['dueafter'], $data['dueignore']) = $this->analyseDate($value); 124 break; 125 case 'completedbefore': 126 $data['completedbefore'] = $this->analyseDate($value)[0]; 127 break; 128 case 'completedafter': 129 $data['completedafter'] = $this->analyseDate($value)[0]; 130 break; 131 } 132 } 133 return $data; 134 } 135 136 /** 137 * Render xhtml output or metadata 138 * 139 * @param string $mode Renderer mode (supported modes: xhtml) 140 * @param Doku_Renderer $renderer The renderer 141 * @param array $data The data from the handler() function 142 * @return bool If rendering was successful. 143 */ 144 public function render($mode, Doku_Renderer &$renderer, $data) { 145 global $conf; 146 147 if($mode != 'xhtml') return false; 148 /** @var Doku_Renderer_xhtml $renderer */ 149 150 $opts['pattern'] = '/<todo([^>]*)>(.*)<\/todo[\W]*?>/'; //all todos in a wiki page 151 //TODO check if storing subpatterns doesn't cost too much resources 152 153 // search(&$data, $base, $func, $opts,$dir='',$lvl=1,$sort='natural') 154 search($todopages, $conf['datadir'], array($this, 'search_todos'), $opts); //browse wiki pages with callback to search_pattern 155 156 $todopages = $this->filterpages($todopages, $data); 157 158 $this->htmlTodoTable($renderer, $todopages, $data); 159 160 return true; 161 } 162 163 /** 164 * Custom search callback 165 * 166 * This function is called for every found file or 167 * directory. When a directory is given to the function it has to 168 * decide if this directory should be traversed (true) or not (false). 169 * Return values for files are ignored 170 * 171 * All functions should check the ACL for document READ rights 172 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this 173 * would break the recursion (You can have an nonreadable dir over a readable 174 * one deeper nested) also make sure to check the file type (for example 175 * in case of lockfiles). 176 * 177 * @param array &$data - Reference to the result data structure 178 * @param string $base - Base usually $conf['datadir'] 179 * @param string $file - current file or directory relative to $base 180 * @param string $type - Type either 'd' for directory or 'f' for file 181 * @param int $lvl - Current recursion depht 182 * @param array $opts - option array as given to search() 183 * @return bool if this directory should be traversed (true) or not (false). Return values for files are ignored. 184 */ 185 public function search_todos(&$data, $base, $file, $type, $lvl, $opts) { 186 $item['id'] = pathID($file); //get current file ID 187 188 //we do nothing with directories 189 if($type == 'd') return true; 190 191 //only search txt files 192 if(substr($file, -4) != '.txt') return true; 193 194 //check ACL 195 if(auth_quickaclcheck($item['id']) < AUTH_READ) return false; 196 197 $wikitext = rawWiki($item['id']); //get wiki text 198 199 $item['count'] = preg_match_all($opts['pattern'], $wikitext, $matches); //count how many times appears the pattern 200 if(!empty($item['count'])) { //if it appears at least once 201 $item['matches'] = $matches; 202 $data[] = $item; 203 } 204 return true; 205 } 206 207 /** 208 * Expand assignee-placeholders 209 * 210 * @param $user String to be worked on 211 * @return expanded string 212 */ 213 private function __todolistExpandAssignees($user) { 214 global $USERINFO; 215 if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER') 216 return $_SERVER['REMOTE_USER']; 217 } 218 // @date 20140317 le: check for logged in user email address 219 if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) { 220 return $USERINFO['mail']; 221 } 222 return $user; 223 } 224 225 /** 226 * Trim input if it's a user 227 * 228 * @param $user String to be worked on 229 * @return trimmed string 230 */ 231 private function __todolistTrimUser($user) { 232 //placeholder (inspired by replacement-patterns - see https://www.dokuwiki.org/namespace_templates#replacement_patterns) 233 if( $user == '@@USER@@' || $user == '@@MAIL@@' ) { 234 return $user; 235 } 236 //user 237 return trim(ltrim($user, '@')); 238 } 239 240 /** 241 * filter the pages 242 * 243 * @param $todopages array pages with all todoitems 244 * @param $data array listing parameters 245 * @return array filtered pages 246 */ 247 private function filterpages($todopages, $data) { 248 $pages = array(); 249 foreach($todopages as $page) { 250 $parsepage = 0; 251 if ($data['ns'] == 'all') { 252 // Always return the todo pages 253 $parsepage = 1; 254 } elseif ($data['ns'] == '/') { 255 // Only return the todo page if it's in the root namespace 256 if (strpos($page['id'], ':') === FALSE) $parsepage = 1; 257 } elseif (substr( $page['id'], 0, strlen($data['ns']) ) === $data['ns']) { 258 // Only return the todo page if it starts with the given string 259 $parsepage = 1; 260 } 261 if ($parsepage == 1) { 262 $todos = array(); 263 // contains 3 arrays: an array with complete matches and 2 arrays with subpatterns 264 foreach($page['matches'][1] as $todoindex => $todomatch) { 265 $todo = array_merge(array('todotitle' => trim($page['matches'][2][$todoindex]), 'todoindex' => $todoindex), $this->parseTodoArgs($todomatch), $data); 266 267 if($this->isRequestedTodo($todo)) { $todos[] = $todo; } 268 } 269 if(count($todos) > 0) { 270 $pages[] = array('id' => $page['id'], 'todos' => $todos); 271 } 272 } 273 } 274 return $pages; 275 } 276 277 /** 278 * Create html for table with todos 279 * 280 * @param Doku_Renderer_xhtml $R 281 * @param array $todopages 282 * @param array $data array with rendering options 283 */ 284 private function htmlTodoTable($R, $todopages, $data) { 285 $R->table_open(); 286 foreach($todopages as $page) { 287 if ($data['header']!='none') { 288 $R->tablerow_open(); 289 $R->tableheader_open(); 290 $R->internallink($page['id'], ($data['header']=='firstheader' ? p_get_first_heading($page['id']) : $page['id'])); 291 $R->tableheader_close(); 292 $R->tablerow_close(); 293 } 294 foreach($page['todos'] as $todo) { 295//echo "<pre>";var_dump($todo);echo "</pre>"; 296 $R->tablerow_open(); 297 $R->tablecell_open(); 298 $R->doc .= $this->createTodoItem($R, $page['id'], array_merge($todo, $data)); 299 $R->tablecell_close(); 300 $R->tablerow_close(); 301 } 302 } 303 $R->table_close(); 304 } 305 306 /** 307 * Check the conditions for adding a todoitem 308 * 309 * @param $data array the defined filters 310 * @param $checked bool completion status of task; true: finished, false: open 311 * @param $todouser string user username of user 312 * @return bool if the todoitem should be listed 313 */ 314 /** 315 * Check the conditions for adding a todoitem 316 * 317 * @param $data array the defined filters 318 * @param $checked bool completion status of task; true: finished, false: open 319 * @param $todouser string user username of user 320 * @return bool if the todoitem should be listed 321 */ 322 private function isRequestedTodo($data) { 323 //completion status 324 $condition1 = $data['completed'] === 'all' //all 325 || $data['completed'] === $data['checked']; //yes or no 326 327 // resolve placeholder in assignees 328 $requestedassignees = array(); 329 if(is_array($data['assigned'])) { 330 $requestedassignees = array_map( array($this,"__todolistExpandAssignees"), $data['assigned'] ); 331 } 332 //assigned 333 $condition2 = $condition2 334 || $data['assigned'] === 'all' //all 335 || (is_bool($data['assigned']) && $data['assigned'] == $data['todouser']); //yes or no 336 337 if (!$condition2 && is_array($data['assigned']) && is_array($data['todousers'])) 338 foreach($data['todousers'] as $todouser) { 339 if(in_array($todouser, $requestedassignees)) { $condition2 = true; break; } 340 } 341 342 //compare start/due dates 343 if($condition1 && $condition2) { 344 $condition3s = true; $condition3d = true; 345 if(isset($data['startbefore']) || isset($data['startafter'])) { 346 if(is_object($data['start'])) { 347 if($data['startignore'] != '!') { 348 if(isset($data['startbefore'])) { $condition3s = $condition3s && new DateTime($data['startbefore']) > $data['start']; } 349 if(isset($data['startafter'])) { $condition3s = $condition3s && new DateTime($data['startafter']) < $data['start']; } 350 } 351 } else { 352 if(!$data['startignore'] == '*') { $condition3s = false; } 353 if($data['startignore'] == '!') { $condition3s = false; } 354 } 355 } 356 357 if(isset($data['duebefore']) || isset($data['dueafter'])) { 358 if(is_object($data['due'])) { 359 if($data['dueignore'] != '!') { 360 if(isset($data['duebefore'])) { $condition3d = $condition3d && new DateTime($data['duebefore']) > $data['due']; } 361 if(isset($data['dueafter'])) { $condition3d = $condition3d && new DateTime($data['dueafter']) < $data['due']; } 362 } 363 } else { 364 if(!$data['dueignore'] == '*') { $condition3d = false; } 365 if($data['dueignore'] == '!') { $condition3d = false; } 366 } 367 } 368 $condition3 = $condition3s && $condition3d; 369 } 370 371 // compare completed date 372 $condition4 = true; 373 if(isset($data['completedbefore'])) { 374 $condition4 = $condition4 && new DateTime($data['completedbefore']) > $data['completeddate']; 375 } 376 if(isset($data['completedafter'])) { 377 $condition4 = $condition4 && new DateTime($data['completedafter']) < $data['completeddate']; 378 } 379 380 return $condition1 AND $condition2 AND $condition3 AND $condition4; 381 } 382 383 384 /** 385 * Analyse of relative/absolute Date and return an absolute date 386 * 387 * @param $date string absolute/relative value of the date to analyse 388 * @return array absolute date or actual date if $date is invalid 389 */ 390 private function analyseDate($date) { 391 $result = array($date, ''); 392 if(is_string($date)) { 393 if($date == '!') { 394 $result = array('', '!'); 395 } elseif ($date =='*') { 396 $result = array('', '*'); 397 } else { 398 if(substr($date, -1) == '*') { 399 $date = substr($date, 0, -1); 400 $result = array($date, '*'); 401 } 402 403 if(date('Y-m-d', strtotime($date)) == $date) { 404 $result[0] = $date; 405 } elseif(preg_match('/^[\+\-]\d+$/', $date)) { // check if we have a valid relative value 406 $newdate = date_create(date('Y-m-d')); 407 date_modify($newdate, $date . ' day'); 408 $result[0] = date_format($newdate, 'Y-m-d'); 409 } else { 410 $result[0] = date('Y-m-d'); 411 } 412 } 413 } else { $result[0] = date('Y-m-d'); } 414 415 return $result; 416 } 417 418 419} 420