Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
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
34 public static function getDescription(): array
35 {
36 return [
37 'DESCRIPTION' => Loc::getMessage('USER_TYPE_IBEL_DESCRIPTION'),
38 'BASE_TYPE' => CUserTypeManager::BASE_TYPE_INT,
39 ];
40 }
41
47 public static function renderField(array $userField, ?array $additionalParameters = []): string
48 {
49 static::getEnumList($userField, $additionalParameters);
50
51 return parent::renderField($userField, $additionalParameters);
52 }
53
61 public static function renderView(array $userField, ?array $additionalParameters = []): string
62 {
63 static::getEnumList(
64 $userField,
65 array_merge(
66 $additionalParameters ?? [],
67 ['mode' => self::MODE_VIEW]
68 )
69 );
70
71 return parent::renderView($userField, $additionalParameters);
72 }
73
81 public static function renderEdit(array $userField, ?array $additionalParameters = []): string
82 {
83 static::getEnumList(
84 $userField,
85 array_merge(
86 $additionalParameters ?? [],
87 ['mode' => self::MODE_EDIT]
88 )
89 );
90
91 return parent::renderEdit($userField, $additionalParameters);
92 }
93
94 public static function renderEditForm(array $userField, ?array $additionalParameters): string
95 {
96 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
97 if(!$enum)
98 {
99 return '';
100 }
101 $items = [];
102 while($item = $enum->GetNext())
103 {
104 $items[$item['ID']] = $item;
105 }
106 $additionalParameters['items'] = $items;
107
108 return parent::renderEditForm($userField, $additionalParameters);
109 }
110
111 public static function renderFilter(array $userField, ?array $additionalParameters): string
112 {
113 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
114 if(!$enum)
115 {
116 return '';
117 }
118 $items = [];
119 while($item = $enum->GetNext())
120 {
121 $items[$item['ID']] = $item['VALUE'];
122 }
123 $additionalParameters['items'] = $items;
124 return parent::renderFilter($userField, $additionalParameters);
125 }
126
127 public static function renderAdminListView(array $userField, ?array $additionalParameters): string
128 {
129 static $cache = [];
130 $empty_caption = '&nbsp;';
131
132 if(!array_key_exists($additionalParameters['VALUE'], $cache))
133 {
134 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
135 if(!$enum)
136 {
137 $additionalParameters['VALUE'] = $empty_caption;
138 return parent::renderAdminListView($userField, $additionalParameters);
139 }
140 while($item = $enum->GetNext())
141 {
142 $cache[$item['ID']] = $item['VALUE'];
143 }
144 }
145 if(!array_key_exists($additionalParameters['VALUE'], $cache))
146 {
147 $cache[$additionalParameters['VALUE']] = $empty_caption;
148 }
149
150 $additionalParameters['VALUE'] = $cache[$additionalParameters['VALUE']];
151 return parent::renderAdminListView($userField, $additionalParameters);
152 }
153
154 public static function renderAdminListEdit(array $userField, ?array $additionalParameters): string
155 {
156 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
157 $values = [];
158 if ($enum)
159 {
160 while($item = $enum->GetNext())
161 {
162 $values[$item['ID']] = $item['VALUE'];
163 }
164 }
165 $additionalParameters['enumItems'] = $values;
166
167 return parent::renderAdminListEdit($userField, $additionalParameters);
168 }
169
173 public static function getDbColumnType(): string
174 {
175 $connection = \Bitrix\Main\Application::getConnection();
176 $helper = $connection->getSqlHelper();
177 return $helper->getColumnTypeByField(new \Bitrix\Main\ORM\Fields\IntegerField('x'));
178 }
179
185 public static function checkFields(array $userField, $value): array
186 {
187 return [];
188 }
189
194 public static function prepareSettings(array $userField): array
195 {
196 $height = (int)($userField['SETTINGS']['LIST_HEIGHT'] ?? 1);
197 $disp = ($userField['SETTINGS']['DISPLAY'] ?? '');
198
199 if ($disp !== static::DISPLAY_CHECKBOX && $disp !== static::DISPLAY_LIST)
200 {
201 $disp = static::DISPLAY_LIST;
202 }
203
204 $iblockId = (int)($userField['SETTINGS']['IBLOCK_ID'] ?? 0);
205
206 if($iblockId <= 0)
207 {
208 $iblockId = '';
209 }
210
211 $elementId = (int)($userField['SETTINGS']['DEFAULT_VALUE'] ?? 0);
212
213 if($elementId <= 0)
214 {
215 $elementId = '';
216 }
217
218 $activeFilter = (($userField['SETTINGS']['ACTIVE_FILTER'] ?? '') === 'Y' ? 'Y' : 'N');
219
220 return [
221 'DISPLAY' => $disp,
222 'LIST_HEIGHT' => (max($height, 1)),
223 'IBLOCK_ID' => $iblockId,
224 'DEFAULT_VALUE' => $elementId,
225 'ACTIVE_FILTER' => $activeFilter,
226 ];
227 }
228
233 public static function onSearchIndex(array $userField): ?string
234 {
235 $res = '';
236 if (!isset($userField['VALUE']))
237 {
238 return $res;
239 }
240
241 if (is_array($userField['VALUE']))
242 {
243 $val = $userField['VALUE'];
244 }
245 else
246 {
247 $val = [$userField['VALUE']];
248 }
249
250 Type\Collection::normalizeArrayValuesByInt($val);
251
252 if (!empty($val) && Loader::includeModule('iblock'))
253 {
254 $iterator = Iblock\ElementTable::getList([
255 'select' => [
256 'NAME',
257 ],
258 'filter' => [
259 '@ID' => $val,
260 ],
261 ]);
262 while ($row = $iterator->fetch())
263 {
264 $res .= $row['NAME'] . "\r\n";
265 }
266 unset($row, $iterator);
267 }
268 unset($val);
269
270 return $res;
271 }
272
278 public static function getFilterData(array $userField, array $additionalParameters): array
279 {
280 $enum = call_user_func([$userField['USER_TYPE']['CLASS_NAME'], 'getlist'], $userField);
281 $items = [];
282 if($enum)
283 {
284 while($item = $enum->GetNext())
285 {
286 $items[$item['ID']] = $item['VALUE'];
287 }
288 }
289 return [
290 'id' => $additionalParameters['ID'],
291 'name' => $additionalParameters['NAME'],
292 'type' => 'list',
293 'items' => $items,
294 'params' => ['multiple' => 'Y'],
295 'filterable' => ''
296 ];
297 }
298
303 public static function getList(array $userField)
304 {
305 $iblockId = (int)($userField['SETTINGS']['IBLOCK_ID'] ?? 0);
306 $activeFilter = (string)($userField['SETTINGS']['ACTIVE_FILTER'] ?? 'N');
307
308 if (self::$iblockIncluded === null)
309 {
310 self::$iblockIncluded = Loader::includeModule('iblock');
311 }
312 if ($iblockId <= 0 || !self::$iblockIncluded)
313 {
314 return false;
315 }
316
317 $cacheTtl = 86400;
318
319 $iblockRights = self::getIblockRightsMode($iblockId, $cacheTtl);
320 if ($iblockRights === null)
321 {
322 return false;
323 }
324
325 $result = false;
326 $filter = [
327 'IBLOCK_ID' => $iblockId
328 ];
329 if ($iblockRights === Iblock\IblockTable::RIGHTS_SIMPLE)
330 {
331 if ($activeFilter === 'Y')
332 {
333 $filter['=ACTIVE'] = 'Y';
334 }
335
336 $rows = [];
337 $elements = \Bitrix\Iblock\ElementTable::getList([
338 'select' => [
339 'ID',
340 'NAME',
341 ],
342 'filter' => \CIBlockElement::getPublicElementsOrmFilter($filter),
343 'order' => [
344 'NAME' => 'ASC',
345 'ID' => 'ASC',
346 ],
347 'cache' => [
348 'ttl' => $cacheTtl,
349 ],
350 ]);
351
352 while($element = $elements->fetch())
353 {
354 $rows[] = $element;
355 }
356 unset($elements);
357
358 if (!empty($rows))
359 {
360 $result = new \CIBlockElementEnum();
361 $result->InitFromArray($rows);
362 }
363 unset($rows);
364 }
365 else
366 {
367 $filter['CHECK_PERMISSIONS'] = 'Y';
368 $filter['MIN_PERMISSION'] = \CIBlockRights::PUBLIC_READ;
369 if ($activeFilter === 'Y')
370 {
371 $filter['ACTIVE'] = 'Y';
372 }
373
374 $result = \CIBlockElement::GetList(
375 [
376 'NAME' => 'ASC',
377 'ID' => 'ASC',
378 ],
379 $filter,
380 false,
381 false,
382 [
383 'ID',
384 'NAME',
385 ]
386 );
387
388 if($result)
389 {
390 $result = new \CIBlockElementEnum($result);
391 }
392 }
393
394 return $result;
395 }
396
401 public static function getEnumList(array &$userField, array $additionalParameters = []): void
402 {
403 if (self::$iblockIncluded === null)
404 {
405 self::$iblockIncluded = Loader::includeModule('iblock');
406 }
407
408 $userField['MANDATORY'] ??= 'N';
409 $userField['SETTINGS']['IBLOCK_ID'] ??= 0;
410 $userField['SETTINGS']['SHOW_NO_VALUE'] ??= 'Y';
411 $userField['SETTINGS']['DISPLAY'] ??= '';
412 $userField['SETTINGS']['ACTIVE_FILTER'] ??= 'N';
413
414 if (
415 !self::$iblockIncluded
416 || (int)$userField['SETTINGS']['IBLOCK_ID'] <= 0
417 )
418 {
419 return;
420 }
421
422 $result = [];
423 $showNoValue = (
424 $userField['MANDATORY'] !== 'Y'
425 || $userField['SETTINGS']['SHOW_NO_VALUE'] !== 'N'
426 || (
427 isset($additionalParameters['SHOW_NO_VALUE'])
428 && $additionalParameters['SHOW_NO_VALUE'] === true
429 )
430 );
431
432 if (
433 $showNoValue
434 && (
435 $userField['SETTINGS']['DISPLAY'] !== 'CHECKBOX'
436 || $userField['MULTIPLE'] !== 'Y'
437 )
438 )
439 {
440 $result = [
441 null => static::getEmptyCaption($userField)
442 ];
443 }
444
445 $filter = [];
446
447 $checkValue = ($additionalParameters['mode'] ?? '') === self::MODE_VIEW;
448 if ($checkValue)
449 {
450 $currentValues = static::getFieldValue($userField, $additionalParameters);
451 if (!empty($currentValues))
452 {
453 if (is_array($currentValues))
454 {
455 Type\Collection::normalizeArrayValuesByInt($currentValues);
456 }
457 else
458 {
459 $currentValues = (int)$currentValues;
460 if ($currentValues <= 0)
461 {
462 $currentValues = null;
463 }
464 }
465 }
466 if (!empty($currentValues))
467 {
468 $filter['ID'] = $currentValues;
469 }
470 else
471 {
472 $userField['USER_TYPE']['FIELDS'] = $result;
473
474 return;
475 }
476 }
477 $filter['ACTIVE'] = $userField['SETTINGS']['ACTIVE_FILTER'] === 'Y';
478
479 $elements = self::getElements(
480 (int)$userField['SETTINGS']['IBLOCK_ID'],
481 $filter
482 );
483
484 if (!is_array($elements))
485 {
486 return;
487 }
488
489 if (!empty($currentValues))
490 {
491 $result = $elements;
492 }
493 else
494 {
495 $result = array_replace($result, $elements);
496 }
497
498 $userField['USER_TYPE']['FIELDS'] = $result;
499 }
500
505 public static function getEmptyCaption(array $userField): string
506 {
507 $message = ($userField['SETTINGS']['CAPTION_NO_VALUE'] ?? '');
508 return
509 $message !== ''
510 ? HtmlFilter::encode($userField['SETTINGS']['CAPTION_NO_VALUE'])
511 : Loc::getMessage('USER_TYPE_IBEL_NO_VALUE')
512 ;
513 }
514
520 public static function getAdminListEditHtmlMulty(array $userField, ?array $additionalParameters): string
521 {
522 return static::renderAdminListEdit($userField, $additionalParameters);
523 }
524
525 public static function getDefaultValue(array $userField, array $additionalParameters = [])
526 {
527 $value = ($userField['SETTINGS']['DEFAULT_VALUE'] ?? '');
528 return ($userField['MULTIPLE'] === 'Y' ? [$value] : $value);
529 }
530
531 public static function onBeforeSave($userField, $value)
532 {
533 return ($userField['MULTIPLE'] !== 'Y' && is_array($value)) ? array_shift($value) : $value;
534 }
535
536 public static function getFieldValue(array $userField, array $additionalParameters = [])
537 {
538 $valueFromForm = ($additionalParameters['bVarsFromForm'] ?? false);
539 if (!$valueFromForm && !isset($additionalParameters['VALUE']))
540 {
541 if(
542 isset($userField['ENTITY_VALUE_ID'], $userField['ENUM'])
543 && $userField['ENTITY_VALUE_ID'] <= 0
544 )
545 {
546 $value = ($userField['MULTIPLE'] === 'Y' ? [] : null);
547 foreach($userField['ENUM'] as $enum)
548 {
549 if($enum['DEF'] === 'Y')
550 {
551 if($userField['MULTIPLE'] === 'Y')
552 {
553 $value[] = $enum['ID'];
554 }
555 else
556 {
557 $value = $enum['ID'];
558 break;
559 }
560 }
561 }
562 }
563 else
564 {
565 $value = $userField['VALUE'] ?? null;
566 }
567 }
568 elseif(isset($additionalParameters['VALUE']))
569 {
570 $value = $additionalParameters['VALUE'];
571 }
572 else
573 {
574 $value = Context::getCurrent()->getRequest()->get($userField['FIELD_NAME']);
575 }
576
577 return $value;
578 }
579
580 protected static function getElements(int $iblockId, array $additionalFilter = [])
581 {
582 if (self::$iblockIncluded === null)
583 {
584 self::$iblockIncluded = Loader::includeModule('iblock');
585 }
586 if ($iblockId <= 0 || !self::$iblockIncluded)
587 {
588 return false;
589 }
590
591 $cacheTtl = 86400;
592
593 $iblockRights = self::getIblockRightsMode($iblockId, $cacheTtl);
594 if ($iblockRights === null)
595 {
596 return false;
597 }
598
599 if ($iblockRights === Iblock\IblockTable::RIGHTS_SIMPLE)
600 {
601 $filter = ['IBLOCK_ID' => $iblockId];
602 if ($additionalFilter['ACTIVE'])
603 {
604 $filter['=ACTIVE'] = 'Y';
605 }
606 if (isset($additionalFilter['ID']))
607 {
608 $filter['@ID'] = $additionalFilter['ID'];
609 }
610
611 $result = [];
612 $elements = \Bitrix\Iblock\ElementTable::getList([
613 'select' => [
614 'ID',
615 'NAME',
616 ],
617 'filter' => \CIBlockElement::getPublicElementsOrmFilter($filter),
618 'order' => [
619 'NAME' => 'ASC',
620 'ID' => 'ASC',
621 ],
622 'cache' => [
623 'ttl' => $cacheTtl,
624 ],
625 ]);
626
627 while($element = $elements->fetch())
628 {
629 $result[$element['ID']] = $element['NAME'];
630 }
631 unset($element, $elements);
632
633 if (empty($result))
634 {
635 $result = false;
636 }
637 }
638 else
639 {
640 $filter = [
641 'IBLOCK_ID' => $iblockId,
642 'CHECK_PERMISSIONS' => 'Y',
643 'MIN_PERMISSION' => \CIBlockRights::PUBLIC_READ,
644 ];
645 if ($additionalFilter['ACTIVE'])
646 {
647 $filter['ACTIVE'] = 'Y';
648 }
649 if (isset($additionalFilter['ID']))
650 {
651 $filter['ID'] = $additionalFilter['ID'];
652 }
653
654 $result = [];
655 $iterator = \CIBlockElement::GetList(
656 [
657 'NAME' => 'ASC',
658 'ID' => 'ASC',
659 ],
660 $filter,
661 false,
662 false,
663 [
664 'ID',
665 'NAME',
666 ]
667 );
668
669 while ($element = $iterator->Fetch())
670 {
671 $result[$element['ID']] = $element['NAME'];
672 }
673 unset($element, $iterator);
674 }
675
676 return $result;
677 }
678
679 private static function getIblockRightsMode(int $iblockId, int $cacheTtl): ?string
680 {
681 $iblock = Iblock\IblockTable::getRow([
682 'select' => [
683 'ID',
684 'RIGHTS_MODE',
685 ],
686 'filter' => [
687 '=ID' => $iblockId
688 ],
689 'cache' => [
690 'ttl' => $cacheTtl,
691 ],
692 ]);
693
694 return ($iblock['RIGHTS_MODE'] ?? null);
695 }
696}
static renderAdminListEdit(array $userField, ?array $additionalParameters)
static renderEdit(array $userField, ?array $additionalParameters=[])
static getDefaultValue(array $userField, array $additionalParameters=[])
static getFilterData(array $userField, array $additionalParameters)
static getElements(int $iblockId, array $additionalFilter=[])
static renderEditForm(array $userField, ?array $additionalParameters)
static getEnumList(array &$userField, array $additionalParameters=[])
static renderField(array $userField, ?array $additionalParameters=[])
static onBeforeSave($userField, $value)
static renderView(array $userField, ?array $additionalParameters=[])
static getFieldValue(array $userField, array $additionalParameters=[])
static checkFields(array $userField, $value)
static renderFilter(array $userField, ?array $additionalParameters)
static getEmptyCaption(array $userField)
static prepareSettings(array $userField)
static renderAdminListView(array $userField, ?array $additionalParameters)
static getAdminListEditHtmlMulty(array $userField, ?array $additionalParameters)
static getCurrent()
Definition context.php:241
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)