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