%PDF- %PDF-
Server IP : 37.220.80.31 / Your IP : 3.142.98.240 Web Server : Apache/2.4.52 (Ubuntu) System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 User : www-root ( 1010) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/catalog/lib/component/ |
Upload File : |
<?php namespace Bitrix\Catalog\Component; use Bitrix\Catalog\Product\PropertyCatalogFeature; use Bitrix\Main\Type\Collection; class SkuTree { /** @var \Bitrix\Catalog\v2\Iblock\IblockInfo */ private $iblockInfo; private $propertyCodes; private $defaultValues = []; public function __construct(\Bitrix\Catalog\v2\Iblock\IblockInfo $iblockInfo) { if (!$iblockInfo || !$iblockInfo->canHaveSku()) { throw new \Bitrix\Main\NotSupportedException(sprintf( 'Selected iblock {%s} can not have SKU.', $iblockInfo->getProductIblockId() )); } $this->iblockInfo = $iblockInfo; } public function setPropertyCodes(array $propertyCodes): void { $this->propertyCodes = $propertyCodes; } public function getPropertyCodes(): array { if ($this->propertyCodes === null) { $this->propertyCodes = $this->loadPropertyCodes(); } return $this->propertyCodes; } private function loadPropertyCodes(): array { return PropertyCatalogFeature::getOfferTreePropertyCodes( $this->iblockInfo->getSkuIblockId(), ['CODE' => 'Y'] ) ?? []; } public function setDefaultValues(array $defaultValues): void { $this->defaultValues = $defaultValues; } public function getDefaultValues(): array { return array_merge( [ 'PICT' => false, 'NAME' => '', ], $this->defaultValues ); } private function prepareProductsOfferTree(array $productIds): array { $offers = $this->getOffersForProductIds($productIds); return $this->prepareOfferTreeProperties($offers); } public function loadWithSelectedOffers(array $productToOfferMap): array { $productIds = array_keys($productToOfferMap); $products = $this->prepareProductsOfferTree($productIds); $treeProperties = $this->prepareTreeProperties(); foreach ($productToOfferMap as $productId => $offerMap) { if (!isset($products[$productId])) { continue; } $product =& $products[$productId]; $offers = []; foreach ((array)$offerMap as $offerId) { $this->editTemplateOfferProps($product, $treeProperties, $offerId); $offers[$offerId] = $product; } $product = $offers; } unset($product); return $products; } public function load(array $productIds): array { $products = $this->prepareProductsOfferTree($productIds); $treeProperties = $this->prepareTreeProperties(); foreach ($products as &$product) { $this->editTemplateOfferProps($product, $treeProperties); } return $products; } protected function editTemplateOfferProps(array &$product, array $skuPropList, int $selectedOfferId = null): void { $matrix = []; $newOffers = []; $double = []; $product['OFFERS_PROP'] = false; $skuPropCodes = array_keys($skuPropList); $matrixFields = array_fill_keys($skuPropCodes, false); foreach ($product['OFFERS'] as $keyOffer => $offer) { if (isset($double[$offer['ID']])) { continue; } $row = []; foreach ($skuPropCodes as $code) { $row[$code] = $this->getTemplatePropCell($code, $offer, $matrixFields, $skuPropList); } $matrix[$keyOffer] = $row; $double[$offer['ID']] = true; $newOffers[$keyOffer] = $offer; } $product['OFFERS'] = $newOffers; $usedFields = []; $existingValues = []; $sortFields = []; foreach ($skuPropCodes as $propCode) { $boolExist = $matrixFields[$propCode]; foreach ($matrix as $keyOffer => $row) { if ($boolExist) { $offer =& $product['OFFERS'][$keyOffer]; $rowValue = $matrix[$keyOffer][$propCode]['VALUE']; $offer['TREE'][$skuPropList[$propCode]['ID']] = $rowValue; if ($selectedOfferId === $offer['ID']) { $product['SELECTED_VALUES'][$skuPropList[$propCode]['ID']] = $rowValue; } $offer['SKU_SORT_'.$propCode] = $matrix[$keyOffer][$propCode]['SORT']; $sortFields['SKU_SORT_'.$propCode] = SORT_NUMERIC; $usedFields[$propCode] = $skuPropList[$propCode]; $existingValues[$propCode][] = $rowValue; } else { unset($matrix[$keyOffer][$propCode]); } } } $product['OFFERS_PROP'] = $usedFields; $product['EXISTING_VALUES'] = $existingValues; Collection::sortByColumn($product['OFFERS'], $sortFields); } protected function getTemplatePropCell($code, $offer, &$matrixFields, $skuPropList): array { $cell = [ 'VALUE' => 0, 'SORT' => PHP_INT_MAX, 'NA' => true, ]; $skuPropSort = array_column($skuPropList[$code]['VALUES'], 'SORT', 'ID'); if (isset($offer['DISPLAY_PROPERTIES'][$code])) { $matrixFields[$code] = true; $cell['NA'] = false; if ($skuPropList[$code]['USER_TYPE'] === 'directory') { $intValue = $skuPropList[$code]['XML_MAP'][$offer['DISPLAY_PROPERTIES'][$code]['VALUE']] ?? 0; $cell['VALUE'] = $intValue; } elseif ($skuPropList[$code]['PROPERTY_TYPE'] === 'L') { $cell['VALUE'] = (int)$offer['DISPLAY_PROPERTIES'][$code]['VALUE_ENUM_ID']; } elseif ($skuPropList[$code]['PROPERTY_TYPE'] === 'E') { $cell['VALUE'] = (int)$offer['DISPLAY_PROPERTIES'][$code]['VALUE']; } $cell['SORT'] = $skuPropSort[$cell['VALUE']]; } return $cell; } private function getOffersForProductIds(array $productIds): array { $productProperty = 'PROPERTY_'.$this->iblockInfo->getSkuPropertyId(); $productPropertyValue = $productProperty.'_VALUE'; $filter = [ 'IBLOCK_ID' => $this->iblockInfo->getSkuIblockId(), 'ACTIVE' => 'Y', 'ACTIVE_DATE' => 'Y', 'CHECK_PERMISSIONS' => 'N', $productProperty => $productIds, ]; $offers = []; $iterator = \CIBlockElement::GetList( [], $filter, false, false, [$productProperty, 'ID'] ); while ($row = $iterator->getNext()) { $row['ID'] = (int)$row['ID']; $row['PARENT_PRODUCT_ID'] = (int)$row[$productPropertyValue]; $row['PROPERTIES'] = []; $offers[$row['ID']] = $row; } if (!empty($offers)) { \CIBlockElement::GetPropertyValuesArray( $offers, $this->iblockInfo->getSkuIblockId(), $filter, [ 'ID' => PropertyCatalogFeature::getOfferTreePropertyCodes($this->iblockInfo->getSkuIblockId()), ] ); } return $offers; } private function prepareTreeProperties(): array { $treeProperties = \CIBlockPriceTools::getTreeProperties( $this->iblockInfo->toArray(), $this->getPropertyCodes(), $this->getDefaultValues() ); $needValues = []; \CIBlockPriceTools::getTreePropertyValues($treeProperties, $needValues); foreach ($treeProperties as &$treeProperty) { if (!empty($treeProperty['VALUES'])) { $treeProperty['VALUES'] = array_values($treeProperty['VALUES']); } } return $treeProperties; } private function prepareOfferTreeProperties(array $offers): array { if (empty($offers)) { return []; } $products = []; foreach ($offers as &$offer) { foreach ($this->getPropertyCodes() as $code) { if (!isset($offer['PROPERTIES'][$code])) continue; $prop = &$offer['PROPERTIES'][$code]; $boolArr = is_array($prop['VALUE']); if ( ($boolArr && !empty($prop['VALUE'])) || (!$boolArr && (string)$prop['VALUE'] !== '') ) { $offer['DISPLAY_PROPERTIES'][$code] = \CIBlockFormatProperties::GetDisplayValue($offer, $prop, 'catalog_out'); } } $products[$offer['PARENT_PRODUCT_ID']]['OFFERS'][] = $offer; } unset($offer); return $products; } }