Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
elementpropertyprovider.php
1<?php
2
4
9
10//use CIBlockPropertyEnum;
11
13{
14 protected const COLUMN_ID_MORE_PHOTO = 'MORE_PHOTO';
15
16 protected const PREFIX_ID = 'PROPERTY_';
17
18 protected array $properties;
19
20 public function prepareColumns(): array
21 {
22 $iblockId = $this->getIblockId();
23 if ($iblockId === null)
24 {
25 return [];
26 }
27
28 $isNewCardEnabled = $this->isNewCardEnabled();
29
30 $morePhotoId = $this->getPropertyMorePhotoId();
31
32 $result = [];
33 foreach ($this->getProperties() as $row)
34 {
35 $columnId = self::getColumnIdByPropertyId($row['ID']);
36 $columnType = Grid\Column\Type::TEXT;
37 $multiple = $row['MULTIPLE'] === 'Y';
38 $preventDefault = true; // TODO: what is this
39
40 $description = [
41 'type' => $columnType,
42 'name' => $row['NAME'],
43 'necessary' => false,
44 'editable' => $multiple ? false : true,
45 'multiple' => $multiple,
46 'select' => [
47 // EMPTY! Properties must be loaded separately due to multiple values.
48 ]
49 ];
50
51 $extendedMorePhoto = $isNewCardEnabled && $row['ID'] === $morePhotoId;
52 if ($extendedMorePhoto)
53 {
55 $columnType = Grid\Column\Type::CUSTOM;
56
57 $description['editable'] = new Grid\Column\Editable\CustomConfig($columnId);
58 }
59 elseif (
60 $row['PROPERTY_TYPE'] === PropertyTable::TYPE_FILE
61 && $multiple
62 && !$extendedMorePhoto
63 )
64 {
65 $description['editable'] = false;
66 $preventDefault = false;
67 }
68
69 if (!$multiple)
70 {
71 $description['sort'] = $columnId;
72 }
73
74 if (isset($row['USER_TYPE']))
75 {
76 $description['type'] = Grid\Column\Type::CUSTOM;
77 $description['editable'] = new Grid\Column\Editable\CustomConfig($columnId);
78 }
79 elseif ($row['PROPERTY_TYPE'] === PropertyTable::TYPE_NUMBER)
80 {
81 $description['type'] = Grid\Column\Type::NUMBER;
82 $description['align'] = 'right';
83 }
84 elseif ($row['PROPERTY_TYPE'] === PropertyTable::TYPE_LIST)
85 {
86 $listItems = $this->getPropertyEnumValues($row['ID']);
87 if (!empty($listItems))
88 {
89 $description['type'] =
90 $multiple
91 ? Grid\Column\Type::MULTISELECT
92 : Grid\Column\Type::DROPDOWN
93 ;
94 $description['editable'] = new Iblock\Grid\Column\Editable\PropertyEnumerationConfig(
95 $columnId,
96 $row,
97 $listItems,
98 );
99 }
100 else
101 {
102 $description['editable'] = false;
103 }
104 unset($listItems);
105 }
106 elseif ($row['PROPERTY_TYPE'] === PropertyTable::TYPE_ELEMENT)
107 {
108 $description['type'] = Grid\Column\Type::CUSTOM;
109 $description['editable'] = new Grid\Column\Editable\CustomConfig($columnId);
110 }
111 elseif ($row['PROPERTY_TYPE'] === PropertyTable::TYPE_SECTION)
112 {
113 $description['type'] = Grid\Column\Type::CUSTOM;
114 $description['editable'] = new Grid\Column\Editable\CustomConfig($columnId);
115 }
116 elseif ($row['PROPERTY_TYPE'] === PropertyTable::TYPE_FILE)
117 {
118 $description['type'] = Grid\Column\Type::FILE;
119 }
120
121 $description['prevent_default'] = $preventDefault; // TODO: what is this
122
123 $result[$columnId] = $description;
124 }
125
126 return $this->createColumns($result);
127 }
128
129 protected function getProperties(): array
130 {
131 if (!isset($this->properties))
132 {
133 $this->loadProperties();
134 }
135
136 return $this->properties;
137 }
138
139 protected function loadProperties(): void
140 {
141 $this->properties = [];
142 $iblockId = $this->getIblockId();
143 if ($iblockId === null)
144 {
145 return;
146 }
147
148 $iterator = PropertyTable::getList([
149 'select' => [
150 '*',
151 ],
152 'filter' => [
153 '=IBLOCK_ID' => $iblockId,
154 '=ACTIVE' => 'Y',
155 ],
156 'order' => [
157 'SORT' => 'ASC',
158 'NAME' => 'ASC',
159 'ID' => 'ASC',
160 ],
161 'cache' => [
162 'ttl' => 86400,
163 ],
164 ]);
165 while ($row = $iterator->fetch())
166 {
167 $row['ID'] = (int)$row['ID'];
168 $row['SORT'] = (int)$row['SORT'];
169 $row['IBLOCK_ID'] = (int)$row['IBLOCK_ID'];
170 if ($row['USER_TYPE'] !== null)
171 {
172 $row['USER_TYPE'] = trim($row['USER_TYPE']);
173 if ($row['USER_TYPE'] === '')
174 {
175 $row['USER_TYPE'] = null;
176 }
177 }
178 $row['USER_TYPE_DESCRIPTION'] = ($row['USER_TYPE'] ? \CIBlockProperty::GetUserType($row['USER_TYPE']) : []);
179 if (!is_array($row['USER_TYPE_SETTINGS_LIST']))
180 {
181 $row['USER_TYPE_SETTINGS_LIST'] = [];
182 }
183 $row['USER_TYPE_SETTINGS'] = $row['USER_TYPE_SETTINGS_LIST'];
184 unset($row['USER_TYPE_SETTINGS_LIST']);
185
186 $this->properties[$row['ID']] = $row;
187 }
188 unset($row, $iterator);
189 }
190
191 protected function getPropertyMorePhotoId(): ?int
192 {
193 $result = null;
194 foreach ($this->getProperties() as $row)
195 {
196 if (
197 $row['PROPERTY_TYPE'] === PropertyTable::TYPE_FILE
198 && $row['CODE'] === \CIBlockPropertyTools::CODE_MORE_PHOTO
199 )
200 {
201 $result = $row['ID'];
202 break;
203 }
204 }
205
206 return $result;
207 }
208
209 private function getPropertyEnumValues(int $propertyId): array
210 {
211 $result = [];
212
213 $iterator = PropertyEnumerationTable::getList([
214 'select' => [
215 'ID',
216 'VALUE',
217 'DEF',
218 'SORT',
219 ],
220 'filter' => [
221 '=PROPERTY_ID' => $propertyId
222 ],
223 'order' => [
224 'SORT' => 'ASC',
225 'VALUE' => 'ASC',
226 'ID' => 'ASC',
227 ],
228 'cache' => [
229 'ttl' => 86400,
230 ],
231 ]);
232 while ($row = $iterator->fetch())
233 {
234 $id = (int)$row['ID'];
235 $result[$id] = [
236 'ID' => $id,
237 'VALUE' => $row['VALUE'],
238 'DEF' => $row['DEF'],
239 ];
240 }
241 unset($row, $iterator);
242
243 return $result;
244 }
245
246 #region static
247
255 public static function getPropertyIdsFromColumnsIds(array $columnIds): array
256 {
257 $result = [];
258
259 $propertyPrefixRe = '/^' . preg_quote(self::PREFIX_ID) . '(\d+)$/';
260
261 foreach ($columnIds as $columnId)
262 {
263 if (preg_match($propertyPrefixRe, $columnId, $m))
264 {
265 $result[$columnId] = (int)$m[1];
266 }
267 }
268
269 return $result;
270 }
271
277 public static function getColumnIdByPropertyId(int $id): string
278 {
279 return self::PREFIX_ID . $id;
280 }
281
282 #endregion static
283}
static getList(array $parameters=array())