Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
propertycatalogfeature.php
1<?php
3
8
9Loc::loadMessages(__FILE__);
10
11if (Loader::includeModule('iblock'))
12{
13 class PropertyCatalogFeature extends Iblock\Model\PropertyFeature
14 {
15 const FEATURE_ID_BASKET_PROPERTY = 'IN_BASKET'; // property added to basket
16 const FEATURE_ID_OFFER_TREE_PROPERTY = 'OFFER_TREE'; // property is used to select offers
17
24 public static function handlerPropertyFeatureBuildList(Main\Event $event): Main\EventResult
25 {
26 $list = [];
27
28 $property = $event->getParameter('property');
29 $description = $event->getParameter('description');
30
31 if (self::checkBasketProperty($property, $description))
32 {
33 $list[] = [
34 'MODULE_ID' => 'catalog',
35 'FEATURE_ID' => self::FEATURE_ID_BASKET_PROPERTY,
36 'FEATURE_NAME' => Loc::getMessage('PROPERTY_CATALOG_FEATURE_NAME_BASKET_PROPERTY')
37 ];
38 }
39 if (self::checkOfferTreeProperty($property, $description))
40 {
41 $list[] = [
42 'MODULE_ID' => 'catalog',
43 'FEATURE_ID' => self::FEATURE_ID_OFFER_TREE_PROPERTY,
44 'FEATURE_NAME' => Loc::getMessage('PROPERTY_CATALOG_FEATURE_NAME_SKU_TREE_PROPERTY')
45 ];
46 }
47 unset($description, $property);
48
49 return new Main\EventResult(Main\EventResult::SUCCESS, $list);
50 }
51
68 public static function getBasketPropertyCodes($iblockId, array $parameters = []): ?array
69 {
70 $iblockId = (int)$iblockId;
71 if ($iblockId <= 0)
72 return null;
73
74 $catalog = \CCatalogSku::GetInfoByIBlock($iblockId);
75 if (empty($catalog))
76 return null;
77
78 if (!self::isEnabledFeatures())
79 return self::getBasketPropertyByTypes($catalog, $parameters);
80
81 $filter = null;
82 switch ($catalog['CATALOG_TYPE'])
83 {
84 case \CCatalogSku::TYPE_CATALOG:
85 case \CCatalogSku::TYPE_PRODUCT:
86 case \CCatalogSku::TYPE_FULL:
87 $filter = [
88 [
89 'LOGIC' => 'OR',
90 [
91 '=PROPERTY.MULTIPLE' => 'Y',
92 '@PROPERTY.PROPERTY_TYPE' => [
93 Iblock\PropertyTable::TYPE_ELEMENT,
94 Iblock\PropertyTable::TYPE_SECTION,
95 Iblock\PropertyTable::TYPE_LIST,
96 Iblock\PropertyTable::TYPE_NUMBER,
97 Iblock\PropertyTable::TYPE_STRING
98 ]
99 ],
100 [
101 '=PROPERTY.MULTIPLE' => 'N',
102 '@PROPERTY.PROPERTY_TYPE' => [
103 Iblock\PropertyTable::TYPE_ELEMENT,
104 Iblock\PropertyTable::TYPE_LIST
105 ]
106 ]
107 ]
108 ];
109 break;
110 case \CCatalogSku::TYPE_OFFERS:
111 $filter = [
112 '!=PROPERTY.PROPERTY_TYPE' => Iblock\PropertyTable::TYPE_FILE,
113 '!=PROPERTY.ID' => $catalog['SKU_PROPERTY_ID']
114 ];
115 break;
116 }
117 unset($catalog);
118
119 if ($filter === null)
120 return null;
121
122 $filter['=MODULE_ID'] = 'catalog';
123 $filter['=FEATURE_ID'] = self::FEATURE_ID_BASKET_PROPERTY;
124
125 return self::getFilteredPropertyCodes($iblockId, $filter, $parameters);
126 }
127
144 public static function getOfferTreePropertyCodes($iblockId, array $parameters = []): ?array
145 {
146 $iblockId = (int)$iblockId;
147 if ($iblockId <= 0)
148 return null;
149
150 $catalog = \CCatalogSku::GetInfoByOfferIBlock($iblockId);
151 if (empty($catalog))
152 return null;
153
154 if (!self::isEnabledFeatures())
155 return self::getOfferTreePropertyByTypes($catalog, $parameters);
156
157 $filter = [
158 '=MODULE_ID' => 'catalog',
159 '=FEATURE_ID' => self::FEATURE_ID_OFFER_TREE_PROPERTY,
160 '=PROPERTY.MULTIPLE' => 'N',
161 '!=PROPERTY.ID' => $catalog['SKU_PROPERTY_ID'],
162 [
163 'LOGIC' => 'OR',
164 [
165 '@PROPERTY.PROPERTY_TYPE' => [
166 Iblock\PropertyTable::TYPE_ELEMENT,
167 Iblock\PropertyTable::TYPE_LIST
168 ]
169 ],
170 [
171 '=PROPERTY.PROPERTY_TYPE' => Iblock\PropertyTable::TYPE_STRING,
172 '=PROPERTY.USER_TYPE' => 'directory'
173 ]
174 ]
175 ];
176
177 return self::getFilteredPropertyCodes($iblockId, $filter, $parameters);
178 }
179
195 private static function getBasketPropertyByTypes(array $catalog, array $parameters = []): ?array
196 {
197 $result = [];
198
199 $getCode = (isset($parameters['CODE']) && $parameters['CODE'] == 'Y');
200 $filter = [];
201 switch ($catalog['CATALOG_TYPE'])
202 {
203 case \CCatalogSku::TYPE_CATALOG:
204 $filter = [
205 '=IBLOCK_ID' => $catalog['IBLOCK_ID'],
206 [
207 'LOGIC' => 'OR',
208 [
209 '=MULTIPLE' => 'Y',
210 '@PROPERTY_TYPE' => [
211 Iblock\PropertyTable::TYPE_ELEMENT,
212 Iblock\PropertyTable::TYPE_SECTION,
213 Iblock\PropertyTable::TYPE_LIST,
214 Iblock\PropertyTable::TYPE_NUMBER,
215 Iblock\PropertyTable::TYPE_STRING
216 ]
217 ],
218 [
219 '=MULTIPLE' => 'N',
220 '@PROPERTY_TYPE' => [
221 Iblock\PropertyTable::TYPE_ELEMENT,
222 Iblock\PropertyTable::TYPE_LIST
223 ]
224 ]
225 ]
226 ];
227 break;
228 case \CCatalogSku::TYPE_PRODUCT:
229 case \CCatalogSku::TYPE_FULL:
230 $filter = [
231 '=IBLOCK_ID' => $catalog['PRODUCT_IBLOCK_ID'],
232 [
233 'LOGIC' => 'OR',
234 [
235 '=MULTIPLE' => 'Y',
236 '@PROPERTY_TYPE' => [
237 Iblock\PropertyTable::TYPE_ELEMENT,
238 Iblock\PropertyTable::TYPE_SECTION,
239 Iblock\PropertyTable::TYPE_LIST,
240 Iblock\PropertyTable::TYPE_NUMBER,
241 Iblock\PropertyTable::TYPE_STRING
242 ]
243 ],
244 [
245 '=MULTIPLE' => 'N',
246 '@PROPERTY_TYPE' => [
247 Iblock\PropertyTable::TYPE_ELEMENT,
248 Iblock\PropertyTable::TYPE_LIST
249 ]
250 ]
251 ]
252 ];
253 break;
254 case \CCatalogSku::TYPE_OFFERS:
255 $filter = [
256 '=IBLOCK_ID' => $catalog['IBLOCK_ID'],
257 '!=PROPERTY_TYPE' => Iblock\PropertyTable::TYPE_FILE,
258 '!=ID' => $catalog['SKU_PROPERTY_ID']
259 ];
260 break;
261 }
262 $filter['=ACTIVE'] = 'Y';
263
264 $iterator = Iblock\PropertyTable::getList([
265 'select' => ['ID', 'CODE', 'SORT'],
266 'filter' => $filter,
267 'order' => ['SORT' => 'ASC', 'ID' => 'ASC']
268 ]);
269 while ($row = $iterator->fetch())
270 $result[(int)$row['ID']] = self::getPropertyCode($row, $getCode);
271 unset($row, $iterator);
272 unset($filter, $getCode);
273
274 return (!empty($result) ? array_values($result) : null);
275 }
276
292 private static function getOfferTreePropertyByTypes(array $catalog, array $parameters = []): ?array
293 {
294 $result = [];
295
296 $getCode = (isset($parameters['CODE']) && $parameters['CODE'] == 'Y');
297 $filter = [
298 '=IBLOCK_ID' => $catalog['IBLOCK_ID'],
299 '!=ID' => $catalog['SKU_PROPERTY_ID'],
300 '=ACTIVE' => 'Y',
301 '=MULTIPLE' => 'N',
302 [
303 'LOGIC' => 'OR',
304 [
305 '@PROPERTY_TYPE' => [
306 Iblock\PropertyTable::TYPE_ELEMENT,
307 Iblock\PropertyTable::TYPE_LIST
308 ]
309 ],
310 [
311 '=PROPERTY_TYPE' => Iblock\PropertyTable::TYPE_STRING,
312 '=USER_TYPE' => 'directory'
313 ]
314 ]
315 ];
316 $iterator = Iblock\PropertyTable::getList([
317 'select' => ['ID', 'CODE', 'SORT'],
318 'filter' => $filter,
319 'order' => ['SORT' => 'ASC', 'ID' => 'ASC']
320 ]);
321 while ($row = $iterator->fetch())
322 $result[(int)$row['ID']] = self::getPropertyCode($row, $getCode);
323 unset($row, $iterator);
324 unset($filter, $getCode);
325
326 return (!empty($result) ? array_values($result) : null);
327 }
328
337 private static function checkBasketProperty(array $property, array $description): bool
338 {
339 if (!isset($property['IBLOCK_ID']))
340 return false;
341 $catalogType = null;
342 if ((int)$property['IBLOCK_ID'] > 0)
343 {
344 $catalog = \CCatalogSku::GetInfoByIBlock($property['IBLOCK_ID']);
345 if (empty($catalog))
346 return false;
347 $catalogType = $catalog['CATALOG_TYPE'];
348 }
349 else
350 {
351 return false;
352 }
353 if ($catalogType === null)
354 return false;
355 if (!isset($property['PROPERTY_TYPE']))
356 return false;
357 if (!isset($property['MULTIPLE']))
358 return false;
359
360 switch ($catalogType)
361 {
362 case \CCatalogSku::TYPE_PRODUCT:
363 case \CCatalogSku::TYPE_CATALOG:
364 case \CCatalogSku::TYPE_FULL:
365 if ($property['MULTIPLE'] == 'Y')
366 {
367 if (
368 $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_ELEMENT
369 && $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_SECTION
370 && $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_LIST
371 && $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_NUMBER
372 && $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_STRING
373 )
374 return false;
375 }
376 elseif ($property['MULTIPLE'] == 'N')
377 {
378 if (
379 $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_ELEMENT
380 && $property['PROPERTY_TYPE'] !== Iblock\PropertyTable::TYPE_LIST
381 )
382 return false;
383 }
384 else
385 {
386 return false;
387 }
388 break;
389 case \CCatalogSku::TYPE_OFFERS:
390 if ($property['PROPERTY_TYPE'] == Iblock\PropertyTable::TYPE_FILE)
391 return false;
392 break;
393 }
394
395 return true;
396 }
397
406 private static function checkOfferTreeProperty(array $property, array $description): bool
407 {
408 if (!isset($property['IBLOCK_ID']))
409 return false;
410 if ((int)$property['IBLOCK_ID'] > 0)
411 {
412 $catalog = \CCatalogSku::GetInfoByOfferIBlock($property['IBLOCK_ID']);
413 if (empty($catalog))
414 return false;
415 }
416 else
417 {
418 return false;
419 }
420
421 if (
422 !isset($property['PROPERTY_TYPE'])
423 || (
424 $property['PROPERTY_TYPE'] != Iblock\PropertyTable::TYPE_ELEMENT
425 && $property['PROPERTY_TYPE'] != Iblock\PropertyTable::TYPE_LIST
426 && $property['PROPERTY_TYPE'] != Iblock\PropertyTable::TYPE_STRING
427 )
428 )
429 return false;
430 if ($property['PROPERTY_TYPE'] == Iblock\PropertyTable::TYPE_ELEMENT)
431 {
432 if (isset($property['USER_TYPE']) && $property['USER_TYPE'] == \CIBlockPropertySKU::USER_TYPE)
433 return false;
434 }
435 if ($property['PROPERTY_TYPE'] == Iblock\PropertyTable::TYPE_STRING)
436 {
437 if (!isset($property['USER_TYPE']) || $property['USER_TYPE'] != 'directory')
438 return false;
439 }
440 if (!isset($property['MULTIPLE']) || $property['MULTIPLE'] != 'N')
441 return false;
442
443 return true;
444 }
445 }
446}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29