1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Creates the table aggregation output 7 * 8 * @package dokuwiki\plugin\struct\meta 9 */ 10class AggregationTable { 11 12 /** 13 * @var string the page id of the page this is rendered to 14 */ 15 protected $id; 16 /** 17 * @var string the Type of renderer used 18 */ 19 protected $mode; 20 /** 21 * @var \Doku_Renderer the DokuWiki renderer used to create the output 22 */ 23 protected $renderer; 24 /** 25 * @var SearchConfig the configured search - gives access to columns etc. 26 */ 27 protected $searchConfig; 28 29 /** 30 * @var Column[] the list of columns to be displayed 31 */ 32 protected $columns; 33 34 /** 35 * @var Value[][] the search result 36 */ 37 protected $result; 38 39 /** 40 * @var int number of all results 41 */ 42 protected $resultCount; 43 44 /** 45 * @var array for summing up columns 46 */ 47 protected $sums; 48 49 /** 50 * @todo we might be able to get rid of this helper and move this to SearchConfig 51 * @var \helper_plugin_struct_config 52 */ 53 protected $helper; 54 55 /** 56 * Initialize the Aggregation renderer and executes the search 57 * 58 * You need to call @see render() on the resulting object. 59 * 60 * @param string $id 61 * @param string $mode 62 * @param \Doku_Renderer $renderer 63 * @param SearchConfig $searchConfig 64 */ 65 public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig) { 66 $this->id = $id; 67 $this->mode = $mode; 68 $this->renderer = $renderer; 69 $this->searchConfig = $searchConfig; 70 $this->data = $searchConfig->getConf(); 71 $this->columns = $searchConfig->getColumns(); 72 73 $this->result = $this->searchConfig->execute(); 74 $this->resultCount = $this->searchConfig->getCount(); 75 $this->helper = plugin_load('helper', 'struct_config'); 76 } 77 78 /** 79 * Create the table on the renderer 80 */ 81 public function render() { 82 83 // abort early if there are no results at all (not filtered) 84 if(!$this->resultCount && !$this->isDynamicallyFiltered()) { 85 $this->startScope(); 86 $this->renderer->cdata($this->helper->getLang('none')); 87 $this->finishScope(); 88 return; 89 } 90 91 // table open 92 $this->startScope(); 93 $this->renderActiveFilters(); 94 $this->renderer->table_open(); 95 96 // header 97 $this->renderer->tablethead_open(); 98 $this->renderColumnHeaders(); 99 $this->renderDynamicFilters(); 100 $this->renderer->tablethead_close(); 101 102 if($this->resultCount) { 103 // actual data 104 $this->renderResult(); 105 106 // footer 107 $this->renderSums(); 108 $this->renderPagingControls(); 109 } else { 110 // nothing found 111 $this->renderEmptyResult(); 112 } 113 114 // table close 115 $this->renderer->table_close(); 116 $this->finishScope(); 117 } 118 119 /** 120 * Adds additional info to document and renderer in XHTML mode 121 * 122 * @see finishScope() 123 */ 124 protected function startScope() { 125 if($this->mode != 'xhtml') return; 126 127 // wrapping div 128 $this->renderer->doc .= "<div class=\"structaggregation\">"; 129 130 // unique identifier for this aggregation 131 $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true)); 132 } 133 134 /** 135 * Closes the table and anything opened in startScope() 136 * 137 * @see startScope() 138 */ 139 protected function finishScope() { 140 if($this->mode != 'xhtml') return; 141 142 // wrapping div 143 $this->renderer->doc .= '</div>'; 144 145 // remove identifier from renderer again 146 if(isset($this->renderer->info['struct_table_hash'])) { 147 unset($this->renderer->info['struct_table_hash']); 148 } 149 } 150 151 /** 152 * Displays info about the currently applied filters 153 */ 154 protected function renderActiveFilters() { 155 if($this->mode != 'xhtml') return; 156 $dynamic = $this->searchConfig->getDynamicParameters(); 157 $filters = $dynamic->getFilters(); 158 if(!$filters) return; 159 160 $fltrs = array(); 161 foreach($filters as $column => $filter) { 162 list($comp, $value) = $filter; 163 $fltrs[] = $column . ' ' . $comp . ' ' . $value; 164 } 165 166 $this->renderer->doc .= '<div class="filter">'; 167 $this->renderer->doc .= '<h4>' . sprintf($this->helper->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>'; 168 $this->renderer->doc .= '<div class="resetfilter">'; 169 $this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter')); 170 $this->renderer->doc .= '</div>'; 171 $this->renderer->doc .= '</div>'; 172 } 173 174 /** 175 * Shows the column headers with links to sort by column 176 */ 177 protected function renderColumnHeaders() { 178 $this->renderer->tablerow_open(); 179 180 // additional column for row numbers 181 if($this->data['rownumbers']) { 182 $this->renderer->tableheader_open(); 183 $this->renderer->cdata('#'); 184 $this->renderer->tableheader_close(); 185 } 186 187 // show all headers 188 foreach($this->data['headers'] as $num => $header) { 189 if(!isset($this->columns[$num])) break; // less columns where available then expected 190 $column = $this->columns[$num]; 191 192 // use field label if no header was set 193 if(blank($header)) { 194 if(is_a($column, 'dokuwiki\plugin\struct\meta\Column')) { 195 $header = $column->getTranslatedLabel(); 196 } else { 197 $header = 'column ' . $num; // this should never happen 198 } 199 } 200 201 // simple mode first 202 if($this->mode != 'xhtml') { 203 $this->renderer->tableheader_open(); 204 $this->renderer->cdata($header); 205 $this->renderer->tableheader_close(); 206 continue; 207 } 208 209 // still here? create custom header for more flexibility 210 211 // width setting 212 $width = ''; 213 if(isset($data['widths'][$num]) && $data['widths'][$num] != '-') { 214 $width = ' style="width: ' . $data['widths'][$num] . ';"'; 215 } 216 217 // sort indicator and link 218 $sortclass = ''; 219 $sorts = $this->searchConfig->getSorts(); 220 $dynamic = $this->searchConfig->getDynamicParameters(); 221 $dynamic->setSort($column, true); 222 if(isset($sorts[$column->getFullQualifiedLabel()])) { 223 list(/*colname*/, $currentSort) = $sorts[$column->getFullQualifiedLabel()]; 224 if($currentSort) { 225 $sortclass = 'sort-down'; 226 $dynamic->setSort($column, false); 227 } else { 228 $sortclass = 'sort-up'; 229 } 230 } 231 $link = wl($this->id, $dynamic->getURLParameters()); 232 233 // output XHTML header 234 $this->renderer->doc .= "<th $width >"; 235 $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>'; 236 $this->renderer->doc .= '</th>'; 237 } 238 239 $this->renderer->tablerow_close(); 240 } 241 242 /** 243 * Is the result set currently dynamically filtered? 244 * @return bool 245 */ 246 protected function isDynamicallyFiltered() { 247 if($this->mode != 'xhtml') return false; 248 if(!$this->data['dynfilters']) return false; 249 250 $dynamic = $this->searchConfig->getDynamicParameters(); 251 return (bool) $dynamic->getFilters(); 252 } 253 254 /** 255 * Add input fields for dynamic filtering 256 */ 257 protected function renderDynamicFilters() { 258 if($this->mode != 'xhtml') return; 259 if(!$this->data['dynfilters']) return; 260 global $conf; 261 262 $this->renderer->doc .= '<tr class="dataflt">'; 263 264 // add extra column for row numbers 265 if($this->data['rownumbers']) { 266 $this->renderer->doc .= '<th></th>'; 267 } 268 269 // each column gets a form 270 foreach($this->columns as $column) { 271 $this->renderer->doc .= '<th>'; 272 { 273 $form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id))); 274 unset($form->_hidden['sectok']); // we don't need it here 275 if(!$conf['userewrite']) $form->addHidden('id', $this->id); 276 277 // current value 278 $dynamic = $this->searchConfig->getDynamicParameters(); 279 $filters = $dynamic->getFilters(); 280 if(isset($filters[$column->getFullQualifiedLabel()])) { 281 list(, $current) = $filters[$column->getFullQualifiedLabel()]; 282 $dynamic->removeFilter($column); 283 } else { 284 $current = ''; 285 } 286 287 // Add current request params 288 $params = $dynamic->getURLParameters(); 289 foreach($params as $key => $val) { 290 $form->addHidden($key, $val); 291 } 292 293 // add input field 294 $key = $column->getFullQualifiedLabel() . '*~'; 295 $form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, '')); 296 $this->renderer->doc .= $form->getForm(); 297 } 298 $this->renderer->doc .= '</th>'; 299 } 300 $this->renderer->doc .= '</tr>'; 301 302 } 303 304 /** 305 * Display the actual table data 306 */ 307 protected function renderResult() { 308 $this->renderer->tabletbody_open(); 309 foreach($this->result as $rownum => $row) { 310 $this->renderer->tablerow_open(); 311 312 // row number column 313 if($this->data['rownumbers']) { 314 $this->renderer->tablecell_open(); 315 $this->renderer->doc .= $rownum + 1; 316 $this->renderer->tablecell_close(); 317 } 318 319 /** @var Value $value */ 320 foreach($row as $colnum => $value) { 321 $this->renderer->tablecell_open(); 322 $value->render($this->renderer, $this->mode); 323 $this->renderer->tablecell_close(); 324 325 // summarize 326 if($this->data['summarize'] && is_numeric($value->getValue())) { 327 if(!isset($this->sums[$colnum])) { 328 $this->sums[$colnum] = 0; 329 } 330 $this->sums[$colnum] += $value->getValue(); 331 } 332 } 333 $this->renderer->tablerow_close(); 334 } 335 $this->renderer->tabletbody_close(); 336 } 337 338 /** 339 * Renders an information row for when no results were found 340 */ 341 protected function renderEmptyResult() { 342 $this->renderer->tablerow_open(); 343 $this->renderer->tablecell_open(count($this->data['cols']) + $this->data['rownumbers'], 'center'); 344 $this->renderer->cdata($this->helper->getLang('none')); 345 $this->renderer->tablecell_close(); 346 $this->renderer->tablerow_close(); 347 } 348 349 /** 350 * Add sums if wanted 351 */ 352 protected function renderSums() { 353 if(empty($this->data['summarize'])) return; 354 355 $this->renderer->tablerow_open(); 356 357 if($this->data['rownumbers']) { 358 $this->renderer->tablecell_open(); 359 $this->renderer->tablecell_close(); 360 } 361 362 $len = count($this->columns); 363 for($i = 0; $i < $len; $i++) { 364 $this->renderer->tablecell_open(1, $this->data['align'][$i]); 365 if(!empty($this->sums[$i])) { 366 $this->renderer->cdata('∑ '); 367 $this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode); 368 } else { 369 if($this->mode == 'xhtml') { 370 $this->renderer->doc .= ' '; 371 } 372 } 373 $this->renderer->tablecell_close(); 374 } 375 $this->renderer->tablerow_close(); 376 } 377 378 /** 379 * Adds paging controls to the table 380 */ 381 protected function renderPagingControls() { 382 if(empty($this->data['limit'])) return; 383 if($this->mode != 'xhtml') ; 384 385 $this->renderer->tablerow_open(); 386 $this->renderer->tableheader_open((count($this->data['cols']) + ($this->data['rownumbers'] ? 1 : 0))); 387 $offset = $this->data['offset']; 388 389 // prev link 390 if($offset) { 391 $prev = $offset - $this->data['limit']; 392 if($prev < 0) { 393 $prev = 0; 394 } 395 396 $dynamic = $this->searchConfig->getDynamicParameters(); 397 $dynamic->setOffset($prev); 398 $link = wl($this->id, $dynamic->getURLParameters()); 399 $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>'; 400 } 401 402 // next link 403 if($this->resultCount > $offset + $this->data['limit']) { 404 $next = $offset + $this->data['limit']; 405 $dynamic = $this->searchConfig->getDynamicParameters(); 406 $dynamic->setOffset($next); 407 $link = wl($this->id, $dynamic->getURLParameters()); 408 $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>'; 409 } 410 411 $this->renderer->tableheader_close(); 412 $this->renderer->tablerow_close(); 413 } 414} 415