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 'header' => $this->getConf("Header"), 61 'completed' => 'all', 62 'assigned' => 'all', 63 'checkbox' => $this->getConf("Checkbox"), 64 'username' => $this->getConf("Username"), 65 ); 66 $allowedvalues = array('yes', 'no'); 67 foreach($options as $option) { 68 @list($key, $value) = explode(':', $option, 2); 69 switch($key) { 70 case 'header': // how should the header be rendered? 71 if(in_array($value, array('id', 'firstheader', 'none'))) { 72 $data['header'] = $value; 73 } 74 break; 75 case 'checkbox': // should checkbox be rendered? 76 if(in_array($value, $allowedvalues)) { 77 $data['checkbox'] = ($value == 'yes'); 78 } 79 break; 80 case 'completed': 81 if(in_array($value, $allowedvalues)) { 82 $data['completed'] = ($value == 'yes'); 83 } 84 break; 85 case 'username': // how should the username be rendered? 86 if(in_array($value, array('user', 'real', 'none'))) { 87 $data['username'] = $value; 88 } 89 break; 90 case 'assigned': 91 if(in_array($value, $allowedvalues)) { 92 $data['assigned'] = ($value == 'yes'); 93 break; 94 } 95 //assigned? 96 $data['assigned'] = explode(',', $value); 97 // @date 20140317 le: if check for logged in user, also check for logged in user email address 98 if( in_array( '@@USER@@', $data['assigned'] ) ) { 99 $data['assigned'][] = '@@MAIL@@'; 100 } 101 $data['assigned'] = array_map( 102 function ($user) { 103 //placeholder (inspired by replacement-patterns - see https://www.dokuwiki.org/namespace_templates#replacement_patterns) 104 if( $user == '@@USER@@' || $user == '@@MAIL@@' ) { 105 return $user; 106 } 107 //user 108 return ltrim($user, '@'); 109 }, $data['assigned'] 110 ); 111 break; 112 } 113 } 114 return $data; 115 } 116 117 /** 118 * Render xhtml output or metadata 119 * 120 * @param string $mode Renderer mode (supported modes: xhtml) 121 * @param Doku_Renderer $renderer The renderer 122 * @param array $data The data from the handler() function 123 * @return bool If rendering was successful. 124 */ 125 public function render($mode, Doku_Renderer &$renderer, $data) { 126 global $conf; 127 128 if($mode != 'xhtml') return false; 129 /** @var Doku_Renderer_xhtml $renderer */ 130 131 $opts['pattern'] = '/<todo([^>]*)>(.*)<\/todo[\W]*?>/'; //all todos in a wiki page 132 //TODO check if storing subpatterns doesn't cost too much resources 133 134 // search(&$data, $base, $func, $opts,$dir='',$lvl=1,$sort='natural') 135 search($todopages, $conf['datadir'], array($this, 'search_todos'), $opts); //browse wiki pages with callback to search_pattern 136 137 $todopages = $this->filterpages($todopages, $data); 138 139 $this->htmlTodoTable($renderer, $todopages, $data); 140 141 return true; 142 } 143 144 /** 145 * Custom search callback 146 * 147 * This function is called for every found file or 148 * directory. When a directory is given to the function it has to 149 * decide if this directory should be traversed (true) or not (false). 150 * Return values for files are ignored 151 * 152 * All functions should check the ACL for document READ rights 153 * namespaces (directories) are NOT checked (when sneaky_index is 0) as this 154 * would break the recursion (You can have an nonreadable dir over a readable 155 * one deeper nested) also make sure to check the file type (for example 156 * in case of lockfiles). 157 * 158 * @param array &$data - Reference to the result data structure 159 * @param string $base - Base usually $conf['datadir'] 160 * @param string $file - current file or directory relative to $base 161 * @param string $type - Type either 'd' for directory or 'f' for file 162 * @param int $lvl - Current recursion depht 163 * @param array $opts - option array as given to search() 164 * @return bool if this directory should be traversed (true) or not (false). Return values for files are ignored. 165 */ 166 public function search_todos(&$data, $base, $file, $type, $lvl, $opts) { 167 $item['id'] = pathID($file); //get current file ID 168 169 //we do nothing with directories 170 if($type == 'd') return true; 171 172 //only search txt files 173 if(substr($file, -4) != '.txt') return true; 174 175 //check ACL 176 if(auth_quickaclcheck($item['id']) < AUTH_READ) return false; 177 178 $wikitext = rawWiki($item['id']); //get wiki text 179 180 $item['count'] = preg_match_all($opts['pattern'], $wikitext, $matches); //count how many times appears the pattern 181 if(!empty($item['count'])) { //if it appears at least once 182 $item['matches'] = $matches; 183 $data[] = $item; 184 } 185 return true; 186 } 187 188 /** 189 * filter the pages 190 * 191 * @param $todopages array pages with all todoitems 192 * @param $data array listing parameters 193 * @return array filtered pages 194 */ 195 private function filterpages($todopages, $data) { 196 $pages = array(); 197 foreach($todopages as $page) { 198 $todos = array(); 199 // contains 3 arrays: an array with complete matches and 2 arrays with subpatterns 200 foreach($page['matches'][1] as $todoindex => $todomatch) { 201 $todoargs = $this->parseTodoArgs($todomatch); 202 $todotitle = trim($page['matches'][2][$todoindex]); 203 204 if($this->isRequestedTodo($data, $todoargs['checked'], $todoargs['todouser'])) { 205 $todos[] = array_merge(array('todotitle' => $todotitle, 'todoindex' => $todoindex, 'todouser' => $todouser, 'checked' => $checked), $this->parseTodoArgs($todomatch)); 206 } 207 } 208 if(count($todos) > 0) { 209 $pages[] = array('id' => $page['id'], 'todos' => $todos); 210 } 211 } 212 return $pages; 213 } 214 215 /** 216 * Create html for table with todos 217 * 218 * @param Doku_Renderer_xhtml $R 219 * @param array $todopages 220 * @param array $data array with rendering options 221 */ 222 private function htmlTodoTable($R, $todopages, $data) { 223 $R->table_open(); 224 foreach($todopages as $page) { 225 if ($data['header']!='none') { 226 $R->tablerow_open(); 227 $R->tableheader_open(); 228 $R->internallink($page['id'], ($data['header']=='firstheader' ? p_get_first_heading($page['id']) : $page['id'])); 229 $R->tableheader_close(); 230 $R->tablerow_close(); 231 } 232 foreach($page['todos'] as $todo) { 233 $R->tablerow_open(); 234 $R->tablecell_open(); 235 $R->doc .= $this->createTodoItem($R, $page['id'], array_merge($todo, $data)); 236 $R->tablecell_close(); 237 $R->tablerow_close(); 238 } 239 } 240 $R->table_close(); 241 } 242 243 /** 244 * Check the conditions for adding a todoitem 245 * 246 * @param $data array the defined filters 247 * @param $checked bool completion status of task; true: finished, false: open 248 * @param $todouser string user username of user 249 * @return bool if the todoitem should be listed 250 */ 251 private function isRequestedTodo($data, $checked, $todouser) { 252 253 //completion status 254 $condition1 = $data['completed'] === 'all' //all 255 || $data['completed'] === $checked; //yes or no 256 257 // resolve placeholder in assignees 258 $requestedassignees = array(); 259 if(is_array($data['assigned'])) { 260 $requestedassignees = array_map( 261 function($user) { 262 global $USERINFO; 263 if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER') 264 return $_SERVER['REMOTE_USER']; 265 } 266 // @date 20140317 le: check for logged in user email address 267 if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) { 268 return $USERINFO['mail']; 269 } 270 return $user; 271 }, 272 $data['assigned'] 273 ); 274 } 275 //assigned 276 $condition2 = $data['assigned'] === 'all' //all 277 || (is_bool($data['assigned']) && $data['assigned'] == $todouser) //yes or no 278 || (is_array($data['assigned']) && in_array($todouser, $requestedassignees)); //one of the requested users? 279 280 return $condition1 AND $condition2; 281 } 282} 283