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