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