1549a0837SAndreas Gohr<?php 2549a0837SAndreas Gohr/** 3549a0837SAndreas Gohr * DokuWiki Plugin struct (Syntax Component) 4549a0837SAndreas Gohr * 5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7549a0837SAndreas Gohr */ 8549a0837SAndreas Gohr 9549a0837SAndreas Gohr// must be run within Dokuwiki 10*01dd90deSAndreas Gohruse plugin\struct\meta\Column; 115511bd5bSAndreas Gohruse plugin\struct\meta\ConfigParser; 1215929be2SAndreas Gohruse plugin\struct\meta\Search; 135511bd5bSAndreas Gohruse plugin\struct\meta\SearchConfig; 1415929be2SAndreas Gohruse plugin\struct\meta\SearchException; 155511bd5bSAndreas Gohruse plugin\struct\meta\StructException; 1615929be2SAndreas Gohr 17549a0837SAndreas Gohrif (!defined('DOKU_INC')) die(); 18549a0837SAndreas Gohr 19549a0837SAndreas Gohrclass syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin { 20549a0837SAndreas Gohr /** 21549a0837SAndreas Gohr * @return string Syntax mode type 22549a0837SAndreas Gohr */ 23549a0837SAndreas Gohr public function getType() { 2415929be2SAndreas Gohr return 'substition'; 25549a0837SAndreas Gohr } 26549a0837SAndreas Gohr /** 27549a0837SAndreas Gohr * @return string Paragraph type 28549a0837SAndreas Gohr */ 29549a0837SAndreas Gohr public function getPType() { 3015929be2SAndreas Gohr return 'block'; 31549a0837SAndreas Gohr } 32549a0837SAndreas Gohr /** 33549a0837SAndreas Gohr * @return int Sort order - Low numbers go before high numbers 34549a0837SAndreas Gohr */ 35549a0837SAndreas Gohr public function getSort() { 365511bd5bSAndreas Gohr return 155; 37549a0837SAndreas Gohr } 38549a0837SAndreas Gohr 39549a0837SAndreas Gohr /** 40549a0837SAndreas Gohr * Connect lookup pattern to lexer. 41549a0837SAndreas Gohr * 42549a0837SAndreas Gohr * @param string $mode Parser mode 43549a0837SAndreas Gohr */ 44549a0837SAndreas Gohr public function connectTo($mode) { 455511bd5bSAndreas Gohr $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table'); 46549a0837SAndreas Gohr } 47549a0837SAndreas Gohr 48549a0837SAndreas Gohr 49549a0837SAndreas Gohr /** 50549a0837SAndreas Gohr * Handle matches of the struct syntax 51549a0837SAndreas Gohr * 52549a0837SAndreas Gohr * @param string $match The match of the syntax 53549a0837SAndreas Gohr * @param int $state The state of the handler 54549a0837SAndreas Gohr * @param int $pos The position in the document 55549a0837SAndreas Gohr * @param Doku_Handler $handler The handler 56549a0837SAndreas Gohr * @return array Data for the renderer 57549a0837SAndreas Gohr */ 58ab466032SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler){ 59549a0837SAndreas Gohr 605511bd5bSAndreas Gohr $lines = explode("\n", $match); 615511bd5bSAndreas Gohr array_shift($lines); 625511bd5bSAndreas Gohr array_pop($lines); 635511bd5bSAndreas Gohr 645511bd5bSAndreas Gohr try { 655511bd5bSAndreas Gohr $parser = new ConfigParser($lines); 665511bd5bSAndreas Gohr return $parser->getConfig(); 675511bd5bSAndreas Gohr } catch (StructException $e) { 685511bd5bSAndreas Gohr msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 695511bd5bSAndreas Gohr return null; 705511bd5bSAndreas Gohr } 71549a0837SAndreas Gohr } 72549a0837SAndreas Gohr 7329877279SMichael Große protected $sums = array(); 7429877279SMichael Große 75549a0837SAndreas Gohr /** 76549a0837SAndreas Gohr * Render xhtml output or metadata 77549a0837SAndreas Gohr * 78549a0837SAndreas Gohr * @param string $mode Renderer mode (supported modes: xhtml) 79549a0837SAndreas Gohr * @param Doku_Renderer $renderer The renderer 80549a0837SAndreas Gohr * @param array $data The data from the handler() function 81549a0837SAndreas Gohr * @return bool If rendering was successful. 82549a0837SAndreas Gohr */ 83ab466032SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) { 84549a0837SAndreas Gohr if($mode != 'xhtml') return false; 855511bd5bSAndreas Gohr if(!$data) return false; 8629877279SMichael Große 8729877279SMichael Große //reset counters 8829877279SMichael Große $this->sums = array(); 8929877279SMichael Große 9015929be2SAndreas Gohr try { 915511bd5bSAndreas Gohr $search = new SearchConfig($data); 921a07b696SMichael Große $data = $search->getConf(); 9329877279SMichael Große $rows = $search->execute(); 94f87e1c10SMichael Große $cnt = $search->getCount(); 95*01dd90deSAndreas Gohr $cols = $search->getColumns(); 965511bd5bSAndreas Gohr 9729877279SMichael Große if ($cnt === 0) { 98f87e1c10SMichael Große $this->nullList($data, $mode, $renderer); 99f87e1c10SMichael Große return true; 10029877279SMichael Große } 1015511bd5bSAndreas Gohr 102*01dd90deSAndreas Gohr $this->renderPreTable($mode, $renderer, $data, $cols); 10329877279SMichael Große $this->renderRows($mode, $renderer, $data, $rows); 10429877279SMichael Große $this->renderPostTable($mode, $renderer, $data, $cnt); 1055511bd5bSAndreas Gohr } catch (StructException $e) { 10615929be2SAndreas Gohr msg($e->getMessage(), -1, $e->getLine(), $e->getFile()); 10715929be2SAndreas Gohr } 10815929be2SAndreas Gohr 109549a0837SAndreas Gohr return true; 110549a0837SAndreas Gohr } 11129877279SMichael Große 11229877279SMichael Große /** 11329877279SMichael Große * create the pretext to the actual table rows 11429877279SMichael Große * 115*01dd90deSAndreas Gohr * @param string $mode 11629877279SMichael Große * @param Doku_Renderer $renderer 117*01dd90deSAndreas Gohr * @param array $data the configuration data 118*01dd90deSAndreas Gohr * @param Column[] $cols 11929877279SMichael Große */ 120*01dd90deSAndreas Gohr protected function renderPreTable($mode, Doku_Renderer $renderer, $data, $cols) { 12129877279SMichael Große $this->startScope($mode, $renderer); 122fc8e2563SMichael Große $this->showActiveFilters($mode, $renderer); 12329877279SMichael Große $this->startTable($mode, $renderer); 12429877279SMichael Große $renderer->tablethead_open(); 125*01dd90deSAndreas Gohr $this->buildColumnHeaders($mode, $renderer, $data, $cols); 1261a07b696SMichael Große $this->addDynamicFilters($mode, $renderer, $data); 12729877279SMichael Große $renderer->tablethead_close(); 12829877279SMichael Große } 12929877279SMichael Große 13029877279SMichael Große /** 13129877279SMichael Große * @param array $data 13229877279SMichael Große * @param int $rowcnt 13329877279SMichael Große * 13429877279SMichael Große * @return string 13529877279SMichael Große */ 13629877279SMichael Große private function renderPostTable($mode, Doku_Renderer $renderer, $data, $rowcnt) { 13729877279SMichael Große $this->summarize($mode, $renderer, $data, $this->sums); 13829877279SMichael Große $this->addLimitControls($mode, $renderer, $data, $rowcnt); 13929877279SMichael Große $this->finishTableAndScope($mode, $renderer); 14029877279SMichael Große } 14129877279SMichael Große 14229877279SMichael Große /** 14329877279SMichael Große * if limit was set, add control 14429877279SMichael Große * 145f87e1c10SMichael Große * @param string $mode the mode of the renderer 146f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 147f87e1c10SMichael Große * @param array $data the configuration of the table/search 14829877279SMichael Große * @param $rowcnt 14929877279SMichael Große */ 15029877279SMichael Große protected function addLimitControls($mode, Doku_Renderer $renderer, $data, $rowcnt) { 1511a07b696SMichael Große global $ID; 15229877279SMichael Große 15329877279SMichael Große if($data['limit']) { 15429877279SMichael Große $renderer->tablerow_open(); 15529877279SMichael Große $renderer->tableheader_open((count($data['cols']) + ($data['rownumbers'] ? 1 : 0))); 15629877279SMichael Große $offset = (int) $_REQUEST['dataofs']; 157fc8e2563SMichael Große 158fc8e2563SMichael Große // keep url params 159fc8e2563SMichael Große $params = array(); 1601a07b696SMichael Große if (!empty($data['current_params']['dataflt'])) {$params['dataflt'] = $data['current_params']['dataflt'];} 1611a07b696SMichael Große if (!empty($data['current_params']['datasrt'])) {$params['datasrt'] = $data['current_params']['datasrt'];} 162fc8e2563SMichael Große 16329877279SMichael Große if($offset) { 16429877279SMichael Große $prev = $offset - $data['limit']; 16529877279SMichael Große if($prev < 0) { 16629877279SMichael Große $prev = 0; 16729877279SMichael Große } 16829877279SMichael Große $params['dataofs'] = $prev; 169fc8e2563SMichael Große $renderer->internallink($ID . '?' . http_build_query($params), $this->getLang('prev')); 17029877279SMichael Große } 17129877279SMichael Große 17293485d71SMichael Große if($rowcnt > $offset + $data['limit']) { 17329877279SMichael Große $next = $offset + $data['limit']; 17429877279SMichael Große $params['dataofs'] = $next; 175fc8e2563SMichael Große $renderer->internallink($ID . '?' . http_build_query($params), $this->getLang('next')); 17629877279SMichael Große } 17729877279SMichael Große $renderer->tableheader_close(); 17829877279SMichael Große $renderer->tablerow_close(); 17929877279SMichael Große } 18029877279SMichael Große } 18129877279SMichael Große 18229877279SMichael Große /** 183f87e1c10SMichael Große * @param string $mode the mode of the renderer 184f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 18529877279SMichael Große */ 186fc8e2563SMichael Große protected function showActiveFilters($mode, Doku_Renderer $renderer) { 1871a07b696SMichael Große global $ID; 18829877279SMichael Große 1891a07b696SMichael Große if($mode == 'xhtml' && !empty($data['current_params']['dataflt'])) { 1901a07b696SMichael Große $filters = $data['current_params']['dataflt']; 19129877279SMichael Große $confHelper = $this->loadHelper('struct_config'); 19229877279SMichael Große $fltrs = array(); 19329877279SMichael Große foreach($filters as $colcomp => $filter) { 19429877279SMichael Große $filter = $confHelper->parseFilterLine('', $colcomp.$filter); 19529877279SMichael Große if(strpos($filter[1], '~') !== false) { 19629877279SMichael Große if(strpos($filter[1], '!~') !== false) { 19729877279SMichael Große $comparator_value = '!~' . str_replace('%', '*', $filter[2]); 19829877279SMichael Große } else { 19929877279SMichael Große $comparator_value = '~' . str_replace('%', '', $filter[2]); 20029877279SMichael Große } 20129877279SMichael Große $fltrs[] = $filter[0] . $comparator_value; 20229877279SMichael Große } else { 20329877279SMichael Große $fltrs[] = $filter[0] . $filter[1] . $filter[2]; 20429877279SMichael Große } 20529877279SMichael Große } 20629877279SMichael Große 20729877279SMichael Große $renderer->doc .= '<div class="filter">'; 20829877279SMichael Große $renderer->doc .= '<h4>' . sprintf($this->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>'; 20929877279SMichael Große $renderer->doc .= '<div class="resetfilter">'; 21029877279SMichael Große $renderer->internallink($ID, $this->getLang('tableresetfilter')); 21129877279SMichael Große $renderer->doc .= '</div>'; 21229877279SMichael Große $renderer->doc .= '</div>'; 21329877279SMichael Große } 21429877279SMichael Große } 21529877279SMichael Große 21629877279SMichael Große /** 217f87e1c10SMichael Große * @param string $mode the mode of the renderer 218f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 219f87e1c10SMichael Große * @param array $data the configuration of the table/search 22029877279SMichael Große */ 2211a07b696SMichael Große protected function addDynamicFilters($mode, Doku_Renderer $renderer, $data) { 22229877279SMichael Große if ($mode != 'xhtml') return; 22329877279SMichael Große 22429877279SMichael Große global $conf, $ID; 22529877279SMichael Große 2261a07b696SMichael Große $cur_params = $data['current_params']; 22729877279SMichael Große $html = ''; 22829877279SMichael Große if($data['dynfilters']) { 22929877279SMichael Große $html .= '<tr class="dataflt">'; 23029877279SMichael Große 23129877279SMichael Große if($data['rownumbers']) { 23229877279SMichael Große $html .= '<th></th>'; 23329877279SMichael Große } 23429877279SMichael Große 23529877279SMichael Große foreach($data['headers'] as $num => $head) { 23629877279SMichael Große $html .= '<th>'; 23729877279SMichael Große $form = new Doku_Form(array('method' => 'GET',)); 23829877279SMichael Große $form->_hidden = array(); 23929877279SMichael Große if(!$conf['userewrite']) { 24029877279SMichael Große $form->addHidden('id', $ID); 24129877279SMichael Große } 24229877279SMichael Große 243fc8e2563SMichael Große $key = $data['cols'][$num] . '*~'; 244fc8e2563SMichael Große $val = isset($cur_params['dataflt'][$key]) ? $cur_params['dataflt'][$key] : ''; 24529877279SMichael Große 24629877279SMichael Große // Add current request params 247fc8e2563SMichael Große if (!empty($cur_params['datasrt'])) { 248fc8e2563SMichael Große $form->addHidden('datasrt', $cur_params['datasrt']); 249fc8e2563SMichael Große } 250fc8e2563SMichael Große if (!empty($cur_params['dataofs'])) { 251fc8e2563SMichael Große $form->addHidden('dataofs', $cur_params['dataofs']); 252fc8e2563SMichael Große } 253fc8e2563SMichael Große foreach($cur_params['dataflt'] as $c_key => $c_val) { 25429877279SMichael Große if($c_val !== '' && $c_key !== $key) { 255fc8e2563SMichael Große $form->addHidden('dataflt[' . $c_key . ']', $c_val); 25629877279SMichael Große } 25729877279SMichael Große } 25829877279SMichael Große 259fc8e2563SMichael Große $form->addElement(form_makeField('text', 'dataflt[' . $key . ']', $val, '')); 26029877279SMichael Große $html .= $form->getForm(); 26129877279SMichael Große $html .= '</th>'; 26229877279SMichael Große } 26329877279SMichael Große $html .= '</tr>'; 26429877279SMichael Große $renderer->doc .= $html; 26529877279SMichael Große } 26629877279SMichael Große } 26729877279SMichael Große 26829877279SMichael Große /** 269f87e1c10SMichael Große * @param string $mode the mode of the renderer 270f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 27129877279SMichael Große */ 27229877279SMichael Große private function startTable($mode, Doku_Renderer $renderer) { 27329877279SMichael Große $renderer->table_open(); 27429877279SMichael Große } 27529877279SMichael Große 27629877279SMichael Große /** 277f87e1c10SMichael Große * @param string $mode the mode of the renderer 278f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 279f87e1c10SMichael Große * @param array $data the configuration of the table/search 280*01dd90deSAndreas Gohr * @param Column[] $cols 28129877279SMichael Große */ 282*01dd90deSAndreas Gohr protected function buildColumnHeaders($mode, Doku_Renderer $renderer, $data, $cols) { 28329877279SMichael Große global $ID; 28429877279SMichael Große 28529877279SMichael Große $renderer->tablerow_open(); 28629877279SMichael Große 28729877279SMichael Große if($data['rownumbers']) { 28829877279SMichael Große $renderer->tableheader_open(); 28929877279SMichael Große $renderer->cdata('#'); 29029877279SMichael Große $renderer->tableheader_close(); 29129877279SMichael Große } 29229877279SMichael Große 29329877279SMichael Große foreach($data['headers'] as $num => $head) { 294f87e1c10SMichael Große $ckey = $data['cols'][$num]; 295*01dd90deSAndreas Gohr if(blank($head)) { 296*01dd90deSAndreas Gohr $head = $cols[$num]->getTranslatedLabel(); 297*01dd90deSAndreas Gohr } 29829877279SMichael Große 29929877279SMichael Große $width = ''; 30029877279SMichael Große if(isset($data['widths'][$num]) AND $data['widths'][$num] != '-') { 30129877279SMichael Große $width = ' style="width: ' . $data['widths'][$num] . ';"'; 30229877279SMichael Große } 30329877279SMichael Große if ($mode == 'xhmtl') { 30429877279SMichael Große $renderer->doc .= '<th' . $width . '>'; 30529877279SMichael Große } else { 30629877279SMichael Große $renderer->tableheader_open(); 30729877279SMichael Große } 30829877279SMichael Große 30929877279SMichael Große // add sort arrow 31029877279SMichael Große if ($mode == 'xhtml') { 31129877279SMichael Große if(isset($data['sort']) && $ckey == $data['sort'][0]) { 31229877279SMichael Große if($data['sort'][1] == 'ASC') { 31329877279SMichael Große $renderer->doc .= '<span>↓</span> '; 31429877279SMichael Große $ckey = '^' . $ckey; 31529877279SMichael Große } else { 31629877279SMichael Große $renderer->doc .= '<span>↑</span> '; 31729877279SMichael Große } 31829877279SMichael Große } 31929877279SMichael Große } 3201a07b696SMichael Große $renderer->internallink($ID . "?" . http_build_query(array('datasrt' => $ckey,) + $data['current_params']), hsc($head)); 32129877279SMichael Große $renderer->tableheader_close(); 32229877279SMichael Große } 32329877279SMichael Große $renderer->tablerow_close(); 32429877279SMichael Große } 32529877279SMichael Große 326f87e1c10SMichael Große /** 327f87e1c10SMichael Große * @param string $mode the mode of the renderer 328f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 329f87e1c10SMichael Große */ 33029877279SMichael Große protected function startScope($mode, \Doku_Renderer $renderer) { 33129877279SMichael Große if ($mode == 'xhtml') { 33229877279SMichael Große $renderer->doc .= '<div class="table structaggegation">'; 33329877279SMichael Große } 33429877279SMichael Große } 33529877279SMichael Große 33629877279SMichael Große /** 33729877279SMichael Große * if summarize was set, add sums 33829877279SMichael Große * 339f87e1c10SMichael Große * @param string $mode the mode of the renderer 340f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 341f87e1c10SMichael Große * @param array $data the configuration of the table/search 342f87e1c10SMichael Große * @param array $sums the summarized output of the numerical fields 34329877279SMichael Große */ 34429877279SMichael Große private function summarize($mode, \Doku_Renderer $renderer, $data, $sums) { 34529877279SMichael Große if($data['summarize']) { 34629877279SMichael Große $renderer->tablerow_open(); 34729877279SMichael Große $len = count($data['cols']); 34829877279SMichael Große 34929877279SMichael Große if($data['rownumbers']) { 35029877279SMichael Große $renderer->tablecell_open(); 35129877279SMichael Große $renderer->tablecell_close(); 35229877279SMichael Große } 35329877279SMichael Große 35429877279SMichael Große for($i = 0; $i < $len; $i++) { 35529877279SMichael Große $renderer->tablecell_open(1, $data['align'][$i]); 35629877279SMichael Große if(!empty($sums[$i])) { 35729877279SMichael Große $renderer->cdata('∑ ' . $sums[$i]); 35829877279SMichael Große } else { 35929877279SMichael Große if ($mode == 'xhtml') { 36029877279SMichael Große $renderer->doc .= ' '; 36129877279SMichael Große } 36229877279SMichael Große } 36329877279SMichael Große $renderer->tablecell_close(); 36429877279SMichael Große } 36529877279SMichael Große $renderer->tablerow_close(); 36629877279SMichael Große } 36729877279SMichael Große } 36829877279SMichael Große 36929877279SMichael Große /** 370f87e1c10SMichael Große * @param string $mode the mode of the renderer 371f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 37229877279SMichael Große * 37329877279SMichael Große */ 37429877279SMichael Große private function finishTableAndScope($mode, Doku_Renderer $renderer) { 37529877279SMichael Große $renderer->table_close(); 37629877279SMichael Große if ($mode == 'xhmtl') { 37729877279SMichael Große $renderer->doc .= '</div>'; 37829877279SMichael Große } 37929877279SMichael Große } 38029877279SMichael Große 38129877279SMichael Große /** 382f87e1c10SMichael Große * @param string $mode the mode of the renderer 383f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 384f87e1c10SMichael Große * @param array $data the configuration of the table/search 38529877279SMichael Große * @param $rows 38629877279SMichael Große * 38729877279SMichael Große */ 38829877279SMichael Große private function renderRows($mode, Doku_Renderer $renderer, $data, $rows) { 38929877279SMichael Große $renderer->tabletbody_open(); 39029877279SMichael Große foreach($rows as $rownum => $row) { 39129877279SMichael Große $renderer->tablerow_open(); 39229877279SMichael Große 39329877279SMichael Große if($data['rownumbers']) { 39429877279SMichael Große $renderer->tablecell_open(); 39529877279SMichael Große $renderer->doc .= $rownum + 1; 39629877279SMichael Große $renderer->tablecell_close(); 39729877279SMichael Große } 39829877279SMichael Große 39929877279SMichael Große /** @var plugin\struct\meta\Value $value */ 40029877279SMichael Große foreach($row as $colnum => $value) { 40129877279SMichael Große $renderer->tablecell_open(); 40229877279SMichael Große $value->render($renderer, $mode); 40329877279SMichael Große $renderer->tablecell_close(); 40429877279SMichael Große 40529877279SMichael Große // summarize 40629877279SMichael Große if($data['summarize'] && is_numeric($value->getValue())) { 40729877279SMichael Große if(!isset($this->sums[$colnum])) { 40829877279SMichael Große $this->sums[$colnum] = 0; 40929877279SMichael Große } 41029877279SMichael Große $this->sums[$colnum] += $value->getValue(); 41129877279SMichael Große } 41229877279SMichael Große } 41329877279SMichael Große $renderer->tablerow_close(); 41429877279SMichael Große } 41529877279SMichael Große $renderer->tabletbody_close(); 41629877279SMichael Große } 417f87e1c10SMichael Große 418f87e1c10SMichael Große /** 419f87e1c10SMichael Große * @param array $data the configuration of the table/search 420f87e1c10SMichael Große * @param string $mode the mode of the renderer 421f87e1c10SMichael Große * @param Doku_Renderer $renderer the renderer 422f87e1c10SMichael Große */ 423f87e1c10SMichael Große private function nullList($data, $mode, Doku_Renderer $renderer) { 424f87e1c10SMichael Große $this->renderPreTable($mode, $renderer, $data); 425f87e1c10SMichael Große $renderer->tablerow_open(); 426f87e1c10SMichael Große $renderer->tablecell_open(count($data['cols']) + $data['rownumbers'], 'center'); 427f87e1c10SMichael Große $renderer->cdata($this->getLang('none')); 428f87e1c10SMichael Große $renderer->tablecell_close(); 429f87e1c10SMichael Große $renderer->tablerow_close(); 430f87e1c10SMichael Große $renderer->table_close(); 431f87e1c10SMichael Große } 432549a0837SAndreas Gohr} 433549a0837SAndreas Gohr 434549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 435