xref: /plugin/discussion/syntax/threads.php (revision ee05896a6eca3674e2b6bef9b3b0b780fcc7e44f)
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            } elseif (substr($flag, 0, 9) == "skipempty") {
70                $customFlags['skipempty'] = true;
71                unset($flags[$key]);
72            } elseif (substr($flag, 0, 15) == "nonewthreadform") {
73                $customFlags['nonewthreadform'] = true;
74                unset($flags[$key]);
75            }
76        }
77
78        // Ignore params if invalid values have been passed
79        if(!array_key_exists('count', $customFlags) || $customFlags['count'] <= 0 || !is_numeric($customFlags['count'])) $customFlags['count'] = false;
80        if(!array_key_exists('skipempty', $customFlags) && !$customFlags['skipempty']) $customFlags['skipempty'] = false;
81
82        list($ns, $refine) = explode(' ', $match, 2);
83
84        if (($ns == '*') || ($ns == ':')) $ns = '';
85        elseif ($ns == '.') $ns = getNS($ID);
86        else $ns = cleanID($ns);
87
88        return array($ns, $flags, $refine, $customFlags);
89    }
90
91    /**
92     * Handles the actual output creation.
93     *
94     * @param   $mode   string        output format being rendered
95     * @param   $renderer Doku_Renderer the current renderer object
96     * @param   $data     array         data created by handler()
97     * @return  boolean                 rendered correctly?
98     */
99    function render($mode, Doku_Renderer $renderer, $data) {
100        list($ns, $flags, $refine, $customFlags) = $data;
101        $count = $customFlags['count'];
102        $skipEmpty = $customFlags['skipempty'];
103        $noNewThreadForm = $customFlags['nonewthreadform'];
104        $i = 0;
105
106        $pages = array();
107        /** @var helper_plugin_discussion $my */
108        if ($my =& plugin_load('helper', 'discussion')) $pages = $my->getThreads($ns, null, $skipEmpty);
109
110        // use tag refinements?
111        if ($refine) {
112            /** @var helper_plugin_tag $tag */
113            if (plugin_isdisabled('tag') || (!$tag = plugin_load('helper', 'tag'))) {
114                msg('The Tag Plugin must be installed to use tag refinements.', -1);
115            } else {
116                $pages = $tag->tagRefine($pages, $refine);
117            }
118        }
119
120        if (!$pages) {
121            if ((auth_quickaclcheck($ns.':*') >= AUTH_CREATE) && ($mode == 'xhtml')) {
122                $renderer->info['cache'] = false;
123                if ($noNewThreadForm !== true) {
124                    $renderer->doc .= $this->_newThreadForm($ns);
125                }
126            }
127            return true; // nothing to display
128        }
129
130        if ($mode == 'xhtml') {
131            /** @var $renderer Doku_Renderer_xhtml */
132            // prevent caching to ensure content is always fresh
133            $renderer->info['cache'] = false;
134
135            // show form to start a new discussion thread?
136            if ($noNewThreadForm !== true) {
137                $perm_create = (auth_quickaclcheck($ns.':*') >= AUTH_CREATE);
138                if ($perm_create && ($this->getConf('threads_formposition') == 'top')) {
139                    $renderer->doc .= $this->_newThreadForm($ns);
140                }
141            }
142
143            // let Pagelist Plugin do the work for us
144            /** @var $pagelist helper_plugin_pagelist */
145            if (plugin_isdisabled('pagelist')
146                    || (!$pagelist =& plugin_load('helper', 'pagelist'))) {
147                msg('The Pagelist Plugin must be installed for threads lists to work.', -1);
148                return false;
149            }
150            $pagelist->column['comments'] = true;
151            $pagelist->setFlags($flags);
152            $pagelist->startList();
153            foreach ($pages as $page) {
154                $page['class'] = 'discussion_status'.$page['status'];
155                $pagelist->addPage($page);
156
157                $i++;
158                if($count != false && $i >= $count) break; // Only display the n discussion threads specified by the count flag
159            }
160            $renderer->doc .= $pagelist->finishList();
161
162            // show form to start a new discussion thread?
163            if ($noNewThreadForm !== true) {
164                if ($perm_create && ($this->getConf('threads_formposition') == 'bottom')) {
165                    $renderer->doc .= $this->_newThreadForm($ns);
166                }
167            }
168
169            return true;
170
171            // for metadata renderer
172        } elseif ($mode == 'metadata') {
173            /** @var $renderer Doku_Renderer_metadata */
174            foreach ($pages as $page) {
175                $renderer->meta['relation']['references'][$page['id']] = true;
176            }
177
178            return true;
179        }
180        return false;
181    }
182
183    /* ---------- (X)HTML Output Functions ---------- */
184
185    /**
186     * Show the form to start a new discussion thread
187     *
188     * @param string $ns
189     * @return string
190     */
191    function _newThreadForm($ns) {
192        global $ID;
193        global $lang;
194
195        return '<div class="newthread_form">'.DOKU_LF.
196            '<form id="discussion__newthread_form"  method="post" action="'.script().'" accept-charset="'.$lang['encoding'].'">'.DOKU_LF.
197            DOKU_TAB.'<fieldset>'.DOKU_LF.
198            DOKU_TAB.DOKU_TAB.'<legend> '.$this->getLang('newthread').': </legend>'.DOKU_LF.
199            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="id" value="'.$ID.'" />'.DOKU_LF.
200            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="do" value="newthread" />'.DOKU_LF.
201            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="ns" value="'.$ns.'" />'.DOKU_LF.
202            DOKU_TAB.DOKU_TAB.'<input class="edit" type="text" name="title" id="discussion__newthread_title" size="40" tabindex="1" />'.DOKU_LF.
203            DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$lang['btn_create'].'" tabindex="2" />'.DOKU_LF.
204            DOKU_TAB.'</fieldset>'.DOKU_LF.
205            '</form>'.DOKU_LF.
206            '</div>'.DOKU_LF;
207    }
208}
209// vim:ts=4:sw=4:et:enc=utf-8:
210