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 public function getType() { return 'substition'; } 20 21 /** 22 * Paragraph Type 23 * 24 * @see Doku_Handler_Block 25 * @return string 26 */ 27 public function getPType() { return 'block'; } 28 29 /** 30 * Sort for applying this mode 31 * 32 * @return int 33 */ 34 public function getSort() { return 306; } 35 36 /** 37 * @param string $mode 38 */ 39 public 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 public 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) = array_pad(explode('&', $match, 2),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 = array_pad(explode('=', $flag, 2),2, 0); 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'])) { 77 $customFlags['count'] = 0; 78 } 79 if(!array_key_exists('skipempty', $customFlags) && !$customFlags['skipempty']) { 80 $customFlags['skipempty'] = false; 81 } 82 83 list($ns, $refine) = array_pad(explode(' ', $match, 2), 2, ''); 84 85 if ($ns == '*' || $ns == ':') { 86 $ns = ''; 87 } elseif ($ns == '.') { 88 $ns = getNS($ID); 89 } else { 90 $ns = cleanID($ns); 91 } 92 93 return [$ns, $flags, $refine, $customFlags]; 94 } 95 96 /** 97 * Handles the actual output creation. 98 * 99 * @param string $format output format being rendered 100 * @param Doku_Renderer $renderer the current renderer object 101 * @param array $data data created by handler() 102 * @return boolean rendered correctly? 103 */ 104 public function render($format, Doku_Renderer $renderer, $data) { 105 list($ns, $flags, $refine, $customFlags) = $data; 106 $count = $customFlags['count']; 107 $skipEmpty = $customFlags['skipempty']; 108 $noNewThreadForm = $customFlags['nonewthreadform']; 109 $i = 0; 110 111 $pages = []; 112 /** @var helper_plugin_discussion $helper */ 113 if ($helper = $this->loadHelper('discussion')) { 114 $pages = $helper->getThreads($ns, null, $skipEmpty); 115 } 116 117 // use tag refinements? 118 if ($refine) { 119 /** @var helper_plugin_tag $tag */ 120 if (!$tag = $this->loadHelper('tag', false)) { 121 msg('The Tag Plugin must be installed to use tag refinements.', -1); 122 } else { 123 $pages = $tag->tagRefine($pages, $refine); 124 } 125 } 126 127 if (!$pages) { 128 if (auth_quickaclcheck($ns.':*') >= AUTH_CREATE && $format == 'xhtml') { 129 $renderer->nocache(); 130 if ($noNewThreadForm !== true) { 131 $renderer->doc .= $this->newThreadForm($ns); 132 } 133 } 134 return true; // nothing to display 135 } 136 137 if ($format == 'xhtml') { 138 /** @var Doku_Renderer_xhtml $renderer */ 139 // prevent caching to ensure content is always fresh 140 $renderer->nocache(); 141 142 // show form to start a new discussion thread? 143 if ($noNewThreadForm !== true) { 144 $hasCreatePermission = auth_quickaclcheck($ns.':*') >= AUTH_CREATE; 145 if ($hasCreatePermission && $this->getConf('threads_formposition') == 'top') { 146 $renderer->doc .= $this->newThreadForm($ns); 147 } 148 } 149 150 // let Pagelist Plugin do the work for us 151 /** @var helper_plugin_pagelist $pagelist */ 152 if (!$pagelist = $this->loadHelper('pagelist', false)) { 153 msg('The Pagelist Plugin must be installed for threads lists to work.', -1); 154 return false; 155 } 156 $pagelist->addColumn('discussion', 'comments'); 157 $pagelist->setFlags($flags); 158 $pagelist->startList(); 159 foreach ($pages as $page) { 160 $page['class'] = 'discussion_status'.$page['status']; 161 $pagelist->addPage($page); 162 163 $i++; 164 if($count > 0 && $i >= $count) { 165 // Only display the n discussion threads specified by the count flag 166 break; 167 } 168 } 169 $renderer->doc .= $pagelist->finishList(); 170 171 // show form to start a new discussion thread? 172 if ($noNewThreadForm !== true) { 173 if ($hasCreatePermission && $this->getConf('threads_formposition') == 'bottom') { 174 $renderer->doc .= $this->newThreadForm($ns); 175 } 176 } 177 178 return true; 179 180 // for metadata renderer 181 } elseif ($format == 'metadata') { 182 /** @var Doku_Renderer_metadata $renderer */ 183 foreach ($pages as $page) { 184 $renderer->meta['relation']['references'][$page['id']] = true; 185 } 186 187 return true; 188 } 189 return false; 190 } 191 192 /* ---------- (X)HTML Output Functions ---------- */ 193 194 /** 195 * Show the form to start a new discussion thread 196 * 197 * @param string $ns 198 * @return string html 199 */ 200 protected function newThreadForm($ns) { 201 global $ID; 202 global $lang; 203 204 return '<div class="newthread_form">' 205 . '<form id="discussion__newthread_form" method="post" action="' . script() . '" accept-charset="' . $lang['encoding'] . '">' 206 . '<fieldset>' 207 . '<legend> ' . $this->getLang('newthread') . ': </legend>' 208 . '<input type="hidden" name="id" value="' . $ID . '" />' 209 . '<input type="hidden" name="do" value="newthread" />' 210 . '<input type="hidden" name="ns" value="' . $ns . '" />' 211 . '<input class="edit" type="text" name="title" id="discussion__newthread_title" size="40" tabindex="1" />' 212 . '<input class="button" type="submit" value="' . $lang['btn_create'] . '" tabindex="2" />' 213 . '</fieldset>' 214 . '</form>' 215 . '</div>'; 216 } 217} 218