Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basefieldassembler.php
1<?php
2
4
5
11
12abstract class BaseFieldAssembler extends FieldAssembler
13{
14 protected const NORMALIZE_EMPTY = 0;
15 protected const NORMALIZE_BY_INT = 1;
16
17 protected int $iblockId;
18
19 protected array $customEditableColumnIds;
20
21 protected array $properties;
22
24 {
25 $this->iblockId = $iblockId;
26
27 $this->customEditableColumnIds = $customEditableColumnIds;
28
29 parent::__construct(
31 );
32 }
33
34 protected function getIblockId(): int
35 {
36 return $this->iblockId;
37 }
38
39 abstract protected function getPropertyFilter(): array;
40
41 abstract protected function validateProperty(array $property): ?array;
42
43 protected function loadProperties(): void
44 {
45 if (isset($this->properties))
46 {
47 return;
48 }
49
50 $this->properties = [];
51 $iterator = PropertyTable::getList([
52 'select' => [
53 'ID',
54 'IBLOCK_ID',
55 'NAME',
56 'SORT',
57 'DEFAULT_VALUE',
58 'PROPERTY_TYPE',
59 'ROW_COUNT',
60 'COL_COUNT',
61 'LIST_TYPE',
62 'MULTIPLE',
63 'FILE_TYPE',
64 'MULTIPLE_CNT',
65 'LINK_IBLOCK_ID',
66 'WITH_DESCRIPTION',
67 'IS_REQUIRED',
68 'USER_TYPE',
69 'USER_TYPE_SETTINGS_LIST',
70 'HINT',
71 ],
72 'filter' => array_merge(
73 [
74 '=IBLOCK_ID' => $this->getIblockId(),
75 '=ACTIVE' => 'Y',
76 ],
77 $this->getPropertyFilter()
78 ),
79 'order' => [
80 'SORT' => 'ASC',
81 'NAME' => 'ASC',
82 'ID' => 'ASC',
83 ],
84 'cache' => [
85 'ttl' => 86400,
86 ],
87 ]);
88
89 while ($row = $iterator->fetch())
90 {
91 $row['ID'] = (int)$row['ID'];
92 $row['IBLOCK_ID'] = (int)$row['IBLOCK_ID'];
93
94 $row['USER_TYPE'] = trim((string)$row['USER_TYPE']);
95 if ($row['USER_TYPE'] === '')
96 {
97 $row['USER_TYPE'] = null;
98 }
99
100 $row['HINT'] = trim((string)$row['HINT']);
101 if ($row['HINT'] === '')
102 {
103 $row['HINT'] = null;
104 }
105
106 $row = $this->validateProperty($row);
107
108 if ($row)
109 {
110 $this->properties[ElementPropertyProvider::getColumnIdByPropertyId($row['ID'])] = $row;
111 }
112 }
113 unset($row, $iterator);
114 }
115
116 protected function isMultipleColumn(string $columnId): bool
117 {
118 return ($this->properties[$columnId]['MULTIPLE'] ?? 'N') === 'Y';
119 }
120
121 protected function getPropertyColumnsIds(): array
122 {
123 $this->loadProperties();
124
125 return array_keys($this->properties);
126 }
127
128 protected function getColumnValues(mixed $rawValues, string $fieldName = 'VALUE'): array
129 {
130 if (!is_array($rawValues))
131 {
132 return [];
133 }
134 if (array_key_exists($fieldName, $rawValues))
135 {
136 return [$rawValues[$fieldName]];
137 }
138 else
139 {
140 $result = [];
141 foreach ($rawValues as $row)
142 {
143 if (is_array($row)&& array_key_exists($fieldName, $row))
144 {
145 $result[] = $row[$fieldName];
146 }
147 }
148
149 return $result;
150 }
151 }
152
153 protected function compileColumnValues(array $rowList, int $normalizationMode = self::NORMALIZE_EMPTY): array
154 {
155 $result = [];
156 foreach ($rowList as $row)
157 {
158 foreach ($this->getColumnIds() as $columnId)
159 {
160 $columnValues = $this->getColumnValues($row['data'][$columnId] ?? null);
161 if (!empty($columnValues))
162 {
163 $result = array_merge($result, $columnValues);
164 }
165 unset($columnValues);
166 }
167 }
168
169 if (empty($result))
170 {
171 return $result;
172 }
173
174 if ($normalizationMode === self::NORMALIZE_BY_INT)
175 {
176 Main\Type\Collection::normalizeArrayValuesByInt($result, false);
177 }
178
179 return $result;
180 }
181
182 protected static function getRowType(array $row): ?string
183 {
184 $rowType = (string)($row['data']['ROW_TYPE'] ?? '');
185 if ($rowType === RowType::ELEMENT || $rowType === RowType::SECTION)
186 {
187 return $rowType;
188 }
189
190 return null;
191 }
192
193 protected static function isElementRow(array $row): bool
194 {
195 return static::getRowType($row) === RowType::ELEMENT;
196 }
197
198 protected static function getFlatColumnValues(mixed $rawValues, $fieldName = 'VALUE')
199 {
200 if (!is_array($rawValues))
201 {
202 return null;
203 }
204 if (array_key_exists($fieldName, $rawValues))
205 {
206 return $rawValues[$fieldName];
207 }
208 else
209 {
210 $result = [];
211 foreach ($rawValues as $row)
212 {
213 if (is_array($row) && array_key_exists($fieldName, $row))
214 {
215 $result[] = $row[$fieldName];
216 }
217 }
218
219 return $result;
220 }
221 }
222}
__construct(int $iblockId, array $customEditableColumnIds)
getColumnValues(mixed $rawValues, string $fieldName='VALUE')
static getFlatColumnValues(mixed $rawValues, $fieldName='VALUE')
compileColumnValues(array $rowList, int $normalizationMode=self::NORMALIZE_EMPTY)
static getList(array $parameters=array())