1<?php 2/** 3 * ToDo Plugin: Creates a checkbox based todo list 4 * 5 * Syntax: <todo [@username] [#]>Name of Action</todo> - 6 * Creates a Checkbox with the "Name of Action" as 7 * the text associated with it. The hash (#, optional) 8 * will cause the checkbox to be checked by default. 9 * The @ sign followed by a username can be used to assign this todo to a user. 10 * examples: 11 * A todo without user assignment 12 * <todo>Something todo</todo> 13 * A completed todo without user assignment 14 * <todo #>Completed todo</todo> 15 * A todo assigned to user User 16 * <todo @leo>Something todo for Leo</todo> 17 * A completed todo assigned to user User 18 * <todo @leo #>Todo completed for Leo</todo> 19 * 20 * In combination with dokuwiki searchpattern plugin version (at least v20130408), 21 * it is a lightweight solution for a task management system based on dokuwiki. 22 * use this searchpattern expression for open todos: 23 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 24 * use this searchpattern expression for completed todos: 25 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 26 * do not forget the no-cache option 27 * ~~NOCACHE~~ 28 * 29 * 30 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 31 * @author Babbage <babbage@digitalbrink.com>; Leo Eibler <dokuwiki@sprossenwanne.at> 32 */ 33 34if(!defined('DOKU_INC')) die(); 35 36/** 37 * All DokuWiki plugins to extend the parser/rendering mechanism 38 * need to inherit from this class 39 */ 40class syntax_plugin_todo_todo extends DokuWiki_Syntax_Plugin { 41 42 const TODO_UNCHECK_ALL = '~~TODO:UNCHECKALL~~'; 43 44 /** 45 * Get the type of syntax this plugin defines. 46 * 47 * @return String 48 */ 49 public function getType() { 50 return 'substition'; 51 } 52 53 /** 54 * Paragraph Type 55 * 56 * 'normal' - The plugin can be used inside paragraphs 57 * 'block' - Open paragraphs need to be closed before plugin output 58 * 'stack' - Special case. Plugin wraps other paragraphs. 59 */ 60 function getPType(){ 61 return 'normal'; 62 } 63 64 /** 65 * Where to sort in? 66 * 67 * @return Integer 68 */ 69 public function getSort() { 70 return 999; 71 } 72 73 /** 74 * Connect lookup pattern to lexer. 75 * 76 * @param $mode String The desired rendermode. 77 * @return void 78 * @see render() 79 */ 80 public function connectTo($mode) { 81 $this->Lexer->addEntryPattern('<todo[\s]*?.*?>(?=.*?</todo>)', $mode, 'plugin_todo_todo'); 82 $this->Lexer->addSpecialPattern(self::TODO_UNCHECK_ALL, $mode, 'plugin_todo_todo'); 83 $this->Lexer->addSpecialPattern('~~NOTODO~~', $mode, 'plugin_todo_todo'); 84 } 85 86 public function postConnect() { 87 $this->Lexer->addExitPattern('</todo>', 'plugin_todo_todo'); 88 } 89 90 /** 91 * Handler to prepare matched data for the rendering process. 92 * 93 * @param $match string The text matched by the patterns. 94 * @param $state int The lexer state for the match. 95 * @param $pos int The character position of the matched text. 96 * @param $handler Doku_Handler Reference to the Doku_Handler object. 97 * @return int The current lexer state for the match. 98 */ 99 public function handle($match, $state, $pos, Doku_Handler $handler) { 100 switch($state) { 101 case DOKU_LEXER_ENTER : 102 #Search to see if the '#' is in the todotag (if so, this means the Action has been completed) 103 $x = preg_match('%<todo([^>]*)>%i', $match, $tododata); 104 if($x) { 105 $handler->todoargs = $this->parseTodoArgs($tododata[1]); 106 } 107 if(!isset($handler->todo_index) || !is_numeric($handler->todo_index)) { 108 $handler->todo_index = 0; 109 } 110 $handler->todo_user = ''; 111 $handler->checked = ''; 112 $handler->todotitle = ''; 113 break; 114 case DOKU_LEXER_MATCHED : 115 break; 116 case DOKU_LEXER_UNMATCHED : 117 /** 118 * Structure: 119 * input(checkbox) 120 * <span> 121 * -<a> (if links is on) or <span> (if links is off) 122 * --<del> (if strikethrough is on) or --NOTHING-- 123 * -</a> or </span> 124 * </span> 125 */ 126 $handler->todotitle = $match; 127 break; 128 case DOKU_LEXER_EXIT : 129 $data = array_merge(array ($state, 'todotitle' => $handler->todotitle, 'todoindex' => $handler->todo_index, 'todouser' => $handler->todo_user, 'checked' => $handler->checked), $handler->todoargs); 130 $handler->todo_index++; 131 #Delete temporary checked variable 132 unset($handler->todo_user); 133 unset($handler->checked); 134 unset($handler->todoargs); 135 unset($handler->todotitle); 136 return $data; 137 case DOKU_LEXER_SPECIAL : 138 if($match == self::TODO_UNCHECK_ALL) { 139 return array($state, $match); 140 } 141 break; 142 } 143 return array(); 144 } 145 146 /** 147 * Handle the actual output creation. 148 * 149 * @param $mode String The output format to generate. 150 * @param $renderer Doku_Renderer A reference to the renderer object. 151 * @param $data Array The data created by the <tt>handle()</tt> method. 152 * @return Boolean true: if rendered successfully, or false: otherwise. 153 */ 154 public function render($mode, Doku_Renderer $renderer, $data) { 155 global $ID; 156 157 if(empty($data)) { 158 return false; 159 } 160 161 [$state, $match] = $data; 162 163 if($mode == 'xhtml') { 164 /** @var $renderer Doku_Renderer_xhtml */ 165 switch($state) { 166 case DOKU_LEXER_EXIT : 167 #Output our result 168 $renderer->doc .= $this->createTodoItem($renderer, $ID, array_merge($data, array('checkbox'=>'yes'))); 169 return true; 170 case DOKU_LEXER_SPECIAL : 171 if($match == self::TODO_UNCHECK_ALL) { 172 $renderer->doc .= '<button type="button" class="todouncheckall">Uncheck all todos</button>'; 173 } 174 return true; 175 } 176 } 177 return false; 178 } 179 180 /** 181 * Parse the arguments of todotag 182 * 183 * @param string $todoargs 184 * @return array(bool, false|string) with checked and user 185 */ 186 protected function parseTodoArgs($todoargs) { 187 $data['checked'] = false; 188 unset($data['start']); 189 unset($data['due']); 190 unset($data['completeddate']); 191 $data['showdate'] = $this->getConf("ShowdateTag"); 192 $data['username'] = $this->getConf("Username"); 193 $options = explode(' ', $todoargs); 194 foreach($options as $option) { 195 $option = trim($option); 196 if(empty($option)) continue; 197 if($option[0] == '@') { 198 $data['todousers'][] = substr($option, 1); //fill todousers array 199 if(!isset($data['todouser'])) $data['todouser'] = substr($option, 1); //set the first/main todouser 200 } 201 elseif($option[0] == '#') { 202 $data['checked'] = true; 203 @list($completeduser, $completeddate) = explode(':', $option, 2); 204 $data['completeduser'] = substr($completeduser, 1); 205 if(date('Y-m-d', strtotime($completeddate)) == $completeddate) { 206 $data['completeddate'] = new DateTime($completeddate); 207 } 208 } 209 else { 210 @list($key, $value) = explode(':', $option, 2); 211 switch($key) { 212 case 'username': 213 if(in_array($value, array('user', 'real', 'none'))) { 214 $data['username'] = $value; 215 } 216 break; 217 case 'start': 218 if(date('Y-m-d', strtotime($value)) == $value) { 219 $data['start'] = new DateTime($value); 220 } 221 break; 222 case 'due': 223 if(date('Y-m-d', strtotime($value)) == $value) { 224 $data['due'] = new DateTime($value); 225 } 226 break; 227 case 'showdate': 228 if(in_array($value, array('yes', 'no'))) { 229 $data['showdate'] = ($value == 'yes'); 230 } 231 break; 232 } 233 } 234 } 235 return $data; 236 } 237 238 /** 239 * @param Doku_Renderer_xhtml $renderer 240 * @param string $id of page 241 * @param array $data data for rendering options 242 * @return string html of an item 243 */ 244 protected function createTodoItem($renderer, $id, $data) { 245 //set correct context 246 global $ID, $INFO; 247 $oldID = $ID; 248 $ID = $id; 249 $todotitle = $data['todotitle']; 250 $todoindex = $data['todoindex']; 251 $checked = $data['checked']; 252 $return = '<span class="todo">'; 253 254 if($data['checkbox']) { 255 $return .= '<input type="checkbox" class="todocheckbox"' 256 . ' data-index="' . $todoindex . '"' 257 . ' data-date="' . hsc(@filemtime(wikiFN($ID))) . '"' 258 . ' data-pageid="' . hsc($ID) . '"' 259 . ' data-strikethrough="' . ($this->getConf("Strikethrough") ? '1' : '0') . '"' 260 . ($checked ? ' checked="checked"' : '') . ' /> '; 261 } 262 263 // Username(s) of todouser(s) 264 if (!isset($data['todousers'])) $data['todousers']=array(); 265 $todousers = array(); 266 foreach($data['todousers'] as $user) { 267 if (($user = $this->_prepUsername($user,$data['username'])) != '') { 268 $todousers[] = $user; 269 } 270 } 271 $todouser=join(', ',$todousers); 272 273 if($todouser!='') { 274 $return .= '<span class="todouser">[' . hsc($todouser) . ']</span>'; 275 } 276 if(isset($data['completeduser']) && ($checkeduser=$this->_prepUsername($data['completeduser'],$data['username']))!='') { 277 $return .= '<span class="todouser">[' . hsc('✓ '.$checkeduser); 278 if(isset($data['completeddate'])) { $return .= ', '.$data['completeddate']->format('Y-m-d'); } 279 $return .= ']</span>'; 280 } 281 282 // start/due date 283 unset($bg); 284 $now = new DateTime("now"); 285 if(!$checked && (isset($data['start']) || isset($data['due'])) && (!isset($data['start']) || $data['start']<$now) && (!isset($data['due']) || $now<$data['due'])) $bg='todostarted'; 286 if(!$checked && isset($data['due']) && $now>=$data['due']) $bg='tododue'; 287 288 // show start/due date 289 if($data['showdate'] == 1 && (isset($data['start']) || isset($data['due']))) { 290 $return .= '<span class="tododates">['; 291 if(isset($data['start'])) { $return .= $data['start']->format('Y-m-d'); } 292 $return .= ' → '; 293 if(isset($data['due'])) { $return .= $data['due']->format('Y-m-d'); } 294 $return .= ']</span>'; 295 } 296 297 $spanclass = 'todotext'; 298 if($this->getConf("CheckboxText") && !$this->getConf("AllowLinks") && $oldID == $ID && $data['checkbox']) { 299 $spanclass .= ' clickabletodo todohlght'; 300 } 301 if(isset($bg)) $spanclass .= ' '.$bg; 302 $return .= '<span class="' . $spanclass . '">'; 303 304 if($checked && $this->getConf("Strikethrough")) { 305 $return .= '<del>'; 306 } 307 $return .= '<span class="todoinnertext">'; 308 if($this->getConf("AllowLinks")) { 309 $return .= $this->_createLink($renderer, $todotitle, $todotitle); 310 } else { 311 if ($oldID != $ID) { 312 $return .= $renderer->internallink($id, $todotitle, null, true); 313 } else { 314 $return .= hsc($todotitle); 315 } 316 } 317 $return .= '</span>'; 318 319 if($checked && $this->getConf("Strikethrough")) { 320 $return .= '</del>'; 321 } 322 323 $return .= '</span></span>'; 324 325 //restore page ID 326 $ID = $oldID; 327 return $return; 328 } 329 330 /** 331 * Prepare user name string. 332 * 333 * @param string $username 334 * @param string $displaytype - one of 'user', 'real', 'none' 335 * @return string 336 */ 337 private function _prepUsername($username, $displaytype) { 338 339 switch ($displaytype) { 340 case "real": 341 global $auth; 342 $username = $auth->getUserData($username)['name']; 343 break; 344 case "none": 345 $username=""; 346 break; 347 case "user": 348 default: 349 break; 350 } 351 352 return $username; 353 } 354 355 /** 356 * Generate links from our Actions if necessary. 357 * 358 * @param Doku_Renderer_xhtml $renderer 359 * @param string $pagename 360 * @param string $name 361 * @return string 362 */ 363 private function _createLink($renderer, $pagename, $name = NULL) { 364 $id = $this->_composePageid($pagename); 365 366 return $renderer->internallink($id, $name, null, true); 367 } 368 369 /** 370 * Compose the pageid of the pages linked by a todoitem 371 * 372 * @param string $pagename 373 * @return string page id 374 */ 375 private function _composePageid($pagename) { 376 #Get the ActionNamespace and make sure it ends with a : (if not, add it) 377 $actionNamespace = $this->getConf("ActionNamespace"); 378 if(strlen($actionNamespace) == 0 || substr($actionNamespace, -1) != ':') { 379 $actionNamespace .= ":"; 380 } 381 382 #Replace ':' in $pagename so we don't create unnecessary namespaces 383 $pagename = str_replace(':', '-', $pagename); 384 385 //resolve and build link 386 $id = $actionNamespace . $pagename; 387 return $id; 388 } 389 390 /** 391 * @brief this function can be called by dokuwiki plugin searchpattern to process the todos found by searchpattern. 392 * use this searchpattern expression for open todos: 393 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 394 * use this searchpattern expression for completed todos: 395 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 396 * this handler method uses the table and layout with css classes from searchpattern plugin 397 * 398 * @param $type string type of the request from searchpattern plugin 399 * (wholeoutput, intable:whole, intable:prefix, intable:match, intable:count, intable:suffix) 400 * wholeoutput = all output is done by THIS plugin (no output will be done by search pattern) 401 * intable:whole = the left side of table (page name) is done by searchpattern, the right side 402 * of the table will be done by THIS plugin 403 * intable:prefix = on the right side of table - THIS plugin will output a prefix header and 404 * searchpattern will continue it's default output 405 * intable:match = if regex, right side of table - THIS plugin will format the current 406 * outputvalue ($value) and output it instead of searchpattern 407 * intable:count = if normal, right side of table - THIS plugin will format the current 408 * outputvalue ($value) and output it instead of searchpattern 409 * intable:suffix = on the right side of table - THIS plugin will output a suffix footer and 410 * searchpattern will continue it's default output 411 * @param Doku_Renderer_xhtml $renderer current rendering object (use $renderer->doc .= 'text' to output text) 412 * @param array $data whole data multidemensional array( array( $page => $countOfMatches ), ... ) 413 * @param array $matches whole regex matches multidemensional array( array( 0 => '1st Match', 1 => '2nd Match', ... ), ... ) 414 * @param string $page id of current page 415 * @param array $params the parameters set by searchpattern (see search pattern documentation) 416 * @param string $value value which should be outputted by searchpattern 417 * @return bool true if THIS method is responsible for the output (using $renderer->doc) OR false if searchpattern should output it's default 418 */ 419 public function _searchpatternHandler($type, $renderer, $data, $matches, $params = array(), $page = null, $value = null) { 420 $renderer->nocache(); 421 422 $type = strtolower($type); 423 switch($type) { 424 case 'wholeoutput': 425 // $matches should hold an array with all <todo>matches</todo> or <todo #>matches</todo> 426 if(!is_array($matches)) { 427 return false; 428 } 429 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($matches,true), FILE_APPEND ); 430 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($params,true), FILE_APPEND ); 431 $renderer->doc .= '<div class="sp_main">'; 432 $renderer->doc .= '<table class="inline sp_main_table">'; //create table 433 434 foreach($matches as $page => $allTodosPerPage) { 435 $renderer->doc .= '<tr class="sp_title"><th class="sp_title" colspan="2"><a href="' . wl($page) . '">' . $page . '</a></td></tr>'; 436 //entry 0 contains all whole matches 437 foreach($allTodosPerPage[0] as $todoindex => $todomatch) { 438 $x = preg_match('%<todo([^>]*)>(.*)</[\W]*todo[\W]*>%i', $todomatch, $tododata); 439 440 if($x) { 441 list($checked, $todouser) = $this->parseTodoArgs($tododata[1]); 442 $todotitle = trim($tododata[2]); 443 if(empty($todotitle)) { 444 continue; 445 } 446 $renderer->doc .= '<tr class="sp_result"><td class="sp_page" colspan="2">'; 447 448 // in case of integration with searchpattern there is no chance to find the index of an element 449 $renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $page, array('checkbox'=>'yes', 'username'=>'user')); 450 451 $renderer->doc .= '</td></tr>'; 452 } 453 } 454 } 455 $renderer->doc .= '</table>'; //end table 456 $renderer->doc .= '</div>'; 457 // true means, that this handler method does the output (searchpattern plugin has nothing to do) 458 return true; 459 break; 460 case 'intable:whole': 461 break; 462 case 'intable:prefix': 463 //$renderer->doc .= '<b>Start on Page '.$page.'</b>'; 464 break; 465 case 'intable:match': 466 //$renderer->doc .= 'regex match on page '.$page.': <pre>'.$value.'</pre>'; 467 break; 468 case 'intable:count': 469 //$renderer->doc .= 'normal count on page '.$page.': <pre>'.$value.'</pre>'; 470 break; 471 case 'intable:suffix': 472 //$renderer->doc .= '<b>End on Page '.$page.'</b>'; 473 break; 474 default: 475 break; 476 } 477 // false means, that this handler method does not output anything. all should be done by searchpattern plugin 478 return false; 479 } 480} 481 482//Setup VIM: ex: et ts=4 enc=utf-8 : 483