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 /** 16 * Syntax Type 17 * 18 * @return string 19 */ 20 public function getType() 21 { 22 return 'substition'; 23 } 24 25 /** 26 * Paragraph Type 27 * 28 * @return string 29 * @see Doku_Handler_Block 30 */ 31 public function getPType() 32 { 33 return 'block'; 34 } 35 36 /** 37 * Sort for applying this mode 38 * 39 * @return int 40 */ 41 public function getSort() 42 { 43 return 306; 44 } 45 46 /** 47 * @param string $mode 48 */ 49 public function connectTo($mode) 50 { 51 $this->Lexer->addSpecialPattern('\{\{threads>.+?\}\}', $mode, 'plugin_discussion_threads'); 52 } 53 54 /** 55 * Handler to prepare matched data for the rendering process 56 * 57 * @param string $match The text matched by the patterns 58 * @param int $state The lexer state for the match 59 * @param int $pos The character position of the matched text 60 * @param Doku_Handler $handler The Doku_Handler object 61 * @return array Return an array with all data you want to use in render 62 */ 63 public function handle($match, $state, $pos, Doku_Handler $handler) 64 { 65 global $ID; 66 $customFlags = [ 67 'count' => 0, 68 'skipempty' => false, 69 'nonewthreadform' => false 70 ]; 71 72 $match = substr($match, 10, -2); // strip {{threads> from start and }} from end 73 list($match, $flags) = array_pad(explode('&', $match, 2), 2, ''); 74 $flags = explode('&', $flags); 75 76 // Identify the count/skipempty flag and remove it before passing it to pagelist 77 foreach ($flags as $key => $flag) { 78 if (substr($flag, 0, 5) == "count") { 79 list(,$cnt) = array_pad(explode('=', $flag, 2), 2, 0); 80 if(is_numeric($cnt) && $cnt > 0) { 81 $customFlags['count'] = $cnt; 82 } 83 unset($flags[$key]); 84 } elseif (substr($flag, 0, 9) == "skipempty") { 85 $customFlags['skipempty'] = true; 86 unset($flags[$key]); 87 } elseif (substr($flag, 0, 15) == "nonewthreadform") { 88 $customFlags['nonewthreadform'] = true; 89 unset($flags[$key]); 90 } 91 } 92 93 list($ns, $refine) = array_pad(explode(' ', $match, 2), 2, ''); 94 95 if ($ns == '*' || $ns == ':') { 96 $ns = ''; 97 } elseif ($ns == '.') { 98 $ns = getNS($ID); 99 } else { 100 $ns = cleanID($ns); 101 } 102 103 return [$ns, $flags, $refine, $customFlags]; 104 } 105 106 /** 107 * Handles the actual output creation. 108 * 109 * @param string $format output format being rendered 110 * @param Doku_Renderer $renderer the current renderer object 111 * @param array $data data created by handler() 112 * @return boolean rendered correctly? 113 */ 114 public function render($format, Doku_Renderer $renderer, $data) 115 { 116 list($ns, $flags, $refine, $customFlags) = $data; 117 $count = $customFlags['count']; 118 $skipEmpty = $customFlags['skipempty']; 119 $noNewThreadForm = $customFlags['nonewthreadform']; 120 $i = 0; 121 122 $pages = []; 123 /** @var helper_plugin_discussion $helper */ 124 if ($helper = $this->loadHelper('discussion')) { 125 $pages = $helper->getThreads($ns, null, $skipEmpty); 126 } 127 128 // use tag refinements? 129 if ($refine) { 130 /** @var helper_plugin_tag $tag */ 131 if (!$tag = $this->loadHelper('tag', false)) { 132 msg('The Tag Plugin must be installed to use tag refinements.', -1); 133 } else { 134 $pages = $tag->tagRefine($pages, $refine); 135 } 136 } 137 138 if (!$pages) { 139 if (auth_quickaclcheck($ns . ':*') >= AUTH_CREATE && $format == 'xhtml') { 140 $renderer->nocache(); 141 if ($noNewThreadForm !== true) { 142 $renderer->doc .= $this->newThreadForm($ns); 143 } 144 } 145 return true; // nothing to display 146 } 147 148 if ($format == 'xhtml') { 149 /** @var Doku_Renderer_xhtml $renderer */ 150 // prevent caching to ensure content is always fresh 151 $renderer->nocache(); 152 153 // show form to start a new discussion thread? 154 if ($noNewThreadForm !== true) { 155 $hasCreatePermission = auth_quickaclcheck($ns . ':*') >= AUTH_CREATE; 156 if ($hasCreatePermission && $this->getConf('threads_formposition') == 'top') { 157 $renderer->doc .= $this->newThreadForm($ns); 158 } 159 } 160 161 // let Pagelist Plugin do the work for us 162 /** @var helper_plugin_pagelist $pagelist */ 163 if (!$pagelist = $this->loadHelper('pagelist', false)) { 164 msg('The Pagelist Plugin must be installed for threads lists to work.', -1); 165 return false; 166 } 167 $pagelist->addColumn('discussion', 'comments'); 168 $pagelist->setFlags($flags); 169 $pagelist->startList(); 170 foreach ($pages as $page) { 171 $page['class'] = 'discussion_status' . $page['status']; 172 $pagelist->addPage($page); 173 174 $i++; 175 if ($count > 0 && $i >= $count) { 176 // Only display the n discussion threads specified by the count flag 177 break; 178 } 179 } 180 $renderer->doc .= $pagelist->finishList(); 181 182 // show form to start a new discussion thread? 183 if ($noNewThreadForm !== true) { 184 if ($hasCreatePermission && $this->getConf('threads_formposition') == 'bottom') { 185 $renderer->doc .= $this->newThreadForm($ns); 186 } 187 } 188 189 return true; 190 191 // for metadata renderer 192 } elseif ($format == 'metadata') { 193 /** @var Doku_Renderer_metadata $renderer */ 194 foreach ($pages as $page) { 195 $renderer->meta['relation']['references'][$page['id']] = true; 196 } 197 198 return true; 199 } 200 return false; 201 } 202 203 /* ---------- (X)HTML Output Functions ---------- */ 204 205 /** 206 * Show the form to start a new discussion thread 207 * 208 * @param string $ns 209 * @return string html 210 */ 211 protected function newThreadForm($ns) 212 { 213 global $ID; 214 global $lang; 215 216 return '<div class="newthread_form">' 217 . '<form id="discussion__newthread_form" method="post" action="' . script() . '" accept-charset="' . $lang['encoding'] . '">' 218 . '<fieldset>' 219 . '<legend> ' . $this->getLang('newthread') . ': </legend>' 220 . '<input type="hidden" name="id" value="' . $ID . '" />' 221 . '<input type="hidden" name="do" value="newthread" />' 222 . '<input type="hidden" name="ns" value="' . $ns . '" />' 223 . '<input class="edit" type="text" name="title" id="discussion__newthread_title" size="40" tabindex="1" />' 224 . '<input class="button" type="submit" value="' . $lang['btn_create'] . '" tabindex="2" />' 225 . '</fieldset>' 226 . '</form>' 227 . '</div>'; 228 } 229} 230