xref: /plugin/struct/meta/AggregationFilter.php (revision bea7239dedd8acf3a331707c77c76579fb97cfbb)
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                $key = $column->getFullQualifiedLabel() . '=';
36                $filter = SearchConfigParameters::$PARAM_FILTER . '[' . urlencode($key) . ']=' . urlencode($displayValue);
37                $column->getType()->renderTagCloudLink($value, $this->renderer, $this->mode, $this->id, $filter, 100);
38                $this->renderer->doc .= '</div></li>';
39            }
40            $this->renderer->doc .= '</ul>';
41            $this->renderer->doc .= '</details>';
42        }
43    }
44
45    /**
46     * Get all values from given search result grouped by column
47     *
48     * @return array
49     */
50    protected function getAllColumnValues($result)
51    {
52        $colValues = [];
53
54        foreach ($result as $row) {
55            foreach ($row as $value) {
56                /** @var Value $value */
57                $colName = $value->getColumn()->getFullQualifiedLabel();
58                $colValues[$colName]['column'] = $value->getColumn();
59                $colValues[$colName]['label'] = $value->getColumn()->getTranslatedLabel();
60                $colValues[$colName]['values'] = $colValues[$colName]['values'] ?? [];
61
62                if (empty($value->getDisplayValue())) continue;
63
64                // create an array with [value => displayValue] pairs
65                // the cast to array will handle single and multi-value fields the same
66                // using the full value as key will make sure we don't have duplicates
67                $pairs = array_combine((array)$value->getValue(), (array)$value->getDisplayValue());
68                $colValues[$colName]['values'] = array_merge($colValues[$colName]['values'], $pairs);
69            }
70        }
71
72        // sort by display value
73        array_walk($colValues, function (&$col) {
74            Sort::asort($col['values']);
75        });
76
77        return array_values($colValues); // reindex
78    }
79}
80