Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sectionfieldassembler.php
1<?php
2
4
9use CIBlockSection;
10
12{
13 private array $names;
14
15 protected function getPropertyFilter(): array
16 {
17 return [
18 '=PROPERTY_TYPE' => PropertyTable::TYPE_SECTION,
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)
90 {
91 if (!isset($this->names))
92 {
93 throw new SystemException('Before need preload sections');
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 = CIBlockSection::GetList(
109 [],
110 [
111 'ID' => $elementIds,
112 ],
113 false,
114 [
115 'ID',
116 'NAME',
117 ]
118 );
119 while ($row = $rows->Fetch())
120 {
121 $this->names[$row['ID']] = $row['NAME'];
122 }
123 }
124
125 private function isCustomEditable(string $columnId): bool
126 {
127 return in_array($columnId, $this->customEditableColumnIds);
128 }
129
130 private function getEditValue(string $rowId, string $columnId, array $property, $values): string
131 {
133 $property,
134 $values ?? null,
135 [
136 'ROW_ID' => $rowId,
137 'FIELD_NAME' => $columnId,
138 ]
139 );
140 }
141}
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 Section.php:12