Lines Matching +full:comparator +full:* +(+path:plugin +path:struct) -(+path:plugin +path:struct +path:lang)

1 <?php
3 namespace dokuwiki\plugin\struct\meta;
6 * Manage dynamic parameters for aggregations
8 * @package dokuwiki\plugin\struct\meta
10 class SearchConfigParameters
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';
19 /** @var SearchConfig */
20 protected $searchConfig;
22 /** @var null|array */
23 protected $sort;
24 /** @var int */
25 protected $offset = 0;
26 /** @var array */
27 protected $filters = [];
30 * Initializes the dynamic parameters from $INPUT
32 * @param SearchConfig $searchConfig
34 public function __construct(SearchConfig $searchConfig)
36 global $INPUT;
37 $this->searchConfig = $searchConfig;
38 /** @var \helper_plugin_struct_config $confHlp */
39 $confHlp = plugin_load('helper', 'struct_config');
41 if ($INPUT->has(self::$PARAM_SORT)) {
42 [$colname, $sort] = $confHlp->parseSort($INPUT->str(self::$PARAM_SORT));
43 $this->setSort($colname, $sort);
46 if ($INPUT->has(self::$PARAM_FILTER)) {
47 foreach ($INPUT->arr(self::$PARAM_FILTER) as $colcomp => $filter) {
48 [$colname, $comp, $value, ] = $confHlp->parseFilterLine('AND', $colcomp . $filter);
49 $this->addFilter($colname, $comp, $value);
53 if ($INPUT->has(self::$PARAM_OFFSET)) {
54 $this->setOffset($INPUT->int(self::$PARAM_OFFSET));
59 * Returns the full qualified name for a given column
61 * @param string|Column $column
62 * @return false|string
64 protected function resolveColumn($column)
66 if (!is_a($column, Column::class)) {
67 $column = $this->searchConfig->findColumn($column);
68 if (!$column) return false;
70 /** @var Column $column */
71 return $column->getFullQualifiedLabel();
75 * Sets the sorting column
77 * @param string|Column $column
78 * @param bool $asc
80 public function setSort($column, $asc = true)
82 $column = $this->resolveColumn($column);
83 if (!$column) return;
84 $this->sort = [$column, $asc];
88 * Remove the sorting column
90 public function removeSort()
92 $this->sort = null;
96 * Set the offset
98 * @param int $offset
100 public function setOffset($offset)
102 $this->offset = $offset;
106 * Removes the offset
108 public function removeOffset()
110 $this->offset = 0;
114 * Adds another filter
116 * When there is a filter for that column already, the new filter overwrites it. Setting a
117 * blank value is the same as calling removeFilter()
119 * @param string|Column $column
120 * @param string $comp the comparator
121 * @param string $value the value to compare against
123 public function addFilter($column, $comp, $value)
125 $column = $this->resolveColumn($column);
126 if (!$column) return;
128 if (trim($value) === '') {
129 $this->removeFilter($column);
130 } else {
131 $this->filters[$column] = [$comp, $value];
136 * Removes the filter for the given column
138 * @param $column
140 public function removeFilter($column)
142 $column = $this->resolveColumn($column);
143 if (!$column) return;
144 if (isset($this->filters[$column])) unset($this->filters[$column]);
148 * Remove all filter
150 public function clearFilters()
152 $this->filters = [];
156 * @return array the current filters
158 public function getFilters()
160 return $this->filters;
164 * Get the current parameters
166 * It creates a flat key value in a form that can be used to
167 * create URLs or Form parameters
170 * @return array
172 public function getURLParameters()
174 $params = [];
175 if ($this->offset) {
176 $params[self::$PARAM_OFFSET] = $this->offset;
179 if ($this->sort) {
180 [$column, $asc] = $this->sort;
181 if (!$asc) $column = "^$column";
182 $params[self::$PARAM_SORT] = $column;
185 if ($this->filters) {
186 foreach ($this->filters as $column => $filter) {
187 [$comp, $value] = $filter;
188 $key = self::$PARAM_FILTER . '[' . $column . $comp . ']';
189 $params[$key] = $value;
193 return $params;
197 * Applies the dynamic filter settings to the SearchConfig
199 public function apply()
201 if ($this->offset) {
202 $this->searchConfig->setOffset($this->offset);
205 if ($this->sort) {
206 $this->searchConfig->clearSort(); // remove any existing sort
207 $this->searchConfig->addSort($this->sort[0], $this->sort[1]);
210 foreach ($this->filters as $colName => $filter) {
211 [$comp, $value] = $filter;
212 $this->searchConfig->addDynamicFilter($colName, $value, $comp, 'AND');