Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
DirectoryItemsProvider.php
1<?php
2
4
13use CFile;
14use CUserTypeManager;
15
20{
21 private int $propertyId;
22 private array $columns;
23 private ?int $directoryId;
24 private ?Entity $directoryEntity;
25
31 public function __construct(int $propertyId, ?int $directoryId, ?Entity $directoryEntity)
32 {
33 $this->propertyId = $propertyId;
34 $this->directoryId = $directoryId;
35 $this->directoryEntity = $directoryEntity;
36 }
37
41 public function getId(): string
42 {
43 return "iblock_property_{$this->propertyId}_directory_items";
44 }
45
49 public function getColumns(): array
50 {
51 if (isset($this->columns))
52 {
53 return $this->columns;
54 }
55
56 if (!isset($this->directoryEntity))
57 {
58 // default new directory columns
59 $this->columns = [
60 [
61 'id' => 'ID',
62 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_ID'),
63 'default' => true,
64 ],
65 [
66 'id' => 'UF_NAME',
67 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_NAME'),
68 'default' => true,
69 'editable' => true,
70 'type' => 'text',
71 ],
72 [
73 'id' => 'UF_SORT',
74 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_SORT'),
75 'default' => true,
76 'editable' => true,
77 'type' => 'number',
78 ],
79 [
80 'id' => 'UF_XML_ID',
81 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_XML_ID'),
82 'default' => true,
83 'editable' => true,
84 'type' => 'text',
85 ],
86 [
87 'id' => 'UF_FILE',
88 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_FILE'),
89 'default' => true,
90 'editable' => true,
91 'type' => 'image',
92 'editable' => [
93 'TYPE' => Types::IMAGE,
94 ],
95 ],
96 [
97 'id' => 'UF_LINK',
98 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_LINK'),
99 'default' => true,
100 'editable' => true,
101 'type' => 'text',
102 ],
103 [
104 'id' => 'UF_DEF',
105 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_DEF'),
106 'default' => true,
107 'editable' => true,
108 'type' => 'checkbox',
109 ],
110 [
111 'id' => 'UF_DESCRIPTION',
112 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_DESCRIPTION'),
113 'default' => true,
114 'editable' => true,
115 'type' => 'text',
116 ],
117 [
118 'id' => 'UF_FULL_DESCRIPTION',
119 'name' => Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_UF_FULL_DESCRIPTION'),
120 'default' => true,
121 'editable' => true,
122 'type' => 'text',
123 ],
124 ];
125 }
126 else
127 {
128 $this->columns = [];
129
130 $fields = $this->directoryEntity->getFields();
131 $userFieldsInfo = $this->getUserFieldsInfo();
132
133 foreach ($fields as $field)
134 {
135 $item = [
136 'default' => true,
137 'editable' => true,
138 'type' => 'text',
139 ];
140
141 if (is_array($field))
142 {
143 $item['id'] = $field['id'];
144 }
145 elseif ($field instanceof ScalarField)
146 {
147 $item['id'] = $field->getName();
148 }
149 else
150 {
151 continue;
152 }
153
154 $info = $userFieldsInfo[$item['id']] ?? null;
155
156 $item['name'] = trim((string)($info['EDIT_FORM_LABEL'] ?? ''));
157 if ($item['name'] === '')
158 {
159 $item['name'] = (string)Loc::getMessage('IBLOCK_UI_GRID_PROPERTY_DIRECTORY_ITEMS_PROVIDER_FIELD_' . $item['id']);
160 }
161 if ($item['name'] === '')
162 {
163 $item['name'] = $item['id'];
164 }
165
166 if (isset($info))
167 {
168 if ($info['USER_TYPE_ID'] === 'file')
169 {
170 $item['type'] = 'image'; // directory items only image support
171 $item['editable'] = [
172 'TYPE' => Types::IMAGE,
173 ];
174 }
175 elseif ($info['USER_TYPE_ID'] === 'double')
176 {
177 $item['type'] = 'number';
178 }
179 elseif ($info['USER_TYPE_ID'] === 'boolean')
180 {
181 $item['type'] = 'checkbox';
182 }
183 }
184
185 if (mb_strtoupper($item['id']) === 'ID')
186 {
187 $item['editable'] = false;
188 }
189
190 $this->columns[] = $item;
191 }
192 }
193
194 return $this->columns;
195 }
196
202 private function getUserFieldsInfo(): array
203 {
204 global $USER_FIELD_MANAGER;
205
210 if (!$this->directoryId)
211 {
212 return $this->directoryId;
213 }
214
215 $entityId = 'HLBLOCK_' . $this->directoryId;
216 if (!$entityId)
217 {
218 return [];
219 }
220
221 $userFieldsInfo = $USER_FIELD_MANAGER->GetUserFields($entityId, 0, LANGUAGE_ID);
222 if (empty($userFieldsInfo))
223 {
224 return [];
225 }
226
227 return $userFieldsInfo;
228 }
229
233 public function toArray(): array
234 {
235 $result = parent::toArray();
236
237 $result['SHOW_GRID_SETTINGS_MENU'] = false;
238 $result['ALLOW_EDIT_SELECTION'] = true;
239 $result['ADVANCED_EDIT_MODE'] = true;
240
241 return $result;
242 }
243
247 protected function getTemplateRow(): ?array
248 {
249 return [
250 'data' => [
251 'SORT' => 500,
252 'DEF' => 'N',
253 ],
254 ];
255 }
256
260 public function prepareRow(array $rawRow): array
261 {
262 $result = parent::prepareRow($rawRow);
263
264 foreach ($this->getColumns() as $column)
265 {
266 $id = $column['id'] ?? null;
267 if (!$id)
268 {
269 continue;
270 }
271
272 $type = $column['type'] ?? null;
273 if ($type === 'image')
274 {
275 $fileId = (int)($result['data'][$id] ?? 0);
276 if ($fileId > 0)
277 {
278 $result['data'][$id] = CFile::GetPath($fileId);
279 }
280 else
281 {
282 $result['data'][$id] = null;
283 }
284 }
285 elseif ($type === 'checkbox')
286 {
287 $value = (string)($result['data'][$id] ?? '');
288 $result['data'][$id] = $value === 'Y' || $value === '1' ? 'Y' : 'N';
289 }
290 }
291
292 return $result;
293 }
294
298 protected function getActionPanel(): ?array
299 {
300 $snippet = new Snippet();
301
302 // update remove button
303 $onchange = new Onchange();
304 $onchange->addAction([
305 'ACTION' => Actions::CALLBACK,
306 'DATA' => [
307 [
308 // the implementation must be filled in at the place of use
309 'JS' => 'javascript:;',
310 ],
311 ],
312 ]);
313
314 $removeButton = $snippet->getRemoveButton();
315 $removeButton['ONCHANGE'] = $onchange->toArray();
316
317 return [
318 'GROUPS' => [
319 [
320 'ITEMS' => [
321 $removeButton,
322 ],
323 ],
324 ],
325 ];
326 }
327}
__construct(int $propertyId, ?int $directoryId, ?Entity $directoryEntity)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29