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