xref: /plugin/struct/meta/AggregationTable.php (revision 7fe2cdf28c472c686961bf42f0123eb33d2f3e60)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5use dokuwiki\Extension\Event;
6
7/**
8 * Creates the table aggregation output
9 *
10 * @package dokuwiki\plugin\struct\meta
11 */
12class AggregationTable extends Aggregation
13{
14    /** @var array for summing up columns */
15    protected $sums;
16
17    /** @var string[] the result PIDs for each row */
18    protected $resultPIDs;
19    protected $resultRids;
20    protected $resultRevs;
21
22    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
23    {
24        parent::__construct($id, $mode, $renderer, $searchConfig);
25        $this->resultPIDs = $this->searchConfig->getPids();
26        $this->resultRids = $this->searchConfig->getRids();
27        $this->resultRevs = $this->searchConfig->getRevs();
28    }
29
30    /** @inheritdoc */
31    public function render($showNotFound = false)
32    {
33
34        // abort early if there are no results at all (not filtered)
35        if (!$this->resultCount && !$this->isDynamicallyFiltered() && $showNotFound) {
36            $this->renderer->cdata($this->helper->getLang('none'));
37            return;
38        }
39
40        $this->renderActiveFilters();
41
42        $rendercontext = [
43            'table' => $this,
44            'renderer' => $this->renderer,
45            'format' => $this->mode,
46            'search' => $this->searchConfig,
47            'columns' => $this->columns,
48            'data' => $this->result
49        ];
50
51        $event = new Event(
52            'PLUGIN_STRUCT_RENDER_AGGREGATION_TABLE',
53            $rendercontext
54        );
55        $event->trigger([$this, 'renderTable']);
56
57        // export handle
58        $this->renderExportControls();
59    }
60
61    /**
62     * Render the default aggregation table
63     */
64    public function renderTable($rendercontext)
65    {
66        $this->renderer->table_open();
67
68        // header
69        $this->renderer->tablethead_open();
70        $this->renderColumnHeaders();
71        $this->renderDynamicFilters();
72        $this->renderer->tablethead_close();
73
74        if ($this->resultCount) {
75            // actual data
76            $this->renderer->tabletbody_open();
77            $this->renderResult();
78            $this->renderer->tabletbody_close();
79
80            // footer (tfoot is develonly currently)
81            if (method_exists($this->renderer, 'tabletfoot_open')) $this->renderer->tabletfoot_open();
82            $this->renderSums();
83            $this->renderPagingControls();
84            if (method_exists($this->renderer, 'tabletfoot_close')) $this->renderer->tabletfoot_close();
85        } else {
86            // nothing found
87            $this->renderEmptyResult();
88        }
89
90        // table close
91        $this->renderer->table_close();
92    }
93
94    /**
95     * Adds additional info to document and renderer in XHTML mode
96     *
97     * @see finishScope()
98     */
99    public function startScope()
100    {
101        // unique identifier for this aggregation
102        $this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true));
103
104        parent::startScope();
105    }
106
107    /**
108     * Closes the table and anything opened in startScope()
109     *
110     * @see startScope()
111     */
112    public function finishScope()
113    {
114        // remove identifier from renderer again
115        if (isset($this->renderer->info['struct_table_hash'])) {
116            unset($this->renderer->info['struct_table_hash']);
117        }
118
119        parent::finishScope();
120    }
121
122    /**
123     * Displays info about the currently applied filters
124     */
125    protected function renderActiveFilters()
126    {
127        if ($this->mode != 'xhtml') return;
128        $dynamic = $this->searchConfig->getDynamicParameters();
129        $filters = $dynamic->getFilters();
130        if (!$filters) return;
131
132        $fltrs = [];
133        foreach ($filters as $column => $filter) {
134            [$comp, $value] = $filter;
135
136            // display the filters in a human readable format
137            foreach ($this->columns as $col) {
138                if ($column === $col->getFullQualifiedLabel()) {
139                    $column = $col->getTranslatedLabel();
140                }
141            }
142            $fltrs[] = sprintf('"%s" %s "%s"', $column, $this->helper->getLang("comparator $comp"), $value);
143        }
144
145        $this->renderer->doc .= '<div class="filter">';
146        $this->renderer->doc .= '<h4>' .
147            sprintf(
148                $this->helper->getLang('tablefilteredby'),
149                hsc(implode(' & ', $fltrs))
150            ) .
151            '</h4>';
152        $this->renderer->doc .= '<div class="resetfilter">';
153        $this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter'));
154        $this->renderer->doc .= '</div>';
155        $this->renderer->doc .= '</div>';
156    }
157
158    /**
159     * Shows the column headers with links to sort by column
160     */
161    protected function renderColumnHeaders()
162    {
163        $this->renderer->tablerow_open();
164
165        // additional column for row numbers
166        if (!empty($this->data['rownumbers'])) {
167            $this->renderer->tableheader_open();
168            $this->renderer->cdata('#');
169            $this->renderer->tableheader_close();
170        }
171
172        // show all headers
173        foreach ($this->columns as $num => $column) {
174            $header = '';
175            if (isset($this->data['headers'][$num])) {
176                $header = $this->data['headers'][$num];
177            }
178
179            // use field label if no header was set
180            if (blank($header)) {
181                if (is_a($column, 'dokuwiki\plugin\struct\meta\Column')) {
182                    $header = $column->getTranslatedLabel();
183                } else {
184                    $header = 'column ' . $num; // this should never happen
185                }
186            }
187
188            // simple mode first
189            if ($this->mode != 'xhtml') {
190                $this->renderer->tableheader_open();
191                $this->renderer->cdata($header);
192                $this->renderer->tableheader_close();
193                continue;
194            }
195
196            // still here? create custom header for more flexibility
197
198            // width setting, widths are prevalidated, no escape needed
199            $width = '';
200            if (isset($this->data['widths'][$num]) && $this->data['widths'][$num] != '-') {
201                $width = ' style="min-width: ' . $this->data['widths'][$num] . ';' .
202                    'max-width: ' . $this->data['widths'][$num] . ';"';
203            }
204
205            // prepare data attribute for inline edits
206            if (
207                !is_a($column, '\dokuwiki\plugin\struct\meta\PageColumn') &&
208                !is_a($column, '\dokuwiki\plugin\struct\meta\RevisionColumn')
209            ) {
210                $data = 'data-field="' . hsc($column->getFullQualifiedLabel()) . '"';
211            } else {
212                $data = '';
213            }
214
215            // sort indicator and link
216            $sortclass = '';
217            $sorts = $this->searchConfig->getSorts();
218            $dynamic = $this->searchConfig->getDynamicParameters();
219            $dynamic->setSort($column, true);
220            if (isset($sorts[$column->getFullQualifiedLabel()])) {
221                [/*colname*/, $currentSort] = $sorts[$column->getFullQualifiedLabel()];
222                if ($currentSort) {
223                    $sortclass = 'sort-down';
224                    $dynamic->setSort($column, false);
225                } else {
226                    $sortclass = 'sort-up';
227                }
228            }
229            $link = wl($this->id, $dynamic->getURLParameters());
230
231            // output XHTML header
232            $this->renderer->doc .= "<th $width $data>";
233            $this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" ' .
234                'title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>';
235            $this->renderer->doc .= '</th>';
236        }
237
238        $this->renderer->tablerow_close();
239    }
240
241    /**
242     * Is the result set currently dynamically filtered?
243     * @return bool
244     */
245    protected function isDynamicallyFiltered()
246    {
247        if ($this->mode != 'xhtml') return false;
248        if (!$this->data['dynfilters']) return false;
249
250        $dynamic = $this->searchConfig->getDynamicParameters();
251        return (bool)$dynamic->getFilters();
252    }
253
254    /**
255     * Add input fields for dynamic filtering
256     */
257    protected function renderDynamicFilters()
258    {
259        if ($this->mode != 'xhtml') return;
260        if (empty($this->data['dynfilters'])) return;
261        if (is_a($this->renderer, 'renderer_plugin_dw2pdf')) {
262            return;
263        }
264        global $conf;
265
266        $this->renderer->doc .= '<tr class="dataflt">';
267
268        // add extra column for row numbers
269        if ($this->data['rownumbers']) {
270            $this->renderer->doc .= '<th></th>';
271        }
272
273        // each column gets a form
274        foreach ($this->columns as $column) {
275            $this->renderer->doc .= '<th>';
276
277            // BEGIN FORM
278            $form = new \Doku_Form(['method' => 'GET', 'action' => wl($this->id)]);
279            unset($form->_hidden['sectok']); // we don't need it here
280            if (!$conf['userewrite']) $form->addHidden('id', $this->id);
281
282            // current value
283            $dynamic = $this->searchConfig->getDynamicParameters();
284            $filters = $dynamic->getFilters();
285            if (isset($filters[$column->getFullQualifiedLabel()])) {
286                [, $current] = $filters[$column->getFullQualifiedLabel()];
287                $dynamic->removeFilter($column);
288            } else {
289                $current = '';
290            }
291
292            // Add current request params
293            $params = $dynamic->getURLParameters();
294            foreach ($params as $key => $val) {
295                $form->addHidden($key, $val);
296            }
297
298            // add input field
299            $key = $column->getFullQualifiedLabel() . $column->getType()->getDefaultComparator();
300            $form->addElement(
301                form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, '')
302            );
303            $this->renderer->doc .= $form->getForm();
304            // END FORM
305
306            $this->renderer->doc .= '</th>';
307        }
308        $this->renderer->doc .= '</tr>';
309    }
310
311    /**
312     * Display the actual table data
313     */
314    protected function renderResult()
315    {
316        foreach ($this->result as $rownum => $row) {
317            $data = [
318                'id' => $this->id,
319                'mode' => $this->mode,
320                'renderer' => $this->renderer,
321                'searchConfig' => $this->searchConfig,
322                'data' => $this->data,
323                'rownum' => &$rownum,
324                'row' => &$row
325            ];
326            $evt = new Event('PLUGIN_STRUCT_AGGREGATIONTABLE_RENDERRESULTROW', $data);
327            if ($evt->advise_before()) {
328                $this->renderResultRow($rownum, $row);
329            }
330            $evt->advise_after();
331        }
332    }
333
334    /**
335     * Render a single result row
336     *
337     * @param int $rownum
338     * @param array $row
339     */
340    protected function renderResultRow($rownum, $row)
341    {
342        $this->renderer->tablerow_open();
343
344        // add data attribute for inline edit
345        if ($this->mode == 'xhtml') {
346            $pid = $this->resultPIDs[$rownum];
347            $rid = $this->resultRids[$rownum];
348            $rev = $this->resultRevs[$rownum];
349            $this->renderer->doc = substr(rtrim($this->renderer->doc), 0, -1); // remove closing '>'
350            $this->renderer->doc .= ' data-pid="' . hsc($pid) . '" data-rev="' . $rev . '" data-rid="' . $rid . '">';
351        }
352
353        // row number column
354        if (!empty($this->data['rownumbers'])) {
355            $this->renderer->tablecell_open();
356            $this->renderer->cdata($rownum + $this->searchConfig->getOffset() + 1);
357            $this->renderer->tablecell_close();
358        }
359
360        /** @var Value $value */
361        foreach ($row as $colnum => $value) {
362            $align = $this->data['align'][$colnum] ?? null;
363            $this->renderer->tablecell_open(1, $align);
364            $value->render($this->renderer, $this->mode);
365            $this->renderer->tablecell_close();
366
367            // summarize
368            if (!empty($this->data['summarize']) && is_numeric($value->getValue())) {
369                if (!isset($this->sums[$colnum])) {
370                    $this->sums[$colnum] = 0;
371                }
372                $this->sums[$colnum] += $value->getValue();
373            }
374        }
375        $this->renderer->tablerow_close();
376    }
377
378    /**
379     * Renders an information row for when no results were found
380     */
381    protected function renderEmptyResult()
382    {
383        $this->renderer->tablerow_open();
384        $this->renderer->tablecell_open(count($this->columns) + $this->data['rownumbers'], 'center');
385        $this->renderer->cdata($this->helper->getLang('none'));
386        $this->renderer->tablecell_close();
387        $this->renderer->tablerow_close();
388    }
389
390    /**
391     * Add sums if wanted
392     */
393    protected function renderSums()
394    {
395        if (empty($this->data['summarize'])) return;
396
397        $this->renderer->info['struct_table_meta'] = true;
398        if ($this->mode == 'xhtml') {
399            /** @noinspection PhpMethodParametersCountMismatchInspection */
400            $this->renderer->tablerow_open('summarize');
401        } else {
402            $this->renderer->tablerow_open();
403        }
404
405        if ($this->data['rownumbers']) {
406            $this->renderer->tableheader_open();
407            $this->renderer->tableheader_close();
408        }
409
410        $len = count($this->columns);
411        for ($i = 0; $i < $len; $i++) {
412            $this->renderer->tableheader_open(1, $this->data['align'][$i]);
413            if (!empty($this->sums[$i])) {
414                $this->renderer->cdata('∑ ');
415                $this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode);
416            } elseif ($this->mode == 'xhtml') {
417                $this->renderer->doc .= '&nbsp;';
418            }
419            $this->renderer->tableheader_close();
420        }
421        $this->renderer->tablerow_close();
422        $this->renderer->info['struct_table_meta'] = false;
423    }
424
425    /**
426     * Adds paging controls to the table
427     */
428    protected function renderPagingControls()
429    {
430        if ($this->mode != 'xhtml') return;
431
432        $limit = $this->searchConfig->getLimit();
433        if (!$limit) return;
434        $offset = $this->searchConfig->getOffset();
435
436        $this->renderer->info['struct_table_meta'] = true;
437        $this->renderer->tablerow_open();
438        $this->renderer->tableheader_open((count($this->columns) + ($this->data['rownumbers'] ? 1 : 0)));
439
440
441        // prev link
442        if ($offset) {
443            $prev = $offset - $limit;
444            if ($prev < 0) {
445                $prev = 0;
446            }
447
448            $dynamic = $this->searchConfig->getDynamicParameters();
449            $dynamic->setOffset($prev);
450            $link = wl($this->id, $dynamic->getURLParameters());
451            $this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>';
452        }
453
454        // next link
455        if ($this->resultCount > $offset + $limit) {
456            $next = $offset + $limit;
457            $dynamic = $this->searchConfig->getDynamicParameters();
458            $dynamic->setOffset($next);
459            $link = wl($this->id, $dynamic->getURLParameters());
460            $this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>';
461        }
462
463        $this->renderer->tableheader_close();
464        $this->renderer->tablerow_close();
465        $this->renderer->info['struct_table_meta'] = true;
466    }
467
468    /**
469     * Adds CSV export controls
470     */
471    protected function renderExportControls()
472    {
473        if ($this->mode != 'xhtml') return;
474        if (empty($this->data['csv'])) return;
475        if (!$this->resultCount) return;
476
477        $dynamic = $this->searchConfig->getDynamicParameters();
478        $params = $dynamic->getURLParameters();
479        $params['hash'] = $this->renderer->info['struct_table_hash'];
480
481        // FIXME apply dynamic filters
482        $link = exportlink($this->id, 'struct_csv', $params);
483
484        $this->renderer->doc .= '<a href="' . $link . '" class="export mediafile mf_csv">' .
485            $this->helper->getLang('csvexport') . '</a>';
486    }
487}
488