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