xref: /plugin/struct/meta/SearchConfigParameters.php (revision b2b64eb33ee2aa7ee31e7192be7b680c709298a3)
100f6af48SAndreas Gohr<?php
200f6af48SAndreas Gohr
300f6af48SAndreas Gohrnamespace plugin\struct\meta;
400f6af48SAndreas Gohr
5*b2b64eb3SAndreas Gohr/**
6*b2b64eb3SAndreas Gohr * Manage dynamic parameters for aggregations
7*b2b64eb3SAndreas Gohr *
8*b2b64eb3SAndreas Gohr * @package plugin\struct\meta
9*b2b64eb3SAndreas Gohr */
1000f6af48SAndreas Gohrclass SearchConfigParameters {
1100f6af48SAndreas Gohr
1200f6af48SAndreas Gohr    /** @var string parameter name to pass filters */
13*b2b64eb3SAndreas Gohr    public static $PARAM_FILTER = 'flt';
1400f6af48SAndreas Gohr    /** @var string parameter name to pass offset */
15*b2b64eb3SAndreas Gohr    public static $PARAM_OFFSET = 'ofs';
1600f6af48SAndreas Gohr    /** @var string parameter name to pass srt */
17*b2b64eb3SAndreas Gohr    public static $PARAM_SORT = 'srt';
1800f6af48SAndreas Gohr
1900f6af48SAndreas Gohr    /** @var SearchConfig */
2000f6af48SAndreas Gohr    protected $searchConfig;
2100f6af48SAndreas Gohr
2200f6af48SAndreas Gohr    /** @var null|array */
2300f6af48SAndreas Gohr    protected $sort = null;
2400f6af48SAndreas Gohr    /** @var int */
2500f6af48SAndreas Gohr    protected $offset = 0;
2600f6af48SAndreas Gohr    /** @var  array */
2700f6af48SAndreas Gohr    protected $filters = array();
2800f6af48SAndreas Gohr
2900f6af48SAndreas Gohr    /**
3000f6af48SAndreas Gohr     * Initializes the dynamic parameters from $INPUT
3100f6af48SAndreas Gohr     *
3200f6af48SAndreas Gohr     * @param SearchConfig $searchConfig
3300f6af48SAndreas Gohr     */
3400f6af48SAndreas Gohr    public function __construct(SearchConfig $searchConfig) {
3500f6af48SAndreas Gohr        global $INPUT;
3600f6af48SAndreas Gohr        $this->searchConfig = $searchConfig;
3700f6af48SAndreas Gohr        /** @var \helper_plugin_struct_config $confHlp */
3800f6af48SAndreas Gohr        $confHlp = plugin_load('helper','struct_config');
3900f6af48SAndreas Gohr
4000f6af48SAndreas Gohr        if($INPUT->has(self::$PARAM_SORT)) {
4100f6af48SAndreas Gohr            list($colname, $sort) = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT));
4200f6af48SAndreas Gohr            $this->setSort($colname, $sort === 'ASC');
4300f6af48SAndreas Gohr        }
4400f6af48SAndreas Gohr
4500f6af48SAndreas Gohr        if($INPUT->has(self::$PARAM_FILTER)) {
4600f6af48SAndreas Gohr            foreach($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) {
4700f6af48SAndreas Gohr                list($colname, $comp, $value,) = $confHlp->parseFilterLine('AND', $colcomp . $filter);
48*b2b64eb3SAndreas Gohr                $this->addFilter($colname, $comp, $value);
4900f6af48SAndreas Gohr            }
5000f6af48SAndreas Gohr        }
5100f6af48SAndreas Gohr
5200f6af48SAndreas Gohr        if($INPUT->has(self::$PARAM_OFFSET)) {
5300f6af48SAndreas Gohr            $this->setOffset($INPUT->int(self::$PARAM_OFFSET));
5400f6af48SAndreas Gohr        }
5500f6af48SAndreas Gohr    }
5600f6af48SAndreas Gohr
5700f6af48SAndreas Gohr    /**
5800f6af48SAndreas Gohr     * Returns the full qualified name for a given column
5900f6af48SAndreas Gohr     *
6000f6af48SAndreas Gohr     * @param string|Column $column
6100f6af48SAndreas Gohr     * @return bool|string
6200f6af48SAndreas Gohr     */
6300f6af48SAndreas Gohr    protected function resolveColumn($column) {
6400f6af48SAndreas Gohr        if(!is_a($column, '\plugin\struct\meta\Column')) {
6500f6af48SAndreas Gohr            $column = $this->searchConfig->findColumn($column);
6600f6af48SAndreas Gohr            if(!$column) return false;
6700f6af48SAndreas Gohr        }
6800f6af48SAndreas Gohr        /** @var Column $column */
6900f6af48SAndreas Gohr        return $column->getFullQualifiedLabel();
7000f6af48SAndreas Gohr    }
7100f6af48SAndreas Gohr
7200f6af48SAndreas Gohr    /**
7300f6af48SAndreas Gohr     * Sets the sorting column
7400f6af48SAndreas Gohr     *
7500f6af48SAndreas Gohr     * @param string|Column $column
7600f6af48SAndreas Gohr     * @param bool $asc
7700f6af48SAndreas Gohr     */
7800f6af48SAndreas Gohr    public function setSort($column, $asc = true) {
7900f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
8000f6af48SAndreas Gohr        if(!$column) return;
8100f6af48SAndreas Gohr        $this->sort = array($column, $asc);
8200f6af48SAndreas Gohr    }
8300f6af48SAndreas Gohr
8400f6af48SAndreas Gohr    /**
8500f6af48SAndreas Gohr     * Remove the sorting column
8600f6af48SAndreas Gohr     */
8700f6af48SAndreas Gohr    public function removeSort() {
8800f6af48SAndreas Gohr        $this->sort = null;
8900f6af48SAndreas Gohr    }
9000f6af48SAndreas Gohr
9100f6af48SAndreas Gohr    /**
9200f6af48SAndreas Gohr     * Set the offset
9300f6af48SAndreas Gohr     *
9400f6af48SAndreas Gohr     * @param int $offset
9500f6af48SAndreas Gohr     */
9600f6af48SAndreas Gohr    public function setOffset($offset) {
9700f6af48SAndreas Gohr        $this->offset = $offset;
9800f6af48SAndreas Gohr    }
9900f6af48SAndreas Gohr
10000f6af48SAndreas Gohr    /**
10100f6af48SAndreas Gohr     * Removes the offset
10200f6af48SAndreas Gohr     */
10300f6af48SAndreas Gohr    public function removeOffset() {
10400f6af48SAndreas Gohr        $this->offset = 0;
10500f6af48SAndreas Gohr    }
10600f6af48SAndreas Gohr
10700f6af48SAndreas Gohr    /**
10800f6af48SAndreas Gohr     * Adds another filter
10900f6af48SAndreas Gohr     *
11007993756SAndreas Gohr     * When there is a filter for that column already, the new filter overwrites it. Setting a
11107993756SAndreas Gohr     * blank value is the same as calling @see removeFilter()
11207993756SAndreas Gohr     *
11300f6af48SAndreas Gohr     * @param string|Column $column
11400f6af48SAndreas Gohr     * @param string $comp the comparator
11500f6af48SAndreas Gohr     * @param string $value the value to compare against
11600f6af48SAndreas Gohr     */
11700f6af48SAndreas Gohr    public function addFilter($column, $comp, $value) {
11800f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
11900f6af48SAndreas Gohr        if(!$column) return;
12000f6af48SAndreas Gohr
12107993756SAndreas Gohr        if(trim($value) === '') {
12207993756SAndreas Gohr            $this->removeFilter($column);
12307993756SAndreas Gohr        } else {
12400f6af48SAndreas Gohr            $this->filters[$column] = array($comp, $value);
12500f6af48SAndreas Gohr        }
12607993756SAndreas Gohr    }
12700f6af48SAndreas Gohr
12800f6af48SAndreas Gohr    /**
12900f6af48SAndreas Gohr     * Removes the filter for the given column
13000f6af48SAndreas Gohr     *
13100f6af48SAndreas Gohr     * @param $column
13200f6af48SAndreas Gohr     */
13300f6af48SAndreas Gohr    public function removeFilter($column) {
13400f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
13500f6af48SAndreas Gohr        if(!$column) return;
13600f6af48SAndreas Gohr        if(isset($this->filters[$column])) unset($this->filters[$column]);
13700f6af48SAndreas Gohr    }
13800f6af48SAndreas Gohr
13900f6af48SAndreas Gohr    /**
14000f6af48SAndreas Gohr     * Remove all filter
14100f6af48SAndreas Gohr     */
14200f6af48SAndreas Gohr    public function clearFilters() {
14300f6af48SAndreas Gohr        $this->filters = array();
14400f6af48SAndreas Gohr    }
14500f6af48SAndreas Gohr
14600f6af48SAndreas Gohr    /**
14707993756SAndreas Gohr     * @return array the current filters
14807993756SAndreas Gohr     */
14907993756SAndreas Gohr    public function getFilters() {
15007993756SAndreas Gohr        return $this->filters;
15107993756SAndreas Gohr    }
15207993756SAndreas Gohr
15307993756SAndreas Gohr    /**
15400f6af48SAndreas Gohr     * Get the current parameters in a form that can be used to create URLs
15500f6af48SAndreas Gohr     */
15600f6af48SAndreas Gohr    public function getURLParameters() {
15700f6af48SAndreas Gohr        $params = array();
15800f6af48SAndreas Gohr        if($this->offset) {
15900f6af48SAndreas Gohr            $params[self::$PARAM_OFFSET] = $this->offset;
16000f6af48SAndreas Gohr        }
16100f6af48SAndreas Gohr
16200f6af48SAndreas Gohr        if($this->sort) {
16300f6af48SAndreas Gohr            list($column, $asc) = $this->sort;
16400f6af48SAndreas Gohr            if(!$asc) $column = "^$column";
16500f6af48SAndreas Gohr            $params[self::$PARAM_SORT] = $column;
16600f6af48SAndreas Gohr        }
16700f6af48SAndreas Gohr
16800f6af48SAndreas Gohr        if($this->filters) {
16900f6af48SAndreas Gohr            $params[self::$PARAM_FILTER] = array();
17000f6af48SAndreas Gohr            foreach($this->filters as $column => $filter) {
17100f6af48SAndreas Gohr                list($comp, $value) = $filter;
17200f6af48SAndreas Gohr                $params[self::$PARAM_FILTER][$column . $comp] = $value;
17300f6af48SAndreas Gohr            }
17400f6af48SAndreas Gohr        }
17500f6af48SAndreas Gohr
17600f6af48SAndreas Gohr        return $params;
17700f6af48SAndreas Gohr    }
17800f6af48SAndreas Gohr
17900f6af48SAndreas Gohr    /**
18000f6af48SAndreas Gohr     * Updates the given config array with the values currently set
18100f6af48SAndreas Gohr     *
18200f6af48SAndreas Gohr     * This should only be called once at the initialization
18300f6af48SAndreas Gohr     *
18400f6af48SAndreas Gohr     * @param array $config
18500f6af48SAndreas Gohr     * @return array
18600f6af48SAndreas Gohr     */
18700f6af48SAndreas Gohr    public function updateConfig($config) {
18800f6af48SAndreas Gohr        if($this->offset) {
18900f6af48SAndreas Gohr            $config['offset'] = $this->offset;
19000f6af48SAndreas Gohr        }
19100f6af48SAndreas Gohr
19200f6af48SAndreas Gohr        if($this->sort) {
19300f6af48SAndreas Gohr            list($column, $asc) = $this->sort;
19400f6af48SAndreas Gohr            $config['sort'] = array(
19500f6af48SAndreas Gohr                array($column, $asc)
19600f6af48SAndreas Gohr            );
19700f6af48SAndreas Gohr        }
19800f6af48SAndreas Gohr
19900f6af48SAndreas Gohr        if($this->filters) {
20000f6af48SAndreas Gohr            if(empty($config['filter'])) $config['filter'] = array();
20100f6af48SAndreas Gohr            foreach($this->filters as $column => $filter) {
20200f6af48SAndreas Gohr                list($comp, $value) = $filter;
20300f6af48SAndreas Gohr                $config['filter'][] = array($column, $comp, $value, 'AND');
20400f6af48SAndreas Gohr            }
20500f6af48SAndreas Gohr        }
20600f6af48SAndreas Gohr
20700f6af48SAndreas Gohr        return $config;
20800f6af48SAndreas Gohr    }
20900f6af48SAndreas Gohr
21000f6af48SAndreas Gohr}
211