Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/ilovecveti.ru/local/services/lib/ |
Upload File : |
<?php namespace Wbs24\Services; use Bitrix\Main\Loader; use Bitrix\Main\Data\Cache; use Bitrix\Sale; class Main { protected $settings; protected $ready = false; public function __construct() { $this->settings = $this->getSettings(); if ( true //&& Loader::includeModule('sale') //&& Loader::includeModule('iblock') //&& Loader::includeModule('catalog') ) { $this->ready = true; } } protected function getErrorIfNotReady() { $error = false; if (!$this->ready) { $error = [ 'error' => 'necessary modules are not installed', ]; } return $error; } public function getJson($data) { $action = $data['action'] ?? false; $result = [ 'error' => 'invalid data', ]; if ($errorNotReady = $this->getErrorIfNotReady()) { $action = false; $result = $errorNotReady; } switch ($action) { case 'getServices': $result = $this->getServices($data); break; case 'addToBasket': $result = $this->addToBasket($data); break; case 'delFromBasket': $result = $this->delFromBasket($data); break; case 'checkInBasket': $result = $this->checkInBasket($data); break; case 'delUnbound': $result = $this->delUnbound(); break; } return json_encode($result); } protected function getSettings() { $settings = []; include_once($_SERVER['DOCUMENT_ROOT'].'/local/services/~settings.php'); return $settings; } // GET SERVICES protected function getServices($data) { $productId = $data['productId'] ?? false; if (!$productId) return; $itemSectionId = $this->getItemSection($productId); $services = $this->getServicesByItemSection($itemSectionId); $preparedServices = $this->prepareServicesForResponse($services); return $preparedServices; } protected function getItemSection($itemId) { if ( !$itemId && !empty($this->settings['productIblockId']) ) return false; $filter = [ "IBLOCK_ID" => $this->settings['productIblockId'], "ACTIVE" => "Y", "ID" => $itemId, ]; $select = [ "ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", ]; $result = $this->getList($filter, $select, $this->settings['cacheSeconds']); $sectionId = $result[0]["IBLOCK_SECTION_ID"] ?: false; return $sectionId; } protected function getServicesByItemSection($itemSectionId) { if ( !$itemSectionId && !empty($this->settings['servicesIblockId']) && !empty($this->settings['propertyCodeForSectionsLink']) ) return []; $filter = [ "IBLOCK_ID" => $this->settings['servicesIblockId'], "ACTIVE" => "Y", "AVAILABLE" => "Y", "PROPERTY_".$this->settings['propertyCodeForSectionsLink'] => $itemSectionId, ]; $select = [ "ID", "IBLOCK_ID", "NAME", "SCALED_PRICE_".$this->getPriceId(), ]; $services = $this->getList($filter, $select, $this->settings['cacheSeconds']); return $services; } protected function prepareServicesForResponse($services) { $preparedServices = []; foreach ($services as $service) { $preparedServices[] = [ 'id' => $service['ID'], 'name' => $service['NAME'], 'price' => round($service['SCALED_PRICE_'.$this->getPriceId()]), ]; } return $preparedServices; } protected function getList($filter, $select = ["ID", "IBLOCK_ID", "NAME"], $cacheSeconds = false) { if ($cacheSeconds) { $cache = Cache::createInstance(); $cacheKey = md5(print_r($filter, true).print_r($select, true)); if ($cache->initCache($cacheSeconds, $cacheKey)) { return $cache->getVars(); } } $list = []; if (Loader::includeModule('iblock')) { $result = \CIBlockElement::GetList(["SORT" => "ASC"], $filter, false, false, $select); while ($fields = $result->Fetch()) { $list[] = $fields; } } if ($cacheSeconds) { $cache->startDataCache(); $cache->endDataCache($list); } return $list; } protected function getPriceId() { return $this->settings['priceId'] ?: 1; } // BASKET protected function addToBasket($data) { if (!Loader::includeModule('sale') && !Loader::includeModule('catalog')) return; $productId = $data['productId'] ?? false; if (!$productId) return; $serviceId = $data['serviceId'] ?? false; if (!$serviceId) return; $basket = $this->getBasket(); $item = $basket->createItem('catalog', $serviceId); $item->setFields([ 'QUANTITY' => 1, 'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(), 'LID' => \Bitrix\Main\Context::getCurrent()->getSite(), 'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider', ]); $basketPropertyCollection = $item->getPropertyCollection(); $basketPropertyCollection->setProperty([ [ 'NAME' => 'ID родительского товара', 'CODE' => 'SERVICEOFPRODUCTID', // свойство должно быть в списке параметра корзины OFFERS_PROPS 'VALUE' => $productId, 'SORT' => 100, ], ]); $basket->save(); return ['result' => 'success']; } protected function delFromBasket($data) { if (!Loader::includeModule('sale') && !Loader::includeModule('catalog')) return; $productId = $data['productId'] ?? false; if (!$productId) return; $serviceId = $data['serviceId'] ?? false; if (!$serviceId) return; list($basket, $item) = $this->getBasketAndItemWithService($productId, $serviceId); if ($item) { $item->delete(); $basket->save(); } return ['result' => 'success']; } protected function checkInBasket($data) { if (!Loader::includeModule('sale') && !Loader::includeModule('catalog')) return; $productId = $data['productId'] ?? false; if (!$productId) return; $serviceId = $data['serviceId'] ?? false; if (!$serviceId) return; list($basket, $item) = $this->getBasketAndItemWithService($productId, $serviceId); return ['result' => ($item ? 'exist' : 'not_exist')]; } protected function delUnbound() { if (!Loader::includeModule('sale') && !Loader::includeModule('catalog')) return; $needRecalc = false; $basket = $this->getBasket(); $productIds = []; $serviceIdsToBasketItemIds = []; $unboundItemIds = []; foreach ($basket as $item) { $itemProductId = $item->getProductId(); $itemParentProductId = $this->getParentProductId($item); $itemId = $item->getId(); if ($itemParentProductId) { $productIdsToBasketItemIds[$itemParentProductId][] = $itemId; } else { $productIds[] = $itemProductId; } } $unboundItemIds = $this->getUnboundItemsIds($productIds, $productIdsToBasketItemIds); foreach ($basket as $item) { $itemId = $item->getId(); if (in_array($itemId, $unboundItemIds)) { $item->delete(); $needRecalc = true; } } if ($needRecalc) { $basket->save(); } return [ 'result' => ($needRecalc ? 'recalc' : 'success'), ]; } protected function getBasketAndItemWithService($productId, $serviceId) { $basket = $this->getBasket(); foreach ($basket as $item) { $itemProductId = $item->getProductId(); $itemParentProductId = $this->getParentProductId($item); if ( $itemProductId == $serviceId && $itemParentProductId == $productId ) { return [$basket, $item]; } } return [$basket, false]; } protected function getParentProductId(&$item) { $basketPropertyCollection = $item->getPropertyCollection(); $itemPropertyValues = $basketPropertyCollection->getPropertyValues(); $itemParentProductId = $itemPropertyValues['SERVICEOFPRODUCTID']['VALUE'] ?? false; return $itemParentProductId; } protected function getUnboundItemsIds($productIds, $productIdsToBasketItemIds) { $unboundItemIds = []; foreach ($productIdsToBasketItemIds as $itemParentProductId => $itemIds) { if (!in_array($itemParentProductId, $productIds)) { foreach ($itemIds as $itemId) $unboundItemIds[] = $itemId; } } return $unboundItemIds; } protected function getBasket() { $basket = Sale\Basket::loadItemsForFUser(Sale\Fuser::getId(), \Bitrix\Main\Context::getCurrent()->getSite()); return $basket; } }