107993756SAndreas Gohr<?php 207993756SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 407993756SAndreas Gohr 5d60f71efSAndreas Gohr/** 6d60f71efSAndreas Gohr * Creates the table aggregation output 7d60f71efSAndreas Gohr * 8ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 9d60f71efSAndreas Gohr */ 1007993756SAndreas Gohrclass AggregationTable { 1107993756SAndreas Gohr 1207993756SAndreas Gohr /** 1307993756SAndreas Gohr * @var string the page id of the page this is rendered to 1407993756SAndreas Gohr */ 1507993756SAndreas Gohr protected $id; 1607993756SAndreas Gohr /** 1707993756SAndreas Gohr * @var string the Type of renderer used 1807993756SAndreas Gohr */ 1907993756SAndreas Gohr protected $mode; 2007993756SAndreas Gohr /** 2107993756SAndreas Gohr * @var \Doku_Renderer the DokuWiki renderer used to create the output 2207993756SAndreas Gohr */ 2307993756SAndreas Gohr protected $renderer; 2407993756SAndreas Gohr /** 2507993756SAndreas Gohr * @var SearchConfig the configured search - gives access to columns etc. 2607993756SAndreas Gohr */ 2707993756SAndreas Gohr protected $searchConfig; 2807993756SAndreas Gohr 2907993756SAndreas Gohr /** 3007993756SAndreas Gohr * @var Column[] the list of columns to be displayed 3107993756SAndreas Gohr */ 3207993756SAndreas Gohr protected $columns; 3307993756SAndreas Gohr 3407993756SAndreas Gohr /** 3507993756SAndreas Gohr * @var Value[][] the search result 3607993756SAndreas Gohr */ 3707993756SAndreas Gohr protected $result; 3807993756SAndreas Gohr 3907993756SAndreas Gohr /** 4007993756SAndreas Gohr * @var int number of all results 4107993756SAndreas Gohr */ 4207993756SAndreas Gohr protected $resultCount; 4307993756SAndreas Gohr 4407993756SAndreas Gohr /** 4507993756SAndreas Gohr * @var array for summing up columns 4607993756SAndreas Gohr */ 4707993756SAndreas Gohr protected $sums; 4807993756SAndreas Gohr 4907993756SAndreas Gohr /** 5007993756SAndreas Gohr * @todo we might be able to get rid of this helper and move this to SearchConfig 5107993756SAndreas Gohr * @var \helper_plugin_struct_config 5207993756SAndreas Gohr */ 5307993756SAndreas Gohr protected $helper; 5407993756SAndreas Gohr 5507993756SAndreas Gohr /** 5607993756SAndreas Gohr * Initialize the Aggregation renderer and executes the search 5707993756SAndreas Gohr * 5807993756SAndreas Gohr * You need to call @see render() on the resulting object. 5907993756SAndreas Gohr * 6007993756SAndreas Gohr * @param string $id 6107993756SAndreas Gohr * @param string $mode 6207993756SAndreas Gohr * @param \Doku_Renderer $renderer 6307993756SAndreas Gohr * @param SearchConfig $searchConfig 6407993756SAndreas Gohr */ 6507993756SAndreas Gohr public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig) { 6607993756SAndreas Gohr $this->id = $id; 6707993756SAndreas Gohr $this->mode = $mode; 6807993756SAndreas Gohr $this->renderer = $renderer; 6907993756SAndreas Gohr $this->searchConfig = $searchConfig; 7007993756SAndreas Gohr $this->data = $searchConfig->getConf(); 7107993756SAndreas Gohr $this->columns = $searchConfig->getColumns(); 7207993756SAndreas Gohr 7307993756SAndreas Gohr $this->result = $this->searchConfig->execute(); 7407993756SAndreas Gohr $this->resultCount = $this->searchConfig->getCount(); 7507993756SAndreas Gohr $this->helper = plugin_load('helper', 'struct_config'); 7607993756SAndreas Gohr } 7707993756SAndreas Gohr 7807993756SAndreas Gohr /** 7907993756SAndreas Gohr * Create the table on the renderer 8007993756SAndreas Gohr */ 8107993756SAndreas Gohr public function render() { 82b7e1d73bSAndreas Gohr 83b7e1d73bSAndreas Gohr // abort early if there are no results at all (not filtered) 84b7e1d73bSAndreas Gohr if(!$this->resultCount && !$this->isDynamicallyFiltered()) { 85b7e1d73bSAndreas Gohr $this->startScope(); 86b7e1d73bSAndreas Gohr $this->renderer->cdata($this->helper->getLang('none')); 87b7e1d73bSAndreas Gohr $this->finishScope(); 88b7e1d73bSAndreas Gohr return; 89b7e1d73bSAndreas Gohr } 90b7e1d73bSAndreas Gohr 9107993756SAndreas Gohr // table open 9207993756SAndreas Gohr $this->startScope(); 93986ab7e6SAndreas Gohr $this->renderActiveFilters(); 9407993756SAndreas Gohr $this->renderer->table_open(); 9507993756SAndreas Gohr 9607993756SAndreas Gohr // header 9707993756SAndreas Gohr $this->renderer->tablethead_open(); 98986ab7e6SAndreas Gohr $this->renderColumnHeaders(); 99986ab7e6SAndreas Gohr $this->renderDynamicFilters(); 10007993756SAndreas Gohr $this->renderer->tablethead_close(); 10107993756SAndreas Gohr 10207993756SAndreas Gohr if($this->resultCount) { 10307993756SAndreas Gohr // actual data 104986ab7e6SAndreas Gohr $this->renderResult(); 10507993756SAndreas Gohr 10607993756SAndreas Gohr // footer 107986ab7e6SAndreas Gohr $this->renderSums(); 108986ab7e6SAndreas Gohr $this->renderPagingControls(); 10907993756SAndreas Gohr } else { 11007993756SAndreas Gohr // nothing found 111986ab7e6SAndreas Gohr $this->renderEmptyResult(); 11207993756SAndreas Gohr } 11307993756SAndreas Gohr 11407993756SAndreas Gohr // table close 11507993756SAndreas Gohr $this->renderer->table_close(); 11607993756SAndreas Gohr $this->finishScope(); 11707993756SAndreas Gohr } 11807993756SAndreas Gohr 11907993756SAndreas Gohr /** 12007993756SAndreas Gohr * Adds additional info to document and renderer in XHTML mode 12107993756SAndreas Gohr * 12207993756SAndreas Gohr * @see finishScope() 12307993756SAndreas Gohr */ 12407993756SAndreas Gohr protected function startScope() { 12507993756SAndreas Gohr if($this->mode != 'xhtml') return; 12607993756SAndreas Gohr 12707993756SAndreas Gohr // wrapping div 12807993756SAndreas Gohr $this->renderer->doc .= "<div class=\"structaggregation\">"; 12907993756SAndreas Gohr 13007993756SAndreas Gohr // unique identifier for this aggregation 13107993756SAndreas Gohr $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true)); 13207993756SAndreas Gohr } 13307993756SAndreas Gohr 13407993756SAndreas Gohr /** 13507993756SAndreas Gohr * Closes the table and anything opened in startScope() 13607993756SAndreas Gohr * 13707993756SAndreas Gohr * @see startScope() 13807993756SAndreas Gohr */ 13907993756SAndreas Gohr protected function finishScope() { 14007993756SAndreas Gohr if($this->mode != 'xhtml') return; 14107993756SAndreas Gohr 14207993756SAndreas Gohr // wrapping div 14307993756SAndreas Gohr $this->renderer->doc .= '</div>'; 14407993756SAndreas Gohr 14507993756SAndreas Gohr // remove identifier from renderer again 14607993756SAndreas Gohr if(isset($this->renderer->info['struct_table_hash'])) { 14707993756SAndreas Gohr unset($this->renderer->info['struct_table_hash']); 14807993756SAndreas Gohr } 14907993756SAndreas Gohr } 15007993756SAndreas Gohr 15107993756SAndreas Gohr /** 15207993756SAndreas Gohr * Displays info about the currently applied filters 15307993756SAndreas Gohr */ 154986ab7e6SAndreas Gohr protected function renderActiveFilters() { 15507993756SAndreas Gohr if($this->mode != 'xhtml') return; 15607993756SAndreas Gohr $dynamic = $this->searchConfig->getDynamicParameters(); 15707993756SAndreas Gohr $filters = $dynamic->getFilters(); 15807993756SAndreas Gohr if(!$filters) return; 15907993756SAndreas Gohr 16007993756SAndreas Gohr $fltrs = array(); 16107993756SAndreas Gohr foreach($filters as $column => $filter) { 16207993756SAndreas Gohr list($comp, $value) = $filter; 163083109a1SAndreas Gohr $fltrs[] = $column . ' ' . $comp . ' ' . $value; 16407993756SAndreas Gohr } 16507993756SAndreas Gohr 16607993756SAndreas Gohr $this->renderer->doc .= '<div class="filter">'; 16707993756SAndreas Gohr $this->renderer->doc .= '<h4>' . sprintf($this->helper->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>'; 16807993756SAndreas Gohr $this->renderer->doc .= '<div class="resetfilter">'; 16907993756SAndreas Gohr $this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter')); 17007993756SAndreas Gohr $this->renderer->doc .= '</div>'; 17107993756SAndreas Gohr $this->renderer->doc .= '</div>'; 17207993756SAndreas Gohr } 17307993756SAndreas Gohr 17407993756SAndreas Gohr /** 17507993756SAndreas Gohr * Shows the column headers with links to sort by column 17607993756SAndreas Gohr */ 177986ab7e6SAndreas Gohr protected function renderColumnHeaders() { 17807993756SAndreas Gohr $this->renderer->tablerow_open(); 17907993756SAndreas Gohr 18007993756SAndreas Gohr // additional column for row numbers 18107993756SAndreas Gohr if($this->data['rownumbers']) { 18207993756SAndreas Gohr $this->renderer->tableheader_open(); 18307993756SAndreas Gohr $this->renderer->cdata('#'); 18407993756SAndreas Gohr $this->renderer->tableheader_close(); 18507993756SAndreas Gohr } 18607993756SAndreas Gohr 18707993756SAndreas Gohr // show all headers 188*8c4ee9beSAndreas Gohr foreach($this->columns as $num => $column) { 189*8c4ee9beSAndreas Gohr $header = ''; 190*8c4ee9beSAndreas Gohr if(isset($this->data['headers'][$num])) { 191*8c4ee9beSAndreas Gohr $header = $this->data['headers'][$num]; 192*8c4ee9beSAndreas Gohr } 19307993756SAndreas Gohr 19407993756SAndreas Gohr // use field label if no header was set 19507993756SAndreas Gohr if(blank($header)) { 19601f8b845SAndreas Gohr if(is_a($column, 'dokuwiki\plugin\struct\meta\Column')) { 19707993756SAndreas Gohr $header = $column->getTranslatedLabel(); 19807993756SAndreas Gohr } else { 19907993756SAndreas Gohr $header = 'column ' . $num; // this should never happen 20007993756SAndreas Gohr } 20107993756SAndreas Gohr } 20207993756SAndreas Gohr 20307993756SAndreas Gohr // simple mode first 20407993756SAndreas Gohr if($this->mode != 'xhtml') { 20507993756SAndreas Gohr $this->renderer->tableheader_open(); 20607993756SAndreas Gohr $this->renderer->cdata($header); 20707993756SAndreas Gohr $this->renderer->tableheader_close(); 20807993756SAndreas Gohr continue; 20907993756SAndreas Gohr } 21007993756SAndreas Gohr 21107993756SAndreas Gohr // still here? create custom header for more flexibility 21207993756SAndreas Gohr 21307993756SAndreas Gohr // width setting 21407993756SAndreas Gohr $width = ''; 21507993756SAndreas Gohr if(isset($data['widths'][$num]) && $data['widths'][$num] != '-') { 216e0216289SAndreas Gohr $width = ' style="width: ' . $data['widths'][$num] . ';"'; // widths are prevalidated, no escape needed 21707993756SAndreas Gohr } 21807993756SAndreas Gohr 21907993756SAndreas Gohr // sort indicator and link 22007993756SAndreas Gohr $sortclass = ''; 22107993756SAndreas Gohr $sorts = $this->searchConfig->getSorts(); 22207993756SAndreas Gohr $dynamic = $this->searchConfig->getDynamicParameters(); 223aa124708SAndreas Gohr $dynamic->setSort($column, true); 22407993756SAndreas Gohr if(isset($sorts[$column->getFullQualifiedLabel()])) { 225aa124708SAndreas Gohr list(/*colname*/, $currentSort) = $sorts[$column->getFullQualifiedLabel()]; 226aa124708SAndreas Gohr if($currentSort) { 22707993756SAndreas Gohr $sortclass = 'sort-down'; 22807993756SAndreas Gohr $dynamic->setSort($column, false); 22907993756SAndreas Gohr } else { 23007993756SAndreas Gohr $sortclass = 'sort-up'; 23107993756SAndreas Gohr } 23207993756SAndreas Gohr } 23307993756SAndreas Gohr $link = wl($this->id, $dynamic->getURLParameters()); 23407993756SAndreas Gohr 23507993756SAndreas Gohr // output XHTML header 23607993756SAndreas Gohr $this->renderer->doc .= "<th $width >"; 23707993756SAndreas Gohr $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>'; 23807993756SAndreas Gohr $this->renderer->doc .= '</th>'; 23907993756SAndreas Gohr } 24007993756SAndreas Gohr 24107993756SAndreas Gohr $this->renderer->tablerow_close(); 24207993756SAndreas Gohr } 24307993756SAndreas Gohr 24407993756SAndreas Gohr /** 245b7e1d73bSAndreas Gohr * Is the result set currently dynamically filtered? 246b7e1d73bSAndreas Gohr * @return bool 247b7e1d73bSAndreas Gohr */ 248b7e1d73bSAndreas Gohr protected function isDynamicallyFiltered() { 249b7e1d73bSAndreas Gohr if($this->mode != 'xhtml') return false; 250b7e1d73bSAndreas Gohr if(!$this->data['dynfilters']) return false; 251b7e1d73bSAndreas Gohr 252b7e1d73bSAndreas Gohr $dynamic = $this->searchConfig->getDynamicParameters(); 253b7e1d73bSAndreas Gohr return (bool) $dynamic->getFilters(); 254b7e1d73bSAndreas Gohr } 255b7e1d73bSAndreas Gohr 256b7e1d73bSAndreas Gohr /** 25707993756SAndreas Gohr * Add input fields for dynamic filtering 25807993756SAndreas Gohr */ 259986ab7e6SAndreas Gohr protected function renderDynamicFilters() { 26007993756SAndreas Gohr if($this->mode != 'xhtml') return; 26107993756SAndreas Gohr if(!$this->data['dynfilters']) return; 26276195677SAndreas Gohr global $conf; 26307993756SAndreas Gohr 26407993756SAndreas Gohr $this->renderer->doc .= '<tr class="dataflt">'; 26507993756SAndreas Gohr 26607993756SAndreas Gohr // add extra column for row numbers 26707993756SAndreas Gohr if($this->data['rownumbers']) { 26807993756SAndreas Gohr $this->renderer->doc .= '<th></th>'; 26907993756SAndreas Gohr } 27007993756SAndreas Gohr 27107993756SAndreas Gohr // each column gets a form 27207993756SAndreas Gohr foreach($this->columns as $column) { 27307993756SAndreas Gohr $this->renderer->doc .= '<th>'; 27407993756SAndreas Gohr { 27507993756SAndreas Gohr $form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id))); 27676195677SAndreas Gohr unset($form->_hidden['sectok']); // we don't need it here 27776195677SAndreas Gohr if(!$conf['userewrite']) $form->addHidden('id', $this->id); 27807993756SAndreas Gohr 27907993756SAndreas Gohr // current value 28007993756SAndreas Gohr $dynamic = $this->searchConfig->getDynamicParameters(); 28107993756SAndreas Gohr $filters = $dynamic->getFilters(); 28207993756SAndreas Gohr if(isset($filters[$column->getFullQualifiedLabel()])) { 28307993756SAndreas Gohr list(, $current) = $filters[$column->getFullQualifiedLabel()]; 28407993756SAndreas Gohr $dynamic->removeFilter($column); 28507993756SAndreas Gohr } else { 28607993756SAndreas Gohr $current = ''; 28707993756SAndreas Gohr } 28807993756SAndreas Gohr 28907993756SAndreas Gohr // Add current request params 29007993756SAndreas Gohr $params = $dynamic->getURLParameters(); 29107993756SAndreas Gohr foreach($params as $key => $val) { 29207993756SAndreas Gohr $form->addHidden($key, $val); 29307993756SAndreas Gohr } 29407993756SAndreas Gohr 29507993756SAndreas Gohr // add input field 29607993756SAndreas Gohr $key = $column->getFullQualifiedLabel() . '*~'; 297d60f71efSAndreas Gohr $form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, '')); 29807993756SAndreas Gohr $this->renderer->doc .= $form->getForm(); 29907993756SAndreas Gohr } 30007993756SAndreas Gohr $this->renderer->doc .= '</th>'; 30107993756SAndreas Gohr } 30207993756SAndreas Gohr $this->renderer->doc .= '</tr>'; 30307993756SAndreas Gohr 30407993756SAndreas Gohr } 30507993756SAndreas Gohr 30607993756SAndreas Gohr /** 30707993756SAndreas Gohr * Display the actual table data 30807993756SAndreas Gohr */ 309986ab7e6SAndreas Gohr protected function renderResult() { 31007993756SAndreas Gohr $this->renderer->tabletbody_open(); 31107993756SAndreas Gohr foreach($this->result as $rownum => $row) { 31207993756SAndreas Gohr $this->renderer->tablerow_open(); 31307993756SAndreas Gohr 31407993756SAndreas Gohr // row number column 31507993756SAndreas Gohr if($this->data['rownumbers']) { 31607993756SAndreas Gohr $this->renderer->tablecell_open(); 31707993756SAndreas Gohr $this->renderer->doc .= $rownum + 1; 31807993756SAndreas Gohr $this->renderer->tablecell_close(); 31907993756SAndreas Gohr } 32007993756SAndreas Gohr 32107993756SAndreas Gohr /** @var Value $value */ 32207993756SAndreas Gohr foreach($row as $colnum => $value) { 32307993756SAndreas Gohr $this->renderer->tablecell_open(); 32407993756SAndreas Gohr $value->render($this->renderer, $this->mode); 32507993756SAndreas Gohr $this->renderer->tablecell_close(); 32607993756SAndreas Gohr 32707993756SAndreas Gohr // summarize 32807993756SAndreas Gohr if($this->data['summarize'] && is_numeric($value->getValue())) { 32907993756SAndreas Gohr if(!isset($this->sums[$colnum])) { 33007993756SAndreas Gohr $this->sums[$colnum] = 0; 33107993756SAndreas Gohr } 33207993756SAndreas Gohr $this->sums[$colnum] += $value->getValue(); 33307993756SAndreas Gohr } 33407993756SAndreas Gohr } 33507993756SAndreas Gohr $this->renderer->tablerow_close(); 33607993756SAndreas Gohr } 33707993756SAndreas Gohr $this->renderer->tabletbody_close(); 33807993756SAndreas Gohr } 33907993756SAndreas Gohr 34007993756SAndreas Gohr /** 34107993756SAndreas Gohr * Renders an information row for when no results were found 34207993756SAndreas Gohr */ 343986ab7e6SAndreas Gohr protected function renderEmptyResult() { 34407993756SAndreas Gohr $this->renderer->tablerow_open(); 34507993756SAndreas Gohr $this->renderer->tablecell_open(count($this->data['cols']) + $this->data['rownumbers'], 'center'); 34607993756SAndreas Gohr $this->renderer->cdata($this->helper->getLang('none')); 34707993756SAndreas Gohr $this->renderer->tablecell_close(); 34807993756SAndreas Gohr $this->renderer->tablerow_close(); 34907993756SAndreas Gohr } 35007993756SAndreas Gohr 35107993756SAndreas Gohr /** 35207993756SAndreas Gohr * Add sums if wanted 35307993756SAndreas Gohr */ 354986ab7e6SAndreas Gohr protected function renderSums() { 355d18090e8SAndreas Gohr if(empty($this->data['summarize'])) return; 35607993756SAndreas Gohr 35707993756SAndreas Gohr $this->renderer->tablerow_open(); 35807993756SAndreas Gohr 35907993756SAndreas Gohr if($this->data['rownumbers']) { 36007993756SAndreas Gohr $this->renderer->tablecell_open(); 36107993756SAndreas Gohr $this->renderer->tablecell_close(); 36207993756SAndreas Gohr } 36307993756SAndreas Gohr 364aee4116bSAndreas Gohr $len = count($this->columns); 36507993756SAndreas Gohr for($i = 0; $i < $len; $i++) { 36607993756SAndreas Gohr $this->renderer->tablecell_open(1, $this->data['align'][$i]); 367aee4116bSAndreas Gohr if(!empty($this->sums[$i])) { 3689b97e610SAndreas Gohr $this->renderer->cdata('∑ '); 3699b97e610SAndreas Gohr $this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode); 37007993756SAndreas Gohr } else { 37107993756SAndreas Gohr if($this->mode == 'xhtml') { 37207993756SAndreas Gohr $this->renderer->doc .= ' '; 37307993756SAndreas Gohr } 37407993756SAndreas Gohr } 37507993756SAndreas Gohr $this->renderer->tablecell_close(); 37607993756SAndreas Gohr } 37707993756SAndreas Gohr $this->renderer->tablerow_close(); 37807993756SAndreas Gohr } 37907993756SAndreas Gohr 38007993756SAndreas Gohr /** 381986ab7e6SAndreas Gohr * Adds paging controls to the table 38207993756SAndreas Gohr */ 383986ab7e6SAndreas Gohr protected function renderPagingControls() { 38407993756SAndreas Gohr if(empty($this->data['limit'])) return; 38507993756SAndreas Gohr if($this->mode != 'xhtml') ; 38607993756SAndreas Gohr 38707993756SAndreas Gohr $this->renderer->tablerow_open(); 38807993756SAndreas Gohr $this->renderer->tableheader_open((count($this->data['cols']) + ($this->data['rownumbers'] ? 1 : 0))); 38907993756SAndreas Gohr $offset = $this->data['offset']; 39007993756SAndreas Gohr 39107993756SAndreas Gohr // prev link 39207993756SAndreas Gohr if($offset) { 39307993756SAndreas Gohr $prev = $offset - $this->data['limit']; 39407993756SAndreas Gohr if($prev < 0) { 39507993756SAndreas Gohr $prev = 0; 39607993756SAndreas Gohr } 39707993756SAndreas Gohr 39807993756SAndreas Gohr $dynamic = $this->searchConfig->getDynamicParameters(); 39907993756SAndreas Gohr $dynamic->setOffset($prev); 40007993756SAndreas Gohr $link = wl($this->id, $dynamic->getURLParameters()); 40107993756SAndreas Gohr $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>'; 40207993756SAndreas Gohr } 40307993756SAndreas Gohr 40407993756SAndreas Gohr // next link 40507993756SAndreas Gohr if($this->resultCount > $offset + $this->data['limit']) { 40607993756SAndreas Gohr $next = $offset + $this->data['limit']; 40707993756SAndreas Gohr $dynamic = $this->searchConfig->getDynamicParameters(); 40807993756SAndreas Gohr $dynamic->setOffset($next); 40907993756SAndreas Gohr $link = wl($this->id, $dynamic->getURLParameters()); 41007993756SAndreas Gohr $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>'; 41107993756SAndreas Gohr } 41207993756SAndreas Gohr 41307993756SAndreas Gohr $this->renderer->tableheader_close(); 41407993756SAndreas Gohr $this->renderer->tablerow_close(); 41507993756SAndreas Gohr } 41607993756SAndreas Gohr} 417