xref: /plugin/struct/meta/AggregationFilter.php (revision fd9c77d37616e4134cc6c576afca1ff9ca472368)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5use dokuwiki\Form\Form;
6use dokuwiki\Utf8\Sort;
7
8/**
9 * Struct filter class
10 */
11class AggregationFilter extends Aggregation
12{
13    /**
14     * Render the filter form.
15     * Reuses the structure of advanced search tools to leverage
16     * the core grouping styles and scripts.
17     *
18     * @param bool $showNotFound Inherited from parent method
19     * @return void
20     */
21    public function render($showNotFound = false)
22    {
23        $colValues = $this->getAllColumnValues($this->result);
24
25        // column dropdowns
26        foreach ($colValues as $num => $colData) {
27            /** @var Column $column */
28            $column = $colData['column'];
29
30            $this->renderer->doc .= '<details>';
31            $this->renderer->doc .= '<summary>' . hsc($colData['label']) . '</summary>';
32            $this->renderer->doc .= '<ul>';
33            foreach ($colData['values'] as $value => $displayValue) {
34                $this->renderer->doc .= '<li><div class="li">';
35
36                $dyn = $this->searchConfig->getDynamicParameters();
37                $dyn->addFilter($column, '=', $displayValue);
38                $params = $dyn->getURLParameters();
39                $filter = buildURLparams($params);
40
41                #$key = $column->getFullQualifiedLabel() . '=';
42                #$filter = SearchConfigParameters::$PARAM_FILTER . '[' . urlencode($key) . ']=' . urlencode($displayValue);
43                $column->getType()->renderTagCloudLink($value, $this->renderer, $this->mode, $this->id, $filter, 100);
44                $this->renderer->doc .= '</div></li>';
45            }
46            $this->renderer->doc .= '</ul>';
47            $this->renderer->doc .= '</details>';
48        }
49    }
50
51    /**
52     * Get all values from given search result grouped by column
53     *
54     * @return array
55     */
56    protected function getAllColumnValues($result)
57    {
58        $colValues = [];
59
60        foreach ($result as $row) {
61            foreach ($row as $value) {
62                /** @var Value $value */
63                $colName = $value->getColumn()->getFullQualifiedLabel();
64                $colValues[$colName]['column'] = $value->getColumn();
65                $colValues[$colName]['label'] = $value->getColumn()->getTranslatedLabel();
66                $colValues[$colName]['values'] = $colValues[$colName]['values'] ?? [];
67
68                if (empty($value->getDisplayValue())) continue;
69
70                // create an array with [value => displayValue] pairs
71                // the cast to array will handle single and multi-value fields the same
72                // using the full value as key will make sure we don't have duplicates
73                $pairs = array_combine((array)$value->getValue(), (array)$value->getDisplayValue());
74                $colValues[$colName]['values'] = array_merge($colValues[$colName]['values'], $pairs);
75            }
76        }
77
78        // sort by display value
79        array_walk($colValues, function (&$col) {
80            Sort::asort($col['values']);
81        });
82
83        return array_values($colValues); // reindex
84    }
85}
86