xref: /plugin/struct/meta/SearchConfigParameters.php (revision b2b64eb33ee2aa7ee31e7192be7b680c709298a3)
1<?php
2
3namespace plugin\struct\meta;
4
5/**
6 * Manage dynamic parameters for aggregations
7 *
8 * @package plugin\struct\meta
9 */
10class SearchConfigParameters {
11
12    /** @var string parameter name to pass filters */
13    public static $PARAM_FILTER = 'flt';
14    /** @var string parameter name to pass offset */
15    public static $PARAM_OFFSET = 'ofs';
16    /** @var string parameter name to pass srt */
17    public static $PARAM_SORT = 'srt';
18
19    /** @var SearchConfig */
20    protected $searchConfig;
21
22    /** @var null|array */
23    protected $sort = null;
24    /** @var int */
25    protected $offset = 0;
26    /** @var  array */
27    protected $filters = array();
28
29    /**
30     * Initializes the dynamic parameters from $INPUT
31     *
32     * @param SearchConfig $searchConfig
33     */
34    public function __construct(SearchConfig $searchConfig) {
35        global $INPUT;
36        $this->searchConfig = $searchConfig;
37        /** @var \helper_plugin_struct_config $confHlp */
38        $confHlp = plugin_load('helper','struct_config');
39
40        if($INPUT->has(self::$PARAM_SORT)) {
41            list($colname, $sort) = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT));
42            $this->setSort($colname, $sort === 'ASC');
43        }
44
45        if($INPUT->has(self::$PARAM_FILTER)) {
46            foreach($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) {
47                list($colname, $comp, $value,) = $confHlp->parseFilterLine('AND', $colcomp . $filter);
48                $this->addFilter($colname, $comp, $value);
49            }
50        }
51
52        if($INPUT->has(self::$PARAM_OFFSET)) {
53            $this->setOffset($INPUT->int(self::$PARAM_OFFSET));
54        }
55    }
56
57    /**
58     * Returns the full qualified name for a given column
59     *
60     * @param string|Column $column
61     * @return bool|string
62     */
63    protected function resolveColumn($column) {
64        if(!is_a($column, '\plugin\struct\meta\Column')) {
65            $column = $this->searchConfig->findColumn($column);
66            if(!$column) return false;
67        }
68        /** @var Column $column */
69        return $column->getFullQualifiedLabel();
70    }
71
72    /**
73     * Sets the sorting column
74     *
75     * @param string|Column $column
76     * @param bool $asc
77     */
78    public function setSort($column, $asc = true) {
79        $column = $this->resolveColumn($column);
80        if(!$column) return;
81        $this->sort = array($column, $asc);
82    }
83
84    /**
85     * Remove the sorting column
86     */
87    public function removeSort() {
88        $this->sort = null;
89    }
90
91    /**
92     * Set the offset
93     *
94     * @param int $offset
95     */
96    public function setOffset($offset) {
97        $this->offset = $offset;
98    }
99
100    /**
101     * Removes the offset
102     */
103    public function removeOffset() {
104        $this->offset = 0;
105    }
106
107    /**
108     * Adds another filter
109     *
110     * When there is a filter for that column already, the new filter overwrites it. Setting a
111     * blank value is the same as calling @see removeFilter()
112     *
113     * @param string|Column $column
114     * @param string $comp the comparator
115     * @param string $value the value to compare against
116     */
117    public function addFilter($column, $comp, $value) {
118        $column = $this->resolveColumn($column);
119        if(!$column) return;
120
121        if(trim($value) === '') {
122            $this->removeFilter($column);
123        } else {
124            $this->filters[$column] = array($comp, $value);
125        }
126    }
127
128    /**
129     * Removes the filter for the given column
130     *
131     * @param $column
132     */
133    public function removeFilter($column) {
134        $column = $this->resolveColumn($column);
135        if(!$column) return;
136        if(isset($this->filters[$column])) unset($this->filters[$column]);
137    }
138
139    /**
140     * Remove all filter
141     */
142    public function clearFilters() {
143        $this->filters = array();
144    }
145
146    /**
147     * @return array the current filters
148     */
149    public function getFilters() {
150        return $this->filters;
151    }
152
153    /**
154     * Get the current parameters in a form that can be used to create URLs
155     */
156    public function getURLParameters() {
157        $params = array();
158        if($this->offset) {
159            $params[self::$PARAM_OFFSET] = $this->offset;
160        }
161
162        if($this->sort) {
163            list($column, $asc) = $this->sort;
164            if(!$asc) $column = "^$column";
165            $params[self::$PARAM_SORT] = $column;
166        }
167
168        if($this->filters) {
169            $params[self::$PARAM_FILTER] = array();
170            foreach($this->filters as $column => $filter) {
171                list($comp, $value) = $filter;
172                $params[self::$PARAM_FILTER][$column . $comp] = $value;
173            }
174        }
175
176        return $params;
177    }
178
179    /**
180     * Updates the given config array with the values currently set
181     *
182     * This should only be called once at the initialization
183     *
184     * @param array $config
185     * @return array
186     */
187    public function updateConfig($config) {
188        if($this->offset) {
189            $config['offset'] = $this->offset;
190        }
191
192        if($this->sort) {
193            list($column, $asc) = $this->sort;
194            $config['sort'] = array(
195                array($column, $asc)
196            );
197        }
198
199        if($this->filters) {
200            if(empty($config['filter'])) $config['filter'] = array();
201            foreach($this->filters as $column => $filter) {
202                list($comp, $value) = $filter;
203                $config['filter'][] = array($column, $comp, $value, 'AND');
204            }
205        }
206
207        return $config;
208    }
209
210}
211