Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
datafilter.php
1<?php
3
4class DataFilter extends Filter
5{
10 public function __construct()
11 {
12 parent::__construct();
13 }
14
18 protected function initFieldTypeHandlers()
19 {
20 $this->fieldTypeHandlers = [
21 'string' => 'addStringValue',
22 'number' => 'addNumberValue',
23 'date' => 'addDateValue',
24 'list' => 'addListValue',
25 'checkbox' => 'addCheckboxValue',
26 'custom_date' => '',
27 'dest_selector' => 'addDestSelectorValue',
28 'custom_entity' => '',
29 'custom' => ''
30 ];
31 }
32
38 protected static function getFilterOperator(array $field, $operator)
39 {
40 $result = '';
41 if ($operator === '')
42 {
43 $operator = 'default';
44 }
45 if (!empty($field['operators']) && is_array($field['operators']))
46 {
47 if (isset($field['operators'][$operator]))
48 {
49 $result = $field['operators'][$operator];
50 }
51 elseif (isset($field['operators']['default']))
52 {
53 $result = $field['operators']['default'];
54 }
55 }
56 return $result;
57 }
58
65 protected function addRows(array &$result, array $items, array $field)
66 {
67 if (empty($items))
68 {
69 return;
70 }
71
72 $entity = (isset($field['entity']) ? $field['entity'] : 'master');
73 if ($entity !== '')
74 {
75 if (!isset($result[$entity]))
76 {
77 $result[$entity] = [];
78 }
79 $result[$entity] = array_merge($result[$entity], $items);
80 }
81 unset($entity);
82 }
83
91 protected function addQuickSearchValue(array &$filter, $fieldId, array $field, array $value)
92 {
93 if (isset($value['VALUE']) && is_string($value['VALUE']))
94 {
95 $findValue = trim($value['VALUE']);
96 if ($findValue !== '')
97 {
98 $operator = $this->getFilterOperator($field, 'quickSearch');
99 if (is_string($operator))
100 {
101 $fieldId = $operator.$fieldId;
102 }
103 unset($operator);
104 $this->addRows($filter, [$fieldId => $findValue], $field);
105 }
106 }
107 }
108
116 protected function addStringValue(array &$result, $fieldId, array $field, array $value)
117 {
118 if (isset($value['VALUE']) && is_string($value['VALUE']) && $value['VALUE'] !== '')
119 {
120 $operator = $this->getFilterOperator($field, 'default');
121 if (is_string($operator))
122 {
123 $fieldId = $operator.$fieldId;
124 }
125 unset($operator);
126 $this->addRows($result, [$fieldId => $value['VALUE']], $field);
127 }
128 }
129
137 protected function addNumberValue(array &$result, $fieldId, array $field, array $value)
138 {
139 if (isset($value['SUB_TYPE'])
140 && isset($value['_from']) && is_string($value['_from'])
141 && isset($value['_to']) && is_string($value['_to'])
142 )
143 {
144 $minValue = trim($value['_from']);
145 $maxValue = trim($value['_to']);
146 if ($minValue === '' && $maxValue === '')
147 {
148 return;
149 }
150
151 $items = [];
152 switch ($value['SUB_TYPE'])
153 {
154 case 'exact':
155 if ($minValue !== '')
156 {
157 $operator = $this->getFilterOperator($field, 'exact');
158 if (is_string($operator))
159 {
160 $fieldId = $operator.$fieldId;
161 }
162 unset($operator);
163 $items[$fieldId] = $minValue;
164 }
165 break;
166 case 'range':
167 if ($minValue !== '' && $maxValue !== '')
168 {
169 $operator = $operator = $this->getFilterOperator($field, 'range');
170 if (is_string($operator))
171 {
172 $fieldId = $operator.$fieldId;
173 }
174 unset($operator);
175 $items[$fieldId] = [$minValue, $maxValue];
176 }
177 break;
178 case 'more':
179 if ($minValue !== '')
180 {
181 $operator = $this->getFilterOperator($field, 'more');
182 if (is_string($operator))
183 {
184 $fieldId = $operator.$fieldId;
185 }
186 unset($operator);
187 $items[$fieldId] = $minValue;
188 }
189 break;
190 case 'less':
191 if ($maxValue !== '')
192 {
193 $operator = $this->getFilterOperator($field, 'less');
194 if (is_string($operator))
195 {
196 $fieldId = $operator.$fieldId;
197 }
198 unset($operator);
199 $items[$fieldId] = $maxValue;
200 }
201 break;
202 }
203 unset($maxValue, $minValue);
204
205 $this->addRows($result, $items, $field);
206 unset($items);
207 }
208 }
209
217 protected function addDateValue(array &$result, $fieldId, array $field, array $value)
218 {
219 if (isset($value['SUB_TYPE'])
220 && isset($value['_from']) && is_string($value['_from'])
221 && isset($value['_to']) && is_string($value['_to']))
222 {
223 $minValue = trim($value['_from']);
224 $maxValue = trim($value['_to']);
225 if ($minValue === '' && $maxValue === '')
226 {
227 return;
228 }
229
230 $items = [];
231 switch ($value['SUB_TYPE'])
232 {
233 case 'EXACT':
234 if ($minValue !== '')
235 {
236 $operator = $this->getFilterOperator($field, 'default');
237 if (is_string($operator))
238 {
239 $fieldId = $operator.$fieldId;
240 }
241 unset($operator);
242 $items[$fieldId] = $minValue;
243 }
244 break;
245 case 'RANGE':
246 default:
247 if ($minValue !== '' && $maxValue !== '')
248 {
249 $operator = $this->getFilterOperator($field, 'range');
250 if (is_string($operator))
251 {
252 $fieldId = $operator.$fieldId;
253 }
254 unset($operator);
255 $items[$fieldId] = [$minValue, $maxValue];
256 }
257 break;
258 }
259 unset($maxValue, $minValue);
260
261 $this->addRows($result, $items, $field);
262 unset($items);
263 }
264 }
265
273 protected function addListValue(array &$result, $fieldId, array $field, array $value)
274 {
275 $check = false;
276 $id = null;
277 $multiple = isset($field['params']['multiple']) && $field['params']['multiple'] == 'Y';
278 if ($multiple)
279 $validRawValue = !empty($value);
280 else
281 $validRawValue = isset($value['VALUE']);
282 if ($validRawValue)
283 {
284 if ($multiple)
285 {
286 $id = [];
287 foreach ($value as $row)
288 {
289 if (is_array($row) && isset($row['VALUE']))
290 {
291 $id[] = $row['VALUE'];
292 }
293 }
294 unset($row);
295 $check = !empty($id);
296 }
297 else
298 {
299 $id = $value['VALUE'];
300 $check = true;
301 }
302 }
303
304 if ($check)
305 {
306 $operator = $this->getFilterOperator($field, ($multiple ? 'enum' : 'exact'));
307 if (is_string($operator))
308 {
309 $fieldId = $operator.$fieldId;
310 }
311 unset($operator);
312 $this->addRows($result, [$fieldId => $id], $field);
313 }
314 unset($check, $id);
315 }
316
324 protected function addCheckboxValue(array &$result, $fieldId, array $field, array $value)
325 {
326 if (
327 isset($value['VALUE'])
328 && ($value['VALUE'] === 'Y' || $value['VALUE'] === 'N')
329 )
330 {
331 $operator = $this->getFilterOperator($field, 'exact');
332 if (is_string($operator))
333 {
334 $fieldId = $operator.$fieldId;
335 }
336 unset($operator);
337 $this->addRows($result, [$fieldId => $value['VALUE']], $field);
338 }
339 }
340
348 protected function addDestSelectorValue(array &$result, $fieldId, array $field, array $value)
349 {
350 if (isset($value['_value']))
351 {
352 $multiple = isset($field['params']['multiple']) && $field['params']['multiple'] == 'Y';
353 if ($multiple)
354 $validRawValue = !empty($value['_value']) && is_array($value['_value']);
355 else
356 $validRawValue = is_string($value['_value']) || is_int($value['_value']);
357 if ($validRawValue)
358 {
359 $operator = $this->getFilterOperator($field, ($multiple ? 'enum' : 'exact'));
360 if (is_string($operator))
361 {
362 $fieldId = $operator.$fieldId;
363 }
364 unset($operator);
365 $this->addRows($result, [$fieldId => $value['VALUE']], $field);
366 }
367 }
368 }
369}
addNumberValue(array &$result, $fieldId, array $field, array $value)
addCheckboxValue(array &$result, $fieldId, array $field, array $value)
addDateValue(array &$result, $fieldId, array $field, array $value)
addQuickSearchValue(array &$filter, $fieldId, array $field, array $value)
addStringValue(array &$result, $fieldId, array $field, array $value)
addListValue(array &$result, $fieldId, array $field, array $value)
addRows(array &$result, array $items, array $field)
addDestSelectorValue(array &$result, $fieldId, array $field, array $value)
static getFilterOperator(array $field, $operator)