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 trim(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 $todo = array_merge(array('todotitle' => trim($page['matches'][2][$todoindex]), 'todoindex' => $todoindex), $this->parseTodoArgs($todomatch), $data); 202 203 if($this->isRequestedTodo($todo)) { $todos[] = $todo; } 204 } 205 if(count($todos) > 0) { 206 $pages[] = array('id' => $page['id'], 'todos' => $todos); 207 } 208 } 209 return $pages; 210 } 211 212 /** 213 * Create html for table with todos 214 * 215 * @param Doku_Renderer_xhtml $R 216 * @param array $todopages 217 * @param array $data array with rendering options 218 */ 219 private function htmlTodoTable($R, $todopages, $data) { 220 $R->table_open(); 221 foreach($todopages as $page) { 222 if ($data['header']!='none') { 223 $R->tablerow_open(); 224 $R->tableheader_open(); 225 $R->internallink($page['id'], ($data['header']=='firstheader' ? p_get_first_heading($page['id']) : $page['id'])); 226 $R->tableheader_close(); 227 $R->tablerow_close(); 228 } 229 foreach($page['todos'] as $todo) { 230//echo "<pre>";var_dump($todo);echo "</pre>"; 231 $R->tablerow_open(); 232 $R->tablecell_open(); 233 $R->doc .= $this->createTodoItem($R, $page['id'], array_merge($todo, $data)); 234 $R->tablecell_close(); 235 $R->tablerow_close(); 236 } 237 } 238 $R->table_close(); 239 } 240 241 /** 242 * Check the conditions for adding a todoitem 243 * 244 * @param $data array the defined filters 245 * @param $checked bool completion status of task; true: finished, false: open 246 * @param $todouser string user username of user 247 * @return bool if the todoitem should be listed 248 */ 249 private function isRequestedTodo($data) { 250 251 //completion status 252 $condition1 = $data['completed'] === 'all' //all 253 || $data['completed'] === $data['checked']; //yes or no 254 255 // resolve placeholder in assignees 256 $requestedassignees = array(); 257 if(is_array($data['assigned'])) { 258 $requestedassignees = array_map( 259 function($user) { 260 global $USERINFO; 261 if($user == '@@USER@@' && !empty($_SERVER['REMOTE_USER'])) { //$INPUT->server->str('REMOTE_USER') 262 return $_SERVER['REMOTE_USER']; 263 } 264 // @date 20140317 le: check for logged in user email address 265 if( $user == '@@MAIL@@' && isset( $USERINFO['mail'] ) ) { 266 return $USERINFO['mail']; 267 } 268 return $user; 269 }, 270 $data['assigned'] 271 ); 272 } 273 //assigned 274 $condition2 = $condition2 275 || $data['assigned'] === 'all' //all 276 || (is_bool($data['assigned']) && $data['assigned'] == $data['todouser']); //yes or no 277 278 if (!$condition2 && is_array($data['assigned']) && is_array($data['todousers'])) 279 foreach($data['todousers'] as $todouser) { 280 if(in_array($todouser, $requestedassignees)) { $condition2 = true; break; } 281 } 282 283 return $condition1 AND $condition2; 284 } 285} 286