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_merge(array($state, 'match' => $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 = $data[0]; 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(isset($data['match']) && $data['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 else { 217 $data['username'] = 'none'; 218 } 219 break; 220 case 'start': 221 if(date('Y-m-d', strtotime($value)) == $value) { 222 $data['start'] = new DateTime($value); 223 } 224 break; 225 case 'due': 226 if(date('Y-m-d', strtotime($value)) == $value) { 227 $data['due'] = new DateTime($value); 228 } 229 break; 230 case 'showdate': 231 if(in_array($value, array('yes', 'no'))) { 232 $data['showdate'] = ($value == 'yes'); 233 } 234 break; 235 } 236 } 237 } 238 return $data; 239 } 240 241 /** 242 * @param Doku_Renderer_xhtml $renderer 243 * @param string $id of page 244 * @param array $data data for rendering options 245 * @return string html of an item 246 */ 247 protected function createTodoItem($renderer, $id, $data) { 248 //set correct context 249 global $ID, $INFO; 250 $oldID = $ID; 251 $ID = $id; 252 $todotitle = $data['todotitle']; 253 $todoindex = $data['todoindex']; 254 $checked = $data['checked']; 255 $return = '<span class="todo">'; 256 257 if($data['checkbox']) { 258 $return .= '<input type="checkbox" class="todocheckbox"' 259 . ' data-index="' . $todoindex . '"' 260 . ' data-date="' . hsc(@filemtime(wikiFN($ID))) . '"' 261 . ' data-pageid="' . hsc($ID) . '"' 262 . ' data-strikethrough="' . ($this->getConf("Strikethrough") ? '1' : '0') . '"' 263 . ($checked ? ' checked="checked"' : '') . ' /> '; 264 } 265 266 // Username(s) of todouser(s) 267 if (!isset($data['todousers'])) $data['todousers']=array(); 268 $todousers = array(); 269 foreach($data['todousers'] as $user) { 270 if (($user = $this->_prepUsername($user,$data['username'])) != '') { 271 $todousers[] = $user; 272 } 273 } 274 $todouser=join(', ',$todousers); 275 276 if($todouser!='') { 277 $return .= '<span class="todouser">[' . hsc($todouser) . ']</span>'; 278 } 279 if(isset($data['completeduser']) && ($checkeduser=$this->_prepUsername($data['completeduser'],$data['username']))!='') { 280 $return .= '<span class="todouser">[' . hsc('✓ '.$checkeduser); 281 if(isset($data['completeddate'])) { $return .= ', '.$data['completeddate']->format('Y-m-d'); } 282 $return .= ']</span>'; 283 } 284 285 // start/due date 286 unset($bg); 287 $now = new DateTime("now"); 288 if(!$checked && (isset($data['start']) || isset($data['due'])) && (!isset($data['start']) || $data['start']<$now) && (!isset($data['due']) || $now<$data['due'])) $bg='todostarted'; 289 if(!$checked && isset($data['due']) && $now>=$data['due']) $bg='tododue'; 290 291 // show start/due date 292 if($data['showdate'] == 1 && (isset($data['start']) || isset($data['due']))) { 293 $return .= '<span class="tododates">['; 294 if(isset($data['start'])) { $return .= $data['start']->format('Y-m-d'); } 295 $return .= ' → '; 296 if(isset($data['due'])) { $return .= $data['due']->format('Y-m-d'); } 297 $return .= ']</span>'; 298 } 299 300 $spanclass = 'todotext'; 301 if($this->getConf("CheckboxText") && !$this->getConf("AllowLinks") && $oldID == $ID && $data['checkbox']) { 302 $spanclass .= ' clickabletodo todohlght'; 303 } 304 if(isset($bg)) $spanclass .= ' '.$bg; 305 $return .= '<span class="' . $spanclass . '">'; 306 307 if($checked && $this->getConf("Strikethrough")) { 308 $return .= '<del>'; 309 } 310 $return .= '<span class="todoinnertext">'; 311 if($this->getConf("AllowLinks")) { 312 $return .= $this->_createLink($renderer, $todotitle, $todotitle); 313 } else { 314 if ($oldID != $ID) { 315 $return .= $renderer->internallink($id, $todotitle, null, true); 316 } else { 317 $return .= hsc($todotitle); 318 } 319 } 320 $return .= '</span>'; 321 322 if($checked && $this->getConf("Strikethrough")) { 323 $return .= '</del>'; 324 } 325 326 $return .= '</span></span>'; 327 328 //restore page ID 329 $ID = $oldID; 330 return $return; 331 } 332 333 /** 334 * Prepare user name string. 335 * 336 * @param string $username 337 * @param string $displaytype - one of 'user', 'real', 'none' 338 * @return string 339 */ 340 private function _prepUsername($username, $displaytype) { 341 342 switch ($displaytype) { 343 case "real": 344 global $auth; 345 $username = $auth->getUserData($username)['name']; 346 break; 347 case "none": 348 $username=""; 349 break; 350 case "user": 351 default: 352 break; 353 } 354 355 return $username; 356 } 357 358 /** 359 * Generate links from our Actions if necessary. 360 * 361 * @param Doku_Renderer_xhtml $renderer 362 * @param string $pagename 363 * @param string $name 364 * @return string 365 */ 366 private function _createLink($renderer, $pagename, $name = NULL) { 367 $id = $this->_composePageid($pagename); 368 369 return $renderer->internallink($id, $name, null, true); 370 } 371 372 /** 373 * Compose the pageid of the pages linked by a todoitem 374 * 375 * @param string $pagename 376 * @return string page id 377 */ 378 private function _composePageid($pagename) { 379 #Get the ActionNamespace and make sure it ends with a : (if not, add it) 380 $actionNamespace = $this->getConf("ActionNamespace"); 381 if(strlen($actionNamespace) == 0 || substr($actionNamespace, -1) != ':') { 382 $actionNamespace .= ":"; 383 } 384 385 #Replace ':' in $pagename so we don't create unnecessary namespaces 386 $pagename = str_replace(':', '-', $pagename); 387 388 //resolve and build link 389 $id = $actionNamespace . $pagename; 390 return $id; 391 } 392 393 /** 394 * @brief this function can be called by dokuwiki plugin searchpattern to process the todos found by searchpattern. 395 * use this searchpattern expression for open todos: 396 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 397 * use this searchpattern expression for completed todos: 398 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 399 * this handler method uses the table and layout with css classes from searchpattern plugin 400 * 401 * @param $type string type of the request from searchpattern plugin 402 * (wholeoutput, intable:whole, intable:prefix, intable:match, intable:count, intable:suffix) 403 * wholeoutput = all output is done by THIS plugin (no output will be done by search pattern) 404 * intable:whole = the left side of table (page name) is done by searchpattern, the right side 405 * of the table will be done by THIS plugin 406 * intable:prefix = on the right side of table - THIS plugin will output a prefix header and 407 * searchpattern will continue it's default output 408 * intable:match = if regex, right side of table - THIS plugin will format the current 409 * outputvalue ($value) and output it instead of searchpattern 410 * intable:count = if normal, right side of table - THIS plugin will format the current 411 * outputvalue ($value) and output it instead of searchpattern 412 * intable:suffix = on the right side of table - THIS plugin will output a suffix footer and 413 * searchpattern will continue it's default output 414 * @param Doku_Renderer_xhtml $renderer current rendering object (use $renderer->doc .= 'text' to output text) 415 * @param array $data whole data multidemensional array( array( $page => $countOfMatches ), ... ) 416 * @param array $matches whole regex matches multidemensional array( array( 0 => '1st Match', 1 => '2nd Match', ... ), ... ) 417 * @param string $page id of current page 418 * @param array $params the parameters set by searchpattern (see search pattern documentation) 419 * @param string $value value which should be outputted by searchpattern 420 * @return bool true if THIS method is responsible for the output (using $renderer->doc) OR false if searchpattern should output it's default 421 */ 422 public function _searchpatternHandler($type, $renderer, $data, $matches, $params = array(), $page = null, $value = null) { 423 $renderer->nocache(); 424 425 $type = strtolower($type); 426 switch($type) { 427 case 'wholeoutput': 428 // $matches should hold an array with all <todo>matches</todo> or <todo #>matches</todo> 429 if(!is_array($matches)) { 430 return false; 431 } 432 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($matches,true), FILE_APPEND ); 433 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($params,true), FILE_APPEND ); 434 $renderer->doc .= '<div class="sp_main">'; 435 $renderer->doc .= '<table class="inline sp_main_table">'; //create table 436 437 foreach($matches as $page => $allTodosPerPage) { 438 $renderer->doc .= '<tr class="sp_title"><th class="sp_title" colspan="2"><a href="' . wl($page) . '">' . $page . '</a></td></tr>'; 439 //entry 0 contains all whole matches 440 foreach($allTodosPerPage[0] as $todoindex => $todomatch) { 441 $x = preg_match('%<todo([^>]*)>(.*)</[\W]*todo[\W]*>%i', $todomatch, $tododata); 442 443 if($x) { 444 list($checked, $todouser) = $this->parseTodoArgs($tododata[1]); 445 $todotitle = trim($tododata[2]); 446 if(empty($todotitle)) { 447 continue; 448 } 449 $renderer->doc .= '<tr class="sp_result"><td class="sp_page" colspan="2">'; 450 451 // in case of integration with searchpattern there is no chance to find the index of an element 452 $renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $page, array('checkbox'=>'yes', 'username'=>'user')); 453 454 $renderer->doc .= '</td></tr>'; 455 } 456 } 457 } 458 $renderer->doc .= '</table>'; //end table 459 $renderer->doc .= '</div>'; 460 // true means, that this handler method does the output (searchpattern plugin has nothing to do) 461 return true; 462 break; 463 case 'intable:whole': 464 break; 465 case 'intable:prefix': 466 //$renderer->doc .= '<b>Start on Page '.$page.'</b>'; 467 break; 468 case 'intable:match': 469 //$renderer->doc .= 'regex match on page '.$page.': <pre>'.$value.'</pre>'; 470 break; 471 case 'intable:count': 472 //$renderer->doc .= 'normal count on page '.$page.': <pre>'.$value.'</pre>'; 473 break; 474 case 'intable:suffix': 475 //$renderer->doc .= '<b>End on Page '.$page.'</b>'; 476 break; 477 default: 478 break; 479 } 480 // false means, that this handler method does not output anything. all should be done by searchpattern plugin 481 return false; 482 } 483} 484 485//Setup VIM: ex: et ts=4 enc=utf-8 : 486