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