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