Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productselectorfieldassembler.php
1<?php
2
4
10use Bitrix\Main\Page\Asset;
11
12Loader::requireModule('iblock');
13
18{
22 private array $skuProducts;
23 private array $skuTree;
24
29 public function __construct(
30 string $columnsId,
31 ProductSettings $settings
32 )
33 {
34 parent::__construct([ $columnsId ], $settings);
35
36 $this->skuProducts = $settings->getSelectedProductOfferIds() ?? [];
37 $this->initSkuTree();
38 $this->preloadResources();
39 }
40
41 public function prepareRows(array $rowList): array
42 {
43 foreach ($rowList as $index => $rowItem)
44 {
45 $type = $rowItem['data']['ROW_TYPE'] ?? null;
46 if (!isset($type))
47 {
48 continue;
49 }
50
51 foreach ($this->getColumnIds() as $columnId)
52 {
53 if ($type !== RowType::ELEMENT || $this->getSettings()->isExcelMode())
54 {
55 $rowList[$index]['columns'][$columnId] = $rowItem['columns']['NAME'] ?? $rowItem['data']['NAME'];
56 }
57 else
58 {
59 $rowList[$index]['columns'][$columnId] = $this->getProductSelectorHtml($rowList[$index]['data']);
60 }
61 }
62 }
63
64 return $rowList;
65 }
66
67 private function initSkuTree(): void
68 {
69 $this->skuTree = [];
70
71 if (empty($this->skuProducts))
72 {
73 return;
74 }
75
77 $skuTree = ServiceContainer::make('sku.tree', [
78 'iblockId' => $this->getSettings()->getIblockId(),
79 ]);
80 if (!$skuTree)
81 {
82 return;
83 }
84
85 $this->skuTree = $skuTree->loadJsonOffers($this->skuProducts);
86 }
87
98 private function preloadResources(): void
99 {
100 Asset::getInstance()->addJs('/bitrix/components/bitrix/catalog.grid.product.field/templates/.default/script.js');
101 Asset::getInstance()->addCss('/bitrix/components/bitrix/catalog.grid.product.field/templates/.default/style.css');
102 }
103
104 private function getProductSelectorHtml(array $row): string
105 {
106 global $APPLICATION;
107
112 $productId = (int)$row['ID'];
113 $skuId = $this->getProductSkuId($productId);
114
115 $productFields = array_merge($row, [
116 'IBLOCK_ID' => $this->getSettings()->getIblockId(),
117 'SKU_IBLOCK_ID' => $this->getSettings()->getOffersIblockId(),
118 'SKU_ID' => $skuId,
119 ]);
120
121 $urlBuilder = $this->getSettings()->getUrlBuilder();
122
123 ob_start();
124 $APPLICATION->IncludeComponent(
125 'bitrix:catalog.grid.product.field',
126 '',
127 [
128 'GRID_ID' => $this->getSettings()->getId(),
129 'COLUMN_NAME' => current($this->getColumnIds()),
130 'ROW_ID' => RowType::ELEMENT . $productId,
131 'ROW_ID_MASK' => 'E#ID#',
132 'PRODUCT_FIELDS' => $productFields,
133 'ENABLE_IMAGE_INPUT' => false,
134 'ENABLE_CHANGES_RENDERING' => false,
135 'USE_SKU_TREE' => true,
136 'BUILDER_CONTEXT' => isset($urlBuilder) ? $urlBuilder->getId() : null,
137 'SKU_TREE' => $this->skuTree[$productId][$skuId] ?? [],
138 ]
139 );
140
141 return ob_get_clean();
142 }
143
144 private function getProductSkuId(int $productId): ?int
145 {
146 return $this->skuProducts[$productId] ?? null;
147 }
148}