Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basefilter.php
1<?php
9
11use Bitrix\Main\UI\Filter\Options as FilterOptions;
12
13Loc::loadMessages(__FILE__);
14
19abstract class BaseFilter extends Base
20{
21 public const FIELD_FOR_PRESET_ALL = 'SENDER_SELECT_ALL';
22 public const FIELD_PRESET_ID = 'BX_PRESET_ID';
23
25 protected $filterSettingsUri = '';
26
32 final public function getForm()
33 {
34 $presets = $this->getUiFilterPresets();
35 $currentPresetId = $this->getCurrentPresetId();
36 if ($currentPresetId && isset($presets[$currentPresetId]))
37 {
38 $preset[$currentPresetId]['default'] = true;
39 }
40
41 $filterId = $this->getUiFilterId();
42 $this->clearFilterState($filterId);
43 $filter = static::getFilterFields(checkAccessRights: $this->isCheckAccessRights());
44 return $this->buildUi($filterId, $currentPresetId, $presets, $filter);
45 }
46
54 final public function getCustomForm(array $params)
55 {
56 $presets = $params['presets']??$this->getUiFilterPresets();
57 $currentPresetId = $params['current_preset_id']??$this->getCurrentPresetId();
58 if ($currentPresetId && isset($presets[$currentPresetId]))
59 {
60 $preset[$currentPresetId]['default'] = true;
61 }
62
63 $filterId = $params['filter_id']??$this->getUiFilterId();
64 $this->clearFilterState($filterId);
65 $filter = static::getFilterFields($params['filter'], $this->isCheckAccessRights());
66
67 return $this->buildUi($filterId, $currentPresetId, $presets, $filter);
68 }
69
70 private function buildUi($filterId, $currentPresetId, $presets, $filter)
71 {
72 ob_start();
73 $GLOBALS['APPLICATION']->includeComponent(
74 "bitrix:main.ui.filter",
75 "",
76 array(
77 "FILTER_ID" => $filterId,
78 "CURRENT_PRESET" => $currentPresetId,
79 "FILTER" => array_filter(
80 $filter,
81 function ($field)
82 {
83 return empty($field['sender_internal']);
84 }
85 ),
86 "FILTER_PRESETS" => $presets,
87 "DISABLE_SEARCH" => true,
88 "ENABLE_LABEL" => true,
89 )
90 );
91
92 foreach ($this->getUiFilterFields() as $field)
93 {
94 if (!isset($field['sender_segment_callback']))
95 {
96 continue;
97 }
98
99 if (!is_callable($field['sender_segment_callback']))
100 {
101 continue;
102 }
103
104 echo $field['sender_segment_callback']($field);
105 }
106
107 return ob_get_clean();
108 }
109
117 protected function getFieldDateFrom($name, $defaultValue = null)
118 {
119 $field = static::getUiFilterField($name);
120 if (!$field)
121 {
122 return null;
123 }
124 $field['value'] = $this->getFieldValue($name, $defaultValue);
125
126 return Filter\DateField::create($field)->getFrom();
127 }
128
136 protected function getFieldDateTo($name, $defaultValue = null)
137 {
138 $field = static::getUiFilterField($name);
139 if (!$field)
140 {
141 return null;
142 }
143 $field['value'] = $this->getFieldValue($name, $defaultValue);
144
145 return Filter\DateField::create($field)->getTo();
146 }
147
148 public function getUiFilterId()
149 {
150 return $this->getId() . '_%CONNECTOR_NUM%';
151 }
152
153 public function getCurrentPresetId()
154 {
155 return $this->getFieldValue(self::FIELD_PRESET_ID, null);
156 }
157
158 public function setFieldValues(array $fieldValues = null)
159 {
160 if (is_array($fieldValues) && count($fieldValues) > 0)
161 {
162 $values = array();
163 $fields = $this->getFilterFields(checkAccessRights: $this->isCheckAccessRights());
164
165 $systemFields = array(self::FIELD_PRESET_ID, self::FIELD_FOR_PRESET_ALL);
166 foreach ($systemFields as $fieldId)
167 {
168 if (!isset($fieldValues[$fieldId]) || !$fieldValues[$fieldId])
169 {
170 continue;
171 }
172
173 $values[$fieldId] = $fieldValues[$fieldId];
174 }
175
176 foreach ($fields as $field)
177 {
178 if (!isset($fieldValues[$field['id']]) && !in_array($field['id'], $systemFields))
179 {
180 continue;
181 }
182 if ($field['type'] === 'dest_selector')
183 {
184 if (is_array($fieldValues) && array_key_exists($field['id'].'_label', $fieldValues))
185 {
186 $values[$field['id'].'_label'] = $fieldValues[$field['id'].'_label'];
187 }
188 elseif (is_array($fieldValues[$field['id']]) && array_key_exists('_label', $fieldValues[$field['id']]))
189 {
190 $values[$field['id'].'_label'] = $fieldValues[$field['id']]['_label'];
191 $fieldValues[$field['id']] = $fieldValues[$field['id']]['_value'];
192 }
193 }
194
195 $values[$field['id']] = $fieldValues[$field['id']] ?? '';
196 }
197
198 $fieldValues = $values;
199 }
200
201 parent::setFieldValues($fieldValues);
202 }
203
210 public static function getUiFilterFields()
211 {
212 return array();
213 }
214
221 public static function getUiFilterField($id)
222 {
223 foreach (static::getFilterFields() as $field)
224 {
225 if ($field['id'] === $id)
226 {
227 return $field;
228 }
229 }
230
231 return null;
232 }
233
240 public static function getUiFilterData($filterId)
241 {
242 $filterFields = static::getFilterFields();
243 $filterOptions = new FilterOptions($filterId, static::getUiFilterPresets());
244 $filterRequest = $filterOptions->getFilter($filterFields);
245
246 $filterData = array();
247 foreach ($filterFields as $field)
248 {
249 $fieldId = $field['id'];
250 if ($field['type'] === 'dest_selector')
251 {
252 $destSelectorData = Filter\DestSelectorField::create($field)->fetchFieldValue($filterRequest);
253 if ($destSelectorData)
254 {
255 $filterData[$fieldId] = $destSelectorData;
256 }
257 }
258 elseif (isset($filterRequest[$fieldId]))
259 {
260 $filterData[$fieldId] = $filterRequest[$fieldId];
261 }
262 elseif ($field['type'] === 'date')
263 {
264 $dateData = Filter\DateField::create($field)->fetchFieldValue($filterRequest);
265 if (is_array($dateData) && count($dateData))
266 {
267 $filterData[$fieldId] = $dateData;
268 }
269 }
270 elseif ($field['type'] === 'number')
271 {
273 $fieldId . '_numsel',
274 $filterRequest
275 );
276 if (is_array($numberData) && count($numberData))
277 {
278 if (count($numberData) > 1 || $numberData[$fieldId . '_numsel'] !== null)
279 {
280 $filterData[$fieldId] = $numberData;
281 }
282 }
283 }
284 }
285
286 if (isset($filterRequest['PRESET_ID']) && array_key_exists($filterRequest['PRESET_ID'], static::getUiFilterPresets()))
287 {
288 $filterData[self::FIELD_PRESET_ID] = $filterRequest['PRESET_ID'];
289 }
290
291 return $filterData;
292 }
293
299 protected static function getUiFilterPresets()
300 {
301 return array();
302 }
303
312 private static function getFilterFields($filter = null, bool $checkAccessRights = true): array
313 {
314 $fields = $filter ?? static::getUiFilterFields($checkAccessRights);
315 $fields = is_array($fields) ? $fields : array();
316 $fields[] = array(
317 "id" => self::FIELD_FOR_PRESET_ALL,
318 "name" => Loc::getMessage('SENDER_CONNECTOR_BASE_FILTER'),
319 'type' => 'checkbox',
320 "default" => false,
321 "sender_segment_filter" => false,
322 );
323
324 return $fields;
325 }
326
327 protected function clearFilterState($filterId)
328 {
329 $filterOptions = new FilterOptions($filterId, static::getUiFilterPresets());
330 $settings = $filterOptions->getOptions();
331 $cleared = false;
332 if ($settings && ($settings['filter'] ?? false))
333 {
334 $filterPresetIds = ['tmp_filter', $settings['filter']];
335 foreach ($filterPresetIds as $filterPresetId)
336 {
337 $presetSettings = $filterOptions->getFilterSettings($filterPresetId);
338 if ($presetSettings && ($presetSettings['fields'] ?? false))
339 {
340 $filterOptions->setFilterSettings($filterPresetId, ['clear_filter' => 'Y']); // clear saved filter state
341 $cleared = true;
342 }
343 }
344 }
345 if ($cleared)
346 {
347 $filterOptions->save();
348 }
349 }
350}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static fetchNumberFieldValue($key="", $filterFields=array())
Definition options.php:670
setFieldValues(array $fieldValues=null)
getFieldDateTo($name, $defaultValue=null)
getFieldDateFrom($name, $defaultValue=null)
getFieldValue($name, $defaultValue=null)
Definition base.php:195
$GLOBALS['____1444769544']
Definition license.php:1