Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
filter.php
1<?php
2namespace Bitrix\Main\Filter;
3
4class Filter
5{
7 protected $ID = '';
9 protected $entityDataProvider = null;
11 protected $extraProviders = null;
12
14 protected $params = null;
15
17 protected $fields = null;
18
19 protected $uiFilterPostfixes = [
20 '_datesel', '_month', '_quarter', '_year', '_days', // date
21 '_numsel', // number
22 '_from', '_to', // date and number ranges
23 '_isEmpty', // has no value
24 '_hasAnyValue', // has any value
25 '_label', // custom entity title
26 ];
27
29 'FILTER_ID',
30 'FILTER_APPLIED',
31 'PRESET_ID',
32 'FIND',
33 ];
34
35 function __construct($ID, DataProvider $entityDataProvider, array $extraDataProviders = null, array $params = null)
36 {
37 $this->ID = $ID;
38 $this->entityDataProvider = $entityDataProvider;
39
40 $this->extraProviders = [];
41 if(is_array($extraDataProviders))
42 {
43 foreach($extraDataProviders as $dataProvider)
44 {
45 if($dataProvider instanceof DataProvider)
46 {
47 $this->extraProviders[] = $dataProvider;
48 }
49 }
50 }
51
52 $this->params = is_array($params) ? $params : [];
53 }
54
59 function getID()
60 {
61 return $this->ID;
62 }
63
68 public function getDefaultFieldIDs()
69 {
70 $results = [];
71 foreach($this->getFields() as $fieldID => $field)
72 {
73 if($field->isDefault())
74 {
75 $results[] = $fieldID;
76 }
77 }
78 return $results;
79 }
80
85 public function getFields()
86 {
87 if($this->fields === null)
88 {
89 $this->fields = $this->entityDataProvider->prepareFields();
90 foreach($this->extraProviders as $dataProvider)
91 {
92 $fields = $dataProvider->prepareFields();
93 if(!empty($fields))
94 {
95 $this->fields += $fields;
96 }
97 }
98 }
99 return $this->fields;
100 }
101
108 public function getFieldArrays(array $fieldMask = [])
109 {
110 $results = [];
111 $allFields = true;
112 if (!empty($fieldMask))
113 {
114 $fieldMask = array_fill_keys($fieldMask, true);
115 $allFields = false;
116 }
117 $fields = $this->getFields();
118 foreach($fields as $field)
119 {
120 if ($allFields || isset($fieldMask[$field->getId()]))
121 {
122 $results[] = $field->toArray();
123 }
124 }
125 return $results;
126 }
127
133 public function getField($fieldID)
134 {
135 $fields = $this->getFields();
136 return $fields[$fieldID] ?? null;
137 }
138
142 public function getEntityDataProvider()
143 {
144 return $this->entityDataProvider;
145 }
146
152 public function prepareListFilterParams(array &$filter)
153 {
154 foreach ($filter as $k => $v)
155 {
156 $this->entityDataProvider->prepareListFilterParam($filter, $k);
157 }
158 }
159
168 public function getValue(?array $rawValue = null): array
169 {
170 $gridId = $this->getEntityDataProvider()->getSettings()->getID();
171
172 if (!isset($rawValue))
173 {
174 $options = new \Bitrix\Main\UI\Filter\Options($gridId);
175 $rawValue =
176 $options->getFilter()
177 + $options->getFilterLogic($this->getFieldArrays())
178 ;
179 }
180
181 $result = $rawValue;
182 $this->removeNotUiFilterFields($result);
183 $this->prepareListFilterParams($result);
184 $this->prepareFilterValue($result);
185 $this->removeServiceUiFilterFields($result);
186
187 return $result;
188 }
189
194 protected function removeServiceUiFilterFields(array &$filter): void
195 {
196 foreach ($filter as $fieldId => $fieldValue)
197 {
198 if (in_array($fieldId, $this->uiFilterServiceFields, true))
199 {
200 unset($filter[$fieldId]);
201 continue;
202 }
203 foreach ($this->uiFilterPostfixes as $postfix)
204 {
205 if (mb_substr($fieldId, -mb_strlen($postfix)) === $postfix)
206 {
207 unset($filter[$fieldId]);
208 continue;
209 }
210 }
211 }
212 }
213
218 protected function removeNotUiFilterFields(array &$filter): void
219 {
220 $filterFieldsIds = array_map(
221 function(Field $filterField)
222 {
223 return $filterField->getId();
224 },
225 $this->getFields()
226 );
227 $sqlWhere = new \CSQLWhere();
228
229 foreach ($filter as $fieldId => $fieldValue)
230 {
231 if (in_array($fieldId, $this->uiFilterServiceFields, true))
232 {
233 continue;
234 }
235 if (in_array($fieldId, $filterFieldsIds, true))
236 {
237 continue;
238 }
239
240 $fieldIdWithoutOperation = $sqlWhere->makeOperation($fieldId)['FIELD'];
241 if (in_array($fieldIdWithoutOperation, $filterFieldsIds, true))
242 {
243 continue;
244 }
245
246 foreach ($this->uiFilterPostfixes as $postfix)
247 {
248 $postfixLength = mb_strlen($postfix);
249 if (mb_substr($fieldId, -$postfixLength) === $postfix)
250 {
251 $realFieldId = mb_substr($fieldId, 0, -$postfixLength);
252 if (in_array($realFieldId, $filterFieldsIds, true))
253 {
254 continue(2);
255 }
256 }
257 }
258 unset($filter[$fieldId]); // not a valid filter field
259 }
260 }
261
262 protected function prepareFilterValue(array &$value): void
263 {
264 $dataProviders = array_merge(
265 [
266 $this->getEntityDataProvider(),
267 ],
268 $this->extraProviders,
269 );
270
271 foreach ($dataProviders as $dataProvider)
272 {
273 $value = $dataProvider->prepareFilterValue($value);
274 }
275 }
276}
__construct($ID, DataProvider $entityDataProvider, array $extraDataProviders=null, array $params=null)
Definition filter.php:35
prepareFilterValue(array &$value)
Definition filter.php:262
removeServiceUiFilterFields(array &$filter)
Definition filter.php:194
removeNotUiFilterFields(array &$filter)
Definition filter.php:218
getValue(?array $rawValue=null)
Definition filter.php:168
getFieldArrays(array $fieldMask=[])
Definition filter.php:108
prepareListFilterParams(array &$filter)
Definition filter.php:152