1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
markingcodegroup.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Product\SystemField;
4
5use Bitrix\Main;
6use Bitrix\Main\Config\Option;
7use Bitrix\Main\Localization\LanguageTable;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Catalog;
10use Bitrix\Catalog\Grid\Panel\ProductGroupAction;
11use Bitrix\UI;
12
14{
15 public const FIELD_ID = 'MARKING_CODE_GROUP';
16
17 protected const SHORT_FIELD_ID = 'PRODUCT_GROUP';
18
19 public const TYPE_ID = Catalog\Product\SystemField\Type\HighloadBlock::class;
20
21 protected const VALUE_NAME_PREFIX = 'MARKING_CODE_GROUP_TYPE_';
22
23 protected const USE_PARENT_PRODUCT_VALUE = -1;
24 protected const USE_PARENT_PRODUCT_XML_VALUE = 'PARENT_MARKING_GROUP';
25
26 public static function getConfig(): ?array
27 {
28 if (!static::isAllowed())
29 {
30 return null;
31 }
32
34 $className = static::getTypeId();
35 $fieldId = static::getFieldId();
36
37 $result = [
38 'HIGHLOADBLOCK' => [
39 'TABLE_NAME' => $className::getTableName($fieldId),
40 'NAME' => $className::getName($fieldId),
41 'FIELDS' => static::getHighloadblockFields(),
42 'RIGHTS' => $className::getDefaultRights(),
43 'TRANSFORM_VALUES' => static::getHighloadblockTransformValues(),
44 'VALUES' => static::getHighloadblockValues(),
45 ],
46 'FIELD' => self::getUserFieldBaseParam() + [
47 'SORT' => 100,
48 'SHOW_FILTER' => 'S',
49 'SHOW_IN_LIST' => 'Y',
50 'EDIT_IN_LIST' => 'Y',
51 'IS_SEARCHABLE' => 'N',
52 'SETTINGS' => $className::getDefaultSettings(),
53 ],
54 'FIELD_CONFIG' => [
55 'HLFIELD_ID' => 'UF_NAME',
56 ],
57 ];
58
59 $titles = static::getMessages(
60 __FILE__,
61 ['TITLES' => 'MARKING_CODE_GROUP_STORAGE_TITLE',]
62 );
63
64 $result['HIGHLOADBLOCK'] = $result['HIGHLOADBLOCK'] + $titles;
65
66 $result['FIELD'] += static::getMessages(
67 __FILE__,
68 [
69 'EDIT_FORM_LABEL' => 'MARKING_CODE_GROUP_FIELD_TITLE',
70 'LIST_COLUMN_LABEL' => 'MARKING_CODE_GROUP_FIELD_TITLE',
71 'LIST_FILTER_LABEL' => 'MARKING_CODE_GROUP_FIELD_TITLE',
72 ]
73 );
74
75 return $result;
76 }
77
78 public static function isAllowed(): bool
79 {
81 $className = static::getTypeId();
82
83 if (!$className::isAllowed())
84 {
85 return false;
86 }
87
88 return Main\Application::getInstance()->getLicense()->getRegion() === 'ru';
89 }
90
91 protected static function getTitleInternal(): ?string
92 {
93 return Loc::getMessage('MARKING_CODE_GROUP_FIELD_TITLE');
94 }
95
96 public static function getUserFieldBaseParam(): array
97 {
99 $className = static::getTypeId();
100
101 return [
102 'ENTITY_ID' => Catalog\ProductTable::getUfId(),
103 'FIELD_NAME' => static::getUserFieldName(self::SHORT_FIELD_ID),
104 'USER_TYPE_ID' => $className::getUserTypeId(),
105 'XML_ID' => static::getFieldId(),
106 'MULTIPLE' => 'N',
107 'MANDATORY' => 'N',
108 ];
109 }
110
114 protected static function getHighloadblockFields(): array
115 {
116 $result = [];
117
118 $fieldSettings = [
119 'XML_ID' => [
120 'DEFAULT_VALUE' => '',
121 'SIZE' => 16,
122 'ROWS' => 1,
123 'MIN_LENGTH' => 0,
124 'MAX_LENGTH' => 0,
125 'REGEXP' => '/^[0-9]{1,16}$/'
126 ],
127 'NAME' => [
128 'DEFAULT_VALUE' => '',
129 'SIZE' => 100,
130 'ROWS' => 1,
131 'MIN_LENGTH' => 1,
132 'MAX_LENGTH' => 255,
133 'REGEXP' => ''
134 ]
135 ];
136
137 $sort = 100;
138 foreach (array_keys($fieldSettings) as $fieldId)
139 {
140 $messageList = static::getMessages(
141 __FILE__,
142 [
143 'EDIT_FORM_LABEL' => 'MARKING_CODE_GROUP_UF_FIELD_'.$fieldId,
144 'LIST_COLUMN_LABEL' => 'MARKING_CODE_GROUP_UF_FIELD_'.$fieldId,
145 'LIST_FILTER_LABEL' => 'MARKING_CODE_GROUP_UF_FIELD_'.$fieldId
146 ]
147 );
148
149 $result[] = [
150 'FIELD_NAME' => static::getUserFieldName($fieldId),
152 'XML_ID' => $fieldId,
153 'SORT' => $sort,
154 'MULTIPLE' => 'N',
155 'MANDATORY' => 'Y',
156 'SHOW_FILTER' => 'S',
157 'SHOW_IN_LIST' => 'Y',
158 'EDIT_IN_LIST' => 'Y',
159 'IS_SEARCHABLE' => 'N',
160 'SETTINGS' => $fieldSettings[$fieldId],
161 ] + $messageList;
162 $sort += 100;
163 }
164
165 return $result;
166 }
167
168 protected static function getHighloadblockValues(): array
169 {
170 $groupCodes = [
171 '02',
172 '03',
173 '05',
174 '17485',
175 '8258',
176 '8721',
177 '9840',
178 '06',
179 '5010',
180 '5137',
181 '5139',
182 '5140',
183 ];
184 $groupTitles = Loc::loadLanguageFile(
185 $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/catalog/regionalsystemfields/markingcodegroup.php',
186 'ru'
187 );
188
189 $result = [];
190 foreach ($groupCodes as $id)
191 {
192 $result[] = [
193 'UF_XML_ID' => $id,
194 'UF_NAME' => $groupTitles[self::VALUE_NAME_PREFIX.$id]
195 ];
196 }
197
198 return $result;
199 }
200
201 protected static function getHighloadblockTransformValues(): array
202 {
203 return [
204 [
205 'OLD_XML_ID' => '5048',
206 'NEW_XML_ID' => '17485',
207 ],
208 [
209 'OLD_XML_ID' => '5408',
210 'NEW_XML_ID' => '17485',
211 ],
212 ];
213 }
214
216 {
217 $catalog = $panel->getCatalogConfig();
218 if (empty($catalog))
219 {
220 return null;
221 }
222 $allowForOffers =
223 $catalog['CATALOG_TYPE'] === \CCatalogSku::TYPE_OFFERS
224 && self::isUsedMarkingOffer()
225 ;
226 if (
227 $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_CATALOG
228 && $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_FULL
229 && $catalog['CATALOG_TYPE'] !== \CCatalogSku::TYPE_PRODUCT
230 && !$allowForOffers
231 )
232 {
233 return null;
234 }
235
236 $field = static::load();
237 if (empty($field))
238 {
239 return null;
240 }
241
242 $config = [
243 'USER_FIELD' => $field,
244 'VISUAL' => [
245 'LIST' => [
246 'ID' => $panel->getFormRowFieldId($field['FIELD_NAME']),
247 'NAME' => $panel->getFormRowFieldName($field['FIELD_NAME']),
248 ],
249 ],
250 ];
251
252 if ($allowForOffers)
253 {
254 $config['ADDITIONAL_ITEMS'] = [
255 'LIST' => [
256 [
257 'VALUE' => self::USE_PARENT_PRODUCT_VALUE,
258 'NAME' => Loc::getMessage('MARKING_CODE_GROUP_MESS_USE_PARENT_PRODUCT_VALUE'),
259 ],
260 ]
261 ];
262 }
263
264 return $config;
265 }
266
267 public static function getAllowedProductTypeList(): array
268 {
269 $result = [
272 ];
273 if (self::isUsedMarkingOffer())
274 {
276 }
277
278 return $result;
279 }
280
281 public static function checkRestictions(array $restrictions): bool
282 {
283 if (!parent::checkRestictions($restrictions))
284 {
285 return false;
286 }
287
288 if (isset($restrictions['IBLOCK_ID']) && !isset($restrictions['CATALOG']))
289 {
291 'select' => [
292 '*',
293 ],
294 'filter' => [
295 '=IBLOCK_ID' => $restrictions['IBLOCK_ID'],
296 ],
297 'cache' => [
298 'ttl' => 86400,
299 ]
300 ]);
301 $row = $iterator->fetch();
302 if (!empty($row))
303 {
304 $restrictions['CATALOG'] = $row;
305 }
306 unset($row, $iterator);
307 }
308
309 if (!empty($restrictions['CATALOG']) && is_array($restrictions['CATALOG']))
310 {
311 if ($restrictions['CATALOG']['SUBSCRIPTION'] === 'Y')
312 {
313 return false;
314 }
315 }
316
317 return true;
318 }
319
320 public static function getAllowedOperations(): array
321 {
322 return [
323 Catalog\Product\SystemField::OPERATION_PROVIDER,
324 Catalog\Product\SystemField::OPERATION_EXPORT,
325 Catalog\Product\SystemField::OPERATION_IMPORT,
326 ];
327 }
328
329 public static function getOperationSelectFieldList(string $operation): array
330 {
331 if (!static::isAllowed())
332 {
333 return [];
334 }
335
336 $fields = static::getUserFieldBaseParam();
337 switch($operation)
338 {
339 case Catalog\Product\SystemField::OPERATION_PROVIDER:
340 case Catalog\Product\SystemField::OPERATION_EXPORT:
341 case Catalog\Product\SystemField::OPERATION_IMPORT:
342 $result = [
343 $fields['XML_ID'] => $fields['FIELD_NAME'],
344 ];
345 break;
346 default:
347 $result = [];
348 break;
349 }
350
351 return $result;
352 }
353
354 public static function prepareValue(string $operation, array $productRow): array
355 {
356 $field = static::load();
357 if ($field === null)
358 {
359 return $productRow;
360 }
361 if (!array_key_exists($field['XML_ID'], $productRow))
362 {
363 return $productRow;
364 }
365
366 switch ($operation)
367 {
368 case Catalog\Product\SystemField::OPERATION_PROVIDER:
369 $productRow = self::prepareValueForProvider($field, $productRow);
370 break;
371 case Catalog\Product\SystemField::OPERATION_IMPORT:
372 $productRow = self::prepareValueForImport($field, $productRow);
373 break;
374 case Catalog\Product\SystemField::OPERATION_EXPORT:
375 $productRow = self::prepareValueForExport($field, $productRow);
376 break;
377 }
378
379 return $productRow;
380 }
381
382 private static function prepareValueForProvider(array $field, array $productRow): array
383 {
384 $value = $productRow[$field['XML_ID']];
385 if ($value !== null)
386 {
387 $value = (int)$value;
388 }
389 if (self::isNeedParent($productRow))
390 {
391 if (
392 !self::isUsedMarkingOffer()
393 || $value === self::USE_PARENT_PRODUCT_VALUE
394 )
395 {
396 $productValue = self::getParentProductValue($productRow['ID'], $field);
397 if ($value === self::USE_PARENT_PRODUCT_VALUE)
398 {
399 $value = $productValue;
400 }
401 else
402 {
403 $value = $productValue ?? $value;
404 }
405 }
406 }
407
408 $productRow[$field['XML_ID']] = ($value !== null
409 ? self::getXmlIdById($field['SETTINGS']['HLBLOCK_ID'], (int)$value)
410 : null
411 );
412
413 return $productRow;
414 }
415
416 private static function prepareValueForImport(array $field, array $productRow): array
417 {
418 $value = $productRow[$field['XML_ID']];
419 if ($value === self::USE_PARENT_PRODUCT_XML_VALUE)
420 {
421 $productRow[$field['FIELD_NAME']] = self::USE_PARENT_PRODUCT_VALUE;
422 }
423 else
424 {
425 $productRow[$field['FIELD_NAME']] = ($value !== null
426 ? self::getIdByXmlId($field['SETTINGS']['HLBLOCK_ID'], $value)
427 : null
428 );
429 }
430 unset($productRow[$field['XML_ID']]);
431
432 return $productRow;
433 }
434
435 private static function prepareValueForExport(array $field, array $productRow): array
436 {
437 $value = $productRow[$field['XML_ID']];
438 if ($value !== null)
439 {
440 $value = (int)$value;
441 }
442 if ($value === self::USE_PARENT_PRODUCT_VALUE)
443 {
444 $productRow[$field['XML_ID']] = self::USE_PARENT_PRODUCT_XML_VALUE;
445 }
446 elseif ($value !== null)
447 {
448 $productRow[$field['XML_ID']] = self::getXmlIdById(
449 $field['SETTINGS']['HLBLOCK_ID'],
450 $value
451 );
452 }
453
454 return $productRow;
455 }
456
457 private static function isNeedParent(array $productRow): bool
458 {
459 return
460 isset($productRow['ID'])
461 && isset($productRow['TYPE'])
462 && (int)$productRow['TYPE'] === Catalog\ProductTable::TYPE_OFFER
463 ;
464 }
465
466 private static function isUsedMarkingOffer(): bool
467 {
468 return Option::get('catalog', 'use_offer_marking_code_group') === 'Y';
469 }
470
471 private static function getParentProductValue(int $id, $field): ?string
472 {
473 $result = null;
474 $parentsList = \CCatalogSku::getProductList($id);
475 if (!empty($parentsList) && isset($parentsList[$id]))
476 {
477 $row = Catalog\ProductTable::getRow([
478 'select' => [
479 'ID',
480 $field['FIELD_NAME'],
481 ],
482 'filter' => [
483 '=ID' => $parentsList[$id]['ID'],
484 ],
485 ]);
486 if ($row !== null)
487 {
488 $result = $row[$field['FIELD_NAME']];
489 }
490 }
491
492 return $result;
493 }
494
495 protected static function afterLoadInternalModify(array $row): array
496 {
497 $row = parent::afterLoadInternalModify($row);
498 if (empty($row['SETTINGS']) || !is_array($row['SETTINGS']))
499 {
500 $row['SETTINGS'] = [];
501 }
502 $row['SETTINGS']['HLBLOCK_ID'] = (int)($row['SETTINGS']['HLBLOCK_ID'] ?? 0);
503 $row['SETTINGS']['HLFIELD_ID'] = (int)($row['SETTINGS']['HLFIELD_ID'] ?? 0);
504
505 return $row;
506 }
507
508 public static function updateProductFormConfiguration(): void
509 {
510 if (!static::isAllowed())
511 {
512 return;
513 }
514 $field = static::load();
515 if ($field === null)
516 {
517 return;
518 }
519
521 [
522 'name' => $field['FIELD_NAME'],
523 'optionFlags' => '1',
524 'options' => [
525 'showCode' => 'true',
526 ]
527 ],
528 Catalog\Update\UiFormConfiguration::PARENT_SECTION_MAIN
529 );
530 }
531
532 public static function renderAdminFormControl(array $field, array $product, array $config): ?string
533 {
534 $result = parent::renderAdminFormControl($field, $product, $config);
535 if ($result !== null)
536 {
537 if ($product['TYPE'] === Catalog\ProductTable::TYPE_OFFER)
538 {
539 $parentSelected = (int)$field['VALUE'] === self::USE_PARENT_PRODUCT_VALUE;
540 $addOption = '<option value="' . self::USE_PARENT_PRODUCT_VALUE . '"'
541 . ($parentSelected ? ' selected' : '')
542 . '>' . htmlspecialcharsbx(Loc::getMessage('MARKING_CODE_GROUP_MESS_USE_PARENT_PRODUCT_VALUE'))
543 . '</option>'
544 ;
545 if ($parentSelected)
546 {
547 $result = str_replace('selected', '', $result);
548 }
549 $index = strpos($result, '</option>');
550 if ($index !== false)
551 {
552 $index += 9; // after first option
553 $result = substr($result, 0, $index)
554 . $addOption
555 . substr($result, $index)
556 ;
557 }
558 }
559 }
560
561 return $result;
562 }
563
564 protected static function getUiDescriptionInternal(array $description, array $userField, array $restrictions): ?array
565 {
567
568 $config = [
569 'RESULT' => [
570 'RETURN_FIELD_ID' => 'Y',
571 ],
572 ];
573
574 if (
575 isset($restrictions['TYPE'])
576 && $restrictions['TYPE'] === Catalog\ProductTable::TYPE_OFFER
577 && self::isUsedMarkingOffer()
578 )
579 {
580 $config['ADDITIONAL_ITEMS'] = [
581 'LIST' => [
582 0 => [
583 'ID' => (string)self::USE_PARENT_PRODUCT_VALUE,
584 'VALUE' => (string)self::USE_PARENT_PRODUCT_VALUE,
585 'NAME' => Loc::getMessage('MARKING_CODE_GROUP_MESS_USE_PARENT_PRODUCT_VALUE'),
586 ]
587 ]
588 ];
589 }
590
591 $items = Type\HighloadBlock::getItems($userField, $config);
592 if ($items !== null)
593 {
594 $description['data'] += [
595 'items' => $items
596 ];
597 }
598 unset($items);
599
600 return $description;
601 }
602}
static getXmlIdById(int $hlblockId, int $id)
Определения highloadblock.php:73
static getIdByXmlId(int $hlblockId, string $xmlId)
Определения highloadblock.php:126
static getUiDescriptionInternal(array $description, array $userField, array $restrictions)
Определения markingcodegroup.php:564
static prepareValue(string $operation, array $productRow)
Определения markingcodegroup.php:354
static getOperationSelectFieldList(string $operation)
Определения markingcodegroup.php:329
static getGridActionConfig(ProductGroupAction $panel)
Определения markingcodegroup.php:215
static renderAdminFormControl(array $field, array $product, array $config)
Определения markingcodegroup.php:532
static checkRestictions(array $restrictions)
Определения markingcodegroup.php:281
static getUfId()
Определения product.php:433
const TYPE_SKU
Определения product.php:72
const TYPE_OFFER
Определения product.php:73
const TYPE_PRODUCT
Определения product.php:70
static addFormField(array $field, string $parentId)
Определения uiformconfiguration.php:336
static getInstance()
Определения application.php:98
static getList(array $parameters=array())
Определения datamanager.php:431
const LIST
Определения type.php:17
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
if(Loader::includeModule( 'bitrix24')) elseif(Loader::includeModule('intranet') &&CIntranetUtils::getPortalZone() !=='ru') $description
Определения .description.php:24
$catalog
Определения iblock_catalog_edit.php:135
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
$panel
Определения options.php:177
htmlspecialcharsbx($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения tools.php:2701
$value
Определения Param.php:39
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$config
Определения quickway.php:69
$items
Определения template.php:224
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501