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 * Compatibility: 30 * Release 2013-03-06 "Weatherwax RC1" 31 * Release 2012-10-13 "Adora Belle" 32 * 33 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 34 * @author Babbage <babbage@digitalbrink.com>; Leo Eibler <dokuwiki@sprossenwanne.at> 35 */ 36 37/** 38 * ChangeLog: 39 * 40 * [05/14/2014]: by Markus Gschwendt <markus@runout.at> 41 * add start-due date: set a start and/or due date and get colored output (css) 42 * clean up some code, so we have less variables in function calls, use arrays instead 43 * [05/11/2014]: by Markus Gschwendt <markus@runout.at> 44 * add options for list rendering: username:user|real|none checkbox:yes|no header:id|firstheader 45 ** [04/13/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 46 ** bugfix: config option Strikethrough 47 * [04/11/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 48 * bugfix: encoding html code (security risk <todo><script>alert('hi')</script></todo>) - bug reported by Andreas 49 * bugfix: use correct <todo> tag if there are more than 1 in the same line. 50 * [04/08/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 51 * migrate changes made by Christian Marg to current version of plugin 52 * [04/08/2013]: by Christian Marg <marg@rz.tu-clausthal.de> 53 * changed behaviour - when multiple todo-items have the same text, only the clicked one is checked. 54 * [04/08/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 55 * add description / comments and syntax howto about integration with searchpattern 56 * check compatibility with dokuwiki release 2012-10-13 "Adora Belle" 57 * remove getInfo() call because it's done by plugin.info.txt (since dokuwiki 2009-12-25 "Lemming") 58 * [04/07/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 59 * add handler method _searchpatternHandler() for dokuwiki searchpattern extension. 60 * add user assignment for todos (with @username syntax in todo tag e.g. <todo @leo>do something</todo>) 61 * [04/05/2013]: by Leo Eibler <dokuwiki@sprossenwanne.at> / http://www.eibler.at 62 * upgrade plugin to work with newest version of dokuwiki (tested version Release 2013-03-06 Weatherwax RC1). 63 * [08/16/2010]: Fixed another bug where javascript would not decode the action 64 * text properly (replaced unescape with decodeURIComponent). 65 * [04/03/2010]: Fixed a bug where javascript would not decode the action text 66 * properly. 67 * [03/31/2010]: Fixed a bug where checking or unchecking an action whose text 68 * appeared outside of the todo tags, would result in mangling the 69 * code on your page. Also added support for using the ampersand 70 * character (&) and html entities inside of your todo action. 71 * [02/27/2010]: Created an action plugin to insert a ToDo button into the 72 * editor toolbar. 73 * [10/14/2009]: Added the feature so that if you have Links turned off and you 74 * click on the text of an action, it will check that action off. 75 * Thanks to Tero for the suggestion! (Plugin Option: CheckboxText) 76 * [10/08/2009]: I am no longer using the short open php tag (<?) for my 77 * ajax.php file. This was causing some problems for people who had 78 * short_open_tags=Off in their php.ini file (thanks Marcus!) 79 * [10/01/2009]: Updated javascript to use .nextSibling instead of .nextElementSibling 80 * to make it compatible with older versions of Firefox and IE. 81 * [09/13/2009]: Replaced ':' with a '-' in the action link so as not to create 82 * unnecessary namespaces (if the links option is active) 83 * [09/10/2009]: Removed unnecessary function calls (urlencode) in _createLink() function 84 * [09/09/2009]: Added ability for user to choose where Action links point to 85 * [08/30/2009]: Initial Release 86 */ 87 88if(!defined('DOKU_INC')) die(); 89 90/** 91 * All DokuWiki plugins to extend the parser/rendering mechanism 92 * need to inherit from this class 93 */ 94class syntax_plugin_todo_todo extends DokuWiki_Syntax_Plugin { 95 96 /** 97 * Get the type of syntax this plugin defines. 98 * 99 * @return String 100 */ 101 public function getType() { 102 return 'substition'; 103 } 104 105 /** 106 * Paragraph Type 107 * 108 * 'normal' - The plugin can be used inside paragraphs 109 * 'block' - Open paragraphs need to be closed before plugin output 110 * 'stack' - Special case. Plugin wraps other paragraphs. 111 */ 112 function getPType(){ 113 return 'normal'; 114 } 115 116 /** 117 * Where to sort in? 118 * 119 * @return Integer 120 */ 121 public function getSort() { 122 return 999; 123 } 124 125 /** 126 * Connect lookup pattern to lexer. 127 * 128 * @param $mode String The desired rendermode. 129 * @return void 130 * @see render() 131 */ 132 public function connectTo($mode) { 133 $this->Lexer->addEntryPattern('<todo[\s]*?.*?>(?=.*?</todo>)', $mode, 'plugin_todo_todo'); 134 } 135 136 public function postConnect() { 137 $this->Lexer->addExitPattern('</todo>', 'plugin_todo_todo'); 138 } 139 140 /** 141 * Handler to prepare matched data for the rendering process. 142 * 143 * @param $match string The text matched by the patterns. 144 * @param $state int The lexer state for the match. 145 * @param $pos int The character position of the matched text. 146 * @param &$handler Doku_Handler Reference to the Doku_Handler object. 147 * @return int The current lexer state for the match. 148 */ 149 public function handle($match, $state, $pos, Doku_Handler &$handler) { 150 switch($state) { 151 case DOKU_LEXER_ENTER : 152 #Search to see if the '#' is in the todotag (if so, this means the Action has been completed) 153 $x = preg_match('%<todo([^>]*)>%i', $match, $tododata); 154 if($x) { 155 $handler->todoargs = $this->parseTodoArgs($tododata[1]); 156 } 157 if(!is_numeric($handler->todo_index)) { 158 $handler->todo_index = 0; 159 } 160 break; 161 case DOKU_LEXER_MATCHED : 162 break; 163 case DOKU_LEXER_UNMATCHED : 164 /** 165 * Structure: 166 * input(checkbox) 167 * <span> 168 * -<a> (if links is on) or <span> (if links is off) 169 * --<del> (if strikethrough is on) or --NOTHING-- 170 * -</a> or </span> 171 * </span> 172 */ 173 174 #Make sure there is actually an action to create 175 if(trim($match) != '') { 176 177 $data = array_merge(array ($state, 'todotitle' => $match, 'todoindex' => $handler->todo_index, 'todouser' => $handler->todo_user, 'checked' => $handler->checked), $handler->todoargs); 178 $handler->todo_index++; 179 return $data; 180 } 181 182 break; 183 case DOKU_LEXER_EXIT : 184 #Delete temporary checked variable 185 unset($handler->todo_user); 186 unset($handler->checked); 187 unset($handler->todoargs); 188 //unset($handler->todo_index); 189 break; 190 case DOKU_LEXER_SPECIAL : 191 break; 192 } 193 return array(); 194 } 195 196 /** 197 * Handle the actual output creation. 198 * 199 * @param $mode String The output format to generate. 200 * @param &$renderer Doku_Renderer A reference to the renderer object. 201 * @param $data Array The data created by the <tt>handle()</tt> method. 202 * @return Boolean true: if rendered successfully, or false: otherwise. 203 */ 204 public function render($mode, Doku_Renderer &$renderer, $data) { 205 global $ID; 206 list($state, $todotitle) = $data; 207 if($mode == 'xhtml') { 208 /** @var $renderer Doku_Renderer_xhtml */ 209 if($state == DOKU_LEXER_UNMATCHED) { 210 211 #Output our result 212 $renderer->doc .= $this->createTodoItem($renderer, $ID, array_merge($data, array('checkbox'=>'yes', 'username'=>'user'))); 213 return true; 214 } 215 216 } elseif($mode == 'metadata') { 217 /** @var $renderer Doku_Renderer_metadata */ 218 if($state == DOKU_LEXER_UNMATCHED) { 219 $id = $this->_composePageid($todotitle); 220 $renderer->internallink($id, $todotitle); 221 } 222 } 223 return false; 224 } 225 226 /** 227 * Parse the arguments of todotag 228 * 229 * @param string $todoargs 230 * @return array(bool, false|string) with checked and user 231 */ 232 protected function parseTodoArgs($todoargs) { 233 $checked = $todouser = false; 234 235/* if(($userStartPos = strpos($todoargs, '@')) !== false && (preg_match('%@([-.\w@]+)%i', substr($todoargs, $userStartPos), $usermatch))) { 236 $todouser = $usermatch[1]; 237 } 238*/ 239 $options = explode(' ', $todoargs); 240/* $data = array( 241 'checked' => strpos($todoargs, '#') !== false, 242 'todouser' => $todouser, 243 ); 244*/ unset($data['start']); 245 unset($data['due']); 246 foreach($options as $option) { 247 if($option[0]=='@') { 248 $data['todousers'][] = substr($option, 1); //fill todousers array 249 if(!isset($data['todouser'])) $data['todouser'] = substr($option, 1); //set the first/main todouser 250 } 251 elseif(strpos($todoargs, '#') !== false) { $data['checked'] = true; 252 } 253 else { 254 @list($key, $value) = explode(':', $option, 2); 255 switch($key) { 256 257 case 'start': 258 if(date('Y-m-d', strtotime($value)) == $value) { 259 $data['start'] = new DateTime($value); 260 } 261 break; 262 case 'due': 263 if(date('Y-m-d', strtotime($value)) == $value) { 264 $data['due'] = new DateTime($value); 265 } 266 break; 267 } 268 } 269 } 270 return $data; 271 } 272 273 /** 274 * @param Doku_Renderer_xhtml &$renderer 275 * @param string $todotitle Title of todoitem 276 * @param int $todoindex which number the todoitem has, null is when not at the page 277 * @param string $todouser User assigned to todoitem 278 * @param bool $checked whether item is done 279 * @param string $id of page 280 * @param array $data data for rendering options 281 * @return string html of an item 282 */ 283 protected function createTodoItem(&$renderer, $id, $data) { 284 //set correct context 285 global $ID, $INFO; 286 $oldID = $ID; 287 $ID = $id; 288 $todotitle = $data['todotitle']; 289 $todoindex = $data['todoindex']; 290 $todouser = $data['todouser']; 291 $checked = $data['checked']; 292 if($data['checkbox']) { 293 $return = '<input type="checkbox" class="todocheckbox"' 294 . ' data-index="' . $todoindex . '"' 295 . ' data-date="' . hsc(@filemtime(wikiFN($ID))) . '"' 296 . ' data-pageid="' . hsc($ID) . '"' 297 . ' data-strikethrough="' . ($this->getConf("Strikethrough") ? '1' : '0') . '"' 298 . ($checked ? 'checked="checked"' : '') . ' /> '; 299 } 300 switch ($data['username']) { 301 case "user": break; 302 case "real": $todouser = userlink($todouser, true); break; 303 case "none": unset($todouser); break; 304 } 305 if($todouser) { 306 $return .= '<span class="todouser">[' . hsc($todouser) . ']</span>'; 307 } 308 309 $spanclass = 'todotext'; 310 if($this->getConf("CheckboxText") && !$this->getConf("AllowLinks") && $data['checkbox']) { 311 $spanclass .= ' clickabletodo todohlght'; 312 } 313 unset($bg); 314 $now = new DateTime("now"); 315 if((!isset($data['start']) || $data['start']<$now) && $now<$data['due']) $bg='todostarted'; 316 if(isset($data['due']) && $now>=$data['due'] && !$checked) $bg='tododue'; 317 if(isset($bg)) $spanclass .= ' '.$bg; 318 $return .= '<span class="' . $spanclass . '">'; 319 320 if($checked && $this->getConf("Strikethrough")) { 321 $return .= '<del>'; 322 } 323 $return .= '<span class="todoinnertext">'; 324 if($this->getConf("AllowLinks")) { 325 $return .= $this->_createLink($renderer, $todotitle, $todotitle); 326 } else { 327 $return .= $renderer->internallink($id, $todotitle, null, true); 328 } 329 $return .= '</span>'; 330 331 if($checked && $this->getConf("Strikethrough")) { 332 $return .= '</del>'; 333 } 334 335 $return .= '</span>'; 336 337 //restore page ID 338 $ID = $oldID; 339 return $return; 340 } 341 342 /** 343 * Generate links from our Actions if necessary. 344 * 345 * @param Doku_Renderer_xhtml &$renderer 346 * @param string $pagename 347 * @param string $name 348 * @return string 349 */ 350 private function _createLink(&$renderer, $pagename, $name = NULL) { 351 $id = $this->_composePageid($pagename); 352 353 return $renderer->internallink($id, $name, null, true); 354 } 355 356 /** 357 * Compose the pageid of the pages linked by a todoitem 358 * 359 * @param string $pagename 360 * @return string page id 361 */ 362 private function _composePageid($pagename) { 363 #Get the ActionNamespace and make sure it ends with a : (if not, add it) 364 $actionNamespace = $this->getConf("ActionNamespace"); 365 if(strlen($actionNamespace) == 0 || substr($actionNamespace, -1) != ':') { 366 $actionNamespace .= ":"; 367 } 368 369 #Replace ':' in $pagename so we don't create unnecessary namespaces 370 $pagename = str_replace(':', '-', $pagename); 371 372 //resolve and build link 373 $id = $actionNamespace . $pagename; 374 return $id; 375 } 376 377 /** 378 * @brief this function can be called by dokuwiki plugin searchpattern to process the todos found by searchpattern. 379 * use this searchpattern expression for open todos: 380 * ~~SEARCHPATTERN#'/<todo[^#>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 381 * use this searchpattern expression for completed todos: 382 * ~~SEARCHPATTERN#'/<todo[^#>]*#[^>]*>.*?<\/todo[\W]*?>/'?? _ToDo ??~~ 383 * this handler method uses the table and layout with css classes from searchpattern plugin 384 * 385 * @param $type string type of the request from searchpattern plugin 386 * (wholeoutput, intable:whole, intable:prefix, intable:match, intable:count, intable:suffix) 387 * wholeoutput = all output is done by THIS plugin (no output will be done by search pattern) 388 * intable:whole = the left side of table (page name) is done by searchpattern, the right side 389 * of the table will be done by THIS plugin 390 * intable:prefix = on the right side of table - THIS plugin will output a prefix header and 391 * searchpattern will continue it's default output 392 * intable:match = if regex, right side of table - THIS plugin will format the current 393 * outputvalue ($value) and output it instead of searchpattern 394 * intable:count = if normal, right side of table - THIS plugin will format the current 395 * outputvalue ($value) and output it instead of searchpattern 396 * intable:suffix = on the right side of table - THIS plugin will output a suffix footer and 397 * searchpattern will continue it's default output 398 * @param Doku_Renderer_xhtml &$renderer current rendering object (use $renderer->doc .= 'text' to output text) 399 * @param array $data whole data multidemensional array( array( $page => $countOfMatches ), ... ) 400 * @param array $matches whole regex matches multidemensional array( array( 0 => '1st Match', 1 => '2nd Match', ... ), ... ) 401 * @param string $page id of current page 402 * @param array $params the parameters set by searchpattern (see search pattern documentation) 403 * @param string $value value which should be outputted by searchpattern 404 * @return bool true if THIS method is responsible for the output (using $renderer->doc) OR false if searchpattern should output it's default 405 */ 406 public function _searchpatternHandler($type, &$renderer, $data, $matches, $params = array(), $page = null, $value = null) { 407 $renderer->nocache(); 408 409 $type = strtolower($type); 410 switch($type) { 411 case 'wholeoutput': 412 // $matches should hold an array with all <todo>matches</todo> or <todo #>matches</todo> 413 if(!is_array($matches)) { 414 return false; 415 } 416 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($matches,true), FILE_APPEND ); 417 //file_put_contents( dirname(__FILE__).'/debug.txt', print_r($params,true), FILE_APPEND ); 418 $renderer->doc .= '<div class="sp_main">'; 419 $renderer->doc .= '<table class="inline sp_main_table">'; //create table 420 421 foreach($matches as $page => $allTodosPerPage) { 422 $renderer->doc .= '<tr class="sp_title"><th class="sp_title" colspan="2"><a href="' . wl($page) . '">' . $page . '</a></td></tr>'; 423 //entry 0 contains all whole matches 424 foreach($allTodosPerPage[0] as $todoindex => $todomatch) { 425 $x = preg_match('%<todo([^>]*)>(.*)</[\W]*todo[\W]*>%i', $todomatch, $tododata); 426 427 if($x) { 428 list($checked, $todouser) = $this->parseTodoArgs($tododata[1]); 429 $todotitle = trim($tododata[2]); 430 if(empty($todotitle)) { 431 continue; 432 } 433 $renderer->doc .= '<tr class="sp_result"><td class="sp_page" colspan="2">'; 434 435 // in case of integration with searchpattern there is no chance to find the index of an element 436 $renderer->doc .= $this->createTodoItem($renderer, $todotitle, $todoindex, $todouser, $checked, $page, array('checkbox'=>'yes', 'username'=>'user')); 437 438 $renderer->doc .= '</td></tr>'; 439 } 440 } 441 } 442 $renderer->doc .= '</table>'; //end table 443 $renderer->doc .= '</div>'; 444 // true means, that this handler method does the output (searchpattern plugin has nothing to do) 445 return true; 446 break; 447 case 'intable:whole': 448 break; 449 case 'intable:prefix': 450 //$renderer->doc .= '<b>Start on Page '.$page.'</b>'; 451 break; 452 case 'intable:match': 453 //$renderer->doc .= 'regex match on page '.$page.': <pre>'.$value.'</pre>'; 454 break; 455 case 'intable:count': 456 //$renderer->doc .= 'normal count on page '.$page.': <pre>'.$value.'</pre>'; 457 break; 458 case 'intable:suffix': 459 //$renderer->doc .= '<b>End on Page '.$page.'</b>'; 460 break; 461 default: 462 break; 463 } 464 // false means, that this handler method does not output anything. all should be done by searchpattern plugin 465 return false; 466 } 467} 468 469//Setup VIM: ex: et ts=4 enc=utf-8 : 470