xref: /plugin/discussion/syntax/threads.php (revision 5644a1af31acbaeafbc3df81e488d482add2d7ba)
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        $customFlags = array();
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/skipempty flag and remove it before passing it to pagelist
37        foreach($flags as $key => $flag) {
38            if(substr($flag, 0, 5) == "count") {
39                $tmp = explode('=', $flag);
40                $customFlags['count'] = $tmp[1];
41                unset($flags[$key]);
42            }
43            if(substr($flag, 0, 9) == "skipempty") {
44                $customFlags['skipempty'] = true;
45                unset($flags[$key]);
46            }
47        }
48
49        // Ignore params if invalid values have been passed
50        if(!array_key_exists('count', $customFlags) || $customFlags['count'] <= 0 || !is_numeric($customFlags['count'])) $customFlags['count'] = false;
51        if(!array_key_exists('skipempty', $customFlags) && !$customFlags['skipempty']) $customFlags['skipempty'] = false;
52
53        list($ns, $refine) = explode(' ', $match, 2);
54
55        if (($ns == '*') || ($ns == ':')) $ns = '';
56        elseif ($ns == '.') $ns = getNS($ID);
57        else $ns = cleanID($ns);
58
59        return array($ns, $flags, $refine, $customFlags);
60    }
61
62    function render($mode, &$renderer, $data) {
63        list($ns, $flags, $refine, $customFlags) = $data;
64        $count = $customFlags['count'];
65        $skipEmpty = $customFlags['skipempty'];
66        $i = 0;
67
68        if ($my =& plugin_load('helper', 'discussion')) $pages = $my->getThreads($ns, NULL, $skipEmpty);
69
70        // use tag refinements?
71        if ($refine) {
72            if (plugin_isdisabled('tag') || (!$tag = plugin_load('helper', 'tag'))) {
73                msg('The Tag Plugin must be installed to use tag refinements.', -1);
74            } else {
75                $pages = $tag->tagRefine($pages, $refine);
76            }
77        }
78
79        if (!$pages) {
80            if ((auth_quickaclcheck($ns.':*') >= AUTH_CREATE) && ($mode == 'xhtml')) {
81                $renderer->info['cache'] = false;
82                $renderer->doc .= $this->_newThreadForm($ns);
83            }
84            return true; // nothing to display
85        }
86
87        if ($mode == 'xhtml') {
88
89            // prevent caching to ensure content is always fresh
90            $renderer->info['cache'] = false;
91
92            // show form to start a new discussion thread?
93            $perm_create = (auth_quickaclcheck($ns.':*') >= AUTH_CREATE);
94            if ($perm_create && ($this->getConf('threads_formposition') == 'top'))
95                $renderer->doc .= $this->_newThreadForm($ns);
96
97            // let Pagelist Plugin do the work for us
98            if (plugin_isdisabled('pagelist')
99                    || (!$pagelist =& plugin_load('helper', 'pagelist'))) {
100                msg('The Pagelist Plugin must be installed for threads lists to work.', -1);
101                return false;
102            }
103            $pagelist->column['comments'] = true;
104            $pagelist->setFlags($flags);
105            $pagelist->startList();
106            foreach ($pages as $key => $page) {
107                $page['class'] = 'discussion_status'.$page['status'];
108                $pagelist->addPage($page);
109
110                $i++;
111                if($count != false && $i >= $count) break; // Only display the n discussion threads specified by the count flag
112            }
113            $renderer->doc .= $pagelist->finishList();
114
115            // show form to start a new discussion thread?
116            if ($perm_create && ($this->getConf('threads_formposition') == 'bottom'))
117                $renderer->doc .= $this->_newThreadForm($ns);
118
119            return true;
120
121            // for metadata renderer
122        } elseif ($mode == 'metadata') {
123            foreach ($pages as $page) {
124                $renderer->meta['relation']['references'][$page['id']] = true;
125            }
126
127            return true;
128        }
129        return false;
130    }
131
132    /* ---------- (X)HTML Output Functions ---------- */
133
134    /**
135     * Show the form to start a new discussion thread
136     */
137    function _newThreadForm($ns) {
138        global $ID;
139        global $lang;
140
141        return '<div class="newthread_form">'.DOKU_LF.
142            '<form id="discussion__newthread_form"  method="post" action="'.script().'" accept-charset="'.$lang['encoding'].'">'.DOKU_LF.
143            DOKU_TAB.'<fieldset>'.DOKU_LF.
144            DOKU_TAB.DOKU_TAB.'<legend> '.$this->getLang('newthread').': </legend>'.DOKU_LF.
145            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="id" value="'.$ID.'" />'.DOKU_LF.
146            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="do" value="newthread" />'.DOKU_LF.
147            DOKU_TAB.DOKU_TAB.'<input type="hidden" name="ns" value="'.$ns.'" />'.DOKU_LF.
148            DOKU_TAB.DOKU_TAB.'<input class="edit" type="text" name="title" id="discussion__newthread_title" size="40" tabindex="1" />'.DOKU_LF.
149            DOKU_TAB.DOKU_TAB.'<input class="button" type="submit" value="'.$lang['btn_create'].'" tabindex="2" />'.DOKU_LF.
150            DOKU_TAB.'</fieldset>'.DOKU_LF.
151            '</form>'.DOKU_LF.
152            '</div>'.DOKU_LF;
153    }
154}
155// vim:ts=4:sw=4:et:enc=utf-8:
156