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