1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
elementtype.php
См. документацию.
1<?php
2
4
12use CDBResult;
13use CUserTypeManager;
14
19class ElementType extends BaseType
20{
21 public const USER_TYPE_ID = 'iblock_element';
22 public const RENDER_COMPONENT = 'bitrix:iblock.field.element';
23
24 public const DISPLAY_LIST = 'LIST';
25 public const DISPLAY_CHECKBOX = 'CHECKBOX';
26 public const DISPLAY_UI = 'UI';
27 public const DISPLAY_DIALOG = 'DIALOG';
28
29 protected static ?bool $iblockIncluded = null;
30
31 protected static array $itemCache = [];
32
36 public static function getDescription(): array
37 {
38 return [
39 'DESCRIPTION' => Loc::getMessage('USER_TYPE_IBEL_DESCRIPTION'),
40 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_INT,
41 ];
42 }
43
51 public static function renderField(array $userField, ?array $additionalParameters = []): string
52 {
53 static::getEnumList($userField, $additionalParameters);
54
55 return parent::renderField($userField, $additionalParameters);
56 }
57
65 public static function renderView(array $userField, ?array $additionalParameters = []): string
66 {
67 static::getEnumList(
68 $userField,
69 array_merge(
70 $additionalParameters ?? [],
71 ['mode' => self::MODE_VIEW]
72 )
73 );
74
75 return parent::renderView($userField, $additionalParameters);
76 }
77
85 public static function renderEdit(array $userField, ?array $additionalParameters = []): string
86 {
87 static::getEnumList(
88 $userField,
89 array_merge(
90 $additionalParameters ?? [],
91 ['mode' => self::MODE_EDIT]
92 )
93 );
94
95 return parent::renderEdit($userField, $additionalParameters);
96 }
97
105 public static function renderEditForm(array $userField, ?array $additionalParameters): string
106 {
107 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
108 if(!$enum)
109 {
110 return '';
111 }
112 $items = [];
113 while($item = $enum->GetNext())
114 {
115 $items[$item['ID']] = $item;
116 }
117 $additionalParameters['items'] = $items;
118
119 return parent::renderEditForm($userField, $additionalParameters);
120 }
121
129 public static function renderFilter(array $userField, ?array $additionalParameters): string
130 {
131 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
132 if(!$enum)
133 {
134 return '';
135 }
136 $items = [];
137 while($item = $enum->GetNext())
138 {
139 $items[$item['ID']] = $item['VALUE'];
140 }
141 $additionalParameters['items'] = $items;
142 return parent::renderFilter($userField, $additionalParameters);
143 }
144
152 public static function renderAdminListView(array $userField, ?array $additionalParameters): string
153 {
154 $emptyCaption = '&nbsp;';
155
156 $value = (int)($additionalParameters['VALUE'] ?? 0);
157 $index = static::USER_TYPE_ID . $value;
158
159 if (!isset(self::$itemCache[$index]))
160 {
161 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
162 if (!$enum)
163 {
164 $additionalParameters['VALUE'] = $emptyCaption;
165
166 return parent::renderAdminListView($userField, $additionalParameters);
167 }
168 while ($item = $enum->Fetch())
169 {
170 self::$itemCache[static::USER_TYPE_ID . $item['ID']] = $item['NAME'];
171 }
172 }
173 if (!isset(self::$itemCache[$index]))
174 {
175 self::$itemCache[$index] = $emptyCaption;
176 }
177
178 $additionalParameters['VALUE'] = self::$itemCache[$index];
179
180 return parent::renderAdminListView($userField, $additionalParameters);
181 }
182
190 public static function renderAdminListEdit(array $userField, ?array $additionalParameters): string
191 {
192 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
193 $values = [];
194 if ($enum)
195 {
196 while($item = $enum->GetNext())
197 {
198 $values[$item['ID']] = $item['VALUE'];
199 }
200 }
201 $additionalParameters['enumItems'] = $values;
202
203 return parent::renderAdminListEdit($userField, $additionalParameters);
204 }
205
211 public static function getDbColumnType(): string
212 {
214 $helper = $connection->getSqlHelper();
215 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
216 }
217
225 public static function checkFields(array $userField, $value): array
226 {
227 return [];
228 }
229
236 public static function prepareSettings(array $userField): array
237 {
238 $height = (int)($userField['SETTINGS']['LIST_HEIGHT'] ?? 1);
239 $display = ($userField['SETTINGS']['DISPLAY'] ?? '');
240
241 $availableDisplayVariants = [
242 static::DISPLAY_DIALOG,
243 static::DISPLAY_UI,
244 static::DISPLAY_LIST,
245 static::DISPLAY_CHECKBOX,
246 ];
247
248 if (!in_array($display, $availableDisplayVariants, true))
249 {
250 $display = static::DISPLAY_UI;
251 }
252
253 $iblockId = (int)($userField['SETTINGS']['IBLOCK_ID'] ?? 0);
254
255 if($iblockId <= 0)
256 {
257 $iblockId = '';
258 }
259
260 $elementId = (int)($userField['SETTINGS']['DEFAULT_VALUE'] ?? 0);
261
262 if($elementId <= 0)
263 {
264 $elementId = '';
265 }
266
267 $activeFilter = (($userField['SETTINGS']['ACTIVE_FILTER'] ?? '') === 'Y' ? 'Y' : 'N');
268
269 return [
270 'DISPLAY' => $display,
271 'LIST_HEIGHT' => (max($height, 1)),
272 'IBLOCK_ID' => $iblockId,
273 'DEFAULT_VALUE' => $elementId,
274 'ACTIVE_FILTER' => $activeFilter,
275 ];
276 }
277
284 public static function onSearchIndex(array $userField): ?string
285 {
286 $res = '';
287 if (!isset($userField['VALUE']))
288 {
289 return $res;
290 }
291
292 if (is_array($userField['VALUE']))
293 {
294 $val = $userField['VALUE'];
295 }
296 else
297 {
298 $val = [$userField['VALUE']];
299 }
300
302
303 if (!empty($val) && Loader::includeModule('iblock'))
304 {
306 'select' => [
307 'NAME',
308 ],
309 'filter' => [
310 '@ID' => $val,
311 ],
312 ]);
313 while ($row = $iterator->fetch())
314 {
315 $res .= $row['NAME'] . "\r\n";
316 }
317 unset($row, $iterator);
318 }
319 unset($val);
320
321 return $res;
322 }
323
331 public static function getFilterData(array $userField, array $additionalParameters): array
332 {
333 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
334 $items = [];
335 if($enum)
336 {
337 while($item = $enum->GetNext())
338 {
339 $items[$item['ID']] = $item['VALUE'];
340 }
341 }
342 return [
343 'id' => $additionalParameters['ID'],
344 'name' => $additionalParameters['NAME'],
345 'type' => 'list',
346 'items' => $items,
347 'params' => ['multiple' => 'Y'],
348 'filterable' => ''
349 ];
350 }
351
358 public static function getList(array $userField)
359 {
360 $iblockId = (int)($userField['SETTINGS']['IBLOCK_ID'] ?? 0);
361 $activeFilter = (string)($userField['SETTINGS']['ACTIVE_FILTER'] ?? 'N');
362
363 if (self::$iblockIncluded === null)
364 {
365 self::$iblockIncluded = Loader::includeModule('iblock');
366 }
367 if ($iblockId <= 0 || !self::$iblockIncluded)
368 {
369 return false;
370 }
371
372 $cacheTtl = 86400;
373
374 $iblockRights = self::getIblockRightsMode($iblockId, $cacheTtl);
375 if ($iblockRights === null)
376 {
377 return false;
378 }
379
380 $result = false;
381 $filter = [
382 'IBLOCK_ID' => $iblockId
383 ];
384 if ($iblockRights === Iblock\IblockTable::RIGHTS_SIMPLE)
385 {
386 if ($activeFilter === 'Y')
387 {
388 $filter['=ACTIVE'] = 'Y';
389 }
390
391 $rows = [];
393 'select' => [
394 'ID',
395 'NAME',
396 ],
397 'filter' => \CIBlockElement::getPublicElementsOrmFilter($filter),
398 'order' => [
399 'NAME' => 'ASC',
400 'ID' => 'ASC',
401 ],
402 'cache' => [
403 'ttl' => $cacheTtl,
404 ],
405 ]);
406
407 while($element = $elements->fetch())
408 {
409 $rows[] = $element;
410 }
411 unset($elements);
412
413 if (!empty($rows))
414 {
415 $result = new \CIBlockElementEnum();
416 $result->InitFromArray($rows);
417 }
418 unset($rows);
419 }
420 else
421 {
422 $filter['CHECK_PERMISSIONS'] = 'Y';
423 $filter['MIN_PERMISSION'] = \CIBlockRights::PUBLIC_READ;
424 if ($activeFilter === 'Y')
425 {
426 $filter['ACTIVE'] = 'Y';
427 }
428
429 $result = \CIBlockElement::GetList(
430 [
431 'NAME' => 'ASC',
432 'ID' => 'ASC',
433 ],
434 $filter,
435 false,
436 false,
437 [
438 'ID',
439 'NAME',
440 ]
441 );
442
443 if($result)
444 {
445 $result = new \CIBlockElementEnum($result);
446 }
447 }
448
449 return $result;
450 }
451
459 public static function getEnumList(array &$userField, array $additionalParameters = []): void
460 {
461 if (self::$iblockIncluded === null)
462 {
463 self::$iblockIncluded = Loader::includeModule('iblock');
464 }
465
466 $userField['MANDATORY'] ??= 'N';
467 $userField['SETTINGS']['IBLOCK_ID'] ??= 0;
468 $userField['SETTINGS']['SHOW_NO_VALUE'] ??= 'Y';
469 $userField['SETTINGS']['DISPLAY'] ??= '';
470 $userField['SETTINGS']['ACTIVE_FILTER'] ??= 'N';
471
472 if (
473 !self::$iblockIncluded
474 || (int)$userField['SETTINGS']['IBLOCK_ID'] <= 0
475 )
476 {
477 return;
478 }
479
480 $result = [];
481 $showNoValue = (
482 $userField['MANDATORY'] !== 'Y'
483 || $userField['SETTINGS']['SHOW_NO_VALUE'] !== 'N'
484 || (
485 isset($additionalParameters['SHOW_NO_VALUE'])
486 && $additionalParameters['SHOW_NO_VALUE'] === true
487 )
488 );
489
490 if (
491 $showNoValue
492 && (
493 $userField['SETTINGS']['DISPLAY'] !== 'CHECKBOX'
494 || $userField['MULTIPLE'] !== 'Y'
495 )
496 )
497 {
498 $result = [
499 null => static::getEmptyCaption($userField)
500 ];
501 }
502
503 $filter = [];
504
505 if (isset($additionalParameters['SKIP_CHECK_PERMISSIONS']) && $additionalParameters['SKIP_CHECK_PERMISSIONS'])
506 {
507 $filter['CHECK_PERMISSIONS'] = 'N';
508 }
509
510 $checkValue = ($additionalParameters['mode'] ?? '') === self::MODE_VIEW;
511 if ($checkValue)
512 {
513 $currentValues = static::getFieldValue($userField, $additionalParameters);
514 if (!empty($currentValues))
515 {
516 if (is_array($currentValues))
517 {
519 }
520 else
521 {
523 if ($currentValues <= 0)
524 {
525 $currentValues = null;
526 }
527 }
528 }
529 if (!empty($currentValues))
530 {
531 $filter['ID'] = $currentValues;
532 }
533 else
534 {
535 $userField['USER_TYPE']['FIELDS'] = $result;
536
537 return;
538 }
539 }
540 $filter['ACTIVE'] = $userField['SETTINGS']['ACTIVE_FILTER'] === 'Y';
541
542 $elements = self::getElements(
543 (int)$userField['SETTINGS']['IBLOCK_ID'],
544 $filter
545 );
546
547 if (!is_array($elements))
548 {
549 return;
550 }
551
552 if (!empty($currentValues))
553 {
554 $result = $elements;
555 }
556 else
557 {
558 $result = array_replace($result, $elements);
559 }
560
561 $userField['USER_TYPE']['FIELDS'] = $result;
562 }
563
570 public static function getEmptyCaption(array $userField): string
571 {
572 $message = ($userField['SETTINGS']['CAPTION_NO_VALUE'] ?? '');
573 return
574 $message !== ''
575 ? HtmlFilter::encode($userField['SETTINGS']['CAPTION_NO_VALUE'])
576 : Loc::getMessage('USER_TYPE_IBEL_NO_VALUE')
577 ;
578 }
579
587 public static function getAdminListEditHtmlMulty(array $userField, ?array $additionalParameters): string
588 {
589 return static::renderAdminListEdit($userField, $additionalParameters);
590 }
591
599 public static function getDefaultValue(array $userField, array $additionalParameters = [])
600 {
601 $value = ($userField['SETTINGS']['DEFAULT_VALUE'] ?? '');
602 return ($userField['MULTIPLE'] === 'Y' ? [$value] : $value);
603 }
604
612 public static function onBeforeSave($userField, $value)
613 {
614 return ($userField['MULTIPLE'] !== 'Y' && is_array($value)) ? array_shift($value) : $value;
615 }
616
624 public static function getFieldValue(array $userField, array $additionalParameters = [])
625 {
626 $valueFromForm = ($additionalParameters['bVarsFromForm'] ?? false);
627 if (!$valueFromForm && !isset($additionalParameters['VALUE']))
628 {
629 if(
630 isset($userField['ENTITY_VALUE_ID'], $userField['ENUM'])
631 && $userField['ENTITY_VALUE_ID'] <= 0
632 )
633 {
634 $value = ($userField['MULTIPLE'] === 'Y' ? [] : null);
635 foreach($userField['ENUM'] as $enum)
636 {
637 if($enum['DEF'] === 'Y')
638 {
639 if($userField['MULTIPLE'] === 'Y')
640 {
641 $value[] = $enum['ID'];
642 }
643 else
644 {
645 $value = $enum['ID'];
646 break;
647 }
648 }
649 }
650 }
651 else
652 {
653 $value = $userField['VALUE'] ?? null;
654 }
655 }
656 elseif(isset($additionalParameters['VALUE']))
657 {
658 $value = $additionalParameters['VALUE'];
659 }
660 else
661 {
662 $value = Context::getCurrent()->getRequest()->get($userField['FIELD_NAME']);
663 }
664
665 return $value;
666 }
667
668 protected static function getElements(int $iblockId, array $additionalFilter = [])
669 {
670 if (self::$iblockIncluded === null)
671 {
672 self::$iblockIncluded = Loader::includeModule('iblock');
673 }
674 if ($iblockId <= 0 || !self::$iblockIncluded)
675 {
676 return false;
677 }
678
679 $cacheTtl = 86400;
680
681 $iblockRights = self::getIblockRightsMode($iblockId, $cacheTtl);
682 if ($iblockRights === null)
683 {
684 return false;
685 }
686
687 $additionalFilter['ACTIVE'] ??= false;
688
689 if ($iblockRights === Iblock\IblockTable::RIGHTS_SIMPLE)
690 {
691 $filter = ['IBLOCK_ID' => $iblockId];
692 if ($additionalFilter['ACTIVE'])
693 {
694 $filter['=ACTIVE'] = 'Y';
695 }
696 if (isset($additionalFilter['ID']))
697 {
698 $filter['@ID'] = $additionalFilter['ID'];
699 }
700
701 $result = [];
703 'select' => [
704 'ID',
705 'NAME',
706 ],
707 'filter' => \CIBlockElement::getPublicElementsOrmFilter($filter),
708 'order' => [
709 'NAME' => 'ASC',
710 'ID' => 'ASC',
711 ],
712 'cache' => [
713 'ttl' => $cacheTtl,
714 ],
715 ]);
716
717 while($element = $elements->fetch())
718 {
719 $result[$element['ID']] = $element['NAME'];
720 }
721 unset($element, $elements);
722
723 if (empty($result))
724 {
725 $result = false;
726 }
727 }
728 else
729 {
730 $filter = [
731 'IBLOCK_ID' => $iblockId,
732 'CHECK_PERMISSIONS' => $additionalFilter['CHECK_PERMISSIONS'] ?? 'Y',
733 'MIN_PERMISSION' => \CIBlockRights::PUBLIC_READ,
734 ];
735 if ($additionalFilter['ACTIVE'])
736 {
737 $filter['ACTIVE'] = 'Y';
738 }
739 if (isset($additionalFilter['ID']))
740 {
741 $filter['ID'] = $additionalFilter['ID'];
742 }
743
744 $result = [];
745 $iterator = \CIBlockElement::GetList(
746 [
747 'NAME' => 'ASC',
748 'ID' => 'ASC',
749 ],
750 $filter,
751 false,
752 false,
753 [
754 'ID',
755 'NAME',
756 ]
757 );
758
759 while ($element = $iterator->Fetch())
760 {
761 $result[$element['ID']] = $element['NAME'];
762 }
763 unset($element, $iterator);
764 }
765
766 return $result;
767 }
768
769 private static function getIblockRightsMode(int $iblockId, int $cacheTtl): ?string
770 {
772 'select' => [
773 'ID',
774 'RIGHTS_MODE',
775 ],
776 'filter' => [
777 '=ID' => $iblockId
778 ],
779 'cache' => [
780 'ttl' => $cacheTtl,
781 ],
782 ]);
783
784 return ($iblock['RIGHTS_MODE'] ?? null);
785 }
786
792 public static function canUseDialogAndUiViews(): bool
793 {
794 return true;
795 }
796}
$connection
Определения actionsdefinitions.php:38
const RIGHTS_SIMPLE
Определения iblocktable.php:87
static renderAdminListEdit(array $userField, ?array $additionalParameters)
Определения elementtype.php:190
static renderEdit(array $userField, ?array $additionalParameters=[])
Определения elementtype.php:85
static getDefaultValue(array $userField, array $additionalParameters=[])
Определения elementtype.php:599
static getFilterData(array $userField, array $additionalParameters)
Определения elementtype.php:331
static getElements(int $iblockId, array $additionalFilter=[])
Определения elementtype.php:668
static renderEditForm(array $userField, ?array $additionalParameters)
Определения elementtype.php:105
static getEnumList(array &$userField, array $additionalParameters=[])
Определения elementtype.php:459
static getList(array $userField)
Определения elementtype.php:358
static renderField(array $userField, ?array $additionalParameters=[])
Определения elementtype.php:51
static onBeforeSave($userField, $value)
Определения elementtype.php:612
static renderView(array $userField, ?array $additionalParameters=[])
Определения elementtype.php:65
static onSearchIndex(array $userField)
Определения elementtype.php:284
static bool $iblockIncluded
Определения elementtype.php:29
static getFieldValue(array $userField, array $additionalParameters=[])
Определения elementtype.php:624
static checkFields(array $userField, $value)
Определения elementtype.php:225
static renderFilter(array $userField, ?array $additionalParameters)
Определения elementtype.php:129
static getEmptyCaption(array $userField)
Определения elementtype.php:570
static prepareSettings(array $userField)
Определения elementtype.php:236
static renderAdminListView(array $userField, ?array $additionalParameters)
Определения elementtype.php:152
static getAdminListEditHtmlMulty(array $userField, ?array $additionalParameters)
Определения elementtype.php:587
static getConnection($name="")
Определения application.php:638
Определения loader.php:13
static includeModule($moduleName)
Определения loader.php:67
static getRow(array $parameters)
Определения datamanager.php:398
static getList(array $parameters=array())
Определения datamanager.php:431
static normalizeArrayValuesByInt(&$map, $sorted=true)
Определения collection.php:150
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
if($request->isPost() && $currentAction !==null &&check_bitrix_sessid()) $currentValues
Определения options.php:198
$iblockId
Определения iblock_catalog_edit.php:30
if(! $catalogEdit->isSuccess()) $iblock
Определения iblock_catalog_edit.php:38
$filter
Определения iblock_catalog_list.php:54
Определения culture.php:9
Определения collection.php:2
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$items
Определения template.php:224
$val
Определения options.php:1793
$rows
Определения options.php:264
$iterator
Определения yandex_run.php:610