Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productmapping.php
1<?php
2
4
11use Bitrix\UI;
12
14{
15 public const FIELD_ID = 'PRODUCT_MAPPING';
16
17 public const TYPE_ID = Catalog\Product\SystemField\Type\HighloadBlock::class;
18
19 public const MAP_LANDING = 'LANDING';
20 public const MAP_FACEBOOK = 'FACEBOOK';
21
22 protected const VALUE_NAME_PREFIX = 'PRODUCT_MAPPING_TYPE_';
23
24 public static function getConfig(): ?array
25 {
26 if (!static::isAllowed())
27 {
28 return null;
29 }
30
32 $className = static::getTypeId();
33 $fieldId = static::getFieldId();
34
35 $result = [
36 'HIGHLOADBLOCK' => [
37 'TABLE_NAME' => $className::getTableName($fieldId),
38 'NAME' => $className::getName($fieldId),
39 'FIELDS' => static::getHighloadblockFields(),
40 'RIGHTS' => $className::getDefaultRights(),
41 'VALUES' => static::getHighloadblockValues(),
42 ],
43 'FIELD' => self::getUserFieldBaseParam() + [
44 'SORT' => 200,
45 'SHOW_FILTER' => 'S',
46 'SHOW_IN_LIST' => 'Y',
47 'EDIT_IN_LIST' => 'Y',
48 'IS_SEARCHABLE' => 'N',
49 'SETTINGS' => $className::getDefaultSettings(),
50 ],
51 'FIELD_CONFIG' => [
52 'HLFIELD_ID' => 'UF_NAME',
53 ],
54 ];
55
56 $result['FIELD']['SETTINGS']['DEFAULT_VALUE'] = [
57 static::MAP_LANDING,
58// static::MAP_FACEBOOK,
59 ];
60
61 $titles = static::getMessages(
62 __FILE__,
63 ['TITLES' => 'PRODUCT_MAPPING_STORAGE_TITLE',]
64 );
65
66 $result['HIGHLOADBLOCK'] = $result['HIGHLOADBLOCK'] + $titles;
67
68
69 $result['FIELD'] += static::getMessages(
70 __FILE__,
71 [
72 'EDIT_FORM_LABEL' => 'PRODUCT_MAPPING_FIELD_TITLE',
73 'LIST_COLUMN_LABEL' => 'PRODUCT_MAPPING_FIELD_TITLE',
74 'LIST_FILTER_LABEL' => 'PRODUCT_MAPPING_FIELD_TITLE',
75 'HELP_MESSAGE' => 'PRODUCT_MAPPING_FIELD_TITLE_HINT',
76 ]
77 );
78
79 return $result;
80 }
81
82 public static function isAllowed(): bool
83 {
84 return Type\HighloadBlock::isAllowed() && static::isBitrix24();
85 }
86
87 protected static function getTitleInternal(): ?string
88 {
89 return Loc::getMessage('PRODUCT_MAPPING_FIELD_TITLE');
90 }
91
92 public static function getUserFieldBaseParam(): array
93 {
95 $className = static::getTypeId();
96 $fieldId = static::getFieldId();
97
98 return [
99 'ENTITY_ID' => Catalog\ProductTable::getUfId(),
100 'FIELD_NAME' => static::getUserFieldName($fieldId),
101 'USER_TYPE_ID' => $className::getUserTypeId(),
102 'XML_ID' => $fieldId,
103 'MULTIPLE' => 'Y',
104 'MANDATORY' => 'N',
105 ];
106 }
107
111 protected static function getHighloadblockFields(): array
112 {
113 $result = [];
114
115 $fieldSettings = [
116 'XML_ID' => [
117 'DEFAULT_VALUE' => '',
118 'SIZE' => 50,
119 'ROWS' => 1,
120 'MIN_LENGTH' => 1,
121 'MAX_LENGTH' => 50,
122 'REGEXP' => ''
123 ],
124 'NAME' => [
125 'DEFAULT_VALUE' => '',
126 'SIZE' => 100,
127 'ROWS' => 1,
128 'MIN_LENGTH' => 1,
129 'MAX_LENGTH' => 255,
130 'REGEXP' => ''
131 ]
132 ];
133
134 $sort = 100;
135 foreach (array_keys($fieldSettings) as $fieldId)
136 {
137 $messageList = static::getMessages(
138 __FILE__,
139 [
140 'EDIT_FORM_LABEL' => 'PRODUCT_MAPPING_UF_FIELD_'.$fieldId,
141 'LIST_COLUMN_LABEL' => 'PRODUCT_MAPPING_UF_FIELD_'.$fieldId,
142 'LIST_FILTER_LABEL' => 'PRODUCT_MAPPING_UF_FIELD_'.$fieldId
143 ]
144 );
145
146 $result[] = [
147 'FIELD_NAME' => static::getUserFieldName($fieldId),
148 'USER_TYPE_ID' => Main\UserField\Types\StringType::USER_TYPE_ID,
149 'XML_ID' => $fieldId,
150 'SORT' => $sort,
151 'MULTIPLE' => 'N',
152 'MANDATORY' => 'Y',
153 'SHOW_FILTER' => 'S',
154 'SHOW_IN_LIST' => 'Y',
155 'EDIT_IN_LIST' => 'Y',
156 'IS_SEARCHABLE' => 'N',
157 'SETTINGS' => $fieldSettings[$fieldId],
158 ] + $messageList;
159 $sort += 100;
160 }
161
162 return $result;
163 }
164
165 protected static function getHighloadblockValues(): array
166 {
167 $mapId = [
168 self::MAP_LANDING,
169// self::MAP_FACEBOOK
170 ];
171
172 $result = [];
173
174 foreach ($mapId as $id)
175 {
176 $title = (string)Loc::getMessage(self::VALUE_NAME_PREFIX.$id);
177 $result[] = [
178 'UF_XML_ID' => $id,
179 'UF_NAME' => $title ?: $id,
180 ];
181 }
182
183 return $result;
184 }
185
186 protected static function getGridActionConfig(ProductGroupAction $panel): ?array
187 {
188 $catalog = $panel->getCatalogConfig();
189 if (empty($catalog))
190 {
191 return null;
192 }
193 if (
194 $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_CATALOG
195 && $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_FULL
196 && $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_PRODUCT
197 )
198 {
199 return null;
200 }
201
202 $field = static::load();
203 if (empty($field))
204 {
205 return null;
206 }
207
208 $config = [
209 'USER_FIELD' => $field,
210 ];
211 $config['VISUAL'] = [
212 'LIST' => [
213 'ID' => $panel->getFormRowFieldId($field['FIELD_NAME']),
214 'NAME' => $panel->getFormRowFieldName($field['FIELD_NAME']),
215 ]
216 ];
217
218 return $config;
219 }
220
221 public static function getAllowedProductTypeList(): array
222 {
223 return [
224 Catalog\ProductTable::TYPE_PRODUCT,
225 Catalog\ProductTable::TYPE_SET,
226 Catalog\ProductTable::TYPE_SKU,
227 Catalog\ProductTable::TYPE_SERVICE,
228 ];
229 }
230
231 public static function getAllowedOperations(): array
232 {
233 return [
234 Catalog\Product\SystemField::OPERATION_EXPORT,
235 Catalog\Product\SystemField::OPERATION_IMPORT,
236 ];
237 }
238
239 public static function getOperationSelectFieldList(string $operation): array
240 {
241 if (!static::isAllowed())
242 {
243 return [];
244 }
245
246 $fields = static::getUserFieldBaseParam();
247 switch($operation)
248 {
249 case Catalog\Product\SystemField::OPERATION_EXPORT:
250 case Catalog\Product\SystemField::OPERATION_IMPORT:
251 $result = [
252 $fields['XML_ID'] => $fields['FIELD_NAME'],
253 ];
254 break;
255 default:
256 $result = [];
257 break;
258 }
259
260 return $result;
261 }
262
263 public static function prepareValue(string $operation, array $productRow): array
264 {
265 $field = static::load();
266 if ($field === null)
267 {
268 return $productRow;
269 }
270 if (!array_key_exists($field['XML_ID'], $productRow))
271 {
272 return $productRow;
273 }
274
275 switch ($operation)
276 {
277 case Catalog\Product\SystemField::OPERATION_IMPORT:
278 $productRow = self::prepareValueForImport($field, $productRow);
279 break;
280 case Catalog\Product\SystemField::OPERATION_EXPORT:
281 $productRow = self::prepareValueForExport($field, $productRow);
282 break;
283 }
284
285 return $productRow;
286 }
287
288 private static function prepareValueForImport(array $field, array $productRow): array
289 {
290 if (!is_array($productRow[$field['XML_ID']]))
291 {
292 $productRow[$field['XML_ID']] = [];
293 }
294 if (!empty($productRow[$field['XML_ID']]))
295 {
296 $productRow[$field['FIELD_NAME']] = array_values(self::getIdListByXmlId(
297 $field['SETTINGS']['HLBLOCK_ID'],
298 $productRow[$field['XML_ID']]
299 ));
300 }
301 else
302 {
303 $productRow[$field['FIELD_NAME']] = [];
304 }
305 unset($productRow[$field['XML_ID']]);
306
307 return $productRow;
308 }
309
310 private static function prepareValueForExport(array $field, array $productRow): array
311 {
312 if (!is_array($productRow[$field['XML_ID']]))
313 {
314 $productRow[$field['XML_ID']] = [];
315 }
316 if (!empty($productRow[$field['XML_ID']]))
317 {
318 $productRow[$field['XML_ID']] = array_values(self::getXmlIdListById(
319 $field['SETTINGS']['HLBLOCK_ID'],
320 $productRow[$field['XML_ID']]
321 ));
322 }
323
324 return $productRow;
325 }
326
327 public static function getExtendedFilterByArea(array $filter, string $areaXmlId): array
328 {
329 if (!static::isAllowed())
330 {
331 return $filter;
332 }
333 if ($areaXmlId === '')
334 {
335 return $filter;
336 }
337
338 $userField = static::load();
339 if ($userField === null)
340 {
341 return $filter;
342 }
343
344 if (empty($userField['SETTINGS']) || !is_array($userField['SETTINGS']))
345 {
346 return $filter;
347 }
348
350 $className = static::getTypeId();
351
352 $list = $className::getIdByXmlId((int)$userField['SETTINGS']['HLBLOCK_ID'], [$areaXmlId]);
353 if (!isset($list[$areaXmlId]))
354 {
355 return $filter;
356 }
357
358 $filter['=PRODUCT_'.static::getUserFieldName(static::getFieldId())] = $list[$areaXmlId];
359
360 return $filter;
361 }
362
363 protected static function afterLoadInternalModify(array $row): array
364 {
365 $row = parent::afterLoadInternalModify($row);
366 if (empty($row['SETTINGS']) || !is_array($row['SETTINGS']))
367 {
368 $row['SETTINGS'] = [];
369 }
370 $row['SETTINGS']['HLBLOCK_ID'] = (int)($row['SETTINGS']['HLBLOCK_ID'] ?? 0);
371 $row['SETTINGS']['HLFIELD_ID'] = (int)($row['SETTINGS']['HLFIELD_ID'] ?? 0);
372
373 return $row;
374 }
375
376 public static function updateProductFormConfiguration(): void
377 {
378 if (!static::isAllowed())
379 {
380 return;
381 }
382 $field = static::load();
383 if ($field === null)
384 {
385 return;
386 }
387
388 Catalog\Update\UiFormConfiguration::addFormField(
389 [
390 'name' => $field['FIELD_NAME'],
391 'optionFlags' => '1',
392 'options' => [
393 'showCode' => 'true',
394 ]
395 ],
396 Catalog\Update\UiFormConfiguration::PARENT_SECTION_MAIN
397 );
398 }
399
400 public static function renderAdminFormControl(array $field, array $product, array $config): ?string
401 {
402 if (!AccessController::getCurrent()->check(ActionDictionary::ACTION_PRODUCT_PUBLIC_VISIBILITY_SET))
403 {
404 $field['EDIT_IN_LIST'] = 'N';
405 }
406
407 return parent::renderAdminFormControl($field, $product, $config);
408 }
409
410 protected static function getUiDescriptionInternal(array $description, array $userField, array $restrictions): ?array
411 {
412 $description['type'] = UI\EntityForm\Control\Type::MULTI_LIST;
413
414 $config = [
415 'RESULT' => [
416 'RETURN_FIELD_ID' => 'Y',
417 ],
418 ];
419
420 $items = Type\HighloadBlock::getItems($userField, $config);
421 if ($items !== null)
422 {
423 $description['data'] += [
424 'items' => $items
425 ];
426 }
427 unset($items);
428
429 if (!AccessController::getCurrent()->check(ActionDictionary::ACTION_PRODUCT_PUBLIC_VISIBILITY_SET))
430 {
431 $description['editable'] = false;
432 $description['defaultValue'] = [];
433 $description['lockText'] = Loc::getMessage('PRODUCT_MAPPING_FIELD_LOCK_TEXT');
434 }
435
436 return $description;
437 }
438}
static getUiDescriptionInternal(array $description, array $userField, array $restrictions)
static prepareValue(string $operation, array $productRow)
static getGridActionConfig(ProductGroupAction $panel)
static renderAdminFormControl(array $field, array $product, array $config)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29