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