1<?php
2/**
3 * DokuWiki Plugin do (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 * @author  Adrian Lang <lang@cosmocode.de>
8 * @author  Dominik Eckelmann <eckelmann@cosmocode.de>
9 */
10
11class syntax_plugin_do_dolist extends DokuWiki_Syntax_Plugin
12{
13
14    /** @inheritDoc */
15    public function getType()
16    {
17        return 'substition';
18    }
19
20    /** @inheritDoc */
21    public function getPType()
22    {
23        return 'block';
24    }
25
26    /** @inheritDoc */
27    public function getSort()
28    {
29        return 155;
30    }
31
32    /** @inheritDoc */
33    public function connectTo($mode)
34    {
35        $this->Lexer->addSpecialPattern('{{dolist>.*?}}', $mode, 'plugin_do_dolist');
36    }
37
38    /**
39     * Handler to prepare matched data for the rendering process
40     *
41     * @param   string       $match   The text matched by the patterns
42     * @param   int          $state   The lexer state for the match
43     * @param   int          $pos     The character position of the matched text
44     * @param   Doku_Handler $handler Reference to the Doku_Handler object
45     *
46     * @return  array Return an array with all data you want to use in render()
47     */
48    public function handle($match, $state, $pos, Doku_Handler $handler)
49    {
50        // parse the given match
51        $match = substr($match, 9, -2);
52
53        // usability enhance
54        //
55        // if there is no ? but a & the user has probably forgot the ? befor the first arg.
56        // in this case we'll replace the first & to a ?
57        if (strpos($match, '?') === false) {
58            $pos = strpos($match, '&');
59            if (is_int($pos)) {
60                $match[$pos] = '?';
61            }
62        }
63
64        $url = explode('?', $match, 2);
65        $ns = $url[0];
66        $args = array();
67
68        // check the arguments
69        if (isset($url[1])) {
70            parse_str($url[1], $args);
71
72            // check for filters
73            $filters = array_keys($args);
74            foreach ($filters as $filter) {
75                if (isset($args[$filter])) {
76                    $args[$filter] = explode(',', $args[$filter]);
77                    $c = count($args[$filter]);
78                    for ($i = 0; $i < $c; $i++) {
79                        $args[$filter][$i] = trim($args[$filter][$i]);
80                    }
81                }
82            }
83        }
84
85        $args['ns'] = $ns;
86
87        return $args;
88    }
89
90    /**
91     * Create output
92     *
93     * @param string        $mode output format being rendered
94     * @param Doku_Renderer $R    reference to the current renderer object
95     * @param array         $data data created by handler()
96     *
97     * @return bool
98     */
99    public function render($mode, Doku_Renderer $R, $data)
100    {
101        if ($mode != 'xhtml') {
102            return false;
103        }
104        $R->info['cache'] = false;
105
106        /** @var helper_plugin_do $hlp */
107        $hlp = plugin_load('helper', 'do');
108        $tasks = $hlp->loadTasks($data);
109
110        if (count($tasks) === 0) {
111            $R->tablerow_open();
112            $R->tablecell_open(6, 'center');
113            $R->cdata($this->getLang('none'));
114            $R->tablecell_close();
115            $R->tablerow_close();
116            $R->doc .= '</table>';
117            return true;
118        }
119
120        $R->doc .= $this->buildTasklistHTML($tasks, isset($data['user']), isset($data['creator']));
121
122        return true;
123    }
124
125    /**
126     * @param array $tasks
127     * @param bool  $hideUser
128     * @param bool  $hideCreator
129     *
130     * @return string
131     */
132    public function buildTasklistHTML(array $tasks, $hideUser, $hideCreator)
133    {
134        $userstyle = $hideUser ? ' style="display: none;"' : '';
135        $creatorstyle = $hideCreator ? ' style="display: none;"' : '';
136
137        $table = '<table class="inline plugin_do">';
138        $table .= '    <tr>';
139        $table .= '    <th>' . $this->getLang('task') . '</th>';
140        $table .= '    <th' . $userstyle . '>' . $this->getLang('user') . '</th>';
141        $table .= '    <th>' . $this->getLang('date') . '</th>';
142        $table .= '    <th>' . $this->getLang('status') . '</th>';
143        $table .= '    <th' . $creatorstyle . '>' . $this->getLang('creator') . '</th>';
144        $table .= '    <th>' . $this->lang['js']['popup_msg'] . '</th>';
145        $table .= '  </tr>';
146
147        global $ID;
148        /** @var helper_plugin_do $hlp */
149        $hlp = plugin_load('helper', 'do');
150
151        foreach ($tasks as $row) {
152            $table .= '<tr>';
153            $table .= '<td class="plugin_do_page">';
154            $table .= '<a title="' . $row['page'] . '" href="' . wl($row['page']) . '#plgdo__' . $row['md5'] . '" class="wikilink1">' . hsc($row['text']) . '</a>';
155            $table .= '</td>';
156            $table .= '<td class="plugin_do_assignee"' . $userstyle . '>';
157            foreach ($row['users'] as &$user) {
158                $user = $hlp->getPrettyUser($user);
159            }
160            unset($user);
161            $table .= implode(', ', $row['users']);
162            $table .= '</td>';
163            $table .= '<td class="plugin_do_date">' . hsc($row['date']) . '</td>';
164            $table .= '<td class="plugin_do_status" align="center">';
165
166            // task status icon...
167            $image = ($row['status']) ? 'done.png' : 'undone.png';
168            $editor = ($row['closedby']) ? $hlp->getPrettyUser($row['closedby']) : '';
169
170            $table .= '<span class="plugin_do_item plugin_do_' . $row['md5'] . ($row['status'] ? ' plugin_do_done' : '') . '">'; // outer span
171
172            // img link
173            $table .= '<a href="' . wl($ID,
174                    array('do' => 'plugin_do', 'do_page' => $row['page'], 'do_md5' => $row['md5']));
175            $table .= '" class="plugin_do_status">';
176
177            $table .= '<img src="' . DOKU_BASE . 'lib/plugins/do/pix/' . $image . '" />';
178            $table .= '</a>';
179            $table .= '<span>' . $editor . '</span>';
180
181            $table .= '</span>'; // outer span end
182
183            $table .= '</td>';
184            $table .= '<td class="plugin_do_creator"' . $creatorstyle . '>';
185            $table .= $hlp->getPrettyUser($row['creator']);
186            $table .= '</td>';
187            $table .= '<td class="plugin_do_commit">';
188            $table .= hsc($row['msg']);
189            $table .= '</td>';
190
191            $table .= '  </tr>';
192        }
193
194        $table .= '</table>';
195        return $table;
196    }
197}
198
199