Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
elementfieldassembler.php
1<?php
2
4
9use CIBlockElement;
10
12{
13 private array $names;
14
15 protected function getPropertyFilter(): array
16 {
17 return [
18 '=PROPERTY_TYPE' => PropertyTable::TYPE_ELEMENT,
19 '=USER_TYPE' => null,
20 ];
21 }
22
23 protected function validateProperty(array $property): ?array
24 {
25 $property['LINK_IBLOCK_ID'] = (int)$property['LINK_IBLOCK_ID'];
26 if ($property['LINK_IBLOCK_ID'] <= 0)
27 {
28 $property['LINK_IBLOCK_ID'] = null;
29 }
30
31 return $property;
32 }
33
34 public function prepareRows(array $rowList): array
35 {
36 $elementIds = $this->compileColumnValues($rowList, BaseFieldAssembler::NORMALIZE_BY_INT);
37 $this->preloadNames($elementIds);
38 unset($elementIds);
39
40 return parent::prepareRows($rowList);
41 }
42
43 protected function prepareRow(array $row): array
44 {
45 if (!self::isElementRow($row))
46 {
47 return $row;
48 }
49
50 $columnIds = $this->getColumnIds();
51 if (empty($columnIds))
52 {
53 return $row;
54 }
55
56 $rowId = RowType::getIndex(self::getRowType($row), (string)($row['data']['ID'] ?? ''));
57
58 $row['columns'] ??= [];
59
60 foreach ($columnIds as $columnId)
61 {
62 $value = $this->getColumnValues($row['data'][$columnId] ?? null);
63 $viewValue = '';
64 if (!empty($value))
65 {
66 $tmp = [];
67 foreach ($value as $valueItem)
68 {
69 $tmp[] = $this->getName((int)$valueItem);
70 }
71
72 $viewValue = join(' / ', $tmp);
73 }
74
75 // view
76 $row['columns'][$columnId] ??= $viewValue;
77
78 // edit
79 if ($this->isCustomEditable($columnId))
80 {
81 $row['data']['~' . $columnId] = $this->getEditValue($rowId, $columnId, $this->properties[$columnId], $value);
82 }
83 unset($value);
84 }
85
86 return $row;
87 }
88
89 private function getName(int $id): string
90 {
91 if (!isset($this->names))
92 {
93 throw new SystemException('Before need preload elements');
94 }
95
96 return $this->names[$id] ?? '';
97 }
98
99 private function preloadNames(array $elementIds): void
100 {
101 $this->names = [];
102
103 if (empty($elementIds))
104 {
105 return;
106 }
107
108 $rows = CIBlockElement::GetList(
109 [],
110 [
111 'ID' => $elementIds,
112 ],
113 false,
114 false,
115 [
116 'ID',
117 'NAME',
118 ]
119 );
120 while ($row = $rows->Fetch())
121 {
122 $this->names[$row['ID']] = $row['NAME'];
123 }
124 unset($row, $rows);
125 }
126
127 private function isCustomEditable(string $columnId): bool
128 {
129 return in_array($columnId, $this->customEditableColumnIds);
130 }
131
132 private function getEditValue(string $rowId, string $columnId, array $property, $values): string
133 {
135 $property,
136 $values ?? null,
137 [
138 'ROW_ID' => $rowId,
139 'FIELD_NAME' => $columnId,
140 ]
141 );
142 }
143}
getColumnValues(mixed $rawValues, string $fieldName='VALUE')
compileColumnValues(array $rowList, int $normalizationMode=self::NORMALIZE_EMPTY)
static getIndex(string $type, string|int $id)
Definition rowtype.php:17
static renderSelector(array $property, array|int|string|null $values, array $config)
Definition Element.php:12