xref: /plugin/discussion/syntax/threads.php (revision dfc5d08b5eea7cef6dcb63789032dcac586bcb87)
1<?php
2/**
3 * Discussion Plugin, threads component: displays a list of recently active discussions
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Esther Brunner <wikidesign@gmail.com>
7 */
8
9/**
10 * Class syntax_plugin_discussion_threads
11 */
12class syntax_plugin_discussion_threads extends DokuWiki_Syntax_Plugin
13{
14
15    /**
16     * Syntax Type
17     *
18     * @return string
19     */
20    public function getType()
21    {
22        return 'substition';
23    }
24
25    /**
26     * Paragraph Type
27     *
28     * @return string
29     * @see Doku_Handler_Block
30     */
31    public function getPType()
32    {
33        return 'block';
34    }
35
36    /**
37     * Sort for applying this mode
38     *
39     * @return int
40     */
41    public function getSort()
42    {
43        return 306;
44    }
45
46    /**
47     * @param string $mode
48     */
49    public function connectTo($mode)
50    {
51        $this->Lexer->addSpecialPattern('\{\{threads>.+?\}\}', $mode, 'plugin_discussion_threads');
52    }
53
54    /**
55     * Handler to prepare matched data for the rendering process
56     *
57     * @param string $match The text matched by the patterns
58     * @param int $state The lexer state for the match
59     * @param int $pos The character position of the matched text
60     * @param Doku_Handler $handler The Doku_Handler object
61     * @return  array Return an array with all data you want to use in render
62     */
63    public function handle($match, $state, $pos, Doku_Handler $handler)
64    {
65        global $ID;
66        $customFlags = array();
67
68        $match = substr($match, 10, -2); // strip {{threads> from start and }} from end
69        list($match, $flags) = array_pad(explode('&', $match, 2), 2, '');
70        $flags = explode('&', $flags);
71
72        // Identify the count/skipempty flag and remove it before passing it to pagelist
73        foreach ($flags as $key => $flag) {
74            if (substr($flag, 0, 5) == "count") {
75                $tmp = array_pad(explode('=', $flag, 2), 2, 0);
76                $customFlags['count'] = $tmp[1];
77                unset($flags[$key]);
78            } elseif (substr($flag, 0, 9) == "skipempty") {
79                $customFlags['skipempty'] = true;
80                unset($flags[$key]);
81            } elseif (substr($flag, 0, 15) == "nonewthreadform") {
82                $customFlags['nonewthreadform'] = true;
83                unset($flags[$key]);
84            }
85        }
86
87        // Ignore params if invalid values have been passed
88        if (!array_key_exists('count', $customFlags) || $customFlags['count'] <= 0 || !is_numeric($customFlags['count'])) {
89            $customFlags['count'] = 0;
90        }
91        if (!array_key_exists('skipempty', $customFlags) && !$customFlags['skipempty']) {
92            $customFlags['skipempty'] = false;
93        }
94
95        list($ns, $refine) = array_pad(explode(' ', $match, 2), 2, '');
96
97        if ($ns == '*' || $ns == ':') {
98            $ns = '';
99        } elseif ($ns == '.') {
100            $ns = getNS($ID);
101        } else {
102            $ns = cleanID($ns);
103        }
104
105        return [$ns, $flags, $refine, $customFlags];
106    }
107
108    /**
109     * Handles the actual output creation.
110     *
111     * @param string $format output format being rendered
112     * @param Doku_Renderer $renderer the current renderer object
113     * @param array $data data created by handler()
114     * @return boolean rendered correctly?
115     */
116    public function render($format, Doku_Renderer $renderer, $data)
117    {
118        list($ns, $flags, $refine, $customFlags) = $data;
119        $count = $customFlags['count'];
120        $skipEmpty = $customFlags['skipempty'];
121        $noNewThreadForm = $customFlags['nonewthreadform'];
122        $i = 0;
123
124        $pages = [];
125        /** @var helper_plugin_discussion $helper */
126        if ($helper = $this->loadHelper('discussion')) {
127            $pages = $helper->getThreads($ns, null, $skipEmpty);
128        }
129
130        // use tag refinements?
131        if ($refine) {
132            /** @var helper_plugin_tag $tag */
133            if (!$tag = $this->loadHelper('tag', false)) {
134                msg('The Tag Plugin must be installed to use tag refinements.', -1);
135            } else {
136                $pages = $tag->tagRefine($pages, $refine);
137            }
138        }
139
140        if (!$pages) {
141            if (auth_quickaclcheck($ns . ':*') >= AUTH_CREATE && $format == 'xhtml') {
142                $renderer->nocache();
143                if ($noNewThreadForm !== true) {
144                    $renderer->doc .= $this->newThreadForm($ns);
145                }
146            }
147            return true; // nothing to display
148        }
149
150        if ($format == 'xhtml') {
151            /** @var Doku_Renderer_xhtml $renderer */
152            // prevent caching to ensure content is always fresh
153            $renderer->nocache();
154
155            // show form to start a new discussion thread?
156            if ($noNewThreadForm !== true) {
157                $hasCreatePermission = auth_quickaclcheck($ns . ':*') >= AUTH_CREATE;
158                if ($hasCreatePermission && $this->getConf('threads_formposition') == 'top') {
159                    $renderer->doc .= $this->newThreadForm($ns);
160                }
161            }
162
163            // let Pagelist Plugin do the work for us
164            /** @var helper_plugin_pagelist $pagelist */
165            if (!$pagelist = $this->loadHelper('pagelist', false)) {
166                msg('The Pagelist Plugin must be installed for threads lists to work.', -1);
167                return false;
168            }
169            $pagelist->addColumn('discussion', 'comments');
170            $pagelist->setFlags($flags);
171            $pagelist->startList();
172            foreach ($pages as $page) {
173                $page['class'] = 'discussion_status' . $page['status'];
174                $pagelist->addPage($page);
175
176                $i++;
177                if ($count > 0 && $i >= $count) {
178                    // Only display the n discussion threads specified by the count flag
179                    break;
180                }
181            }
182            $renderer->doc .= $pagelist->finishList();
183
184            // show form to start a new discussion thread?
185            if ($noNewThreadForm !== true) {
186                if ($hasCreatePermission && $this->getConf('threads_formposition') == 'bottom') {
187                    $renderer->doc .= $this->newThreadForm($ns);
188                }
189            }
190
191            return true;
192
193            // for metadata renderer
194        } elseif ($format == 'metadata') {
195            /** @var Doku_Renderer_metadata $renderer */
196            foreach ($pages as $page) {
197                $renderer->meta['relation']['references'][$page['id']] = true;
198            }
199
200            return true;
201        }
202        return false;
203    }
204
205    /* ---------- (X)HTML Output Functions ---------- */
206
207    /**
208     * Show the form to start a new discussion thread
209     *
210     * @param string $ns
211     * @return string html
212     */
213    protected function newThreadForm($ns)
214    {
215        global $ID;
216        global $lang;
217
218        return '<div class="newthread_form">'
219                . '<form id="discussion__newthread_form"  method="post" action="' . script() . '" accept-charset="' . $lang['encoding'] . '">'
220                    . '<fieldset>'
221                        . '<legend> ' . $this->getLang('newthread') . ': </legend>'
222                        . '<input type="hidden" name="id" value="' . $ID . '" />'
223                        . '<input type="hidden" name="do" value="newthread" />'
224                        . '<input type="hidden" name="ns" value="' . $ns . '" />'
225                        . '<input class="edit" type="text" name="title" id="discussion__newthread_title" size="40" tabindex="1" />'
226                        . '<input class="button" type="submit" value="' . $lang['btn_create'] . '" tabindex="2" />'
227                    . '</fieldset>'
228                . '</form>'
229            . '</div>';
230    }
231}
232