xref: /plugin/todo/syntax/list.php (revision a212ca221efb6ea9c4aaeb526b5393b0cac08c69)
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);
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            // contains 3 arrays: an array with complete matches and 2 arrays with subpatterns
174            foreach($page['matches'][1] as $todoindex => $todomatch) {
175                list($checked, $todouser) = $this->parseTodoArgs($todomatch);
176                $todotitle = trim($page['matches'][2][$todoindex]);
177
178                if(
179                    (
180                        //completed?
181                        $data['completed'] === 'all'
182                        OR $data['completed'] === $checked //yes or no
183                    ) AND (
184                        //assigned?
185                        $data['assigned'] === 'all'
186                        OR (is_bool($data['assigned']) && $data['assigned'] == $todouser) //yes or no
187                        OR (is_array($data['assigned']) && in_array($todouser, $data['assigned'])) //one of requested users?
188                    )
189                ) {
190                    $todos[] = array($todotitle, $todoindex, $todouser, $checked);
191                }
192            }
193            if(count($todos) > 0) {
194                $pages[] = array('id' => $page['id'], 'todos' => $todos);
195            }
196        }
197        return $pages;
198    }
199
200    /**
201     * Create html for table with todos
202     *
203     * @param Doku_Renderer_xhtml $R
204     * @param array $todopages
205     */
206    private function htmlTodoTable($R, $todopages) {
207        $R->table_open();
208        foreach($todopages as $page) {
209            $R->tablerow_open();
210            $R->tableheader_open();
211            $R->internallink($page['id'], $page['id']);
212            $R->tableheader_close();
213            $R->tablerow_close();
214            foreach($page['todos'] as $todo) {
215                $R->tablerow_open();
216                $R->tablecell_open();
217                $R->doc .= $this->createTodoItem($R, $todo[0], $todo[1], $todo[2], $todo[3], $page['id']);
218                $R->tablecell_close();
219                $R->tablerow_close();
220            }
221        }
222        $R->table_close();
223    }
224}
225