1<?php 2 3namespace plugin\struct\meta; 4 5/** 6 * Creates the table aggregation output 7 * 8 * @package 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 155 if(strpos($comp, '~') !== false) { 156 if(strpos($comp, '!~') !== false) { 157 $comparator_value = '!~' . str_replace('%', '*', $value); 158 } else { 159 $comparator_value = '~' . str_replace('%', '', $value); 160 } 161 $fltrs[] = $column . $comparator_value; 162 } else { 163 $fltrs[] = $column . $comp . $value; 164 } 165 } 166 167 $this->renderer->doc .= '<div class="filter">'; 168 $this->renderer->doc .= '<h4>' . sprintf($this->helper->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>'; 169 $this->renderer->doc .= '<div class="resetfilter">'; 170 $this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter')); 171 $this->renderer->doc .= '</div>'; 172 $this->renderer->doc .= '</div>'; 173 } 174 175 /** 176 * Shows the column headers with links to sort by column 177 */ 178 protected function renderColumnHeaders() { 179 $this->renderer->tablerow_open(); 180 181 // additional column for row numbers 182 if($this->data['rownumbers']) { 183 $this->renderer->tableheader_open(); 184 $this->renderer->cdata('#'); 185 $this->renderer->tableheader_close(); 186 } 187 188 // show all headers 189 foreach($this->data['headers'] as $num => $header) { 190 $column = $this->columns[$num]; 191 192 // use field label if no header was set 193 if(blank($header)) { 194 if(is_a($column, 'plugin\struct\meta\PageColumn')) { 195 $header = $this->helper->getLang('pagelabel'); // @todo this could be part of PageColumn::getTranslatedLabel 196 } else if(is_a($column, 'plugin\struct\meta\Column')) { 197 $header = $column->getTranslatedLabel(); 198 } else { 199 $header = 'column ' . $num; // this should never happen 200 } 201 } 202 203 // simple mode first 204 if($this->mode != 'xhtml') { 205 $this->renderer->tableheader_open(); 206 $this->renderer->cdata($header); 207 $this->renderer->tableheader_close(); 208 continue; 209 } 210 211 // still here? create custom header for more flexibility 212 213 // width setting 214 $width = ''; 215 if(isset($data['widths'][$num]) && $data['widths'][$num] != '-') { 216 $width = ' style="width: ' . $data['widths'][$num] . ';"'; 217 } 218 219 // sort indicator and link 220 $sortclass = ''; 221 $sorts = $this->searchConfig->getSorts(); 222 $dynamic = $this->searchConfig->getDynamicParameters(); 223 if(isset($sorts[$column->getFullQualifiedLabel()])) { 224 list(, $currentSort) = $sorts[$column->getFullQualifiedLabel()]; 225 if($currentSort[1]) { 226 $sortclass = 'sort-down'; 227 $dynamic->setSort($column, false); 228 } else { 229 $sortclass = 'sort-up'; 230 } 231 } 232 $dynamic->setSort($column, true); 233 $link = wl($this->id, $dynamic->getURLParameters()); 234 235 // output XHTML header 236 $this->renderer->doc .= "<th $width >"; 237 $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>'; 238 $this->renderer->doc .= '</th>'; 239 } 240 241 $this->renderer->tablerow_close(); 242 } 243 244 /** 245 * Add input fields for dynamic filtering 246 */ 247 protected function renderDynamicFilters() { 248 if($this->mode != 'xhtml') return; 249 if(!$this->data['dynfilters']) return; 250 251 $this->renderer->doc .= '<tr class="dataflt">'; 252 253 // add extra column for row numbers 254 if($this->data['rownumbers']) { 255 $this->renderer->doc .= '<th></th>'; 256 } 257 258 // each column gets a form 259 foreach($this->columns as $column) { 260 $this->renderer->doc .= '<th>'; 261 { 262 $form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id))); 263 264 // current value 265 $dynamic = $this->searchConfig->getDynamicParameters(); 266 $filters = $dynamic->getFilters(); 267 if(isset($filters[$column->getFullQualifiedLabel()])) { 268 list(, $current) = $filters[$column->getFullQualifiedLabel()]; 269 $dynamic->removeFilter($column); 270 } else { 271 $current = ''; 272 } 273 274 // Add current request params 275 $params = $dynamic->getURLParameters(); 276 foreach($params as $key => $val) { 277 $form->addHidden($key, $val); 278 } 279 280 // add input field 281 $key = $column->getFullQualifiedLabel() . '*~'; 282 $form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER. '[' . $key . ']', $current, '')); 283 $this->renderer->doc .= $form->getForm(); 284 } 285 $this->renderer->doc .= '</th>'; 286 } 287 $this->renderer->doc .= '</tr>'; 288 289 } 290 291 /** 292 * Display the actual table data 293 */ 294 protected function renderResult() { 295 $this->renderer->tabletbody_open(); 296 foreach($this->result as $rownum => $row) { 297 $this->renderer->tablerow_open(); 298 299 // row number column 300 if($this->data['rownumbers']) { 301 $this->renderer->tablecell_open(); 302 $this->renderer->doc .= $rownum + 1; 303 $this->renderer->tablecell_close(); 304 } 305 306 /** @var Value $value */ 307 foreach($row as $colnum => $value) { 308 $this->renderer->tablecell_open(); 309 $value->render($this->renderer, $this->mode); 310 $this->renderer->tablecell_close(); 311 312 // summarize 313 if($this->data['summarize'] && is_numeric($value->getValue())) { 314 if(!isset($this->sums[$colnum])) { 315 $this->sums[$colnum] = 0; 316 } 317 $this->sums[$colnum] += $value->getValue(); 318 } 319 } 320 $this->renderer->tablerow_close(); 321 } 322 $this->renderer->tabletbody_close(); 323 } 324 325 /** 326 * Renders an information row for when no results were found 327 */ 328 protected function renderEmptyResult() { 329 $this->renderer->tablerow_open(); 330 $this->renderer->tablecell_open(count($this->data['cols']) + $this->data['rownumbers'], 'center'); 331 $this->renderer->cdata($this->helper->getLang('none')); 332 $this->renderer->tablecell_close(); 333 $this->renderer->tablerow_close(); 334 } 335 336 /** 337 * Add sums if wanted 338 */ 339 protected function renderSums() { 340 if($this->data['summarize']) return; 341 342 $this->renderer->tablerow_open(); 343 $len = count($this->data['cols']); 344 345 if($this->data['rownumbers']) { 346 $this->renderer->tablecell_open(); 347 $this->renderer->tablecell_close(); 348 } 349 350 for($i = 0; $i < $len; $i++) { 351 $this->renderer->tablecell_open(1, $this->data['align'][$i]); 352 if(!empty($sums[$i])) { 353 $this->renderer->cdata('∑ ' . $sums[$i]); 354 } else { 355 if($this->mode == 'xhtml') { 356 $this->renderer->doc .= ' '; 357 } 358 } 359 $this->renderer->tablecell_close(); 360 } 361 $this->renderer->tablerow_close(); 362 } 363 364 /** 365 * Adds paging controls to the table 366 */ 367 protected function renderPagingControls() { 368 if(empty($this->data['limit'])) return; 369 if($this->mode != 'xhtml') ; 370 371 $this->renderer->tablerow_open(); 372 $this->renderer->tableheader_open((count($this->data['cols']) + ($this->data['rownumbers'] ? 1 : 0))); 373 $offset = $this->data['offset']; 374 375 // prev link 376 if($offset) { 377 $prev = $offset - $this->data['limit']; 378 if($prev < 0) { 379 $prev = 0; 380 } 381 382 $dynamic = $this->searchConfig->getDynamicParameters(); 383 $dynamic->setOffset($prev); 384 $link = wl($this->id, $dynamic->getURLParameters()); 385 $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>'; 386 } 387 388 // next link 389 if($this->resultCount > $offset + $this->data['limit']) { 390 $next = $offset + $this->data['limit']; 391 $dynamic = $this->searchConfig->getDynamicParameters(); 392 $dynamic->setOffset($next); 393 $link = wl($this->id, $dynamic->getURLParameters()); 394 $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>'; 395 } 396 397 $this->renderer->tableheader_close(); 398 $this->renderer->tablerow_close(); 399 } 400} 401