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