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 /** 14 * @var string the page id of the page this is rendered to 15 */ 16 protected $id; 17 /** 18 * @var string the Type of renderer used 19 */ 20 protected $mode; 21 /** 22 * @var \Doku_Renderer the DokuWiki renderer used to create the output 23 */ 24 protected $renderer; 25 /** 26 * @var SearchConfig the configured search - gives access to columns etc. 27 */ 28 protected $searchConfig; 29 30 /** 31 * @var Column[] the list of columns to be displayed 32 */ 33 protected $columns; 34 35 /** 36 * @var Value[][] the search result 37 */ 38 protected $result; 39 40 /** 41 * @var int number of all results 42 */ 43 protected $resultCount; 44 45 /** 46 * @var string[] the result PIDs for each row 47 */ 48 protected $resultPIDs; 49 protected $resultRids; 50 protected $resultRevs; 51 52 /** 53 * @var array for summing up columns 54 */ 55 protected $sums; 56 57 /** 58 * @var bool skip full table when no results found 59 */ 60 protected $simplenone = true; 61 62 /** 63 * @todo we might be able to get rid of this helper and move this to SearchConfig 64 * @var \helper_plugin_struct_config 65 */ 66 protected $helper; 67 68 /** 69 * Initialize the Aggregation renderer and executes the search 70 * 71 * You need to call @see render() on the resulting object. 72 * 73 * @param string $id 74 * @param string $mode 75 * @param \Doku_Renderer $renderer 76 * @param SearchConfig $searchConfig 77 */ 78 public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig) 79 { 80 $this->id = $id; 81 $this->mode = $mode; 82 $this->renderer = $renderer; 83 $this->searchConfig = $searchConfig; 84 $this->data = $searchConfig->getConf(); 85 $this->columns = $searchConfig->getColumns(); 86 87 $this->result = $this->searchConfig->execute(); 88 $this->resultCount = $this->searchConfig->getCount(); 89 $this->resultPIDs = $this->searchConfig->getPids(); 90 $this->resultRids = $this->searchConfig->getRids(); 91 $this->resultRevs = $this->searchConfig->getRevs(); 92 $this->helper = plugin_load('helper', 'struct_config'); 93 } 94 95 /** 96 * Returns the page id for the table 97 */ 98 public function getID() 99 { 100 return $this->id; 101 } 102 103 /** 104 * Create the table on the renderer 105 */ 106 public function render() 107 { 108 109 // abort early if there are no results at all (not filtered) 110 if (!$this->resultCount && !$this->isDynamicallyFiltered() && $this->simplenone) { 111 $this->startScope(); 112 $this->renderer->cdata($this->helper->getLang('none')); 113 $this->finishScope(); 114 return; 115 } 116 117 $this->startScope(); 118 $this->renderActiveFilters(); 119 120 $rendercontext = array( 121 'table' => $this, 122 'renderer' => $this->renderer, 123 'format' => $this->mode, 124 'search' => $this->searchConfig, 125 'columns' => $this->columns, 126 'data' => $this->result 127 ); 128 129 $event = new \Doku_Event( 130 'PLUGIN_STRUCT_RENDER_AGGREGATION_TABLE', 131 $rendercontext 132 ); 133 $event->trigger([$this, 'renderTable']); 134 135 // export handle 136 $this->renderExportControls(); 137 $this->finishScope(); 138 } 139 140 /** 141 * Render the default aggregation table 142 */ 143 public function renderTable($rendercontext) 144 { 145 $this->renderer->table_open(); 146 147 // header 148 $this->renderer->tablethead_open(); 149 $this->renderColumnHeaders(); 150 $this->renderDynamicFilters(); 151 $this->renderer->tablethead_close(); 152 153 if ($this->resultCount) { 154 // actual data 155 $this->renderer->tabletbody_open(); 156 $this->renderResult(); 157 $this->renderer->tabletbody_close(); 158 159 // footer (tfoot is develonly currently) 160 if (method_exists($this->renderer, 'tabletfoot_open')) $this->renderer->tabletfoot_open(); 161 $this->renderSums(); 162 $this->renderPagingControls(); 163 if (method_exists($this->renderer, 'tabletfoot_close')) $this->renderer->tabletfoot_close(); 164 } else { 165 // nothing found 166 $this->renderEmptyResult(); 167 } 168 169 // table close 170 $this->renderer->table_close(); 171 } 172 173 /** 174 * Adds additional info to document and renderer in XHTML mode 175 * 176 * @see finishScope() 177 */ 178 protected function startScope() 179 { 180 // unique identifier for this aggregation 181 $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true)); 182 183 // wrapping div 184 if ($this->mode != 'xhtml') return; 185 $this->renderer->doc .= "<div class=\"structaggregation\">"; 186 } 187 188 /** 189 * Closes the table and anything opened in startScope() 190 * 191 * @see startScope() 192 */ 193 protected function finishScope() 194 { 195 // remove identifier from renderer again 196 if (isset($this->renderer->info['struct_table_hash'])) { 197 unset($this->renderer->info['struct_table_hash']); 198 } 199 200 // wrapping div 201 if ($this->mode != 'xhtml') return; 202 $this->renderer->doc .= '</div>'; 203 } 204 205 /** 206 * Displays info about the currently applied filters 207 */ 208 protected function renderActiveFilters() 209 { 210 if ($this->mode != 'xhtml') return; 211 $dynamic = $this->searchConfig->getDynamicParameters(); 212 $filters = $dynamic->getFilters(); 213 if (!$filters) return; 214 215 $fltrs = array(); 216 foreach ($filters as $column => $filter) { 217 list($comp, $value) = $filter; 218 219 // display the filters in a human readable format 220 foreach ($this->columns as $col) { 221 if ($column === $col->getFullQualifiedLabel()) { 222 $column = $col->getTranslatedLabel(); 223 } 224 } 225 $fltrs[] = sprintf('"%s" %s "%s"', $column, $this->helper->getLang("comparator $comp"), $value); 226 } 227 228 $this->renderer->doc .= '<div class="filter">'; 229 $this->renderer->doc .= '<h4>' . sprintf($this->helper->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>'; 230 $this->renderer->doc .= '<div class="resetfilter">'; 231 $this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter')); 232 $this->renderer->doc .= '</div>'; 233 $this->renderer->doc .= '</div>'; 234 } 235 236 /** 237 * Shows the column headers with links to sort by column 238 */ 239 protected function renderColumnHeaders() 240 { 241 $this->renderer->tablerow_open(); 242 243 // additional column for row numbers 244 if (!empty($this->data['rownumbers'])) { 245 $this->renderer->tableheader_open(); 246 $this->renderer->cdata('#'); 247 $this->renderer->tableheader_close(); 248 } 249 250 // show all headers 251 foreach ($this->columns as $num => $column) { 252 $header = ''; 253 if (isset($this->data['headers'][$num])) { 254 $header = $this->data['headers'][$num]; 255 } 256 257 // use field label if no header was set 258 if (blank($header)) { 259 if (is_a($column, 'dokuwiki\plugin\struct\meta\Column')) { 260 $header = $column->getTranslatedLabel(); 261 } else { 262 $header = 'column ' . $num; // this should never happen 263 } 264 } 265 266 // simple mode first 267 if ($this->mode != 'xhtml') { 268 $this->renderer->tableheader_open(); 269 $this->renderer->cdata($header); 270 $this->renderer->tableheader_close(); 271 continue; 272 } 273 274 // still here? create custom header for more flexibility 275 276 // width setting, widths are prevalidated, no escape needed 277 $width = ''; 278 if (isset($this->data['widths'][$num]) && $this->data['widths'][$num] != '-') { 279 $width = ' style="min-width: ' . $this->data['widths'][$num] . ';' . 280 'max-width: ' . $this->data['widths'][$num] . ';"'; 281 } 282 283 // prepare data attribute for inline edits 284 if ( 285 !is_a($column, '\dokuwiki\plugin\struct\meta\PageColumn') && 286 !is_a($column, '\dokuwiki\plugin\struct\meta\RevisionColumn') 287 ) { 288 $data = 'data-field="' . hsc($column->getFullQualifiedLabel()) . '"'; 289 } else { 290 $data = ''; 291 } 292 293 // sort indicator and link 294 $sortclass = ''; 295 $sorts = $this->searchConfig->getSorts(); 296 $dynamic = $this->searchConfig->getDynamicParameters(); 297 $dynamic->setSort($column, true); 298 if (isset($sorts[$column->getFullQualifiedLabel()])) { 299 list(/*colname*/, $currentSort) = $sorts[$column->getFullQualifiedLabel()]; 300 if ($currentSort) { 301 $sortclass = 'sort-down'; 302 $dynamic->setSort($column, false); 303 } else { 304 $sortclass = 'sort-up'; 305 } 306 } 307 $link = wl($this->id, $dynamic->getURLParameters()); 308 309 // output XHTML header 310 $this->renderer->doc .= "<th $width $data>"; 311 if (is_a($this->renderer, 'renderer_plugin_dw2pdf')) { 312 $this->renderer->doc .= hsc($header); 313 } else { 314 $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>'; 315 } 316 $this->renderer->doc .= '</th>'; 317 } 318 319 $this->renderer->tablerow_close(); 320 } 321 322 /** 323 * Is the result set currently dynamically filtered? 324 * @return bool 325 */ 326 protected function isDynamicallyFiltered() 327 { 328 if ($this->mode != 'xhtml') return false; 329 if (!$this->data['dynfilters']) return false; 330 331 $dynamic = $this->searchConfig->getDynamicParameters(); 332 return (bool) $dynamic->getFilters(); 333 } 334 335 /** 336 * Add input fields for dynamic filtering 337 */ 338 protected function renderDynamicFilters() 339 { 340 if ($this->mode != 'xhtml') return; 341 if (!$this->data['dynfilters']) return; 342 if (is_a($this->renderer, 'renderer_plugin_dw2pdf')) { 343 return; 344 } 345 global $conf; 346 347 $this->renderer->doc .= '<tr class="dataflt">'; 348 349 // add extra column for row numbers 350 if ($this->data['rownumbers']) { 351 $this->renderer->doc .= '<th></th>'; 352 } 353 354 // each column gets a form 355 foreach ($this->columns as $column) { 356 $this->renderer->doc .= '<th>'; 357 { 358 $form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id))); 359 unset($form->_hidden['sectok']); // we don't need it here 360 if (!$conf['userewrite']) $form->addHidden('id', $this->id); 361 362 // current value 363 $dynamic = $this->searchConfig->getDynamicParameters(); 364 $filters = $dynamic->getFilters(); 365 if (isset($filters[$column->getFullQualifiedLabel()])) { 366 list(, $current) = $filters[$column->getFullQualifiedLabel()]; 367 $dynamic->removeFilter($column); 368 } else { 369 $current = ''; 370 } 371 372 // Add current request params 373 $params = $dynamic->getURLParameters(); 374 foreach ($params as $key => $val) { 375 $form->addHidden($key, $val); 376 } 377 378 // add input field 379 $key = $column->getFullQualifiedLabel() . $column->getType()->getDefaultComparator(); 380 $form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, '')); 381 $this->renderer->doc .= $form->getForm(); 382 } 383 $this->renderer->doc .= '</th>'; 384 } 385 $this->renderer->doc .= '</tr>'; 386 } 387 388 /** 389 * Display the actual table data 390 */ 391 protected function renderResult() 392 { 393 foreach ($this->result as $rownum => $row) { 394 $data = array( 395 'id' => $this->id, 396 'mode' => $this->mode, 397 'renderer' => $this->renderer, 398 'searchConfig' => $this->searchConfig, 399 'data' => $this->data, 400 'rownum' => &$rownum, 401 'row' => &$row, 402 ); 403 $evt = new \Doku_Event('PLUGIN_STRUCT_AGGREGATIONTABLE_RENDERRESULTROW', $data); 404 if ($evt->advise_before()) { 405 $this->renderResultRow($rownum, $row); 406 } 407 $evt->advise_after(); 408 } 409 } 410 411 /** 412 * Render a single result row 413 * 414 * @param int $rownum 415 * @param array $row 416 */ 417 protected function renderResultRow($rownum, $row) 418 { 419 $this->renderer->tablerow_open(); 420 421 // add data attribute for inline edit 422 if ($this->mode == 'xhtml') { 423 $pid = $this->resultPIDs[$rownum]; 424 $rid = $this->resultRids[$rownum]; 425 $rev = $this->resultRevs[$rownum]; 426 $this->renderer->doc = substr(rtrim($this->renderer->doc), 0, -1); // remove closing '>' 427 $this->renderer->doc .= ' data-pid="' . hsc($pid) . '" data-rev="' . $rev . '" data-rid="' . $rid . '">'; 428 } 429 430 // row number column 431 if (!empty($this->data['rownumbers'])) { 432 $this->renderer->tablecell_open(); 433 $searchConfigConf = $this->searchConfig->getConf(); 434 $this->renderer->cdata($rownum + $searchConfigConf['offset'] + 1); 435 $this->renderer->tablecell_close(); 436 } 437 438 /** @var Value $value */ 439 foreach ($row as $colnum => $value) { 440 $align = isset($this->data['align'][$colnum]) ? $this->data['align'][$colnum] : null; 441 $this->renderer->tablecell_open(1, $align); 442 $value->render($this->renderer, $this->mode); 443 $this->renderer->tablecell_close(); 444 445 // summarize 446 if ($this->data['summarize'] && is_numeric($value->getValue())) { 447 if (!isset($this->sums[$colnum])) { 448 $this->sums[$colnum] = 0; 449 } 450 $this->sums[$colnum] += $value->getValue(); 451 } 452 } 453 $this->renderer->tablerow_close(); 454 } 455 456 /** 457 * Renders an information row for when no results were found 458 */ 459 protected function renderEmptyResult() 460 { 461 $this->renderer->tablerow_open(); 462 $this->renderer->tablecell_open(count($this->columns) + $this->data['rownumbers'], 'center'); 463 $this->renderer->cdata($this->helper->getLang('none')); 464 $this->renderer->tablecell_close(); 465 $this->renderer->tablerow_close(); 466 } 467 468 /** 469 * Add sums if wanted 470 */ 471 protected function renderSums() 472 { 473 if (empty($this->data['summarize'])) return; 474 475 $this->renderer->info['struct_table_meta'] = true; 476 if ($this->mode == 'xhtml') { 477 /** @noinspection PhpMethodParametersCountMismatchInspection */ 478 $this->renderer->tablerow_open('summarize'); 479 } else { 480 $this->renderer->tablerow_open(); 481 } 482 483 if ($this->data['rownumbers']) { 484 $this->renderer->tableheader_open(); 485 $this->renderer->tableheader_close(); 486 } 487 488 $len = count($this->columns); 489 for ($i = 0; $i < $len; $i++) { 490 $this->renderer->tableheader_open(1, $this->data['align'][$i]); 491 if (!empty($this->sums[$i])) { 492 $this->renderer->cdata('∑ '); 493 $this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode); 494 } else { 495 if ($this->mode == 'xhtml') { 496 $this->renderer->doc .= ' '; 497 } 498 } 499 $this->renderer->tableheader_close(); 500 } 501 $this->renderer->tablerow_close(); 502 $this->renderer->info['struct_table_meta'] = false; 503 } 504 505 /** 506 * Adds paging controls to the table 507 */ 508 protected function renderPagingControls() 509 { 510 if (empty($this->data['limit'])) return; 511 if ($this->mode != 'xhtml') return; 512 513 $this->renderer->info['struct_table_meta'] = true; 514 $this->renderer->tablerow_open(); 515 $this->renderer->tableheader_open((count($this->columns) + ($this->data['rownumbers'] ? 1 : 0))); 516 $offset = $this->data['offset']; 517 518 // prev link 519 if ($offset) { 520 $prev = $offset - $this->data['limit']; 521 if ($prev < 0) { 522 $prev = 0; 523 } 524 525 $dynamic = $this->searchConfig->getDynamicParameters(); 526 $dynamic->setOffset($prev); 527 $link = wl($this->id, $dynamic->getURLParameters()); 528 $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>'; 529 } 530 531 // next link 532 if ($this->resultCount > $offset + $this->data['limit']) { 533 $next = $offset + $this->data['limit']; 534 $dynamic = $this->searchConfig->getDynamicParameters(); 535 $dynamic->setOffset($next); 536 $link = wl($this->id, $dynamic->getURLParameters()); 537 $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>'; 538 } 539 540 $this->renderer->tableheader_close(); 541 $this->renderer->tablerow_close(); 542 $this->renderer->info['struct_table_meta'] = true; 543 } 544 545 /** 546 * Adds CSV export controls 547 */ 548 protected function renderExportControls() 549 { 550 if ($this->mode != 'xhtml') return; 551 if (empty($this->data['csv'])) return; 552 if (!$this->resultCount) return; 553 554 $dynamic = $this->searchConfig->getDynamicParameters(); 555 $params = $dynamic->getURLParameters(); 556 $params['hash'] = $this->renderer->info['struct_table_hash']; 557 558 // FIXME apply dynamic filters 559 $link = exportlink($this->id, 'struct_csv', $params); 560 561 $this->renderer->doc .= '<a href="' . $link . '" class="export mediafile mf_csv">' . $this->helper->getLang('csvexport') . '</a>'; 562 } 563} 564