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