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