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 'completeduserlist' => 'all', 64 'ns' => 'all', 65 'showdate' => $this->getConf("ShowdateList"), 66 'checkbox' => $this->getConf("Checkbox"), 67 'username' => $this->getConf("Username"), 68 'short' => false, 69 ); 70 $allowedvalues = array('yes', 'no'); 71 foreach($options as $option) { 72 @list($key, $value) = explode(':', $option, 2); 73 switch($key) { 74 case 'header': // how should the header be rendered? 75 if(in_array($value, array('id', 'firstheader', 'none'))) { 76 $data['header'] = $value; 77 } 78 break; 79 case 'short': 80 if(in_array($value, $allowedvalues)) { 81 $data['short'] = ($value == 'yes'); 82 } 83 break; 84 case 'showdate': 85 if(in_array($value, $allowedvalues)) { 86 $data['showdate'] = ($value == 'yes'); 87 } 88 break; 89 case 'checkbox': // should checkbox be rendered? 90 if(in_array($value, $allowedvalues)) { 91 $data['checkbox'] = ($value == 'yes'); 92 } 93 break; 94 case 'completed': 95 if(in_array($value, $allowedvalues)) { 96 $data['completed'] = ($value == 'yes'); 97 } 98 break; 99 case 'username': // how should the username be rendered? 100 if(in_array($value, array('user', 'real', 'none'))) { 101 $data['username'] = $value; 102 } 103 break; 104 case 'assigned': 105 if(in_array($value, $allowedvalues)) { 106 $data['assigned'] = ($value == 'yes'); 107 break; 108 } 109 //assigned? 110 $data['assigned'] = explode(',', $value); 111 // @date 20140317 le: if check for logged in user, also check for logged in user email address 112 if( in_array( '@@USER@@', $data['assigned'] ) ) { 113 $data['assigned'][] = '@@MAIL@@'; 114 } 115 $data['assigned'] = array_map( array($this,"__todolistTrimUser"), $data['assigned'] ); 116 break; 117 case 'completeduser': 118 $data['completeduserlist'] = explode(',', $value); 119 // @date 20140317 le: if check for logged in user, also check for logged in user email address 120 if(in_array('@@USER@@', $data['completeduserlist'])) { 121 $data['completeduserlist'][] = '@@MAIL@@'; 122 } 123 $data['completeduserlist'] = array_map( array($this,"__todolistTrimUser"), $data['completeduserlist'] ); 124 break; 125 case 'ns': 126 $data['ns'] = $value; 127 break; 128 case 'startbefore': 129 list($data['startbefore'], $data['startignore']) = $this->analyseDate($value); 130 break; 131 case 'startafter': 132 list($data['startafter'], $data['startignore']) = $this->analyseDate($value); 133 break; 134 case 'duebefore': 135 list($data['duebefore'], $data['dueignore']) = $this->analyseDate($value); 136 break; 137 case 'dueafter': 138 list($data['dueafter'], $data['dueignore']) = $this->analyseDate($value); 139 break; 140 case 'completedbefore': 141 list($data['completedbefore']) = $this->analyseDate($value); 142 break; 143 case 'completedafter': 144 list($data['completedafter']) = $this->analyseDate($value); 145 break; 146 } 147 } 148 return $data; 149 } 150 151 /** 152 * Render xhtml output or metadata 153 * 154 * @param string $mode Renderer mode (supported modes: xhtml) 155 * @param Doku_Renderer $renderer The renderer 156 * @param array $data The data from the handler() function 157 * @return bool If rendering was successful. 158 */ 159 public function render($mode, Doku_Renderer &$renderer, $data) { 160 global $conf; 161 162 if($mode != 'xhtml') return false; 163 /** @var Doku_Renderer_xhtml $renderer */ 164 165 $opts['pattern'] = '/<todo([^>]*)>(.*)<\/todo[\W]*?>/s'; //all todos in a wiki page 166 $opts['ns'] = $data['ns']; 167 //TODO check if storing subpatterns doesn't cost too much resources 168 169 // search(&$data, $base, $func, $opts,$dir='',$lvl=1,$sort='natural') 170 search($todopages, $conf['datadir'], array($this, 'search_todos'), $opts); //browse wiki pages with callback to search_pattern 171 172 $todopages = $this->filterpages($todopages, $data); 173 174 if($data['short']) { 175 $this->htmlShort($renderer, $todopages, $data); 176 } else { 177 $this->htmlTodoTable($renderer, $todopages, $data); 178 } 179 180 return true; 181 } 182 183 /** 184 * Custom search callback 185 * 186 * This function is called for every found file or 187 * directory. When a directory is given to the function it has to 188 * decide if this directory should be traversed (true) or not (false). 189 * Return values for files are ignored 190 * 191 * All functions should check the ACL for document READ rights 192 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this 193 * would break the recursion (You can have an nonreadable dir over a readable 194 * one deeper nested) also make sure to check the file type (for example 195 * in case of lockfiles). 196 * 197 * @param array &$data - Reference to the result data structure 198 * @param string $base - Base usually $conf['datadir'] 199 * @param string $file - current file or directory relative to $base 200 * @param string $type - Type either 'd' for directory or 'f' for file 201 * @param int $lvl - Current recursion depht 202 * @param array $opts - option array as given to search() 203 * @return bool if this directory should be traversed (true) or not (false). Return values for files are ignored. 204 */ 205 public function search_todos(&$data, $base, $file, $type, $lvl, $opts) { 206 $item['id'] = pathID($file); //get current file ID 207 208 //we do nothing with directories 209 if($type == 'd') return true; 210 211 //only search txt files 212 if(substr($file, -4) != '.txt') return true; 213 214 //check ACL 215 if(auth_quickaclcheck($item['id']) < AUTH_READ) return false; 216 217 // filter namespaces 218 if(!$this->filter_ns($item['id'], $opts['ns'])) return false; 219 220 $wikitext = rawWiki($item['id']); //get wiki text 221 222 // check if ~~NOTODO~~ is set on the page to skip this page 223 if(1 == preg_match('/~~NOTODO~~/', $wikitext)) return false; 224 225 $item['count'] = preg_match_all($opts['pattern'], $wikitext, $matches); //count how many times appears the pattern 226 if(!empty($item['count'])) { //if it appears at least once 227 $item['matches'] = $matches; 228 $data[] = $item; 229 } 230 return true; 231 } 232 233 /** 234 * filter namespaces 235 * 236 * @param $todopages array pages with all todoitems 237 * @param $item string listing parameters 238 * @return boolean if item id is in namespace 239 */ 240 private function filter_ns($item, $ns) { 241 global $ID; 242 // check if we should accept currant namespace+subnamespaces or only subnamespaces 243 $wildsubns = substr($ns, -2) == '.:'; 244 $onlysubns = !$wildsubns && (substr($ns, -1) == ':' || substr($ns, -2) == ':.'); 245// $onlyns = $onlysubns && substr($ns, -1) == '.'; 246 247 // if first char of ns is '.'replace it with current ns 248 if ($ns[0] == '.') { 249 $ns = substr($ID, 0, strrpos($ID, ':')+1).ltrim($ns, '.:'); 250 } 251 $ns = trim($ns, '.:'); 252 $len = strlen($ns); 253 $parsepage = false; 254 255 if ($parsepage = $ns == 'all') { 256 // Always return the todo pages 257 } elseif ($ns == '/') { 258 // Only return the todo page if it's in the root namespace 259 $parsepage = strpos($item, ':') === FALSE; 260 } elseif ($wildsubns) { 261 $p = strpos($item.':', ':', $len+1); 262 $x = substr($item, $len+1, $p-$len); 263 $parsepage = 0 === strpos($item, rtrim($ns.':'.$x, ':').':'); 264 } elseif ($onlysubns) { 265 $parsepage = 0 === strpos($item, $ns.':'); 266 } elseif ($parsepage = substr($item, 0, $len) == $ns) { 267 } 268 return $parsepage; 269 } 270 271 /** 272 * Expand assignee-placeholders 273 * 274 * @param $user String to be worked on 275 * @return expanded string 276 */ 277 private function __todolistExpandAssignees($user) { 278 global $USERINFO; 279 if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER') 280 return $_SERVER['REMOTE_USER']; 281 } 282 // @date 20140317 le: check for logged in user email address 283 if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) { 284 return $USERINFO['mail']; 285 } 286 return $user; 287 } 288 289 /** 290 * Trim input if it's a user 291 * 292 * @param $user String to be worked on 293 * @return trimmed string 294 */ 295 private function __todolistTrimUser($user) { 296 //placeholder (inspired by replacement-patterns - see https://www.dokuwiki.org/namespace_templates#replacement_patterns) 297 if( $user == '@@USER@@' || $user == '@@MAIL@@' ) { 298 return $user; 299 } 300 //user 301 return trim(ltrim($user, '@')); 302 } 303 304 /** 305 * filter the pages 306 * 307 * @param $todopages array pages with all todoitems 308 * @param $data array listing parameters 309 * @return array filtered pages 310 */ 311 private function filterpages($todopages, $data) { 312 $pages = array(); 313 foreach($todopages as $page) { 314 $todos = array(); 315 // contains 3 arrays: an array with complete matches and 2 arrays with subpatterns 316 foreach($page['matches'][1] as $todoindex => $todomatch) { 317 $todo = array_merge(array('todotitle' => trim($page['matches'][2][$todoindex]), 'todoindex' => $todoindex), $this->parseTodoArgs($todomatch), $data); 318 319 if($this->isRequestedTodo($todo)) { $todos[] = $todo; } 320 } 321 if(count($todos) > 0) { 322 $pages[] = array('id' => $page['id'], 'todos' => $todos); 323 } 324 } 325 return $pages; 326 } 327 328 private function htmlShort($R, $todopages, $data) { 329 $done = 0; $todo = 0; 330//echo "<pre>"; 331//print_r($todopages); 332//die; 333 foreach($todopages as $page) { 334 foreach($page['todos'] as $value) { 335 $todo++; 336 if ($value['checked']) { 337 $done++; 338 } 339 } 340 } 341 342 $R->cdata("($done/$todo)"); 343 } 344 345 /** 346 * Create html for table with todos 347 * 348 * @param Doku_Renderer_xhtml $R 349 * @param array $todopages 350 * @param array $data array with rendering options 351 */ 352 private function htmlTodoTable($R, $todopages, $data) { 353 $R->table_open(); 354 foreach($todopages as $page) { 355 if ($data['header']!='none') { 356 $R->tablerow_open(); 357 $R->tableheader_open(); 358 $R->internallink($page['id'], ($data['header']=='firstheader' ? p_get_first_heading($page['id']) : $page['id'])); 359 $R->tableheader_close(); 360 $R->tablerow_close(); 361 } 362 foreach($page['todos'] as $todo) { 363//echo "<pre>";var_dump($todo);echo "</pre>"; 364 $R->tablerow_open(); 365 $R->tablecell_open(); 366 $R->doc .= $this->createTodoItem($R, $page['id'], array_merge($todo, $data)); 367 $R->tablecell_close(); 368 $R->tablerow_close(); 369 } 370 } 371 $R->table_close(); 372 } 373 374 /** 375 * Check the conditions for adding a todoitem 376 * 377 * @param $data array the defined filters 378 * @param $checked bool completion status of task; true: finished, false: open 379 * @param $todouser string user username of user 380 * @return bool if the todoitem should be listed 381 */ 382 /** 383 * Check the conditions for adding a todoitem 384 * 385 * @param $data array the defined filters 386 * @param $checked bool completion status of task; true: finished, false: open 387 * @param $todouser string user username of user 388 * @return bool if the todoitem should be listed 389 */ 390 private function isRequestedTodo($data) { 391 //completion status 392 $condition1 = $data['completed'] === 'all' //all 393 || $data['completed'] === $data['checked']; //yes or no 394 395 // resolve placeholder in assignees 396 $requestedassignees = array(); 397 if(is_array($data['assigned'])) { 398 $requestedassignees = array_map( array($this,"__todolistExpandAssignees"), $data['assigned'] ); 399 } 400 //assigned 401 $condition2 = $condition2 402 || $data['assigned'] === 'all' //all 403 || (is_bool($data['assigned']) && $data['assigned'] == $data['todouser']); //yes or no 404 405 if (!$condition2 && is_array($data['assigned']) && is_array($data['todousers'])) 406 foreach($data['todousers'] as $todouser) { 407 if(in_array($todouser, $requestedassignees)) { $condition2 = true; break; } 408 } 409 410 //completed by 411 if($condition2 && is_array($data['completeduserlist'])) 412 $condition2 = in_array($data['completeduser'], $data['completeduserlist']); 413 414 //compare start/due dates 415 if($condition1 && $condition2) { 416 $condition3s = true; $condition3d = true; 417 if(isset($data['startbefore']) || isset($data['startafter'])) { 418 if(is_object($data['start'])) { 419 if($data['startignore'] != '!') { 420 if(isset($data['startbefore'])) { $condition3s = $condition3s && new DateTime($data['startbefore']) > $data['start']; } 421 if(isset($data['startafter'])) { $condition3s = $condition3s && new DateTime($data['startafter']) < $data['start']; } 422 } 423 } else { 424 if(!$data['startignore'] == '*') { $condition3s = false; } 425 if($data['startignore'] == '!') { $condition3s = false; } 426 } 427 } 428 429 if(isset($data['duebefore']) || isset($data['dueafter'])) { 430 if(is_object($data['due'])) { 431 if($data['dueignore'] != '!') { 432 if(isset($data['duebefore'])) { $condition3d = $condition3d && new DateTime($data['duebefore']) > $data['due']; } 433 if(isset($data['dueafter'])) { $condition3d = $condition3d && new DateTime($data['dueafter']) < $data['due']; } 434 } 435 } else { 436 if(!$data['dueignore'] == '*') { $condition3d = false; } 437 if($data['dueignore'] == '!') { $condition3d = false; } 438 } 439 } 440 $condition3 = $condition3s && $condition3d; 441 } 442 443 // compare completed date 444 $condition4 = true; 445 if(isset($data['completedbefore'])) { 446 $condition4 = $condition4 && new DateTime($data['completedbefore']) > $data['completeddate']; 447 } 448 if(isset($data['completedafter'])) { 449 $condition4 = $condition4 && new DateTime($data['completedafter']) < $data['completeddate']; 450 } 451 452 return $condition1 AND $condition2 AND $condition3 AND $condition4; 453 } 454 455 456 /** 457 * Analyse of relative/absolute Date and return an absolute date 458 * 459 * @param $date string absolute/relative value of the date to analyse 460 * @return array absolute date or actual date if $date is invalid 461 */ 462 private function analyseDate($date) { 463 $result = array($date, ''); 464 if(is_string($date)) { 465 if($date == '!') { 466 $result = array('', '!'); 467 } elseif ($date =='*') { 468 $result = array('', '*'); 469 } else { 470 if(substr($date, -1) == '*') { 471 $date = substr($date, 0, -1); 472 $result = array($date, '*'); 473 } 474 475 if(date('Y-m-d', strtotime($date)) == $date) { 476 $result[0] = $date; 477 } elseif(preg_match('/^[\+\-]\d+$/', $date)) { // check if we have a valid relative value 478 $newdate = date_create(date('Y-m-d')); 479 date_modify($newdate, $date . ' day'); 480 $result[0] = date_format($newdate, 'Y-m-d'); 481 } else { 482 $result[0] = date('Y-m-d'); 483 } 484 } 485 } else { $result[0] = date('Y-m-d'); } 486 487 return $result; 488 } 489 490 491} 492