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/rospirotorg.ru/bitrix/modules/wbs24.hitproducts/lib/ |
Upload File : |
<? namespace Wbs24\Hitproducts; use Bitrix\Main\Loader; class Helper { use Exception; protected $main; protected $moduleId; protected $wrappers; //protected $iblockId; protected $priceId; //protected $siteId; protected $newLifetime; protected $userGroups; protected $saleOn; protected $newOn; protected $siteMinSalePercents = []; protected $cachedSites; public function __construct($objects = []) { $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects, [ 'Option', 'CIBlockElement', 'CCatalogDiscount', ]); $this->Mark = $objects['Mark'] ?? new Mark(); //$this->iblockId = $this->wrappers->Option->get($this->moduleId, "IBLOCK_ID"); $this->priceId = $this->wrappers->Option->get($this->moduleId, "PRICE_ID"); //$this->siteId = $this->wrappers->Option->get($this->moduleId, "SITE_ID"); $this->newLifetime = $this->wrappers->Option->get($this->moduleId, "NEW_LIFETIME"); $this->userGroups = [2]; $this->saleOn = ($this->wrappers->Option->get($this->moduleId, "SALE_ON") == 'Y'); $this->newOn = ($this->wrappers->Option->get($this->moduleId, "NEW_ON") == 'Y'); $this->siteMinSalePercents = $this->getMinSalePercents(); } protected function getMinSalePercents() { $percents = []; $sites = $this->getSites(); foreach ($sites as $siteId) { $percents[$siteId] = $this->wrappers->Option->get($this->moduleId, "SITE_MIN_SALE_PERCENT_".$siteId); } return $percents; } public function getSites() { if ($this->cachedSites !== null) return $this->cachedSites; $sites = []; $query = \Bitrix\Main\SiteTable::getList(); while ($site = $query->Fetch()) { $sites[] = $site['LID']; } $this->cachedSites = $sites; return $sites; } public function verifyAndSetProductProperty($fromId = 0, $iblockId, $siteId) { if (!Loader::includeModule("catalog")) return false; if (!$iblockId || !$this->priceId || !$siteId) return false; $maxDuring = $fromId ? $this->wrappers->Option->get($this->moduleId, "MAX_DURING") : 1; $startTime = time(); $lastId = false; $arSelect = ["ID", "IBLOCK_ID", "PRICE_".$this->priceId, "DATE_CREATE"]; $arFilter = ["IBLOCK_ID" => $iblockId, "ACTIVE"=>"Y", ">ID" => $fromId]; $res = $this->wrappers->CIBlockElement->GetList(["ID" => "ASC"], $arFilter, false, false, $arSelect); while($productFields = $res->Fetch()) { $lastId = $this->productProcessing($productFields, $iblockId, $siteId); if ( (time() - $startTime) > $maxDuring || $this->memoryOverload() ) { break; } } return $lastId; } public function productProcessing($productFields, $iblockId, $siteId) { $needSet = []; $needUnset = []; $id = $productFields["ID"]; if ($this->saleOn) { $maxDiscountPercent = $this->getProductMaxDiscount($productFields, $siteId); $saleFlag = $this->Mark::SALE_MARK_TYPE; $maxDiscountPercent ? $needSet[] = $saleFlag : $needUnset[] = $saleFlag; } if ($this->newOn) { $newFlag = $this->Mark::NEW_MARK_TYPE; $this->productIsNew($productFields) ? $needSet[] = $newFlag : $needUnset[] = $newFlag; } if ($needSet) { $this->Mark->set($id, $iblockId, $needSet); } if ($needUnset) { $this->Mark->unset($id, $iblockId, $needUnset); } return $id; } public function getProductMaxDiscount($productFields, $siteId) { $maxDiscountPercent = 0; $id = $productFields["ID"]; $arDiscounts = $this->wrappers->CCatalogDiscount->GetDiscountByProduct( $id, $this->userGroups, "N", [$this->priceId], $siteId, ); if ($arDiscounts) { foreach ($arDiscounts as $discount) { if ($discount['VALUE_TYPE'] == 'P') { if ($discount['VALUE'] > $maxDiscountPercent) $maxDiscountPercent = intval($discount['VALUE']); } } } if ($maxDiscountPercent < $this->siteMinSalePercents[$siteId]) $maxDiscountPercent = 0; return $maxDiscountPercent; } public function productIsNew($productFields) { $createdTime = strtotime($productFields['DATE_CREATE']); $productTime = intval((time() - $createdTime) / 86400); return ($this->newLifetime > $productTime); } public function memoryOverload($maxPercent = 50) { $overload = false; $memoryUsage = memory_get_usage(); $memoryLimit = intval(ini_get('memory_limit')) * 1000000; $allowMemoryUsage = $memoryLimit * ($maxPercent / 100); if ($memoryUsage > $allowMemoryUsage) $overload = true; if ($overload) { $this->createReport('memory_overload_log', [ 'memoryUsage' => $memoryUsage, 'memoryLimit' => $memoryLimit, 'allowMemoryUsage' => $allowMemoryUsage, ]); } return $overload; } }