xref: /plugin/struct/meta/SearchConfigParameters.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
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 */
10*d6d97f60SAnna Dabrowskaclass SearchConfigParameters
11*d6d97f60SAnna Dabrowska{
1200f6af48SAndreas Gohr
1300f6af48SAndreas Gohr    /** @var string parameter name to pass filters */
14b2b64eb3SAndreas Gohr    public static $PARAM_FILTER = 'flt';
1500f6af48SAndreas Gohr    /** @var string parameter name to pass offset */
16b2b64eb3SAndreas Gohr    public static $PARAM_OFFSET = 'ofs';
1700f6af48SAndreas Gohr    /** @var string parameter name to pass srt */
18b2b64eb3SAndreas Gohr    public static $PARAM_SORT = 'srt';
1900f6af48SAndreas Gohr
2000f6af48SAndreas Gohr    /** @var SearchConfig */
2100f6af48SAndreas Gohr    protected $searchConfig;
2200f6af48SAndreas Gohr
2300f6af48SAndreas Gohr    /** @var null|array */
2400f6af48SAndreas Gohr    protected $sort = null;
2500f6af48SAndreas Gohr    /** @var int */
2600f6af48SAndreas Gohr    protected $offset = 0;
2700f6af48SAndreas Gohr    /** @var  array */
2800f6af48SAndreas Gohr    protected $filters = array();
2900f6af48SAndreas Gohr
3000f6af48SAndreas Gohr    /**
3100f6af48SAndreas Gohr     * Initializes the dynamic parameters from $INPUT
3200f6af48SAndreas Gohr     *
3300f6af48SAndreas Gohr     * @param SearchConfig $searchConfig
3400f6af48SAndreas Gohr     */
35*d6d97f60SAnna Dabrowska    public function __construct(SearchConfig $searchConfig)
36*d6d97f60SAnna Dabrowska    {
3700f6af48SAndreas Gohr        global $INPUT;
3800f6af48SAndreas Gohr        $this->searchConfig = $searchConfig;
3900f6af48SAndreas Gohr        /** @var \helper_plugin_struct_config $confHlp */
4000f6af48SAndreas Gohr        $confHlp = plugin_load('helper', 'struct_config');
4100f6af48SAndreas Gohr
4200f6af48SAndreas Gohr        if ($INPUT->has(self::$PARAM_SORT)) {
4300f6af48SAndreas Gohr            list($colname, $sort) = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT));
44aa124708SAndreas Gohr            $this->setSort($colname, $sort);
4500f6af48SAndreas Gohr        }
4600f6af48SAndreas Gohr
4700f6af48SAndreas Gohr        if ($INPUT->has(self::$PARAM_FILTER)) {
4800f6af48SAndreas Gohr            foreach ($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) {
4900f6af48SAndreas Gohr                list($colname, $comp, $value,) = $confHlp->parseFilterLine('AND', $colcomp . $filter);
50b2b64eb3SAndreas Gohr                $this->addFilter($colname, $comp, $value);
5100f6af48SAndreas Gohr            }
5200f6af48SAndreas Gohr        }
5300f6af48SAndreas Gohr
5400f6af48SAndreas Gohr        if ($INPUT->has(self::$PARAM_OFFSET)) {
5500f6af48SAndreas Gohr            $this->setOffset($INPUT->int(self::$PARAM_OFFSET));
5600f6af48SAndreas Gohr        }
5700f6af48SAndreas Gohr    }
5800f6af48SAndreas Gohr
5900f6af48SAndreas Gohr    /**
6000f6af48SAndreas Gohr     * Returns the full qualified name for a given column
6100f6af48SAndreas Gohr     *
6200f6af48SAndreas Gohr     * @param string|Column $column
6300f6af48SAndreas Gohr     * @return bool|string
6400f6af48SAndreas Gohr     */
65*d6d97f60SAnna Dabrowska    protected function resolveColumn($column)
66*d6d97f60SAnna Dabrowska    {
67ba766201SAndreas Gohr        if (!is_a($column, '\dokuwiki\plugin\struct\meta\Column')) {
6800f6af48SAndreas Gohr            $column = $this->searchConfig->findColumn($column);
6900f6af48SAndreas Gohr            if (!$column) return false;
7000f6af48SAndreas Gohr        }
7100f6af48SAndreas Gohr        /** @var Column $column */
7200f6af48SAndreas Gohr        return $column->getFullQualifiedLabel();
7300f6af48SAndreas Gohr    }
7400f6af48SAndreas Gohr
7500f6af48SAndreas Gohr    /**
7600f6af48SAndreas Gohr     * Sets the sorting column
7700f6af48SAndreas Gohr     *
7800f6af48SAndreas Gohr     * @param string|Column $column
7900f6af48SAndreas Gohr     * @param bool $asc
8000f6af48SAndreas Gohr     */
81*d6d97f60SAnna Dabrowska    public function setSort($column, $asc = true)
82*d6d97f60SAnna Dabrowska    {
8300f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
8400f6af48SAndreas Gohr        if (!$column) return;
8500f6af48SAndreas Gohr        $this->sort = array($column, $asc);
8600f6af48SAndreas Gohr    }
8700f6af48SAndreas Gohr
8800f6af48SAndreas Gohr    /**
8900f6af48SAndreas Gohr     * Remove the sorting column
9000f6af48SAndreas Gohr     */
91*d6d97f60SAnna Dabrowska    public function removeSort()
92*d6d97f60SAnna Dabrowska    {
9300f6af48SAndreas Gohr        $this->sort = null;
9400f6af48SAndreas Gohr    }
9500f6af48SAndreas Gohr
9600f6af48SAndreas Gohr    /**
9700f6af48SAndreas Gohr     * Set the offset
9800f6af48SAndreas Gohr     *
9900f6af48SAndreas Gohr     * @param int $offset
10000f6af48SAndreas Gohr     */
101*d6d97f60SAnna Dabrowska    public function setOffset($offset)
102*d6d97f60SAnna Dabrowska    {
10300f6af48SAndreas Gohr        $this->offset = $offset;
10400f6af48SAndreas Gohr    }
10500f6af48SAndreas Gohr
10600f6af48SAndreas Gohr    /**
10700f6af48SAndreas Gohr     * Removes the offset
10800f6af48SAndreas Gohr     */
109*d6d97f60SAnna Dabrowska    public function removeOffset()
110*d6d97f60SAnna Dabrowska    {
11100f6af48SAndreas Gohr        $this->offset = 0;
11200f6af48SAndreas Gohr    }
11300f6af48SAndreas Gohr
11400f6af48SAndreas Gohr    /**
11500f6af48SAndreas Gohr     * Adds another filter
11600f6af48SAndreas Gohr     *
11707993756SAndreas Gohr     * When there is a filter for that column already, the new filter overwrites it. Setting a
11807993756SAndreas Gohr     * blank value is the same as calling @see removeFilter()
11907993756SAndreas Gohr     *
12000f6af48SAndreas Gohr     * @param string|Column $column
12100f6af48SAndreas Gohr     * @param string $comp the comparator
12200f6af48SAndreas Gohr     * @param string $value the value to compare against
12300f6af48SAndreas Gohr     */
124*d6d97f60SAnna Dabrowska    public function addFilter($column, $comp, $value)
125*d6d97f60SAnna Dabrowska    {
12600f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
12700f6af48SAndreas Gohr        if (!$column) return;
12800f6af48SAndreas Gohr
12907993756SAndreas Gohr        if (trim($value) === '') {
13007993756SAndreas Gohr            $this->removeFilter($column);
13107993756SAndreas Gohr        } else {
13200f6af48SAndreas Gohr            $this->filters[$column] = array($comp, $value);
13300f6af48SAndreas Gohr        }
13407993756SAndreas Gohr    }
13500f6af48SAndreas Gohr
13600f6af48SAndreas Gohr    /**
13700f6af48SAndreas Gohr     * Removes the filter for the given column
13800f6af48SAndreas Gohr     *
13900f6af48SAndreas Gohr     * @param $column
14000f6af48SAndreas Gohr     */
141*d6d97f60SAnna Dabrowska    public function removeFilter($column)
142*d6d97f60SAnna Dabrowska    {
14300f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
14400f6af48SAndreas Gohr        if (!$column) return;
14500f6af48SAndreas Gohr        if (isset($this->filters[$column])) unset($this->filters[$column]);
14600f6af48SAndreas Gohr    }
14700f6af48SAndreas Gohr
14800f6af48SAndreas Gohr    /**
14900f6af48SAndreas Gohr     * Remove all filter
15000f6af48SAndreas Gohr     */
151*d6d97f60SAnna Dabrowska    public function clearFilters()
152*d6d97f60SAnna Dabrowska    {
15300f6af48SAndreas Gohr        $this->filters = array();
15400f6af48SAndreas Gohr    }
15500f6af48SAndreas Gohr
15600f6af48SAndreas Gohr    /**
15707993756SAndreas Gohr     * @return array the current filters
15807993756SAndreas Gohr     */
159*d6d97f60SAnna Dabrowska    public function getFilters()
160*d6d97f60SAnna Dabrowska    {
16107993756SAndreas Gohr        return $this->filters;
16207993756SAndreas Gohr    }
16307993756SAndreas Gohr
16407993756SAndreas Gohr    /**
165d60f71efSAndreas Gohr     * Get the current parameters
166d60f71efSAndreas Gohr     *
167d60f71efSAndreas Gohr     * It creates a flat key value in a form that can be used to
168d60f71efSAndreas Gohr     * create URLs or Form parameters
169d60f71efSAndreas Gohr     *
170d60f71efSAndreas Gohr     *
171d60f71efSAndreas Gohr     * @return array
17200f6af48SAndreas Gohr     */
173*d6d97f60SAnna Dabrowska    public function getURLParameters()
174*d6d97f60SAnna Dabrowska    {
17500f6af48SAndreas Gohr        $params = array();
17600f6af48SAndreas Gohr        if ($this->offset) {
17700f6af48SAndreas Gohr            $params[self::$PARAM_OFFSET] = $this->offset;
17800f6af48SAndreas Gohr        }
17900f6af48SAndreas Gohr
18000f6af48SAndreas Gohr        if ($this->sort) {
18100f6af48SAndreas Gohr            list($column, $asc) = $this->sort;
18200f6af48SAndreas Gohr            if (!$asc) $column = "^$column";
18300f6af48SAndreas Gohr            $params[self::$PARAM_SORT] = $column;
18400f6af48SAndreas Gohr        }
18500f6af48SAndreas Gohr
18600f6af48SAndreas Gohr        if ($this->filters) {
18700f6af48SAndreas Gohr            foreach ($this->filters as $column => $filter) {
18800f6af48SAndreas Gohr                list($comp, $value) = $filter;
189d60f71efSAndreas Gohr                $key = self::$PARAM_FILTER . '[' . $column . $comp . ']';
190d60f71efSAndreas Gohr                $params[$key] = $value;
19100f6af48SAndreas Gohr            }
19200f6af48SAndreas Gohr        }
19300f6af48SAndreas Gohr
19400f6af48SAndreas Gohr        return $params;
19500f6af48SAndreas Gohr    }
19600f6af48SAndreas Gohr
19700f6af48SAndreas Gohr    /**
19800f6af48SAndreas Gohr     * Updates the given config array with the values currently set
19900f6af48SAndreas Gohr     *
20000f6af48SAndreas Gohr     * This should only be called once at the initialization
20100f6af48SAndreas Gohr     *
20200f6af48SAndreas Gohr     * @param array $config
20300f6af48SAndreas Gohr     * @return array
20400f6af48SAndreas Gohr     */
205*d6d97f60SAnna Dabrowska    public function updateConfig($config)
206*d6d97f60SAnna Dabrowska    {
20700f6af48SAndreas Gohr        if ($this->offset) {
20800f6af48SAndreas Gohr            $config['offset'] = $this->offset;
20900f6af48SAndreas Gohr        }
21000f6af48SAndreas Gohr
21100f6af48SAndreas Gohr        if ($this->sort) {
21200f6af48SAndreas Gohr            list($column, $asc) = $this->sort;
21300f6af48SAndreas Gohr            $config['sort'] = array(
21400f6af48SAndreas Gohr                array($column, $asc)
21500f6af48SAndreas Gohr            );
21600f6af48SAndreas Gohr        }
21700f6af48SAndreas Gohr
21800f6af48SAndreas Gohr        if ($this->filters) {
21900f6af48SAndreas Gohr            if (empty($config['filter'])) $config['filter'] = array();
22000f6af48SAndreas Gohr            foreach ($this->filters as $column => $filter) {
22100f6af48SAndreas Gohr                list($comp, $value) = $filter;
22200f6af48SAndreas Gohr                $config['filter'][] = array($column, $comp, $value, 'AND');
22300f6af48SAndreas Gohr            }
22400f6af48SAndreas Gohr        }
22500f6af48SAndreas Gohr
22600f6af48SAndreas Gohr        return $config;
22700f6af48SAndreas Gohr    }
22800f6af48SAndreas Gohr}
229