xref: /plugin/discussion/syntax/threads.php (revision 912b905302633294c634d8d91070944df4e2eff1)
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
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
15
16require_once(DOKU_PLUGIN.'syntax.php');
17
18class syntax_plugin_discussion_threads extends DokuWiki_Syntax_Plugin {
19
20    function getType() { return 'substition'; }
21    function getPType() { return 'block'; }
22    function getSort() { return 306; }
23
24    function connectTo($mode) {
25        $this->Lexer->addSpecialPattern('\{\{threads>.+?\}\}', $mode, 'plugin_discussion_threads');
26    }
27
28    function handle($match, $state, $pos, &$handler) {
29        global $ID;
30        $flagCount = -1;
31
32        $match = substr($match, 10, -2); // strip {{threads> from start and }} from end
33        list($match, $flags) = explode('&', $match, 2);
34        $flags = explode('&', $flags);
35
36        // Identify the "count" flag and remove it before passing it to pagelist
37        foreach($flags as $key => $flag) {
38            if(substr($flag, 0, 5) == "count") {
39                $flagCount = $flag;
40                unset($flags[$key]);
41                break;
42            }
43        }
44
45        $flagCount = explode('=', $flagCount);
46        $flagCount = $flagCount[1]; // Holds the value of "count"
47        if($flagCount <= 0 || !is_numeric($flagCount)) $flagCount = false; // Ignore param if invalid value is passed
48
49        list($ns, $refine) = explode(' ', $match, 2);
50
51        if (($ns == '*') || ($ns == ':')) $ns = '';
52        elseif ($ns == '.') $ns = getNS($ID);
53        else $ns = cleanID($ns);
54
55        return array($ns, $flags, $refine, $flagCount);
56    }
57
58    function render($mode, &$renderer, $data) {
59        list($ns, $flags, $refine, $count) = $data;
60        $i = 0;
61
62        if ($my =& plugin_load('helper', 'discussion')) $pages = $my->getThreads($ns);
63
64        // use tag refinements?
65        if ($refine) {
66            if (plugin_isdisabled('tag') || (!$tag = plugin_load('helper', 'tag'))) {
67                msg('The Tag Plugin must be installed to use tag refinements.', -1);
68            } else {
69                $pages = $tag->tagRefine($pages, $refine);
70            }
71        }
72
73        if (!$pages) {
74            if ((auth_quickaclcheck($ns.':*') >= AUTH_CREATE) && ($mode == 'xhtml')) {
75                $renderer->info['cache'] = false;
76                $renderer->doc .= $this->_newThreadForm($ns);
77            }
78            return true; // nothing to display
79        }
80
81        if ($mode == 'xhtml') {
82
83            // prevent caching to ensure content is always fresh
84            $renderer->info['cache'] = false;
85
86            // show form to start a new discussion thread?
87            $perm_create = (auth_quickaclcheck($ns.':*') >= AUTH_CREATE);
88            if ($perm_create && ($this->getConf('threads_formposition') == 'top'))
89                $renderer->doc .= $this->_newThreadForm($ns);
90
91            // let Pagelist Plugin do the work for us
92            if (plugin_isdisabled('pagelist')
93                    || (!$pagelist =& plugin_load('helper', 'pagelist'))) {
94                msg('The Pagelist Plugin must be installed for threads lists to work.', -1);
95                return false;
96            }
97            $pagelist->column['comments'] = true;
98            $pagelist->setFlags($flags);
99            $pagelist->startList();
100            foreach ($pages as $key => $page) {
101                $page['class'] = 'discussion_status'.$page['status'];
102                $pagelist->addPage($page);
103
104                $i++;
105                if($count != false && $i >= $count) break; // Only display the n discussion threads specified by the count flag
106            }
107            $renderer->doc .= $pagelist->finishList();
108
109            // show form to start a new discussion thread?
110            if ($perm_create && ($this->getConf('threads_formposition') == 'bottom'))
111                $renderer->doc .= $this->_newThreadForm($ns);
112
113            return true;
114
115            // for metadata renderer
116        } elseif ($mode == 'metadata') {
117            foreach ($pages as $page) {
118                $renderer->meta['relation']['references'][$page['id']] = true;
119            }
120
121            return true;
122        }
123        return false;
124    }
125
126    /* ---------- (X)HTML Output Functions ---------- */
127
128    /**
129     * Show the form to start a new discussion thread
130     */
131    function _newThreadForm($ns) {
132        global $ID;
133        global $lang;
134
135        return '<div class="newthread_form">'.DOKU_LF.
136            '<form id="discussion__newthread_form"  method="post" action="'.script().'" accept-charset="'.$lang['encoding'].'">'.DOKU_LF.
137            DOKU_TAB.'<fieldset>'.DOKU_LF.
138            DOKU_TAB.DOKU_TAB.'<legend> '.$this->getLang('newthread').': </legend>'.DOKU_LF.
139            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="id" value="'.$ID.'" />'.DOKU_LF.
140            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="do" value="newthread" />'.DOKU_LF.
141            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="ns" value="'.$ns.'" />'.DOKU_LF.
142            DOKU_TAB.DOKU_TAB.'<input class="edit" type="text" name="title" id="discussion__newthread_title" size="40" tabindex="1" />'.DOKU_LF.
143            DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$lang['btn_create'].'" tabindex="2" />'.DOKU_LF.
144            DOKU_TAB.'</fieldset>'.DOKU_LF.
145            '</form>'.DOKU_LF.
146            '</div>'.DOKU_LF;
147    }
148}
149// vim:ts=4:sw=4:et:enc=utf-8:
150