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 $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>'; 312 $this->renderer->doc .= '</th>'; 313 } 314 315 $this->renderer->tablerow_close(); 316 } 317 318 /** 319 * Is the result set currently dynamically filtered? 320 * @return bool 321 */ 322 protected function isDynamicallyFiltered() 323 { 324 if ($this->mode != 'xhtml') return false; 325 if (!$this->data['dynfilters']) return false; 326 327 $dynamic = $this->searchConfig->getDynamicParameters(); 328 return (bool) $dynamic->getFilters(); 329 } 330 331 /** 332 * Add input fields for dynamic filtering 333 */ 334 protected function renderDynamicFilters() 335 { 336 if ($this->mode != 'xhtml') return; 337 if (!$this->data['dynfilters']) return; 338 if (is_a($this->renderer, 'renderer_plugin_dw2pdf')) { 339 return; 340 } 341 global $conf; 342 343 $this->renderer->doc .= '<tr class="dataflt">'; 344 345 // add extra column for row numbers 346 if ($this->data['rownumbers']) { 347 $this->renderer->doc .= '<th></th>'; 348 } 349 350 // each column gets a form 351 foreach ($this->columns as $column) { 352 $this->renderer->doc .= '<th>'; 353 { 354 $form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id))); 355 unset($form->_hidden['sectok']); // we don't need it here 356 if (!$conf['userewrite']) $form->addHidden('id', $this->id); 357 358 // current value 359 $dynamic = $this->searchConfig->getDynamicParameters(); 360 $filters = $dynamic->getFilters(); 361 if (isset($filters[$column->getFullQualifiedLabel()])) { 362 list(, $current) = $filters[$column->getFullQualifiedLabel()]; 363 $dynamic->removeFilter($column); 364 } else { 365 $current = ''; 366 } 367 368 // Add current request params 369 $params = $dynamic->getURLParameters(); 370 foreach ($params as $key => $val) { 371 $form->addHidden($key, $val); 372 } 373 374 // add input field 375 $key = $column->getFullQualifiedLabel() . $column->getType()->getDefaultComparator(); 376 $form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, '')); 377 $this->renderer->doc .= $form->getForm(); 378 } 379 $this->renderer->doc .= '</th>'; 380 } 381 $this->renderer->doc .= '</tr>'; 382 } 383 384 /** 385 * Display the actual table data 386 */ 387 protected function renderResult() 388 { 389 foreach ($this->result as $rownum => $row) { 390 $data = array( 391 'id' => $this->id, 392 'mode' => $this->mode, 393 'renderer' => $this->renderer, 394 'searchConfig' => $this->searchConfig, 395 'data' => $this->data, 396 'rownum' => &$rownum, 397 'row' => &$row, 398 ); 399 $evt = new \Doku_Event('PLUGIN_STRUCT_AGGREGATIONTABLE_RENDERRESULTROW', $data); 400 if ($evt->advise_before()) { 401 $this->renderResultRow($rownum, $row); 402 } 403 $evt->advise_after(); 404 } 405 } 406 407 /** 408 * Render a single result row 409 * 410 * @param int $rownum 411 * @param array $row 412 */ 413 protected function renderResultRow($rownum, $row) 414 { 415 $this->renderer->tablerow_open(); 416 417 // add data attribute for inline edit 418 if ($this->mode == 'xhtml') { 419 $pid = $this->resultPIDs[$rownum]; 420 $rid = $this->resultRids[$rownum]; 421 $rev = $this->resultRevs[$rownum]; 422 $this->renderer->doc = substr(rtrim($this->renderer->doc), 0, -1); // remove closing '>' 423 $this->renderer->doc .= ' data-pid="' . hsc($pid) . '" data-rev="' . $rev . '" data-rid="' . $rid . '">'; 424 } 425 426 // row number column 427 if (!empty($this->data['rownumbers'])) { 428 $this->renderer->tablecell_open(); 429 $searchConfigConf = $this->searchConfig->getConf(); 430 $this->renderer->cdata($rownum + $searchConfigConf['offset'] + 1); 431 $this->renderer->tablecell_close(); 432 } 433 434 /** @var Value $value */ 435 foreach ($row as $colnum => $value) { 436 $align = isset($this->data['align'][$colnum]) ? $this->data['align'][$colnum] : null; 437 $this->renderer->tablecell_open(1, $align); 438 $value->render($this->renderer, $this->mode); 439 $this->renderer->tablecell_close(); 440 441 // summarize 442 if ($this->data['summarize'] && is_numeric($value->getValue())) { 443 if (!isset($this->sums[$colnum])) { 444 $this->sums[$colnum] = 0; 445 } 446 $this->sums[$colnum] += $value->getValue(); 447 } 448 } 449 $this->renderer->tablerow_close(); 450 } 451 452 /** 453 * Renders an information row for when no results were found 454 */ 455 protected function renderEmptyResult() 456 { 457 $this->renderer->tablerow_open(); 458 $this->renderer->tablecell_open(count($this->columns) + $this->data['rownumbers'], 'center'); 459 $this->renderer->cdata($this->helper->getLang('none')); 460 $this->renderer->tablecell_close(); 461 $this->renderer->tablerow_close(); 462 } 463 464 /** 465 * Add sums if wanted 466 */ 467 protected function renderSums() 468 { 469 if (empty($this->data['summarize'])) return; 470 471 $this->renderer->info['struct_table_meta'] = true; 472 if ($this->mode == 'xhtml') { 473 /** @noinspection PhpMethodParametersCountMismatchInspection */ 474 $this->renderer->tablerow_open('summarize'); 475 } else { 476 $this->renderer->tablerow_open(); 477 } 478 479 if ($this->data['rownumbers']) { 480 $this->renderer->tableheader_open(); 481 $this->renderer->tableheader_close(); 482 } 483 484 $len = count($this->columns); 485 for ($i = 0; $i < $len; $i++) { 486 $this->renderer->tableheader_open(1, $this->data['align'][$i]); 487 if (!empty($this->sums[$i])) { 488 $this->renderer->cdata('∑ '); 489 $this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode); 490 } else { 491 if ($this->mode == 'xhtml') { 492 $this->renderer->doc .= ' '; 493 } 494 } 495 $this->renderer->tableheader_close(); 496 } 497 $this->renderer->tablerow_close(); 498 $this->renderer->info['struct_table_meta'] = false; 499 } 500 501 /** 502 * Adds paging controls to the table 503 */ 504 protected function renderPagingControls() 505 { 506 if (empty($this->data['limit'])) return; 507 if ($this->mode != 'xhtml') return; 508 509 $this->renderer->info['struct_table_meta'] = true; 510 $this->renderer->tablerow_open(); 511 $this->renderer->tableheader_open((count($this->columns) + ($this->data['rownumbers'] ? 1 : 0))); 512 $offset = $this->data['offset']; 513 514 // prev link 515 if ($offset) { 516 $prev = $offset - $this->data['limit']; 517 if ($prev < 0) { 518 $prev = 0; 519 } 520 521 $dynamic = $this->searchConfig->getDynamicParameters(); 522 $dynamic->setOffset($prev); 523 $link = wl($this->id, $dynamic->getURLParameters()); 524 $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>'; 525 } 526 527 // next link 528 if ($this->resultCount > $offset + $this->data['limit']) { 529 $next = $offset + $this->data['limit']; 530 $dynamic = $this->searchConfig->getDynamicParameters(); 531 $dynamic->setOffset($next); 532 $link = wl($this->id, $dynamic->getURLParameters()); 533 $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>'; 534 } 535 536 $this->renderer->tableheader_close(); 537 $this->renderer->tablerow_close(); 538 $this->renderer->info['struct_table_meta'] = true; 539 } 540 541 /** 542 * Adds CSV export controls 543 */ 544 protected function renderExportControls() 545 { 546 if ($this->mode != 'xhtml') return; 547 if (empty($this->data['csv'])) return; 548 if (!$this->resultCount) return; 549 550 $dynamic = $this->searchConfig->getDynamicParameters(); 551 $params = $dynamic->getURLParameters(); 552 $params['hash'] = $this->renderer->info['struct_table_hash']; 553 554 // FIXME apply dynamic filters 555 $link = exportlink($this->id, 'struct_csv', $params); 556 557 $this->renderer->doc .= '<a href="' . $link . '" class="export mediafile mf_csv">' . $this->helper->getLang('csvexport') . '</a>'; 558 } 559} 560