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