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