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 array An empty array for most cases, except: 98 - DOKU_LEXER_EXIT: An array containing the current lexer state 99 and information about the just lexed todo. 100 - DOKU_LEXER_SPECIAL: For the special pattern of the Uncheck-All-Button, an 101 array containing the current lexer state and the matched text. 102 */ 103 public function handle($match, $state, $pos, Doku_Handler $handler) { 104 switch($state) { 105 case DOKU_LEXER_ENTER : 106 #Search to see if the '#' is in the todotag (if so, this means the Action has been completed) 107 $x = preg_match('%<todo([^>]*)>%i', $match, $tododata); 108 if($x) { 109 $handler->todoargs = $this->parseTodoArgs($tododata[1]); 110 } 111 if(!isset($handler->todo_index) || !is_numeric($handler->todo_index)) { 112 $handler->todo_index = 0; 113 } 114 $handler->todo_user = ''; 115 $handler->checked = ''; 116 $handler->todotitle = ''; 117 break; 118 case DOKU_LEXER_MATCHED : 119 break; 120 case DOKU_LEXER_UNMATCHED : 121 /** 122 * Structure: 123 * input(checkbox) 124 * <span> 125 * -<a> (if links is on) or <span> (if links is off) 126 * --<del> (if strikethrough is on) or --NOTHING-- 127 * -</a> or </span> 128 * </span> 129 */ 130 $handler->todotitle = $match; 131 break; 132 case DOKU_LEXER_EXIT : 133 $data = array_merge(array ($state, 'todotitle' => $handler->todotitle, 'todoindex' => $handler->todo_index, 'todouser' => $handler->todo_user, 'checked' => $handler->checked), $handler->todoargs); 134 $handler->todo_index++; 135 #Delete temporary checked variable 136 unset($handler->todo_user); 137 unset($handler->checked); 138 unset($handler->todoargs); 139 unset($handler->todotitle); 140 return $data; 141 case DOKU_LEXER_SPECIAL : 142 if($match == self::TODO_UNCHECK_ALL) { 143 return array_merge(array($state, 'match' => $match)); 144 } 145 break; 146 } 147 return array(); 148 } 149 150 /** 151 * Handle the actual output creation. 152 * 153 * @param $mode String The output format to generate. 154 * @param $renderer Doku_Renderer A reference to the renderer object. 155 * @param $data Array The data created by the <tt>handle()</tt> method. 156 * @return Boolean true: if rendered successfully, or false: otherwise. 157 */ 158 public function render($mode, Doku_Renderer $renderer, $data) { 159 global $ID; 160 161 if(empty($data)) { 162 return false; 163 } 164 165 $state = $data[0]; 166 167 if($mode == 'xhtml') { 168 /** @var $renderer Doku_Renderer_xhtml */ 169 switch($state) { 170 case DOKU_LEXER_EXIT : 171 #Output our result 172 $renderer->doc .= $this->createTodoItem($renderer, $ID, array_merge($data, array('checkbox'=>'yes'))); 173 return true; 174 case DOKU_LEXER_SPECIAL : 175 if(isset($data['match']) && $data['match'] == self::TODO_UNCHECK_ALL) { 176 $renderer->doc .= '<button type="button" class="todouncheckall">Uncheck all todos</button>'; 177 } 178 return true; 179 } 180 } 181 return false; 182 } 183 184 /** 185 * Parse the arguments of todotag 186 * 187 * @param string $todoargs 188 * @return array(bool, false|string) with checked and user 189 */ 190 protected function parseTodoArgs($todoargs) { 191 $data['checked'] = false; 192 unset($data['start']); 193 unset($data['due']); 194 unset($data['completeddate']); 195 $data['showdate'] = $this->getConf("ShowdateTag"); 196 $data['username'] = $this->getConf("Username"); 197 $options = explode(' ', $todoargs); 198 foreach($options as $option) { 199 $option = trim($option); 200 if(empty($option)) continue; 201 if($option[0] == '@') { 202 $data['todousers'][] = substr($option, 1); //fill todousers array 203 if(!isset($data['todouser'])) $data['todouser'] = substr($option, 1); //set the first/main todouser 204 } 205 elseif($option[0] == '#') { 206 $data['checked'] = true; 207 @list($completeduser, $completeddate) = explode(':', $option, 2); 208 $data['completeduser'] = substr($completeduser, 1); 209 if(date('Y-m-d', strtotime($completeddate)) == $completeddate) { 210 $data['completeddate'] = new DateTime($completeddate); 211 } 212 } 213 else { 214 @list($key, $value) = explode(':', $option, 2); 215 switch($key) { 216 case 'username': 217 if(in_array($value, array('user', 'real', 'none'))) { 218 $data['username'] = $value; 219 } 220 else { 221 $data['username'] = 'none'; 222 } 223 break; 224 case 'start': 225 if(date('Y-m-d', strtotime($value)) == $value) { 226 $data['start'] = new DateTime($value); 227 } 228 break; 229 case 'due': 230 if(date('Y-m-d', strtotime($value)) == $value) { 231 $data['due'] = new DateTime($value); 232 } 233 break; 234 case 'showdate': 235 if(in_array($value, array('yes', 'no'))) { 236 $data['showdate'] = ($value == 'yes'); 237 } 238 break; 239 } 240 } 241 } 242 return $data; 243 } 244 245 /** 246 * @param Doku_Renderer_xhtml $renderer 247 * @param string $id of page 248 * @param array $data data for rendering options 249 * @return string html of an item 250 */ 251 protected function createTodoItem($renderer, $id, $data) { 252 //set correct context 253 global $ID, $INFO; 254 $oldID = $ID; 255 $ID = $id; 256 $todotitle = $data['todotitle']; 257 $todoindex = $data['todoindex']; 258 $checked = $data['checked']; 259 $return = '<span class="todo">'; 260 261 if($data['checkbox']) { 262 $return .= '<input type="checkbox" class="todocheckbox"' 263 . ' data-index="' . $todoindex . '"' 264 . ' data-date="' . hsc(@filemtime(wikiFN($ID))) . '"' 265 . ' data-pageid="' . hsc($ID) . '"' 266 . ' data-strikethrough="' . ($this->getConf("Strikethrough") ? '1' : '0') . '"' 267 . ($checked ? ' checked="checked"' : '') . ' /> '; 268 } 269 270 // Username(s) of todouser(s) 271 if (!isset($data['todousers'])) $data['todousers']=array(); 272 $todousers = array(); 273 foreach($data['todousers'] as $user) { 274 if (($user = $this->_prepUsername($user,$data['username'])) != '') { 275 $todousers[] = $user; 276 } 277 } 278 $todouser=join(', ',$todousers); 279 280 if($todouser!='') { 281 $return .= '<span class="todouser">[' . hsc($todouser) . ']</span>'; 282 } 283 if(isset($data['completeduser']) && ($checkeduser=$this->_prepUsername($data['completeduser'],$data['username']))!='') { 284 $return .= '<span class="todouser">[' . hsc('✓ '.$checkeduser); 285 if(isset($data['completeddate'])) { $return .= ', '.$data['completeddate']->format('Y-m-d'); } 286 $return .= ']</span>'; 287 } 288 289 // start/due date 290 unset($bg); 291 $now = new DateTime("now"); 292 if(!$checked && (isset($data['start']) || isset($data['due'])) && (!isset($data['start']) || $data['start']<$now) && (!isset($data['due']) || $now<$data['due'])) $bg='todostarted'; 293 if(!$checked && isset($data['due']) && $now>=$data['due']) $bg='tododue'; 294 295 // show start/due date 296 if($data['showdate'] == 1 && (isset($data['start']) || isset($data['due']))) { 297 $return .= '<span class="tododates">['; 298 if(isset($data['start'])) { $return .= $data['start']->format('Y-m-d'); } 299 $return .= ' → '; 300 if(isset($data['due'])) { $return .= $data['due']->format('Y-m-d'); } 301 $return .= ']</span>'; 302 } 303 304 $spanclass = 'todotext'; 305 if($this->getConf("CheckboxText") && !$this->getConf("AllowLinks") && $oldID == $ID && $data['checkbox']) { 306 $spanclass .= ' clickabletodo todohlght'; 307 } 308 if(isset($bg)) $spanclass .= ' '.$bg; 309 $return .= '<span class="' . $spanclass . '">'; 310 311 if($checked && $this->getConf("Strikethrough")) { 312 $return .= '<del>'; 313 } 314 $return .= '<span class="todoinnertext">'; 315 if($this->getConf("AllowLinks")) { 316 $return .= $this->_createLink($renderer, $todotitle, $todotitle); 317 } else { 318 if ($oldID != $ID) { 319 $return .= $renderer->internallink($id, $todotitle, null, true); 320 } else { 321 $return .= hsc($todotitle); 322 } 323 } 324 $return .= '</span>'; 325 326 if($checked && $this->getConf("Strikethrough")) { 327 $return .= '</del>'; 328 } 329 330 $return .= '</span></span>'; 331 332 //restore page ID 333 $ID = $oldID; 334 return $return; 335 } 336 337 /** 338 * Prepare user name string. 339 * 340 * @param string $username 341 * @param string $displaytype - one of 'user', 'real', 'none' 342 * @return string 343 */ 344 private function _prepUsername($username, $displaytype) { 345 346 switch ($displaytype) { 347 case "real": 348 global $auth; 349 $username = $auth->getUserData($username)['name']; 350 break; 351 case "none": 352 $username=""; 353 break; 354 case "user": 355 default: 356 break; 357 } 358 359 return $username; 360 } 361 362 /** 363 * Generate links from our Actions if necessary. 364 * 365 * @param Doku_Renderer_xhtml $renderer 366 * @param string $pagename 367 * @param string $name 368 * @return string 369 */ 370 private function _createLink($renderer, $pagename, $name = NULL) { 371 $id = $this->_composePageid($pagename); 372 373 return $renderer->internallink($id, $name, null, true); 374 } 375 376 /** 377 * Compose the pageid of the pages linked by a todoitem 378 * 379 * @param string $pagename 380 * @return string page id 381 */ 382 private function _composePageid($pagename) { 383 #Get the ActionNamespace and make sure it ends with a : (if not, add it) 384 $actionNamespace = $this->getConf("ActionNamespace"); 385 if(strlen($actionNamespace) == 0 || substr($actionNamespace, -1) != ':') { 386 $actionNamespace .= ":"; 387 } 388 389 #Replace ':' in $pagename so we don't create unnecessary namespaces 390 $pagename = str_replace(':', '-', $pagename); 391 392 //resolve and build link 393 $id = $actionNamespace . $pagename; 394 return $id; 395 } 396 397 /** 398 * @brief this function can be called by dokuwiki plugin searchpattern to process the todos found by searchpattern. 399 * use this searchpattern expression for open todos: 400 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 401 * use this searchpattern expression for completed todos: 402 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 403 * this handler method uses the table and layout with css classes from searchpattern plugin 404 * 405 * @param $type string type of the request from searchpattern plugin 406 * (wholeoutput, intable:whole, intable:prefix, intable:match, intable:count, intable:suffix) 407 * wholeoutput = all output is done by THIS plugin (no output will be done by search pattern) 408 * intable:whole = the left side of table (page name) is done by searchpattern, the right side 409 * of the table will be done by THIS plugin 410 * intable:prefix = on the right side of table - THIS plugin will output a prefix header and 411 * searchpattern will continue it's default output 412 * intable:match = if regex, right side of table - THIS plugin will format the current 413 * outputvalue ($value) and output it instead of searchpattern 414 * intable:count = if normal, right side of table - THIS plugin will format the current 415 * outputvalue ($value) and output it instead of searchpattern 416 * intable:suffix = on the right side of table - THIS plugin will output a suffix footer and 417 * searchpattern will continue it's default output 418 * @param Doku_Renderer_xhtml $renderer current rendering object (use $renderer->doc .= 'text' to output text) 419 * @param array $data whole data multidemensional array( array( $page => $countOfMatches ), ... ) 420 * @param array $matches whole regex matches multidemensional array( array( 0 => '1st Match', 1 => '2nd Match', ... ), ... ) 421 * @param string $page id of current page 422 * @param array $params the parameters set by searchpattern (see search pattern documentation) 423 * @param string $value value which should be outputted by searchpattern 424 * @return bool true if THIS method is responsible for the output (using $renderer->doc) OR false if searchpattern should output it's default 425 */ 426 public function _searchpatternHandler($type, $renderer, $data, $matches, $params = array(), $page = null, $value = null) { 427 $renderer->nocache(); 428 429 $type = strtolower($type); 430 switch($type) { 431 case 'wholeoutput': 432 // $matches should hold an array with all <todo>matches</todo> or <todo #>matches</todo> 433 if(!is_array($matches)) { 434 return false; 435 } 436 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($matches,true), FILE_APPEND ); 437 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($params,true), FILE_APPEND ); 438 $renderer->doc .= '<div class="sp_main">'; 439 $renderer->doc .= '<table class="inline sp_main_table">'; //create table 440 441 foreach($matches as $page => $allTodosPerPage) { 442 $renderer->doc .= '<tr class="sp_title"><th class="sp_title" colspan="2"><a href="' . wl($page) . '">' . $page . '</a></td></tr>'; 443 //entry 0 contains all whole matches 444 foreach($allTodosPerPage[0] as $todoindex => $todomatch) { 445 $x = preg_match('%<todo([^>]*)>(.*)</[\W]*todo[\W]*>%i', $todomatch, $tododata); 446 447 if($x) { 448 list($checked, $todouser) = $this->parseTodoArgs($tododata[1]); 449 $todotitle = trim($tododata[2]); 450 if(empty($todotitle)) { 451 continue; 452 } 453 $renderer->doc .= '<tr class="sp_result"><td class="sp_page" colspan="2">'; 454 455 // in case of integration with searchpattern there is no chance to find the index of an element 456 $renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $page, array('checkbox'=>'yes', 'username'=>'user')); 457 458 $renderer->doc .= '</td></tr>'; 459 } 460 } 461 } 462 $renderer->doc .= '</table>'; //end table 463 $renderer->doc .= '</div>'; 464 // true means, that this handler method does the output (searchpattern plugin has nothing to do) 465 return true; 466 break; 467 case 'intable:whole': 468 break; 469 case 'intable:prefix': 470 //$renderer->doc .= '<b>Start on Page '.$page.'</b>'; 471 break; 472 case 'intable:match': 473 //$renderer->doc .= 'regex match on page '.$page.': <pre>'.$value.'</pre>'; 474 break; 475 case 'intable:count': 476 //$renderer->doc .= 'normal count on page '.$page.': <pre>'.$value.'</pre>'; 477 break; 478 case 'intable:suffix': 479 //$renderer->doc .= '<b>End on Page '.$page.'</b>'; 480 break; 481 default: 482 break; 483 } 484 // false means, that this handler method does not output anything. all should be done by searchpattern plugin 485 return false; 486 } 487} 488 489//Setup VIM: ex: et ts=4 enc=utf-8 : 490