Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalogbuilder.php
1<?php
3
6
7if (Loader::includeModule('iblock'))
8{
9 class CatalogBuilder extends Iblock\Url\AdminPage\IblockBuilder
10 {
11 public const TYPE_ID = 'CATALOG';
12
13 protected const TYPE_WEIGHT = 200;
14
15 public const PAGE_OFFER_DETAIL = 'offerDetail';
17 protected $catalog = null;
19 protected $catalogType = null;
20
21 protected $parents = [];
22
23 public function use(): bool
24 {
25 return (defined('CATALOG_PRODUCT') && parent::use());
26 }
27
28 public function setIblockId(int $iblockId): void
29 {
30 if ($this->iblockId !== $iblockId)
31 {
32 parent::setIblockId($iblockId);
33 $this->setCatalog();
34 }
35 }
36
37 public function clearPreloadedUrlData(): void
38 {
39 $this->parents = [];
40 }
41
42 public function getProductDetailUrl(int $entityId, array $options = [], string $additional = ''): string
43 {
44 if (empty($this->catalog))
45 {
46 return '';
47 }
48 if ($this->catalogType !== \CCatalogSku::TYPE_OFFERS)
49 {
50 return $this->getElementDetailUrl($entityId, $options, $additional);
51 }
52 else
53 {
54 $result = '';
55 $parentId = $this->getProductParent($entityId);
56 if (!empty($parentId))
57 {
58 $result = $this->fillUrlTemplate(
59 $this->getUrlTemplate(self::PAGE_OFFER_DETAIL),
60 $this->getOfferVariables($entityId, $parentId, $options, $additional)
61 );
62 }
63 unset($parentId);
64 return $result;
65 }
66 }
67
68 protected function resetIblock(): void
69 {
70 parent::resetIblock();
71 $this->catalog = null;
72 $this->catalogType = null;
73 }
74
75 protected function setCatalog()
76 {
77 $this->catalog = \CCatalogSku::GetInfoByIBlock($this->iblockId);
78 if ($this->catalog === false)
79 {
80 $this->catalog = null;
81 }
82 if (empty($this->catalog))
83 {
84 $this->catalogType = null;
85 $this->setTemplateVariable('#PRODUCT_IBLOCK_ID#', '');
86 $this->setTemplateVariable('#OFFER_IBLOCK_ID#', '');
87 }
88 else
89 {
90 $this->catalogType = $this->catalog['CATALOG_TYPE'];
91 $productIblockId = '';
92 $offerIblockId = '';
93 switch ($this->catalogType)
94 {
95 case \CCatalogSku::TYPE_CATALOG:
96 $productIblockId = $this->catalog['IBLOCK_ID'];
97 break;
98 case \CCatalogSku::TYPE_OFFERS:
99 case \CCatalogSku::TYPE_FULL:
100 case \CCatalogSku::TYPE_PRODUCT:
101 $productIblockId = $this->catalog['PRODUCT_IBLOCK_ID'];
102 $offerIblockId = $this->catalog['IBLOCK_ID'];
103 break;
104 }
105 $this->setTemplateVariable('#PRODUCT_IBLOCK_ID#', $productIblockId);
106 $this->setTemplateVariable('#OFFER_IBLOCK_ID#', $offerIblockId);
107 unset($offerIblockId, $productIblockId);
108 }
109 }
110
111 protected function initUrlTemplates(): void
112 {
113 $this->urlTemplates[self::PAGE_SECTION_LIST] = '#PATH_PREFIX#'
114 .($this->iblockListMixed ? 'cat_product_list.php' : 'cat_section_admin.php')
115 .'?#BASE_PARAMS#'
116 .'#PARENT_FILTER#'
117 .'#ADDITIONAL_PARAMETERS#';
118 $this->urlTemplates[self::PAGE_SECTION_DETAIL] = '#PATH_PREFIX#'
119 .'cat_section_edit.php'
120 .'?#BASE_PARAMS#'
121 .'&ID=#ENTITY_ID#'
122 .'#ADDITIONAL_PARAMETERS#';
123 $this->urlTemplates[self::PAGE_SECTION_COPY] = $this->urlTemplates[self::PAGE_SECTION_DETAIL]
124 .$this->getCopyAction();
125 $this->urlTemplates[self::PAGE_SECTION_SAVE] = '#PATH_PREFIX#'
126 .'cat_section_edit.php'
127 .'?#BASE_PARAMS#'
128 .'#ADDITIONAL_PARAMETERS#';
129 $this->urlTemplates[self::PAGE_SECTION_SEARCH] = '/bitrix/tools/iblock/section_search.php'
130 .'?#LANGUAGE#'
131 .'#ADDITIONAL_PARAMETERS#';
132
133 $this->urlTemplates[self::PAGE_ELEMENT_LIST] = '#PATH_PREFIX#'
134 .($this->iblockListMixed ? 'cat_product_list.php' : 'cat_product_admin.php')
135 .'?#BASE_PARAMS#'
136 .'#PARENT_FILTER#'
137 .'#ADDITIONAL_PARAMETERS#';
138 $this->urlTemplates[self::PAGE_ELEMENT_DETAIL] = '#PATH_PREFIX#'
139 .'cat_product_edit.php'
140 .'?#BASE_PARAMS#'
141 .'&ID=#ENTITY_ID#'
142 .'#ADDITIONAL_PARAMETERS#';
143 $this->urlTemplates[self::PAGE_ELEMENT_COPY] = $this->urlTemplates[self::PAGE_ELEMENT_DETAIL]
144 .$this->getCopyAction();
145 $this->urlTemplates[self::PAGE_ELEMENT_SAVE] = '#PATH_PREFIX#'
146 .'cat_product_edit.php'
147 .'?#BASE_PARAMS#'
148 .'#ADDITIONAL_PARAMETERS#';
149 $this->urlTemplates[self::PAGE_ELEMENT_SEARCH] = '/bitrix/tools/iblock/element_search.php'
150 .'?#LANGUAGE#'
151 .'#ADDITIONAL_PARAMETERS#';
152 $this->urlTemplates[self::PAGE_OFFER_DETAIL] = $this->urlTemplates[self::PAGE_ELEMENT_DETAIL];
153 }
154
155 protected function preloadElementUrlData(array $elementIds): void
156 {
157 if ($this->catalogType !== \CCatalogSku::TYPE_OFFERS)
158 {
159 parent::preloadElementUrlData($elementIds);
160 }
161 else
162 {
163 $load = [];
164 foreach ($elementIds as $id)
165 {
166 if (!isset($this->parents[$id]))
167 {
168 $this->parents[$id] = false;
169 $load[] = $id;
170 }
171 }
172 unset($id);
173 if (!empty($load))
174 {
175 $parents = \CCatalogSku::getProductList($load, $this->iblockId);
176 if (!empty($parents))
177 {
178 foreach ($parents as $id => $data)
179 {
180 $this->parents[$id] = $data['ID'];
181 }
182 unset($id, $data);
183 }
184 unset($parents);
185 }
186 unset($load);
187 }
188 }
189
190 protected function getProductParent(int $entityId): ?int
191 {
192 if (!isset($this->parents[$entityId]))
193 {
194 $this->parents[$entityId] = false;
195 $parents = \CCatalogSku::getProductList([$entityId], $this->iblockId);
196 if (!empty($parents) && isset($parents[$entityId]))
197 {
198 $this->parents[$entityId] = $parents[$entityId]['ID'];
199 }
200 unset($parents);
201 }
202 return (!empty($this->parents[$entityId]) ? $this->parents[$entityId] : null);
203 }
204
205 protected function getOfferVariables(int $entityId, int $productId, array $options = [], string $additional = ''): array
206 {
207 $replaces = $this->getExtendedVariables($options, $additional);
208 $replaces['#PRODUCT_ID#'] = (string)$productId;
209 $replaces['#ENTITY_ID#'] = (string)$entityId;
210 $replaces['#ENTITY_FILTER#'] = $this->getEntityFilter($entityId);
211 return $replaces;
212 }
213 }
214}