Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
uiformconfiguration.php
1<?php
2
4
6use Bitrix\Ui\Form\EntityEditorConfigScope;
7
9{
10 protected const SET_MODE_FIRST = 'first';
11 protected const SET_MODE_LAST = 'last';
12 protected const SET_MODE_BEFORE = 'before';
13 protected const SET_MODE_AFTER = 'after';
14
15 protected const FORM_CATEGORY = 'ui.form.editor';
16
17 protected const PRODUCT_FORM_ID = 'CATALOG_PRODUCT_CARD';
18
19 public const PARENT_SECTION_MAIN = 'main';
20
27 protected static function isValidColumnConfig($config): bool
28 {
29 if (empty($config) || !is_array($config))
30 {
31 return false;
32 }
33 if (empty($config['elements']) || !is_array($config['elements']))
34 {
35 return false;
36 }
37
38 return true;
39 }
40
47 protected static function isValidElementListConfig($config): bool
48 {
49 if (empty($config) || !is_array($config))
50 {
51 return false;
52 }
53 if (
54 !isset($config['type'])
55 || (
56 empty($config['elements'])
57 || !is_array($config['elements'])
58 )
59 )
60 {
61 return false;
62 }
63
64 return true;
65 }
66
73 protected static function isValidRowConfig($config): bool
74 {
75 if (empty($config) || !is_array($config))
76 {
77 return false;
78 }
79 if (!isset($config['name']))
80 {
81 return false;
82 }
83
84 return true;
85 }
86
87 protected static function getFieldIndex(array $formSettings, string $fieldName): ?array
88 {
89 if (
90 empty($formSettings)
91 || $fieldName === ''
92 )
93 {
94 return null;
95 }
96
97 foreach ($formSettings as $columnIndex => $column)
98 {
99 if (!static::isValidColumnConfig($column))
100 {
101 continue;
102 }
103
104 foreach ($column['elements'] as $listIndex => $list)
105 {
106 if (!static::isValidElementListConfig($list))
107 {
108 continue;
109 }
110 if ($list['type'] !== 'section')
111 {
112 continue;
113 }
114
115 foreach ($list['elements'] as $rowIndex => $row)
116 {
117 if (!static::isValidRowConfig($row))
118 {
119 continue;
120 }
121
122 if ($row['name'] === $fieldName)
123 {
124 return [
125 'COLUMN' => $columnIndex,
126 'LIST' => $listIndex,
127 'ROW' => $rowIndex,
128 ];
129 }
130 }
131 }
132 }
133
134 return null;
135 }
136
137 protected static function getListIndex(array $formSettings, string $listName): ?array
138 {
139 if (
140 empty($formSettings)
141 || $listName === ''
142 )
143 {
144 return null;
145 }
146
147 foreach ($formSettings as $columnIndex => $column)
148 {
149 if (!static::isValidColumnConfig($column))
150 {
151 continue;
152 }
153
154 foreach ($column['elements'] as $listIndex => $list)
155 {
156 if (!static::isValidElementListConfig($list))
157 {
158 continue;
159 }
160 if ($list['type'] !== 'section')
161 {
162 continue;
163 }
164 if ($list['name'] === $listName)
165 {
166 return [
167 'COLUMN' => $columnIndex,
168 'LIST' => $listIndex,
169 'ROW' => 0,
170 ];
171 }
172 }
173 }
174
175 return null;
176 }
177
178 protected static function checkRowIndex(array $index): bool
179 {
180 return (isset($index['COLUMN']) && isset($index['LIST']) && isset($index['ROW']));
181 }
182
183 protected static function isRowExists(array $formSettings, array $index): bool
184 {
185 if (empty($formSettings) || !static::checkRowIndex($index))
186 {
187 return false;
188 }
189
190 $column = $index['COLUMN'];
191 $list = $index['LIST'];
192 $row = $index['ROW'];
193
194 if (!isset($formSettings[$column]))
195 {
196 return false;
197 }
198 if (!isset($formSettings[$column]['elements'][$list]))
199 {
200 return false;
201 }
202 if (!isset($formSettings[$column]['elements'][$list]['elements'][$row]))
203 {
204 return false;
205 }
206
207 return true;
208 }
209
210 protected static function unsetField(array $formSettings, array $index): array
211 {
212 if (!static::isRowExists($formSettings, $index))
213 {
214 return $formSettings;
215 }
216
217 $column = $index['COLUMN'];
218 $list = $index['LIST'];
219 $row = $index['ROW'];
220
221 unset($formSettings[$column]['elements'][$list]['elements'][$row]);
222 $formSettings[$column]['elements'][$list]['elements'] = array_values(
223 $formSettings[$column]['elements'][$list]['elements']
224 );
225
226 return $formSettings;
227 }
228
229 protected static function replaceField(array $formSettings, array $index, array $field): array
230 {
231 if (!static::isRowExists($formSettings, $index))
232 {
233 return $formSettings;
234 }
235
236 $column = $index['COLUMN'];
237 $list = $index['LIST'];
238 $row = $index['ROW'];
239
240 $formSettings[$column]['elements'][$list]['elements'][$row] = $field;
241
242 return $formSettings;
243 }
244
245 protected static function setField(array $formSettings, array $index, array $field, string $mode): array
246 {
247 if (empty($formSettings) || !static::checkRowIndex($index))
248 {
249 return $formSettings;
250 }
251
252 $column = $index['COLUMN'];
253 $list = $index['LIST'];
254 $row = $index['ROW'];
255
256 if (!isset($formSettings[$column]))
257 {
258 return $formSettings;
259 }
260 if (!isset($formSettings[$column]['elements'][$list]))
261 {
262 return $formSettings;
263 }
264 if (
265 !isset($formSettings[$column]['elements'][$list]['elements'])
266 || !is_array($formSettings[$column]['elements'][$list]['elements'])
267 )
268 {
269 return $formSettings;
270 }
271
272 switch ($mode)
273 {
275 array_unshift(
276 $formSettings[$column]['elements'][$list]['elements'],
277 $field
278 );
279 break;
281 $formSettings[$column]['elements'][$list]['elements'][] = $field;
282 break;
284 if (static::isRowExists($formSettings, $index))
285 {
286 if ($row === 0)
287 {
288 array_unshift(
289 $formSettings[$column]['elements'][$list]['elements'],
290 $field
291 );
292 }
293 else
294 {
295 $before = array_slice($formSettings[$column]['elements'][$list]['elements'], 0, $row);
296 $before[] = $field;
297 $after = array_slice($formSettings[$column]['elements'][$list]['elements'], $row);
298 $formSettings[$column]['elements'][$list]['elements'] = array_merge(
299 $before,
300 $after
301 );
302 unset($after, $before);
303 }
304 }
305 break;
307 if (static::isRowExists($formSettings, $index))
308 {
309 if ($row === count($formSettings[$column]['elements'][$list]['elements']))
310 {
311 $formSettings[$column]['elements'][$list]['elements'][] = $field;
312 }
313 else
314 {
315 $before = array_slice($formSettings[$column]['elements'][$list]['elements'], 0, $row + 1);
316 $before[] = $field;
317 $after = array_slice($formSettings[$column]['elements'][$list]['elements'], $row + 1);
318 $formSettings[$column]['elements'][$list]['elements'] = array_merge(
319 $before,
320 $after
321 );
322 unset($after, $before);
323 }
324 }
325 break;
326 }
327
328 return $formSettings;
329 }
330
331 protected static function getConfiguration(): EntityEditorConfiguration
332 {
333 return new EntityEditorConfiguration(self::FORM_CATEGORY);
334 }
335
336 public static function addFormField(array $field, string $parentId): void
337 {
338 if (empty($field) || !isset($field['name']) || !is_string($field['name']))
339 {
340 return;
341 }
342 if ($parentId === '')
343 {
344 return;
345 }
346
347 $config = static::getConfiguration();
348 $formSettings = $config->get(self::PRODUCT_FORM_ID, EntityEditorConfigScope::COMMON);
349
350 if (empty($formSettings) || !is_array($formSettings))
351 {
352 return;
353 }
354
355 if (static::getFieldIndex($formSettings, $field['name']) !== null)
356 {
357 return;
358 }
359
360 $listIndex = static::getListIndex($formSettings, $parentId);
361 if ($listIndex === null)
362 {
363 return;
364 }
365
366 $formSettings = static::setField($formSettings, $listIndex, $field, self::SET_MODE_LAST);
367 $config->set(
368 self::PRODUCT_FORM_ID,
369 $formSettings,
370 ['scope' => EntityEditorConfigScope::COMMON]
371 );
372 unset($config);
373 }
374}
static addFormField(array $field, string $parentId)
static unsetField(array $formSettings, array $index)
static replaceField(array $formSettings, array $index, array $field)
static setField(array $formSettings, array $index, array $field, string $mode)
static getListIndex(array $formSettings, string $listName)
static isRowExists(array $formSettings, array $index)
static getFieldIndex(array $formSettings, string $fieldName)