xref: /plugin/struct/meta/AggregationTable.php (revision 34ea6e1056993bf65c34d15d1eb9730d4dfba9af)
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 */
10d6d97f60SAnna Dabrowskaclass AggregationTable
11d6d97f60SAnna Dabrowska{
1207993756SAndreas Gohr
1307993756SAndreas Gohr    /**
1407993756SAndreas Gohr     * @var string the page id of the page this is rendered to
1507993756SAndreas Gohr     */
1607993756SAndreas Gohr    protected $id;
1707993756SAndreas Gohr    /**
1807993756SAndreas Gohr     * @var string the Type of renderer used
1907993756SAndreas Gohr     */
2007993756SAndreas Gohr    protected $mode;
2107993756SAndreas Gohr    /**
2207993756SAndreas Gohr     * @var \Doku_Renderer the DokuWiki renderer used to create the output
2307993756SAndreas Gohr     */
2407993756SAndreas Gohr    protected $renderer;
2507993756SAndreas Gohr    /**
2607993756SAndreas Gohr     * @var SearchConfig the configured search - gives access to columns etc.
2707993756SAndreas Gohr     */
2807993756SAndreas Gohr    protected $searchConfig;
2907993756SAndreas Gohr
3007993756SAndreas Gohr    /**
3107993756SAndreas Gohr     * @var Column[] the list of columns to be displayed
3207993756SAndreas Gohr     */
3307993756SAndreas Gohr    protected $columns;
3407993756SAndreas Gohr
3507993756SAndreas Gohr    /**
3607993756SAndreas Gohr     * @var  Value[][] the search result
3707993756SAndreas Gohr     */
3807993756SAndreas Gohr    protected $result;
3907993756SAndreas Gohr
4007993756SAndreas Gohr    /**
4107993756SAndreas Gohr     * @var int number of all results
4207993756SAndreas Gohr     */
4307993756SAndreas Gohr    protected $resultCount;
4407993756SAndreas Gohr
4507993756SAndreas Gohr    /**
46d4b5a17cSAndreas Gohr     * @var string[] the result PIDs for each row
47d4b5a17cSAndreas Gohr     */
48d4b5a17cSAndreas Gohr    protected $resultPIDs;
490ceefd5cSAnna Dabrowska    protected $resultRids;
506fd73b4bSAnna Dabrowska    protected $resultRevs;
51d4b5a17cSAndreas Gohr
52d4b5a17cSAndreas Gohr    /**
5307993756SAndreas Gohr     * @var array for summing up columns
5407993756SAndreas Gohr     */
5507993756SAndreas Gohr    protected $sums;
5607993756SAndreas Gohr
5707993756SAndreas Gohr    /**
586b5e52fdSAndreas Gohr     * @var bool skip full table when no results found
596b5e52fdSAndreas Gohr     */
606b5e52fdSAndreas Gohr    protected $simplenone = true;
616b5e52fdSAndreas Gohr
626b5e52fdSAndreas Gohr    /**
6307993756SAndreas Gohr     * @todo we might be able to get rid of this helper and move this to SearchConfig
6407993756SAndreas Gohr     * @var \helper_plugin_struct_config
6507993756SAndreas Gohr     */
6607993756SAndreas Gohr    protected $helper;
6707993756SAndreas Gohr
6807993756SAndreas Gohr    /**
6907993756SAndreas Gohr     * Initialize the Aggregation renderer and executes the search
7007993756SAndreas Gohr     *
7107993756SAndreas Gohr     * You need to call @see render() on the resulting object.
7207993756SAndreas Gohr     *
7307993756SAndreas Gohr     * @param string $id
7407993756SAndreas Gohr     * @param string $mode
7507993756SAndreas Gohr     * @param \Doku_Renderer $renderer
7607993756SAndreas Gohr     * @param SearchConfig $searchConfig
7707993756SAndreas Gohr     */
788ce43f5aSAnna Dabrowska    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
79d6d97f60SAnna Dabrowska    {
8007993756SAndreas Gohr        $this->id = $id;
8107993756SAndreas Gohr        $this->mode = $mode;
8207993756SAndreas Gohr        $this->renderer = $renderer;
8307993756SAndreas Gohr        $this->searchConfig = $searchConfig;
8407993756SAndreas Gohr        $this->data = $searchConfig->getConf();
8507993756SAndreas Gohr        $this->columns = $searchConfig->getColumns();
8607993756SAndreas Gohr
878ce43f5aSAnna Dabrowska        $this->result = $this->searchConfig->execute();
8807993756SAndreas Gohr        $this->resultCount = $this->searchConfig->getCount();
89d4b5a17cSAndreas Gohr        $this->resultPIDs = $this->searchConfig->getPids();
900ceefd5cSAnna Dabrowska        $this->resultRids = $this->searchConfig->getRids();
916fd73b4bSAnna Dabrowska        $this->resultRevs = $this->searchConfig->getRevs();
9207993756SAndreas Gohr        $this->helper = plugin_load('helper', 'struct_config');
9307993756SAndreas Gohr    }
9407993756SAndreas Gohr
9507993756SAndreas Gohr    /**
9607993756SAndreas Gohr     * Create the table on the renderer
9707993756SAndreas Gohr     */
98d6d97f60SAnna Dabrowska    public function render()
99d6d97f60SAnna Dabrowska    {
100b7e1d73bSAndreas Gohr
101b7e1d73bSAndreas Gohr        // abort early if there are no results at all (not filtered)
1026b5e52fdSAndreas Gohr        if (!$this->resultCount && !$this->isDynamicallyFiltered() && $this->simplenone) {
103b7e1d73bSAndreas Gohr            $this->startScope();
104b7e1d73bSAndreas Gohr            $this->renderer->cdata($this->helper->getLang('none'));
105b7e1d73bSAndreas Gohr            $this->finishScope();
106b7e1d73bSAndreas Gohr            return;
107b7e1d73bSAndreas Gohr        }
108b7e1d73bSAndreas Gohr
10907993756SAndreas Gohr        // table open
11007993756SAndreas Gohr        $this->startScope();
111986ab7e6SAndreas Gohr        $this->renderActiveFilters();
11207993756SAndreas Gohr        $this->renderer->table_open();
11307993756SAndreas Gohr
11407993756SAndreas Gohr        // header
11507993756SAndreas Gohr        $this->renderer->tablethead_open();
116986ab7e6SAndreas Gohr        $this->renderColumnHeaders();
117986ab7e6SAndreas Gohr        $this->renderDynamicFilters();
11807993756SAndreas Gohr        $this->renderer->tablethead_close();
11907993756SAndreas Gohr
12007993756SAndreas Gohr        if ($this->resultCount) {
12107993756SAndreas Gohr            // actual data
122a9fd81f9SAndreas Gohr            $this->renderer->tabletbody_open();
123986ab7e6SAndreas Gohr            $this->renderResult();
124a9fd81f9SAndreas Gohr            $this->renderer->tabletbody_close();
12507993756SAndreas Gohr
126a9fd81f9SAndreas Gohr            // footer (tfoot is develonly currently)
127a9fd81f9SAndreas Gohr            if (method_exists($this->renderer, 'tabletfoot_open')) $this->renderer->tabletfoot_open();
128986ab7e6SAndreas Gohr            $this->renderSums();
129986ab7e6SAndreas Gohr            $this->renderPagingControls();
130a9fd81f9SAndreas Gohr            if (method_exists($this->renderer, 'tabletfoot_close')) $this->renderer->tabletfoot_close();
13107993756SAndreas Gohr        } else {
13207993756SAndreas Gohr            // nothing found
133986ab7e6SAndreas Gohr            $this->renderEmptyResult();
13407993756SAndreas Gohr        }
13507993756SAndreas Gohr
13607993756SAndreas Gohr        // table close
13707993756SAndreas Gohr        $this->renderer->table_close();
13809dd691aSAndreas Gohr
13909dd691aSAndreas Gohr        // export handle
14009dd691aSAndreas Gohr        $this->renderExportControls();
14107993756SAndreas Gohr        $this->finishScope();
14207993756SAndreas Gohr    }
14307993756SAndreas Gohr
14407993756SAndreas Gohr    /**
14507993756SAndreas Gohr     * Adds additional info to document and renderer in XHTML mode
14607993756SAndreas Gohr     *
14707993756SAndreas Gohr     * @see finishScope()
14807993756SAndreas Gohr     */
149d6d97f60SAnna Dabrowska    protected function startScope()
150d6d97f60SAnna Dabrowska    {
15107993756SAndreas Gohr        // unique identifier for this aggregation
15207993756SAndreas Gohr        $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true));
15309dd691aSAndreas Gohr
15409dd691aSAndreas Gohr        // wrapping div
15509dd691aSAndreas Gohr        if ($this->mode != 'xhtml') return;
15609dd691aSAndreas Gohr        $this->renderer->doc .= "<div class=\"structaggregation\">";
15707993756SAndreas Gohr    }
15807993756SAndreas Gohr
15907993756SAndreas Gohr    /**
16007993756SAndreas Gohr     * Closes the table and anything opened in startScope()
16107993756SAndreas Gohr     *
16207993756SAndreas Gohr     * @see startScope()
16307993756SAndreas Gohr     */
164d6d97f60SAnna Dabrowska    protected function finishScope()
165d6d97f60SAnna Dabrowska    {
16607993756SAndreas Gohr        // remove identifier from renderer again
16707993756SAndreas Gohr        if (isset($this->renderer->info['struct_table_hash'])) {
16807993756SAndreas Gohr            unset($this->renderer->info['struct_table_hash']);
16907993756SAndreas Gohr        }
17009dd691aSAndreas Gohr
17109dd691aSAndreas Gohr        // wrapping div
17209dd691aSAndreas Gohr        if ($this->mode != 'xhtml') return;
17309dd691aSAndreas Gohr        $this->renderer->doc .= '</div>';
17407993756SAndreas Gohr    }
17507993756SAndreas Gohr
17607993756SAndreas Gohr    /**
17707993756SAndreas Gohr     * Displays info about the currently applied filters
17807993756SAndreas Gohr     */
179d6d97f60SAnna Dabrowska    protected function renderActiveFilters()
180d6d97f60SAnna Dabrowska    {
18107993756SAndreas Gohr        if ($this->mode != 'xhtml') return;
18207993756SAndreas Gohr        $dynamic = $this->searchConfig->getDynamicParameters();
18307993756SAndreas Gohr        $filters = $dynamic->getFilters();
18407993756SAndreas Gohr        if (!$filters) return;
18507993756SAndreas Gohr
18607993756SAndreas Gohr        $fltrs = array();
18707993756SAndreas Gohr        foreach ($filters as $column => $filter) {
18807993756SAndreas Gohr            list($comp, $value) = $filter;
18904ec4785SAnna Dabrowska
19004ec4785SAnna Dabrowska            // display the filters in a human readable format
19104ec4785SAnna Dabrowska            foreach ($this->columns as $col) {
19204ec4785SAnna Dabrowska                if ($column === $col->getFullQualifiedLabel()) {
19304ec4785SAnna Dabrowska                    $column = $col->getTranslatedLabel();
19404ec4785SAnna Dabrowska                }
19504ec4785SAnna Dabrowska            }
1961f075418SAnna Dabrowska            $fltrs[] = sprintf('"%s" %s "%s"', $column, $this->helper->getLang("comparator $comp"), $value);
19707993756SAndreas Gohr        }
19807993756SAndreas Gohr
19907993756SAndreas Gohr        $this->renderer->doc .= '<div class="filter">';
20007993756SAndreas Gohr        $this->renderer->doc .= '<h4>' . sprintf($this->helper->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>';
20107993756SAndreas Gohr        $this->renderer->doc .= '<div class="resetfilter">';
20207993756SAndreas Gohr        $this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter'));
20307993756SAndreas Gohr        $this->renderer->doc .= '</div>';
20407993756SAndreas Gohr        $this->renderer->doc .= '</div>';
20507993756SAndreas Gohr    }
20607993756SAndreas Gohr
20707993756SAndreas Gohr    /**
20807993756SAndreas Gohr     * Shows the column headers with links to sort by column
20907993756SAndreas Gohr     */
210d6d97f60SAnna Dabrowska    protected function renderColumnHeaders()
211d6d97f60SAnna Dabrowska    {
21207993756SAndreas Gohr        $this->renderer->tablerow_open();
21307993756SAndreas Gohr
21407993756SAndreas Gohr        // additional column for row numbers
215*34ea6e10SAnna Dabrowska        if (!empty($this->data['rownumbers'])) {
21607993756SAndreas Gohr            $this->renderer->tableheader_open();
21707993756SAndreas Gohr            $this->renderer->cdata('#');
21807993756SAndreas Gohr            $this->renderer->tableheader_close();
21907993756SAndreas Gohr        }
22007993756SAndreas Gohr
22107993756SAndreas Gohr        // show all headers
2228c4ee9beSAndreas Gohr        foreach ($this->columns as $num => $column) {
2238c4ee9beSAndreas Gohr            $header = '';
2248c4ee9beSAndreas Gohr            if (isset($this->data['headers'][$num])) {
2258c4ee9beSAndreas Gohr                $header = $this->data['headers'][$num];
2268c4ee9beSAndreas Gohr            }
22707993756SAndreas Gohr
22807993756SAndreas Gohr            // use field label if no header was set
22907993756SAndreas Gohr            if (blank($header)) {
23001f8b845SAndreas Gohr                if (is_a($column, 'dokuwiki\plugin\struct\meta\Column')) {
23107993756SAndreas Gohr                    $header = $column->getTranslatedLabel();
23207993756SAndreas Gohr                } else {
23307993756SAndreas Gohr                    $header = 'column ' . $num; // this should never happen
23407993756SAndreas Gohr                }
23507993756SAndreas Gohr            }
23607993756SAndreas Gohr
23707993756SAndreas Gohr            // simple mode first
23807993756SAndreas Gohr            if ($this->mode != 'xhtml') {
23907993756SAndreas Gohr                $this->renderer->tableheader_open();
24007993756SAndreas Gohr                $this->renderer->cdata($header);
24107993756SAndreas Gohr                $this->renderer->tableheader_close();
24207993756SAndreas Gohr                continue;
24307993756SAndreas Gohr            }
24407993756SAndreas Gohr
24507993756SAndreas Gohr            // still here? create custom header for more flexibility
24607993756SAndreas Gohr
2479113d04aSAndreas Gohr            // width setting, widths are prevalidated, no escape needed
24807993756SAndreas Gohr            $width = '';
2499113d04aSAndreas Gohr            if (isset($this->data['widths'][$num]) && $this->data['widths'][$num] != '-') {
2509113d04aSAndreas Gohr                $width = ' style="min-width: ' . $this->data['widths'][$num] . ';' .
2519113d04aSAndreas Gohr                         'max-width: ' . $this->data['widths'][$num] . ';"';
25207993756SAndreas Gohr            }
25307993756SAndreas Gohr
254d4b5a17cSAndreas Gohr            // prepare data attribute for inline edits
255d6d97f60SAnna Dabrowska            if (
256d6d97f60SAnna Dabrowska                !is_a($column, '\dokuwiki\plugin\struct\meta\PageColumn') &&
257d4b5a17cSAndreas Gohr                !is_a($column, '\dokuwiki\plugin\struct\meta\RevisionColumn')
258d4b5a17cSAndreas Gohr            ) {
259d4b5a17cSAndreas Gohr                $data = 'data-field="' . hsc($column->getFullQualifiedLabel()) . '"';
260d4b5a17cSAndreas Gohr            } else {
261d4b5a17cSAndreas Gohr                $data = '';
262d4b5a17cSAndreas Gohr            }
263d4b5a17cSAndreas Gohr
26407993756SAndreas Gohr            // sort indicator and link
26507993756SAndreas Gohr            $sortclass = '';
26607993756SAndreas Gohr            $sorts = $this->searchConfig->getSorts();
26707993756SAndreas Gohr            $dynamic = $this->searchConfig->getDynamicParameters();
268aa124708SAndreas Gohr            $dynamic->setSort($column, true);
26907993756SAndreas Gohr            if (isset($sorts[$column->getFullQualifiedLabel()])) {
270aa124708SAndreas Gohr                list(/*colname*/, $currentSort) = $sorts[$column->getFullQualifiedLabel()];
271aa124708SAndreas Gohr                if ($currentSort) {
27207993756SAndreas Gohr                    $sortclass = 'sort-down';
27307993756SAndreas Gohr                    $dynamic->setSort($column, false);
27407993756SAndreas Gohr                } else {
27507993756SAndreas Gohr                    $sortclass = 'sort-up';
27607993756SAndreas Gohr                }
27707993756SAndreas Gohr            }
27807993756SAndreas Gohr            $link = wl($this->id, $dynamic->getURLParameters());
27907993756SAndreas Gohr
28007993756SAndreas Gohr            // output XHTML header
281d4b5a17cSAndreas Gohr            $this->renderer->doc .= "<th $width $data>";
28207993756SAndreas Gohr            $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>';
28307993756SAndreas Gohr            $this->renderer->doc .= '</th>';
28407993756SAndreas Gohr        }
28507993756SAndreas Gohr
28607993756SAndreas Gohr        $this->renderer->tablerow_close();
28707993756SAndreas Gohr    }
28807993756SAndreas Gohr
28907993756SAndreas Gohr    /**
290b7e1d73bSAndreas Gohr     * Is the result set currently dynamically filtered?
291b7e1d73bSAndreas Gohr     * @return bool
292b7e1d73bSAndreas Gohr     */
293d6d97f60SAnna Dabrowska    protected function isDynamicallyFiltered()
294d6d97f60SAnna Dabrowska    {
295b7e1d73bSAndreas Gohr        if ($this->mode != 'xhtml') return false;
296b7e1d73bSAndreas Gohr        if (!$this->data['dynfilters']) return false;
297b7e1d73bSAndreas Gohr
298b7e1d73bSAndreas Gohr        $dynamic = $this->searchConfig->getDynamicParameters();
299b7e1d73bSAndreas Gohr        return (bool) $dynamic->getFilters();
300b7e1d73bSAndreas Gohr    }
301b7e1d73bSAndreas Gohr
302b7e1d73bSAndreas Gohr    /**
30307993756SAndreas Gohr     * Add input fields for dynamic filtering
30407993756SAndreas Gohr     */
305d6d97f60SAnna Dabrowska    protected function renderDynamicFilters()
306d6d97f60SAnna Dabrowska    {
30707993756SAndreas Gohr        if ($this->mode != 'xhtml') return;
30807993756SAndreas Gohr        if (!$this->data['dynfilters']) return;
3091bc467a4SMichael Grosse        if (is_a($this->renderer, 'renderer_plugin_dw2pdf')) {
310e6ae02ecSMichael Grosse            return;
311e6ae02ecSMichael Grosse        }
3121bc467a4SMichael Grosse        global $conf;
31307993756SAndreas Gohr
31407993756SAndreas Gohr        $this->renderer->doc .= '<tr class="dataflt">';
31507993756SAndreas Gohr
31607993756SAndreas Gohr        // add extra column for row numbers
31707993756SAndreas Gohr        if ($this->data['rownumbers']) {
31807993756SAndreas Gohr            $this->renderer->doc .= '<th></th>';
31907993756SAndreas Gohr        }
32007993756SAndreas Gohr
32107993756SAndreas Gohr        // each column gets a form
32207993756SAndreas Gohr        foreach ($this->columns as $column) {
32307993756SAndreas Gohr            $this->renderer->doc .= '<th>';
32407993756SAndreas Gohr            {
32507993756SAndreas Gohr                $form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id)));
32676195677SAndreas Gohr                unset($form->_hidden['sectok']); // we don't need it here
32776195677SAndreas Gohr                if (!$conf['userewrite']) $form->addHidden('id', $this->id);
32807993756SAndreas Gohr
32907993756SAndreas Gohr                // current value
33007993756SAndreas Gohr                $dynamic = $this->searchConfig->getDynamicParameters();
33107993756SAndreas Gohr                $filters = $dynamic->getFilters();
33207993756SAndreas Gohr            if (isset($filters[$column->getFullQualifiedLabel()])) {
33307993756SAndreas Gohr                list(, $current) = $filters[$column->getFullQualifiedLabel()];
33407993756SAndreas Gohr                $dynamic->removeFilter($column);
33507993756SAndreas Gohr            } else {
33607993756SAndreas Gohr                $current = '';
33707993756SAndreas Gohr            }
33807993756SAndreas Gohr
33907993756SAndreas Gohr                // Add current request params
34007993756SAndreas Gohr                $params = $dynamic->getURLParameters();
34107993756SAndreas Gohr            foreach ($params as $key => $val) {
34207993756SAndreas Gohr                $form->addHidden($key, $val);
34307993756SAndreas Gohr            }
34407993756SAndreas Gohr
34507993756SAndreas Gohr                // add input field
346db9b8745SAndreas Gohr                $key = $column->getFullQualifiedLabel() . $column->getType()->getDefaultComparator();
347d60f71efSAndreas Gohr                $form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, ''));
34807993756SAndreas Gohr                $this->renderer->doc .= $form->getForm();
34907993756SAndreas Gohr            }
35007993756SAndreas Gohr            $this->renderer->doc .= '</th>';
35107993756SAndreas Gohr        }
35207993756SAndreas Gohr        $this->renderer->doc .= '</tr>';
35307993756SAndreas Gohr    }
35407993756SAndreas Gohr
35507993756SAndreas Gohr    /**
35607993756SAndreas Gohr     * Display the actual table data
35707993756SAndreas Gohr     */
358d6d97f60SAnna Dabrowska    protected function renderResult()
359d6d97f60SAnna Dabrowska    {
36007993756SAndreas Gohr        foreach ($this->result as $rownum => $row) {
36147eb8cceSSzymon Olewniczak            $data = array(
36247eb8cceSSzymon Olewniczak                'id' => $this->id,
36347eb8cceSSzymon Olewniczak                'mode' => $this->mode,
36447eb8cceSSzymon Olewniczak                'renderer' => $this->renderer,
36547eb8cceSSzymon Olewniczak                'searchConfig' => $this->searchConfig,
36647eb8cceSSzymon Olewniczak                'data' => $this->data,
36747eb8cceSSzymon Olewniczak                'rownum' => &$rownum,
36847eb8cceSSzymon Olewniczak                'row' => &$row,
36947eb8cceSSzymon Olewniczak            );
37047eb8cceSSzymon Olewniczak            $evt = new \Doku_Event('PLUGIN_STRUCT_AGGREGATIONTABLE_RENDERRESULTROW', $data);
37147eb8cceSSzymon Olewniczak            if ($evt->advise_before()) {
372f107f479SAndreas Gohr                $this->renderResultRow($rownum, $row);
373f107f479SAndreas Gohr            }
37447eb8cceSSzymon Olewniczak            $evt->advise_after();
37547eb8cceSSzymon Olewniczak        }
376f107f479SAndreas Gohr    }
377f107f479SAndreas Gohr
378f107f479SAndreas Gohr    /**
379f107f479SAndreas Gohr     * Render a single result row
380f107f479SAndreas Gohr     *
381f107f479SAndreas Gohr     * @param int $rownum
382f107f479SAndreas Gohr     * @param array $row
383f107f479SAndreas Gohr     */
384d6d97f60SAnna Dabrowska    protected function renderResultRow($rownum, $row)
385d6d97f60SAnna Dabrowska    {
38607993756SAndreas Gohr        $this->renderer->tablerow_open();
38707993756SAndreas Gohr
388d4b5a17cSAndreas Gohr        // add data attribute for inline edit
389d4b5a17cSAndreas Gohr        if ($this->mode == 'xhtml') {
390d4b5a17cSAndreas Gohr            $pid = $this->resultPIDs[$rownum];
3910ceefd5cSAnna Dabrowska            $rid = $this->resultRids[$rownum];
3926fd73b4bSAnna Dabrowska            $rev = $this->resultRevs[$rownum];
393d4b5a17cSAndreas Gohr            $this->renderer->doc = substr(rtrim($this->renderer->doc), 0, -1); // remove closing '>'
3946fd73b4bSAnna Dabrowska            $this->renderer->doc .= ' data-pid="' . hsc($pid) . '" data-rev="' . $rev . '" data-rid="' . $rid . '">';
395d4b5a17cSAndreas Gohr        }
396d4b5a17cSAndreas Gohr
39707993756SAndreas Gohr        // row number column
398*34ea6e10SAnna Dabrowska        if (!empty($this->data['rownumbers'])) {
39907993756SAndreas Gohr            $this->renderer->tablecell_open();
4003215aebfSSzymon Olewniczak            $searchConfigConf = $this->searchConfig->getConf();
4013215aebfSSzymon Olewniczak            $this->renderer->cdata($rownum + $searchConfigConf['offset'] + 1);
40207993756SAndreas Gohr            $this->renderer->tablecell_close();
40307993756SAndreas Gohr        }
40407993756SAndreas Gohr
40507993756SAndreas Gohr        /** @var Value $value */
40607993756SAndreas Gohr        foreach ($row as $colnum => $value) {
407*34ea6e10SAnna Dabrowska            $align = isset( $this->data['align'][$colnum]) ?  $this->data['align'][$colnum] : null;
408*34ea6e10SAnna Dabrowska            $this->renderer->tablecell_open(1, $align);
40907993756SAndreas Gohr            $value->render($this->renderer, $this->mode);
41007993756SAndreas Gohr            $this->renderer->tablecell_close();
41107993756SAndreas Gohr
41207993756SAndreas Gohr            // summarize
41307993756SAndreas Gohr            if ($this->data['summarize'] && is_numeric($value->getValue())) {
41407993756SAndreas Gohr                if (!isset($this->sums[$colnum])) {
41507993756SAndreas Gohr                    $this->sums[$colnum] = 0;
41607993756SAndreas Gohr                }
41707993756SAndreas Gohr                $this->sums[$colnum] += $value->getValue();
41807993756SAndreas Gohr            }
41907993756SAndreas Gohr        }
42007993756SAndreas Gohr        $this->renderer->tablerow_close();
42107993756SAndreas Gohr    }
42207993756SAndreas Gohr
42307993756SAndreas Gohr    /**
42407993756SAndreas Gohr     * Renders an information row for when no results were found
42507993756SAndreas Gohr     */
426d6d97f60SAnna Dabrowska    protected function renderEmptyResult()
427d6d97f60SAnna Dabrowska    {
42807993756SAndreas Gohr        $this->renderer->tablerow_open();
42970cf6339SAndreas Gohr        $this->renderer->tablecell_open(count($this->columns) + $this->data['rownumbers'], 'center');
43007993756SAndreas Gohr        $this->renderer->cdata($this->helper->getLang('none'));
43107993756SAndreas Gohr        $this->renderer->tablecell_close();
43207993756SAndreas Gohr        $this->renderer->tablerow_close();
43307993756SAndreas Gohr    }
43407993756SAndreas Gohr
43507993756SAndreas Gohr    /**
43607993756SAndreas Gohr     * Add sums if wanted
43707993756SAndreas Gohr     */
438d6d97f60SAnna Dabrowska    protected function renderSums()
439d6d97f60SAnna Dabrowska    {
440d18090e8SAndreas Gohr        if (empty($this->data['summarize'])) return;
44107993756SAndreas Gohr
442a0bf8bb2SAndreas Gohr        $this->renderer->info['struct_table_meta'] = true;
4438925ba29SAndreas Gohr        if ($this->mode == 'xhtml') {
4448925ba29SAndreas Gohr            /** @noinspection PhpMethodParametersCountMismatchInspection */
4458925ba29SAndreas Gohr            $this->renderer->tablerow_open('summarize');
4468925ba29SAndreas Gohr        } else {
44707993756SAndreas Gohr            $this->renderer->tablerow_open();
4488925ba29SAndreas Gohr        }
44907993756SAndreas Gohr
45007993756SAndreas Gohr        if ($this->data['rownumbers']) {
4518925ba29SAndreas Gohr            $this->renderer->tableheader_open();
4528925ba29SAndreas Gohr            $this->renderer->tableheader_close();
45307993756SAndreas Gohr        }
45407993756SAndreas Gohr
455aee4116bSAndreas Gohr        $len = count($this->columns);
45607993756SAndreas Gohr        for ($i = 0; $i < $len; $i++) {
4578925ba29SAndreas Gohr            $this->renderer->tableheader_open(1, $this->data['align'][$i]);
458aee4116bSAndreas Gohr            if (!empty($this->sums[$i])) {
4599b97e610SAndreas Gohr                $this->renderer->cdata('∑ ');
4609b97e610SAndreas Gohr                $this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode);
46107993756SAndreas Gohr            } else {
46207993756SAndreas Gohr                if ($this->mode == 'xhtml') {
46307993756SAndreas Gohr                    $this->renderer->doc .= '&nbsp;';
46407993756SAndreas Gohr                }
46507993756SAndreas Gohr            }
4668925ba29SAndreas Gohr            $this->renderer->tableheader_close();
46707993756SAndreas Gohr        }
46807993756SAndreas Gohr        $this->renderer->tablerow_close();
469a0bf8bb2SAndreas Gohr        $this->renderer->info['struct_table_meta'] = false;
47007993756SAndreas Gohr    }
47107993756SAndreas Gohr
47207993756SAndreas Gohr    /**
473986ab7e6SAndreas Gohr     * Adds paging controls to the table
47407993756SAndreas Gohr     */
475d6d97f60SAnna Dabrowska    protected function renderPagingControls()
476d6d97f60SAnna Dabrowska    {
47707993756SAndreas Gohr        if (empty($this->data['limit'])) return;
478a0bf8bb2SAndreas Gohr        if ($this->mode != 'xhtml') return;
47907993756SAndreas Gohr
480a0bf8bb2SAndreas Gohr        $this->renderer->info['struct_table_meta'] = true;
48107993756SAndreas Gohr        $this->renderer->tablerow_open();
4824bc1074dSMichael Grosse        $this->renderer->tableheader_open((count($this->columns) + ($this->data['rownumbers'] ? 1 : 0)));
48307993756SAndreas Gohr        $offset = $this->data['offset'];
48407993756SAndreas Gohr
48507993756SAndreas Gohr        // prev link
48607993756SAndreas Gohr        if ($offset) {
48707993756SAndreas Gohr            $prev = $offset - $this->data['limit'];
48807993756SAndreas Gohr            if ($prev < 0) {
48907993756SAndreas Gohr                $prev = 0;
49007993756SAndreas Gohr            }
49107993756SAndreas Gohr
49207993756SAndreas Gohr            $dynamic = $this->searchConfig->getDynamicParameters();
49307993756SAndreas Gohr            $dynamic->setOffset($prev);
49407993756SAndreas Gohr            $link = wl($this->id, $dynamic->getURLParameters());
49507993756SAndreas Gohr            $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>';
49607993756SAndreas Gohr        }
49707993756SAndreas Gohr
49807993756SAndreas Gohr        // next link
49907993756SAndreas Gohr        if ($this->resultCount > $offset + $this->data['limit']) {
50007993756SAndreas Gohr            $next = $offset + $this->data['limit'];
50107993756SAndreas Gohr            $dynamic = $this->searchConfig->getDynamicParameters();
50207993756SAndreas Gohr            $dynamic->setOffset($next);
50307993756SAndreas Gohr            $link = wl($this->id, $dynamic->getURLParameters());
50407993756SAndreas Gohr            $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>';
50507993756SAndreas Gohr        }
50607993756SAndreas Gohr
50707993756SAndreas Gohr        $this->renderer->tableheader_close();
50807993756SAndreas Gohr        $this->renderer->tablerow_close();
509a0bf8bb2SAndreas Gohr        $this->renderer->info['struct_table_meta'] = true;
51007993756SAndreas Gohr    }
51109dd691aSAndreas Gohr
51209dd691aSAndreas Gohr    /**
51309dd691aSAndreas Gohr     * Adds CSV export controls
51409dd691aSAndreas Gohr     */
515d6d97f60SAnna Dabrowska    protected function renderExportControls()
516d6d97f60SAnna Dabrowska    {
51709dd691aSAndreas Gohr        if ($this->mode != 'xhtml') return;
5187b240ca8SAndreas Gohr        if (empty($this->data['csv'])) return;
51909dd691aSAndreas Gohr        if (!$this->resultCount) return;
52009dd691aSAndreas Gohr
521c8ccdaf8SAndreas Gohr        $dynamic = $this->searchConfig->getDynamicParameters();
522c8ccdaf8SAndreas Gohr        $params = $dynamic->getURLParameters();
523c8ccdaf8SAndreas Gohr        $params['hash'] = $this->renderer->info['struct_table_hash'];
524c8ccdaf8SAndreas Gohr
52509dd691aSAndreas Gohr        // FIXME apply dynamic filters
526eafc109fSAndreas Gohr        $link = exportlink($this->id, 'struct_csv', $params);
52709dd691aSAndreas Gohr
52809dd691aSAndreas Gohr        $this->renderer->doc .= '<a href="' . $link . '" class="export mediafile mf_csv">' . $this->helper->getLang('csvexport') . '</a>';
52909dd691aSAndreas Gohr    }
53007993756SAndreas Gohr}
531