1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
elementfilterfields.php
См. документацию.
1<?php
2
4
11use CIBlockSection;
12
14{
15 private const PROPERTY_PREFIX = 'PROPERTY_';
16 private const PROPERTY_MASK = '/^' . self::PROPERTY_PREFIX . '([0-9]+)$/';
17
18 private int $iblockId;
19 private bool $isShowXmlId;
20 private bool $isShowSections;
21 private array $propertyList = [];
22 private array $userTypeList;
23
24 public function __construct(int $iblockId, bool $isShowXmlId, bool $isShowSections)
25 {
26 $this->iblockId = $iblockId;
27 $this->isShowXmlId = $isShowXmlId;
28 $this->isShowSections = $isShowSections;
29 }
30
32 {
33 return new static(
34 $settings->getIblockId(),
35 $settings->isShowXmlId(),
36 $settings->isShowSections()
37 );
38 }
39
40 public function getElementFieldsParams(): array
41 {
42 $fields = [];
43
44 $fields['NAME'] = [
45 'type' => 'string',
46 'default' => true,
47 ];
48 $fields['ID'] = [
49 'type' => 'number',
50 'default' => true,
51 ];
52
53 if ($this->isShowSections)
54 {
55 $fields['SECTION_ID'] = [
56 'type' => 'list',
57 'default' => true,
58 'partial' => true,
59 ];
60 $fields['INCLUDE_SUBSECTIONS'] = [
61 'type' => 'checkbox',
62 'default' => true,
63 ];
64 }
65
66 $fields['ACTIVE'] = [
67 'type' => 'checkbox',
68 'default' => false,
69 ];
70
71 if ($this->isShowXmlId)
72 {
73 $fields['XML_ID'] = [
74 'type' => 'string',
75 'default' => false,
76 ];
77 }
78 $fields['CODE'] = [
79 'type' => 'string',
80 'default' => false,
81 ];
82 $fields['TIMESTAMP_X'] = [
83 'type' => 'date',
84 'default' => false,
85 ];
86 $fields['MODIFIED_BY'] = [
87 'type' => 'entity_selector',
88 'partial' => true,
89 ];
90 $fields['DATE_CREATE'] = [
91 'type' => 'date',
92 'default' => false,
93 ];
94 $fields['CREATED_BY'] = [
95 'type' => 'entity_selector',
96 'partial' => true,
97 ];
98 $fields['DATE_ACTIVE_FROM'] = [
99 'type' => 'date',
100 'default' => false,
101 ];
102 $fields['DATE_ACTIVE_TO'] = [
103 'type' => 'date',
104 'default' => false,
105 ];
106
107 return $fields;
108 }
109
111 {
112 $properties = [];
113
114 $typesMap = [
115 PropertyTable::TYPE_STRING => 'string',
116 PropertyTable::TYPE_NUMBER => 'number',
117 PropertyTable::TYPE_LIST => 'list',
118 PropertyTable::TYPE_ELEMENT => 'entity_selector',
119 PropertyTable::TYPE_SECTION => 'entity_selector',
126 ];
127
129 'select' => [
130 'ID',
131 'IBLOCK_ID',
132 'NAME',
133 'SORT',
134 'PROPERTY_TYPE',
135 'LIST_TYPE',
136 'MULTIPLE',
137 'LINK_IBLOCK_ID',
138 'USER_TYPE',
139 'USER_TYPE_SETTINGS_LIST',
140 ],
141 'filter' => [
142 '=IBLOCK_ID' => $this->iblockId,
143 '=ACTIVE' => 'Y',
144 '=FILTRABLE' => 'Y',
145 ],
146 'order' => [
147 'SORT' => 'ASC',
148 'ID' => 'ASC',
149 ],
150 'cache' => [
151 'ttl' => 84600,
152 ],
153 ]);
154 while ($row = $iterator->fetch())
155 {
156 $row = $this->validateProperty($row);
157 if ($row === null)
158 {
159 continue;
160 }
161
162 $fullType = $this->getFullPropertyType($row);
163
164 $fieldType = $typesMap[$fullType] ?? null;
165 if (!$fieldType)
166 {
167 continue;
168 }
169
170 $id = $this->getPropertyId($row['ID']);
171 $field = [
172 'type' => $fieldType,
173 'name' => $row['NAME'],
174 ];
175
176 if ($row['USER_TYPE'] === '')
177 {
178 switch ($row['PROPERTY_TYPE'])
179 {
183 $field['partial'] = true;
184 break;
185 }
186 }
187 else
188 {
189 $userType = $this->userTypeList[$row['USER_TYPE']];
190 if (isset($userType['GetUIFilterProperty']) && is_callable($userType['GetUIFilterProperty']))
191 {
192 call_user_func_array(
193 $userType['GetUIFilterProperty'],
194 [
195 $row,
196 [],
197 &$field
198 ]
199 );
200 if (empty($field) || !is_array($field))
201 {
202 continue;
203 }
204 $field['partial'] = true;
205 }
206 }
207
208 $properties[$id] = $field;
209
210 $this->propertyList[$id] = $row;
211 }
212 unset($row, $iterator);
213
214 return $properties;
215 }
216
217 public function prepareFilterValue(array $rawFilterValue): array
218 {
219 $result = [];
220 foreach ($rawFilterValue as $fieldId => $values)
221 {
222 $field = \CIBlock::MkOperationFilter($fieldId);
223 $id = $field['FIELD'];
224 if (!$this->isPropertyId($id))
225 {
226 $result[$fieldId] = $values;
227 }
228 elseif (isset($this->propertyList[$id]))
229 {
230 $prepareResult = $this->preparePropertyValues($id, $values);
231 if ($prepareResult)
232 {
233 $result[$fieldId] = $prepareResult;
234 }
235 unset($prepareResult);
236 }
237 }
238
239 return $result;
240 }
241
242 private function preparePropertyValues(string $propertyId, $values): mixed
243 {
244 $result = null;
245 $row = $this->propertyList[$propertyId];
246 if ($row['USER_TYPE'] === '')
247 {
248 $result = $values;
249 }
250 else
251 {
252 $userType = $this->userTypeList[$row['USER_TYPE']] ?? null;
253 if ($userType)
254 {
255 if (isset($userType['ConvertToDB']) && is_callable($userType['ConvertToDB']))
256 {
257 if (is_array($values))
258 {
259 $prepareValues = [];
260 foreach ($values as $item)
261 {
262 $prepareItem = $this->convertPropertyValueToDb(
263 $userType['ConvertToDB'],
264 $row,
265 $item
266 );
267 if ($prepareItem)
268 {
269 $prepareValues[] = $prepareItem;
270 }
271 unset($prepareItem);
272 }
273 if (!empty($prepareValues))
274 {
275 $result = $prepareValues;
276 }
277 unset($prepareValues);
278 }
279 else
280 {
281 $prepareValue = $this->convertPropertyValueToDb(
282 $userType['ConvertToDB'],
283 $row,
284 $values
285 );
286 if ($prepareValue)
287 {
288 $result = $prepareValue;
289 }
290 unset($prepareValue);
291 }
292 }
293 else
294 {
295 $result = $values;
296 }
297 }
298 unset($userType);
299 }
300 unset($row);
301
302 return $result;
303 }
304
305 private function convertPropertyValueToDb(callable $function, array $property, $value): mixed
306 {
307 $result = call_user_func_array(
308 $function,
309 [
310 $property,
311 [
312 'VALUE' => $value,
313 ],
314 ]
315 );
316
317 if (
318 is_array($result) && isset($result['VALUE']) && (string)$result['VALUE'] !== ''
319 )
320 {
321 return $result['VALUE'];
322 }
323
324 return null;
325 }
326
327 public function getSectionListItems(): array
328 {
329 $result = [];
330
331 $result[''] = null;
332 $result[0] = Loc::getMessage('IBLOCK_FILTER_DATAPROVIDER_ELEMENT_FIELDS_SECTION_TOP_LEVEL');
333
334 $iterator = CIBlockSection::GetList(
335 [
336 'LEFT_MARGIN' => 'ASC',
337 ],
338 [
339 'IBLOCK_ID' => $this->iblockId,
340 'CHECK_PERMISSIONS' => 'Y',
341 'MIN_PERMISSION' => 'R',
342 ],
343 false,
344 [
345 'ID',
346 'NAME',
347 'IBLOCK_ID',
348 'DEPTH_LEVEL',
349 'LEFT_MARGIN',
350 ]
351 );
352 while ($row = $iterator->Fetch())
353 {
354 $margin = max((int)$row['DEPTH_LEVEL'], 1) - 1;
355 $result[$row['ID']] = str_repeat('.', $margin) . $row['NAME'];
356 }
357 unset($row, $iterator);
358
359 return $result;
360 }
361
362 public function isPropertyEnumField(string $fieldId): bool
363 {
364 if (!$this->isPropertyId($fieldId))
365 {
366 return false;
367 }
368 if (!isset($this->propertyList[$fieldId]))
369 {
370 return false;
371 }
372 $row = $this->propertyList[$fieldId];
373
374 return
375 $row['PROPERTY_TYPE'] === PropertyTable::TYPE_LIST
376 && $row['USER_TYPE'] === ''
377 ;
378 }
379
380 /*
381 * @deprecated
382 */
383 public function getPropertyEnumFieldListItems(string $fieldId): array
384 {
385 $propertyId = $this->propertyList[$fieldId]['ID'] ?? null;
386 if ($propertyId)
387 {
388 return $this->getPropertyEnumValueListItems($propertyId);
389 }
390
391 return [];
392 }
393
394 private function getPropertyEnumValueListItems(int $propertyId): array
395 {
396 $result = [];
397 $iterator = PropertyEnumerationTable::getList([
398 'select' => [
399 'ID',
400 'VALUE',
401 'SORT',
402 ],
403 'filter' => [
404 '=PROPERTY_ID' => $propertyId,
405 ],
406 'order' => [
407 'SORT' => 'ASC',
408 'VALUE' => 'ASC',
409 'ID' => 'ASC',
410 ],
411 'cache' => [
412 'ttl' => 86400,
413 ],
414 ]);
415 while ($row = $iterator->fetch())
416 {
417 $result[$row['ID']] = $row['VALUE'];
418 }
419 unset($row, $iterator);
420
421 return $result;
422 }
423
424 private function getPropertyId(string|int $id): string
425 {
426 return self::PROPERTY_PREFIX . $id;
427 }
428
429 public function isPropertyId(string $id): bool
430 {
431 return (preg_match(self::PROPERTY_MASK, $id) === 1);
432 }
433
434 public function getPropertyDescription(string $id): ?array
435 {
436 if (!isset($this->propertyList[$id]))
437 {
438 return null;
439 }
440
441 $row = $this->propertyList[$id];
442
443 $description = null;
444 if ($row['USER_TYPE'] === '')
445 {
446 switch ($row['PROPERTY_TYPE'])
447 {
449 $description = [
450 'items' => $this->getPropertyEnumValueListItems($row['ID']),
451 ];
452 if (count($description['items']) > 1)
453 {
454 $description['params'] = [
455 'multiple' => true,
456 ];
457 }
458 break;
460 $description = $this->getElementPropertyDescription($row);
461 break;
463 $description = $this->getSectionPropertyDescription($row);
464 break;
465 }
466 }
467 else
468 {
469 $userType = $this->userTypeList[$row['USER_TYPE']];
470 if (isset($userType['GetUIFilterProperty']) && is_callable($userType['GetUIFilterProperty']))
471 {
472 $description = [];
473 call_user_func_array(
474 $userType['GetUIFilterProperty'],
475 [
476 $row,
477 [],
479 ]
480 );
481 if (empty($description) || !is_array($description))
482 {
483 $description = null;
484 }
485 }
486 }
487
488 return $description;
489 }
490
491 private function validateProperty(array $row): ?array
492 {
493 $row['ID'] = (int)$row['ID'];
494 $row['USER_TYPE'] = (string)$row['USER_TYPE'];
495 $row['USER_TYPE_SETTINGS'] = $row['USER_TYPE_SETTINGS_LIST'];
496 unset($row['USER_TYPE_SETTINGS_LIST']);
497 $row['FULL_PROPERTY_TYPE'] = $this->getFullPropertyType($row);
498 if ($row['USER_TYPE'] === '')
499 {
500 return $row;
501 }
502
503 if (!isset($this->userTypeList))
504 {
505 $this->userTypeList = \CIBlockProperty::GetUserType();
506 }
507 $userTypeId = $row['USER_TYPE'];
508 if (!isset($this->userTypeList[$userTypeId]))
509 {
510 return null;
511 }
512
513 return $row;
514 }
515
516 private function getFullPropertyType(array $row): string
517 {
518 return
519 $row['USER_TYPE'] === ''
520 ? $row['PROPERTY_TYPE']
521 : $row['PROPERTY_TYPE'] . ':' . $row['USER_TYPE']
522 ;
523 }
524
525 private function getElementPropertyDescription(array $property): array
526 {
527 return [
528 'type' => 'entity_selector',
529 'params' => [
530 'multiple' => 'Y',
531 'dialogOptions' => [
532 'entities' => [
533 [
534 'id' => IblockPropertyElementProvider::ENTITY_ID,
535 'dynamicLoad' => true,
536 'dynamicSearch' => true,
537 'options' => [
538 'iblockId' => (int)($property['LINK_IBLOCK_ID'] ?? 0),
539 ],
540 ],
541 ],
542 'searchOptions' => [
543 'allowCreateItem' => false,
544 ],
545 ],
546 ],
547 ];
548 }
549
550 private function getSectionPropertyDescription(array $property): array
551 {
552 return [
553 'type' => 'entity_selector',
554 'params' => [
555 'multiple' => 'Y',
556 'dialogOptions' => [
557 'entities' => [
558 [
559 'id' => IblockPropertySectionProvider::ENTITY_ID,
560 'dynamicLoad' => true,
561 'dynamicSearch' => true,
562 'options' => [
563 'iblockId' => (int)($property['LINK_IBLOCK_ID'] ?? 0),
564 ],
565 ],
566 ],
567 'searchOptions' => [
568 'allowCreateItem' => false,
569 ],
570 ],
571 ],
572 ];
573 }
574}
static createFromElementSettings(ElementSettings $settings)
Определения elementfilterfields.php:31
__construct(int $iblockId, bool $isShowXmlId, bool $isShowSections)
Определения elementfilterfields.php:24
const USER_TYPE_ELEMENT_AUTOCOMPLETE
Определения propertytable.php:82
const TYPE_ELEMENT
Определения propertytable.php:68
const USER_TYPE_SKU
Определения propertytable.php:83
const TYPE_STRING
Определения propertytable.php:65
const TYPE_LIST
Определения propertytable.php:70
const USER_TYPE_DATETIME
Определения propertytable.php:76
const TYPE_SECTION
Определения propertytable.php:69
const USER_TYPE_DIRECTORY
Определения propertytable.php:93
const TYPE_NUMBER
Определения propertytable.php:66
const USER_TYPE_SECTION_AUTOCOMPLETE
Определения propertytable.php:84
const USER_TYPE_DATE
Определения propertytable.php:75
static getList(array $parameters=array())
Определения datamanager.php:431
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(Loader::includeModule( 'bitrix24')) elseif(Loader::includeModule('intranet') &&CIntranetUtils::getPortalZone() !=='ru') $description
Определения .description.php:24
$settings
Определения product_settings.php:43
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if(CSalePaySystemAction::GetParamValue('BACKGROUND', false)) $margin
Определения html.php:61
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501