xref: /plugin/struct/meta/SearchConfigParameters.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
100f6af48SAndreas Gohr<?php
200f6af48SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
400f6af48SAndreas Gohr
5b2b64eb3SAndreas Gohr/**
6b2b64eb3SAndreas Gohr * Manage dynamic parameters for aggregations
7b2b64eb3SAndreas Gohr *
8ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
9b2b64eb3SAndreas Gohr */
10d6d97f60SAnna Dabrowskaclass SearchConfigParameters
11d6d97f60SAnna Dabrowska{
1200f6af48SAndreas Gohr    /** @var string parameter name to pass filters */
13b2b64eb3SAndreas Gohr    public static $PARAM_FILTER = 'flt';
1400f6af48SAndreas Gohr    /** @var string parameter name to pass offset */
15b2b64eb3SAndreas Gohr    public static $PARAM_OFFSET = 'ofs';
1600f6af48SAndreas Gohr    /** @var string parameter name to pass srt */
17b2b64eb3SAndreas Gohr    public static $PARAM_SORT = 'srt';
1800f6af48SAndreas Gohr
1900f6af48SAndreas Gohr    /** @var SearchConfig */
2000f6af48SAndreas Gohr    protected $searchConfig;
2100f6af48SAndreas Gohr
2200f6af48SAndreas Gohr    /** @var null|array */
23*7234bfb1Ssplitbrain    protected $sort;
2400f6af48SAndreas Gohr    /** @var int */
2500f6af48SAndreas Gohr    protected $offset = 0;
2600f6af48SAndreas Gohr    /** @var  array */
27*7234bfb1Ssplitbrain    protected $filters = [];
2800f6af48SAndreas Gohr
2900f6af48SAndreas Gohr    /**
3000f6af48SAndreas Gohr     * Initializes the dynamic parameters from $INPUT
3100f6af48SAndreas Gohr     *
3200f6af48SAndreas Gohr     * @param SearchConfig $searchConfig
3300f6af48SAndreas Gohr     */
34d6d97f60SAnna Dabrowska    public function __construct(SearchConfig $searchConfig)
35d6d97f60SAnna Dabrowska    {
3600f6af48SAndreas Gohr        global $INPUT;
3700f6af48SAndreas Gohr        $this->searchConfig = $searchConfig;
3800f6af48SAndreas Gohr        /** @var \helper_plugin_struct_config $confHlp */
3900f6af48SAndreas Gohr        $confHlp = plugin_load('helper', 'struct_config');
4000f6af48SAndreas Gohr
4100f6af48SAndreas Gohr        if ($INPUT->has(self::$PARAM_SORT)) {
42*7234bfb1Ssplitbrain            [$colname, $sort] = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT));
43aa124708SAndreas Gohr            $this->setSort($colname, $sort);
4400f6af48SAndreas Gohr        }
4500f6af48SAndreas Gohr
4600f6af48SAndreas Gohr        if ($INPUT->has(self::$PARAM_FILTER)) {
4700f6af48SAndreas Gohr            foreach ($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) {
48*7234bfb1Ssplitbrain                [$colname, $comp, $value, ] = $confHlp->parseFilterLine('AND', $colcomp . $filter);
49b2b64eb3SAndreas Gohr                $this->addFilter($colname, $comp, $value);
5000f6af48SAndreas Gohr            }
5100f6af48SAndreas Gohr        }
5200f6af48SAndreas Gohr
5300f6af48SAndreas Gohr        if ($INPUT->has(self::$PARAM_OFFSET)) {
5400f6af48SAndreas Gohr            $this->setOffset($INPUT->int(self::$PARAM_OFFSET));
5500f6af48SAndreas Gohr        }
5600f6af48SAndreas Gohr    }
5700f6af48SAndreas Gohr
5800f6af48SAndreas Gohr    /**
5900f6af48SAndreas Gohr     * Returns the full qualified name for a given column
6000f6af48SAndreas Gohr     *
6100f6af48SAndreas Gohr     * @param string|Column $column
62b3a9db22SAndreas Gohr     * @return false|string
6300f6af48SAndreas Gohr     */
64d6d97f60SAnna Dabrowska    protected function resolveColumn($column)
65d6d97f60SAnna Dabrowska    {
66b3a9db22SAndreas Gohr        if (!is_a($column, Column::class)) {
6700f6af48SAndreas Gohr            $column = $this->searchConfig->findColumn($column);
6800f6af48SAndreas Gohr            if (!$column) return false;
6900f6af48SAndreas Gohr        }
7000f6af48SAndreas Gohr        /** @var Column $column */
7100f6af48SAndreas Gohr        return $column->getFullQualifiedLabel();
7200f6af48SAndreas Gohr    }
7300f6af48SAndreas Gohr
7400f6af48SAndreas Gohr    /**
7500f6af48SAndreas Gohr     * Sets the sorting column
7600f6af48SAndreas Gohr     *
7700f6af48SAndreas Gohr     * @param string|Column $column
7800f6af48SAndreas Gohr     * @param bool $asc
7900f6af48SAndreas Gohr     */
80d6d97f60SAnna Dabrowska    public function setSort($column, $asc = true)
81d6d97f60SAnna Dabrowska    {
8200f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
8300f6af48SAndreas Gohr        if (!$column) return;
84*7234bfb1Ssplitbrain        $this->sort = [$column, $asc];
8500f6af48SAndreas Gohr    }
8600f6af48SAndreas Gohr
8700f6af48SAndreas Gohr    /**
8800f6af48SAndreas Gohr     * Remove the sorting column
8900f6af48SAndreas Gohr     */
90d6d97f60SAnna Dabrowska    public function removeSort()
91d6d97f60SAnna Dabrowska    {
9200f6af48SAndreas Gohr        $this->sort = null;
9300f6af48SAndreas Gohr    }
9400f6af48SAndreas Gohr
9500f6af48SAndreas Gohr    /**
9600f6af48SAndreas Gohr     * Set the offset
9700f6af48SAndreas Gohr     *
9800f6af48SAndreas Gohr     * @param int $offset
9900f6af48SAndreas Gohr     */
100d6d97f60SAnna Dabrowska    public function setOffset($offset)
101d6d97f60SAnna Dabrowska    {
10200f6af48SAndreas Gohr        $this->offset = $offset;
10300f6af48SAndreas Gohr    }
10400f6af48SAndreas Gohr
10500f6af48SAndreas Gohr    /**
10600f6af48SAndreas Gohr     * Removes the offset
10700f6af48SAndreas Gohr     */
108d6d97f60SAnna Dabrowska    public function removeOffset()
109d6d97f60SAnna Dabrowska    {
11000f6af48SAndreas Gohr        $this->offset = 0;
11100f6af48SAndreas Gohr    }
11200f6af48SAndreas Gohr
11300f6af48SAndreas Gohr    /**
11400f6af48SAndreas Gohr     * Adds another filter
11500f6af48SAndreas Gohr     *
11607993756SAndreas Gohr     * When there is a filter for that column already, the new filter overwrites it. Setting a
117b3a9db22SAndreas Gohr     * blank value is the same as calling removeFilter()
118b3a9db22SAndreas Gohr     *
119b3a9db22SAndreas Gohr     * @param string|Column $column
12000f6af48SAndreas Gohr     * @param string $comp the comparator
12100f6af48SAndreas Gohr     * @param string $value the value to compare against
12200f6af48SAndreas Gohr     */
123d6d97f60SAnna Dabrowska    public function addFilter($column, $comp, $value)
124d6d97f60SAnna Dabrowska    {
12500f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
12600f6af48SAndreas Gohr        if (!$column) return;
12700f6af48SAndreas Gohr
12807993756SAndreas Gohr        if (trim($value) === '') {
12907993756SAndreas Gohr            $this->removeFilter($column);
13007993756SAndreas Gohr        } else {
131*7234bfb1Ssplitbrain            $this->filters[$column] = [$comp, $value];
13200f6af48SAndreas Gohr        }
13307993756SAndreas Gohr    }
13400f6af48SAndreas Gohr
13500f6af48SAndreas Gohr    /**
13600f6af48SAndreas Gohr     * Removes the filter for the given column
13700f6af48SAndreas Gohr     *
13800f6af48SAndreas Gohr     * @param $column
13900f6af48SAndreas Gohr     */
140d6d97f60SAnna Dabrowska    public function removeFilter($column)
141d6d97f60SAnna Dabrowska    {
14200f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
14300f6af48SAndreas Gohr        if (!$column) return;
14400f6af48SAndreas Gohr        if (isset($this->filters[$column])) unset($this->filters[$column]);
14500f6af48SAndreas Gohr    }
14600f6af48SAndreas Gohr
14700f6af48SAndreas Gohr    /**
14800f6af48SAndreas Gohr     * Remove all filter
14900f6af48SAndreas Gohr     */
150d6d97f60SAnna Dabrowska    public function clearFilters()
151d6d97f60SAnna Dabrowska    {
152*7234bfb1Ssplitbrain        $this->filters = [];
15300f6af48SAndreas Gohr    }
15400f6af48SAndreas Gohr
15500f6af48SAndreas Gohr    /**
15607993756SAndreas Gohr     * @return array the current filters
15707993756SAndreas Gohr     */
158d6d97f60SAnna Dabrowska    public function getFilters()
159d6d97f60SAnna Dabrowska    {
16007993756SAndreas Gohr        return $this->filters;
16107993756SAndreas Gohr    }
16207993756SAndreas Gohr
16307993756SAndreas Gohr    /**
164d60f71efSAndreas Gohr     * Get the current parameters
165d60f71efSAndreas Gohr     *
166d60f71efSAndreas Gohr     * It creates a flat key value in a form that can be used to
167d60f71efSAndreas Gohr     * create URLs or Form parameters
168d60f71efSAndreas Gohr     *
169d60f71efSAndreas Gohr     *
170d60f71efSAndreas Gohr     * @return array
17100f6af48SAndreas Gohr     */
172d6d97f60SAnna Dabrowska    public function getURLParameters()
173d6d97f60SAnna Dabrowska    {
174*7234bfb1Ssplitbrain        $params = [];
17500f6af48SAndreas Gohr        if ($this->offset) {
17600f6af48SAndreas Gohr            $params[self::$PARAM_OFFSET] = $this->offset;
17700f6af48SAndreas Gohr        }
17800f6af48SAndreas Gohr
17900f6af48SAndreas Gohr        if ($this->sort) {
180*7234bfb1Ssplitbrain            [$column, $asc] = $this->sort;
18100f6af48SAndreas Gohr            if (!$asc) $column = "^$column";
18200f6af48SAndreas Gohr            $params[self::$PARAM_SORT] = $column;
18300f6af48SAndreas Gohr        }
18400f6af48SAndreas Gohr
18500f6af48SAndreas Gohr        if ($this->filters) {
18600f6af48SAndreas Gohr            foreach ($this->filters as $column => $filter) {
187*7234bfb1Ssplitbrain                [$comp, $value] = $filter;
188d60f71efSAndreas Gohr                $key = self::$PARAM_FILTER . '[' . $column . $comp . ']';
189d60f71efSAndreas Gohr                $params[$key] = $value;
19000f6af48SAndreas Gohr            }
19100f6af48SAndreas Gohr        }
19200f6af48SAndreas Gohr
19300f6af48SAndreas Gohr        return $params;
19400f6af48SAndreas Gohr    }
19500f6af48SAndreas Gohr
19600f6af48SAndreas Gohr    /**
197b3a9db22SAndreas Gohr     * Applies the dynamic filter settings to the SearchConfig
19800f6af48SAndreas Gohr     */
199b3a9db22SAndreas Gohr    public function apply()
200d6d97f60SAnna Dabrowska    {
20100f6af48SAndreas Gohr        if ($this->offset) {
202b3a9db22SAndreas Gohr            $this->searchConfig->setOffset($this->offset);
20300f6af48SAndreas Gohr        }
20400f6af48SAndreas Gohr
20500f6af48SAndreas Gohr        if ($this->sort) {
206b3a9db22SAndreas Gohr            $this->searchConfig->clearSort(); // remove any existing sort
207b3a9db22SAndreas Gohr            $this->searchConfig->addSort($this->sort[0], $this->sort[1]);
20800f6af48SAndreas Gohr        }
20900f6af48SAndreas Gohr
210fd9c77d3SAndreas Gohr        foreach ($this->filters as $colName => $filter) {
211*7234bfb1Ssplitbrain            [$comp, $value] = $filter;
212fd9c77d3SAndreas Gohr            $this->searchConfig->addDynamicFilter($colName, $value, $comp, 'AND');
21300f6af48SAndreas Gohr        }
21400f6af48SAndreas Gohr    }
21500f6af48SAndreas Gohr}
216