1<?php 2/** 3 * DokuWiki Plugin todo_list (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 */ 7 8// must be run within Dokuwiki 9if(!defined('DOKU_INC')) die(); 10 11/** 12 * Class syntax_plugin_todo_list 13 */ 14class syntax_plugin_todo_list extends syntax_plugin_todo_todo { 15 16 /** 17 * @return string Syntax mode type 18 */ 19 public function getType() { 20 return 'substition'; 21 } 22 23 /** 24 * @return string Paragraph type 25 */ 26 public function getPType() { 27 return 'block'; 28 } 29 30 /** 31 * @return int Sort order - Low numbers go before high numbers 32 */ 33 public function getSort() { 34 return 250; 35 } 36 37 /** 38 * Connect lookup pattern to lexer. 39 * 40 * @param string $mode Parser mode 41 */ 42 public function connectTo($mode) { 43 $this->Lexer->addSpecialPattern('~~TODOLIST[^~]*~~', $mode, 'plugin_todo_list'); 44 } 45 46 /** 47 * Handle matches of the todolist syntax 48 * 49 * @param string $match The match of the syntax 50 * @param int $state The state of the handler 51 * @param int $pos The position in the document 52 * @param Doku_Handler $handler The handler 53 * @return array Data for the renderer 54 */ 55 public function handle($match, $state, $pos, Doku_Handler &$handler) { 56 57 $options = substr($match, 10, -2); // strip markup 58 $options = explode(' ', $options); 59 $data = array( 60 'completed' => 'all', 61 'assigned' => 'all', 62 'checkbox' => 'yes', 63 'username' => 'user' 64 ); 65 $allowedvalues = array('yes', 'no'); 66 foreach($options as $option) { 67 @list($key, $value) = explode(':', $option, 2); 68 switch($key) { 69 case 'checkbox': // should checkbox be rendered? 70 if(in_array($value, $allowedvalues)) { 71 $data['checkbox'] = ($value == 'yes'); 72 } 73 break; 74 case 'completed': 75 if(in_array($value, $allowedvalues)) { 76 $data['completed'] = ($value == 'yes'); 77 } 78 break; 79 case 'username': // how should the username be rendered? 80 if(in_array($value, array('user', 'real', 'none'))) { 81 $data['username'] = $value; 82 } 83 break; 84 case 'assigned': 85 if(in_array($value, $allowedvalues)) { 86 $data['assigned'] = ($value == 'yes'); 87 break; 88 } 89 //assigned? 90 $data['assigned'] = explode(',', $value); 91 // @date 20140317 le: if check for logged in user, also check for logged in user email address 92 if( in_array( '@@USER@@', $data['assigned'] ) ) { 93 $data['assigned'][] = '@@MAIL@@'; 94 } 95 $data['assigned'] = array_map( 96 function ($user) { 97 //placeholder (inspired by replacement-patterns - see https://www.dokuwiki.org/namespace_templates#replacement_patterns) 98 if( $user == '@@USER@@' || $user == '@@MAIL@@' ) { 99 return $user; 100 } 101 //user 102 return ltrim($user, '@'); 103 }, $data['assigned'] 104 ); 105 break; 106 } 107 } 108 return $data; 109 } 110 111 /** 112 * Render xhtml output or metadata 113 * 114 * @param string $mode Renderer mode (supported modes: xhtml) 115 * @param Doku_Renderer $renderer The renderer 116 * @param array $data The data from the handler() function 117 * @return bool If rendering was successful. 118 */ 119 public function render($mode, Doku_Renderer &$renderer, $data) { 120 global $conf; 121 122 if($mode != 'xhtml') return false; 123 /** @var Doku_Renderer_xhtml $renderer */ 124 125 $opts['pattern'] = '/<todo([^>]*)>(.*)<\/todo[\W]*?>/'; //all todos in a wiki page 126 //TODO check if storing subpatterns doesn't cost too much resources 127 128 // search(&$data, $base, $func, $opts,$dir='',$lvl=1,$sort='natural') 129 search($todopages, $conf['datadir'], array($this, 'search_todos'), $opts); //browse wiki pages with callback to search_pattern 130 131 $todopages = $this->filterpages($todopages, $data); 132 133 $this->htmlTodoTable($renderer, $todopages, $data); 134 135 return true; 136 } 137 138 /** 139 * Custom search callback 140 * 141 * This function is called for every found file or 142 * directory. When a directory is given to the function it has to 143 * decide if this directory should be traversed (true) or not (false). 144 * Return values for files are ignored 145 * 146 * All functions should check the ACL for document READ rights 147 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this 148 * would break the recursion (You can have an nonreadable dir over a readable 149 * one deeper nested) also make sure to check the file type (for example 150 * in case of lockfiles). 151 * 152 * @param array &$data - Reference to the result data structure 153 * @param string $base - Base usually $conf['datadir'] 154 * @param string $file - current file or directory relative to $base 155 * @param string $type - Type either 'd' for directory or 'f' for file 156 * @param int $lvl - Current recursion depht 157 * @param array $opts - option array as given to search() 158 * @return bool if this directory should be traversed (true) or not (false). Return values for files are ignored. 159 */ 160 public function search_todos(&$data, $base, $file, $type, $lvl, $opts) { 161 $item['id'] = pathID($file); //get current file ID 162 163 //we do nothing with directories 164 if($type == 'd') return true; 165 166 //only search txt files 167 if(substr($file, -4) != '.txt') return true; 168 169 //check ACL 170 if(auth_quickaclcheck($item['id']) < AUTH_READ) return false; 171 172 $wikitext = rawWiki($item['id']); //get wiki text 173 174 $item['count'] = preg_match_all($opts['pattern'], $wikitext, $matches); //count how many times appears the pattern 175 if(!empty($item['count'])) { //if it appears at least once 176 $item['matches'] = $matches; 177 $data[] = $item; 178 } 179 return true; 180 } 181 182 /** 183 * filter the pages 184 * 185 * @param $todopages array pages with all todoitems 186 * @param $data array listing parameters 187 * @return array filtered pages 188 */ 189 private function filterpages($todopages, $data) { 190 $pages = array(); 191 foreach($todopages as $page) { 192 $todos = array(); 193 // contains 3 arrays: an array with complete matches and 2 arrays with subpatterns 194 foreach($page['matches'][1] as $todoindex => $todomatch) { 195 list($checked, $todouser) = $this->parseTodoArgs($todomatch); 196 $todotitle = trim($page['matches'][2][$todoindex]); 197 198 if($this->isRequestedTodo($data, $checked, $todouser)) { 199 $todos[] = array($todotitle, $todoindex, $todouser, $checked); 200 } 201 } 202 if(count($todos) > 0) { 203 $pages[] = array('id' => $page['id'], 'todos' => $todos); 204 } 205 } 206 return $pages; 207 } 208 209 /** 210 * Create html for table with todos 211 * 212 * @param Doku_Renderer_xhtml $R 213 * @param array $todopages 214 * @param array $data array with rendering options 215 */ 216 private function htmlTodoTable($R, $todopages, $data) { 217 $R->table_open(); 218 foreach($todopages as $page) { 219 $R->tablerow_open(); 220 $R->tableheader_open(); 221 $R->internallink($page['id'], $page['id']); 222 $R->tableheader_close(); 223 $R->tablerow_close(); 224 foreach($page['todos'] as $todo) { 225 $R->tablerow_open(); 226 $R->tablecell_open(); 227 $R->doc .= $this->createTodoItem($R, $todo[0], $todo[1], $todo[2], $todo[3], $page['id'], $data); 228 $R->tablecell_close(); 229 $R->tablerow_close(); 230 } 231 } 232 $R->table_close(); 233 } 234 235 /** 236 * Check the conditions for adding a todoitem 237 * 238 * @param $data array the defined filters 239 * @param $checked bool completion status of task; true: finished, false: open 240 * @param $todouser string user username of user 241 * @return bool if the todoitem should be listed 242 */ 243 private function isRequestedTodo($data, $checked, $todouser) { 244 245 //completion status 246 $condition1 = $data['completed'] === 'all' //all 247 || $data['completed'] === $checked; //yes or no 248 249 // resolve placeholder in assignees 250 $requestedassignees = array(); 251 if(is_array($data['assigned'])) { 252 $requestedassignees = array_map( 253 function($user) { 254 global $USERINFO; 255 if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER') 256 return $_SERVER['REMOTE_USER']; 257 } 258 // @date 20140317 le: check for logged in user email address 259 if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) { 260 return $USERINFO['mail']; 261 } 262 return $user; 263 }, 264 $data['assigned'] 265 ); 266 } 267 //assigned 268 $condition2 = $data['assigned'] === 'all' //all 269 || (is_bool($data['assigned']) && $data['assigned'] == $todouser) //yes or no 270 || (is_array($data['assigned']) && in_array($todouser, $requestedassignees)); //one of the requested users? 271 272 return $condition1 AND $condition2; 273 } 274} 275