xref: /plugin/struct/meta/SearchConfigParameters.php (revision 07993756737e69247e6a2b15c689ba4f83661a7c)
100f6af48SAndreas Gohr<?php
200f6af48SAndreas Gohr
300f6af48SAndreas Gohrnamespace plugin\struct\meta;
400f6af48SAndreas Gohr
500f6af48SAndreas Gohrclass SearchConfigParameters {
600f6af48SAndreas Gohr
700f6af48SAndreas Gohr    /** @var string parameter name to pass filters */
800f6af48SAndreas Gohr    public static $PARAM_FILTER = 'flt'; // @todo search code for hardcoded dataflt
900f6af48SAndreas Gohr    /** @var string parameter name to pass offset */
1000f6af48SAndreas Gohr    public static $PARAM_OFFSET = 'ofs'; // @todo search code for hardcoded dataofs
1100f6af48SAndreas Gohr    /** @var string parameter name to pass srt */
1200f6af48SAndreas Gohr    public static $PARAM_SORT = 'srt'; // @todo search code for hardcoded datasrt
1300f6af48SAndreas Gohr
1400f6af48SAndreas Gohr    /** @var SearchConfig */
1500f6af48SAndreas Gohr    protected $searchConfig;
1600f6af48SAndreas Gohr
1700f6af48SAndreas Gohr    /** @var null|array */
1800f6af48SAndreas Gohr    protected $sort = null;
1900f6af48SAndreas Gohr    /** @var int */
2000f6af48SAndreas Gohr    protected $offset = 0;
2100f6af48SAndreas Gohr    /** @var  array */
2200f6af48SAndreas Gohr    protected $filters = array();
2300f6af48SAndreas Gohr
2400f6af48SAndreas Gohr    /**
2500f6af48SAndreas Gohr     * Initializes the dynamic parameters from $INPUT
2600f6af48SAndreas Gohr     *
2700f6af48SAndreas Gohr     * @param SearchConfig $searchConfig
2800f6af48SAndreas Gohr     */
2900f6af48SAndreas Gohr    public function __construct(SearchConfig $searchConfig) {
3000f6af48SAndreas Gohr        global $INPUT;
3100f6af48SAndreas Gohr        $this->searchConfig = $searchConfig;
3200f6af48SAndreas Gohr        /** @var \helper_plugin_struct_config $confHlp */
3300f6af48SAndreas Gohr        $confHlp = plugin_load('helper','struct_config');
3400f6af48SAndreas Gohr
3500f6af48SAndreas Gohr        if($INPUT->has(self::$PARAM_SORT)) {
3600f6af48SAndreas Gohr            list($colname, $sort) = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT));
3700f6af48SAndreas Gohr            $this->setSort($colname, $sort === 'ASC');
3800f6af48SAndreas Gohr        }
3900f6af48SAndreas Gohr
4000f6af48SAndreas Gohr        if($INPUT->has(self::$PARAM_FILTER)) {
4100f6af48SAndreas Gohr            foreach($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) {
4200f6af48SAndreas Gohr                list($colname, $comp, $value,) = $confHlp->parseFilterLine('AND', $colcomp . $filter);
4300f6af48SAndreas Gohr                $this->addFilter($colname, $value, $comp);
4400f6af48SAndreas Gohr            }
4500f6af48SAndreas Gohr        }
4600f6af48SAndreas Gohr
4700f6af48SAndreas Gohr        if($INPUT->has(self::$PARAM_OFFSET)) {
4800f6af48SAndreas Gohr            $this->setOffset($INPUT->int(self::$PARAM_OFFSET));
4900f6af48SAndreas Gohr        }
5000f6af48SAndreas Gohr    }
5100f6af48SAndreas Gohr
5200f6af48SAndreas Gohr    /**
5300f6af48SAndreas Gohr     * Returns the full qualified name for a given column
5400f6af48SAndreas Gohr     *
5500f6af48SAndreas Gohr     * @param string|Column $column
5600f6af48SAndreas Gohr     * @return bool|string
5700f6af48SAndreas Gohr     */
5800f6af48SAndreas Gohr    protected function resolveColumn($column) {
5900f6af48SAndreas Gohr        if(!is_a($column, '\plugin\struct\meta\Column')) {
6000f6af48SAndreas Gohr            $column = $this->searchConfig->findColumn($column);
6100f6af48SAndreas Gohr            if(!$column) return false;
6200f6af48SAndreas Gohr        }
6300f6af48SAndreas Gohr        /** @var Column $column */
6400f6af48SAndreas Gohr        return $column->getFullQualifiedLabel();
6500f6af48SAndreas Gohr    }
6600f6af48SAndreas Gohr
6700f6af48SAndreas Gohr    /**
6800f6af48SAndreas Gohr     * Sets the sorting column
6900f6af48SAndreas Gohr     *
7000f6af48SAndreas Gohr     * @param string|Column $column
7100f6af48SAndreas Gohr     * @param bool $asc
7200f6af48SAndreas Gohr     */
7300f6af48SAndreas Gohr    public function setSort($column, $asc = true) {
7400f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
7500f6af48SAndreas Gohr        if(!$column) return;
7600f6af48SAndreas Gohr        $this->sort = array($column, $asc);
7700f6af48SAndreas Gohr    }
7800f6af48SAndreas Gohr
7900f6af48SAndreas Gohr    /**
8000f6af48SAndreas Gohr     * Remove the sorting column
8100f6af48SAndreas Gohr     */
8200f6af48SAndreas Gohr    public function removeSort() {
8300f6af48SAndreas Gohr        $this->sort = null;
8400f6af48SAndreas Gohr    }
8500f6af48SAndreas Gohr
8600f6af48SAndreas Gohr    /**
8700f6af48SAndreas Gohr     * Set the offset
8800f6af48SAndreas Gohr     *
8900f6af48SAndreas Gohr     * @param int $offset
9000f6af48SAndreas Gohr     */
9100f6af48SAndreas Gohr    public function setOffset($offset) {
9200f6af48SAndreas Gohr        $this->offset = $offset;
9300f6af48SAndreas Gohr    }
9400f6af48SAndreas Gohr
9500f6af48SAndreas Gohr    /**
9600f6af48SAndreas Gohr     * Removes the offset
9700f6af48SAndreas Gohr     */
9800f6af48SAndreas Gohr    public function removeOffset() {
9900f6af48SAndreas Gohr        $this->offset = 0;
10000f6af48SAndreas Gohr    }
10100f6af48SAndreas Gohr
10200f6af48SAndreas Gohr    /**
10300f6af48SAndreas Gohr     * Adds another filter
10400f6af48SAndreas Gohr     *
105*07993756SAndreas Gohr     * When there is a filter for that column already, the new filter overwrites it. Setting a
106*07993756SAndreas Gohr     * blank value is the same as calling @see removeFilter()
107*07993756SAndreas Gohr     *
10800f6af48SAndreas Gohr     * @param string|Column $column
10900f6af48SAndreas Gohr     * @param string $comp the comparator
11000f6af48SAndreas Gohr     * @param string $value the value to compare against
11100f6af48SAndreas Gohr     */
11200f6af48SAndreas Gohr    public function addFilter($column, $comp, $value) {
11300f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
11400f6af48SAndreas Gohr        if(!$column) return;
11500f6af48SAndreas Gohr
116*07993756SAndreas Gohr        if(trim($value) === '') {
117*07993756SAndreas Gohr            $this->removeFilter($column);
118*07993756SAndreas Gohr        } else {
11900f6af48SAndreas Gohr            $this->filters[$column] = array($comp, $value);
12000f6af48SAndreas Gohr        }
121*07993756SAndreas Gohr    }
12200f6af48SAndreas Gohr
12300f6af48SAndreas Gohr    /**
12400f6af48SAndreas Gohr     * Removes the filter for the given column
12500f6af48SAndreas Gohr     *
12600f6af48SAndreas Gohr     * @param $column
12700f6af48SAndreas Gohr     */
12800f6af48SAndreas Gohr    public function removeFilter($column) {
12900f6af48SAndreas Gohr        $column = $this->resolveColumn($column);
13000f6af48SAndreas Gohr        if(!$column) return;
13100f6af48SAndreas Gohr        if(isset($this->filters[$column])) unset($this->filters[$column]);
13200f6af48SAndreas Gohr    }
13300f6af48SAndreas Gohr
13400f6af48SAndreas Gohr    /**
13500f6af48SAndreas Gohr     * Remove all filter
13600f6af48SAndreas Gohr     */
13700f6af48SAndreas Gohr    public function clearFilters() {
13800f6af48SAndreas Gohr        $this->filters = array();
13900f6af48SAndreas Gohr    }
14000f6af48SAndreas Gohr
14100f6af48SAndreas Gohr    /**
142*07993756SAndreas Gohr     * @return array the current filters
143*07993756SAndreas Gohr     */
144*07993756SAndreas Gohr    public function getFilters() {
145*07993756SAndreas Gohr        return $this->filters;
146*07993756SAndreas Gohr    }
147*07993756SAndreas Gohr
148*07993756SAndreas Gohr    /**
14900f6af48SAndreas Gohr     * Get the current parameters in a form that can be used to create URLs
15000f6af48SAndreas Gohr     */
15100f6af48SAndreas Gohr    public function getURLParameters() {
15200f6af48SAndreas Gohr        $params = array();
15300f6af48SAndreas Gohr        if($this->offset) {
15400f6af48SAndreas Gohr            $params[self::$PARAM_OFFSET] = $this->offset;
15500f6af48SAndreas Gohr        }
15600f6af48SAndreas Gohr
15700f6af48SAndreas Gohr        if($this->sort) {
15800f6af48SAndreas Gohr            list($column, $asc) = $this->sort;
15900f6af48SAndreas Gohr            if(!$asc) $column = "^$column";
16000f6af48SAndreas Gohr            $params[self::$PARAM_SORT] = $column;
16100f6af48SAndreas Gohr        }
16200f6af48SAndreas Gohr
16300f6af48SAndreas Gohr        if($this->filters) {
16400f6af48SAndreas Gohr            $params[self::$PARAM_FILTER] = array();
16500f6af48SAndreas Gohr            foreach($this->filters as $column => $filter) {
16600f6af48SAndreas Gohr                list($comp, $value) = $filter;
16700f6af48SAndreas Gohr                $params[self::$PARAM_FILTER][$column . $comp] = $value;
16800f6af48SAndreas Gohr            }
16900f6af48SAndreas Gohr        }
17000f6af48SAndreas Gohr
17100f6af48SAndreas Gohr        return $params;
17200f6af48SAndreas Gohr    }
17300f6af48SAndreas Gohr
17400f6af48SAndreas Gohr    /**
17500f6af48SAndreas Gohr     * Updates the given config array with the values currently set
17600f6af48SAndreas Gohr     *
17700f6af48SAndreas Gohr     * This should only be called once at the initialization
17800f6af48SAndreas Gohr     *
17900f6af48SAndreas Gohr     * @param array $config
18000f6af48SAndreas Gohr     * @return array
18100f6af48SAndreas Gohr     */
18200f6af48SAndreas Gohr    public function updateConfig($config) {
18300f6af48SAndreas Gohr        if($this->offset) {
18400f6af48SAndreas Gohr            $config['offset'] = $this->offset;
18500f6af48SAndreas Gohr        }
18600f6af48SAndreas Gohr
18700f6af48SAndreas Gohr        if($this->sort) {
18800f6af48SAndreas Gohr            list($column, $asc) = $this->sort;
18900f6af48SAndreas Gohr            $config['sort'] = array(
19000f6af48SAndreas Gohr                array($column, $asc)
19100f6af48SAndreas Gohr            );
19200f6af48SAndreas Gohr        }
19300f6af48SAndreas Gohr
19400f6af48SAndreas Gohr        if($this->filters) {
19500f6af48SAndreas Gohr            if(empty($config['filter'])) $config['filter'] = array();
19600f6af48SAndreas Gohr            foreach($this->filters as $column => $filter) {
19700f6af48SAndreas Gohr                list($comp, $value) = $filter;
19800f6af48SAndreas Gohr                $config['filter'][] = array($column, $comp, $value, 'AND');
19900f6af48SAndreas Gohr            }
20000f6af48SAndreas Gohr        }
20100f6af48SAndreas Gohr
20200f6af48SAndreas Gohr        return $config;
20300f6af48SAndreas Gohr    }
20400f6af48SAndreas Gohr
20500f6af48SAndreas Gohr}
206