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.ozonapinew/lib/ |
Upload File : |
<?php namespace Wbs24\Ozonapinew; use Bitrix\Main\Localization\Loc; /** * Контроллер для запросов из административной части */ class AjaxController { use Exception; use Accounts; protected $iblockId; public function __construct($objects = []) { $this->main = $objects['Main'] ?? new Main(); $this->moduleId = $this->main->getModuleId(); $this->wrappers = new Wrappers($objects); $this->suffix = strtoupper($this->moduleId); $this->Lang = $objects['Lang'] ?? new Lang(); $this->ProductsProperties = $objects['ProductsProperties'] ?? new Products\Properties(); $this->ProductsSections = $objects['ProductsSections'] ?? new Products\Sections(); $this->Settings = $objects['Settings'] ?? new Settings(); $this->ProductsWarehouses = $objects['ProductsWarehouses'] ?? new Products\Warehouses(); } public function process($data) { $action = $data['action'] ?: false; $this->iblockId = $data['iblock_id'] ?? false; $mode = $data['mode'] ?? 'products'; if (!$action) return; $result = ['result' => 'success']; switch ($action) { // получить список языковых фраз case 'getLang': $result = $this->getLang(); break; // получить параметр case 'getParam': $profileId = $data['profile_id'] ?? false; $code = $data['code'] ?? false; $result = $this->getParam($profileId, $code); break; // сохранить параметр case 'saveParam': $profileId = $data['profile_id'] ?? false; $code = $data['code'] ?? false; $value = $data['value'] ?? []; $this->saveParam($profileId, $code, json_encode($value, JSON_UNESCAPED_UNICODE)); break; // варианты для условий для фильтра case 'getConditions': $profileId = $data['profile_id'] ?? false; if ($mode == 'products') { $result = $this->getConditions($profileId); } else { $result = $this->getOffersConditions($profileId); } break; case 'getSectionTree': $profileId = $data['profile_id'] ?? false; $result = $this->getSectionTree($profileId); break; case 'saveEntityTypeFilter': $profileId = $data['profile_id'] ?? false; $code = $data['code'] ?? false; $value = $data['value'] ?? ''; $this->saveParam($profileId, $code, $value); break; } return json_encode($result); } protected function getLang() { return $this->Lang->getPhrases(); } protected function getParam($profileId, $code) { $this->setAccount($profileId); return $this->wrappers->Option->get($this->moduleId, $code); } protected function saveParam($profileId, $code, $value) { $this->setAccount($profileId); $this->wrappers->Option->set($this->moduleId, $code, $value); } protected function getConditions($profileId) { $iblockId = $this->iblockId; // logic $logic = [ [ 'code' => 'LOGIC', 'name' => Loc::getMessage($this->suffix.".SELECT_CONDITIONS_GROUP"), 'type' => '', ], ]; // fields $productFieldsLabel = [ [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_PRODUCT_FIELDS_LABEL"), 'type' => '', ], ]; $productFields = $this->getProductBaseFields(); // price $priceLabel = []; $prices = $this->getPriceFields(); if ($prices) { $priceLabel[] = [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_PRICES_LABEL"), 'type' => '', ]; } // warehouses $warehouseLabel = []; $warehouses = $this->getStockFields(); if ($warehouses) { $warehouseLabel[] = [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_WAREHOUSES_LABEL"), 'type' => '', ]; } // properties $propertiesLabel = []; $properties = $this->getAvailableProperties($profileId, $iblockId); if ($properties) { $propertiesLabel[] = [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_PROPERTIES_LABEL"), 'type' => '', ]; } return array_merge( $logic, $productFieldsLabel, $productFields, $priceLabel, $prices, $warehouseLabel, $warehouses, $propertiesLabel, $properties ); } protected function getOffersConditions($profileId) { $offersIblockId = $this->iblockId; // logic $logic = [ [ 'code' => 'LOGIC', 'name' => Loc::getMessage($this->suffix.".SELECT_CONDITIONS_GROUP"), 'type' => '', ], ]; // fields $offersFieldsLabel = [ [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_OFFER_FIELDS_LABEL"), 'type' => '', ], ]; $offersFields = $this->getOfferBaseFields(); // price $priceLabel = []; $prices = $this->getPriceFields(); if ($prices) { $priceLabel[] = [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_PRICES_LABEL"), 'type' => '', ]; } // warehouses $warehouseLabel = []; $warehouses = $this->getStockFields(); if ($warehouses) { $warehouseLabel[] = [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_WAREHOUSES_LABEL"), 'type' => '', ]; } // properties $offersPropertiesLabel = []; $offersProperties = $this->getAvailableProperties($profileId, $offersIblockId); if ($offersProperties) { $offersPropertiesLabel[] = [ 'code' => '', 'name' => Loc::getMessage($this->suffix.".CONDITION_OFFERS_PROPERTIES_LABEL"), 'type' => '', ]; } return array_merge( $logic, $offersFieldsLabel, $offersFields, $priceLabel, $prices, $warehouseLabel, $warehouses, $offersPropertiesLabel, $offersProperties ); } protected function getProductBaseFields($forSelect = false) { $fields = [ [ 'code' => 'ID', 'name' => Loc::getMessage($this->suffix.".SELECT_PRODUCT_ID"), 'type' => 'N', ], [ 'code' => 'CODE', 'name' => Loc::getMessage($this->suffix.".SELECT_CODE"), 'type' => 'N', ], [ 'code' => 'XML_ID', 'name' => Loc::getMessage($this->suffix.".SELECT_XML_ID"), 'type' => 'N', ], [ 'code' => 'NAME', 'name' => Loc::getMessage($this->suffix.".SELECT_NAME"), 'type' => 'S', ], [ 'code' => 'DETAIL_PAGE_URL', 'name' => Loc::getMessage($this->suffix.".SELECT_DETAIL_PAGE_URL"), 'type' => 'S', ], [ 'code' => 'ACTIVE', 'name' => Loc::getMessage($this->suffix.".SELECT_ACTIVE"), 'type' => 'B', 'values' => [ [ 'name' => Loc::getMessage($this->suffix.".Y"), 'value' => 'Y', ], [ 'name' => Loc::getMessage($this->suffix.".N"), 'value' => 'N', ], ], ], [ 'code' => 'AVAILABLE', 'name' => Loc::getMessage($this->suffix.".SELECT_AVAILABLE"), 'type' => 'B', 'values' => [ [ 'name' => Loc::getMessage($this->suffix.".Y"), 'value' => 'Y', ], [ 'name' => Loc::getMessage($this->suffix.".N"), 'value' => 'N', ], ], ], [ 'code' => 'TYPE', 'name' => Loc::getMessage($this->suffix.".SELECT_TYPE"), 'type' => 'N', ], [ 'code' => $forSelect ? 'IBLOCK_SECTION_ID' : 'SECTION_ID', 'name' => Loc::getMessage($this->suffix.".SELECT_SECTION_ID"), 'type' => 'N', ], [ 'code' => 'QUANTITY', 'name' => Loc::getMessage($this->suffix.".SELECT_QUANTITY"), 'type' => 'N', ], [ 'code' => 'WEIGHT', 'name' => Loc::getMessage($this->suffix.".SELECT_WEIGHT"), 'type' => 'N', ], [ 'code' => 'WIDTH', 'name' => Loc::getMessage($this->suffix.".SELECT_WIDTH"), 'type' => 'N', ], [ 'code' => 'LENGTH', 'name' => Loc::getMessage($this->suffix.".SELECT_LENGTH"), 'type' => 'N', ], [ 'code' => 'HEIGHT', 'name' => Loc::getMessage($this->suffix.".SELECT_HEIGHT"), 'type' => 'N', ], ]; if ($forSelect) { $fields[] = [ 'code' => 'PREVIEW_PICTURE', 'name' => Loc::getMessage($this->suffix.".SELECT_PREVIEW_PICTURE"), 'type' => 'S', ]; $fields[] = [ 'code' => 'DETAIL_PICTURE', 'name' => Loc::getMessage($this->suffix.".SELECT_DETAIL_PICTURE"), 'type' => 'S', ]; $fields[] = [ 'code' => 'PREVIEW_TEXT', 'name' => Loc::getMessage($this->suffix.".SELECT_PREVIEW_TEXT"), 'type' => 'S', ]; $fields[] = [ 'code' => 'DETAIL_TEXT', 'name' => Loc::getMessage($this->suffix.".SELECT_DETAIL_TEXT"), 'type' => 'S', ]; } return $fields; } protected function getOfferBaseFields($forSelect = false) { $disallowedFields = [ 'DETAIL_PAGE_URL', 'IBLOCK_SECTION_ID', 'SECTION_ID', ]; $fields = $this->getProductBaseFields($forSelect); foreach ($fields as $key => $field) { $code = $field['code'] ?? ''; if (in_array($code, $disallowedFields)) { unset($fields[$key]); } } return $fields; } protected function getPriceFields() { $fields = []; $priceToCodes = $this->Settings->getPriceTypes(); foreach ($priceToCodes as $id => $code) { $fields[] = [ 'code' => 'PRICE_'.$id, 'name' => $code, 'type' => 'N', ]; } return $fields; } protected function getStockFields() { $fields = []; $idsToNames = $this->ProductsWarehouses->getIdsToNames(); foreach ($idsToNames as $id => $name) { $fields[] = [ 'code' => 'STORE_AMOUNT_'.$id, 'name' => '['.$id.'] '.$name, 'type' => 'N', ]; } return $fields; } protected function getAvailableProperties($profileId, $iblockId) { $properties = []; $allowedPropertyTypes = ['S', 'N', 'L', 'E']; if ($iblockId) { $props = $this->ProductsProperties->get($iblockId); foreach ($props as $prop) { $propType = $prop['PROPERTY_TYPE'] ?? ''; if (!in_array($propType, $allowedPropertyTypes)) continue; $code = 'PROPERTY_'.$prop['CODE']; if ($propType == 'E') { $code = 'PROPERTY_'.$prop['CODE'].'.NAME'; } $properties[] = [ 'code' => $code, 'name' => '['.$prop['ID'].'] '.$prop['NAME'], 'type' => $propType, ]; } } return $properties; } protected function getSectionTree($profileId) { //demo /* return [ [ 'id' => 1, 'name' => 'Category 1', 'items' => [], ], [ 'id' => 2, 'name' => 'Category 2', 'items' => [ [ 'id' => 11, 'name' => 'Category 11', 'items' => [], ], [ 'id' => 12, 'name' => 'Category 12', 'items' => [], ], [ 'id' => 13, 'name' => 'Category 13', 'items' => [], ], ], ], [ 'id' => 3, 'name' => 'Category 3', 'items' => [ [ 'id' => 14, 'name' => 'Category 14', 'items' => [], ], [ 'id' => 15, 'name' => 'Category 15', 'items' => [], ], [ 'id' => 16, 'name' => 'Category 16', 'items' => [], ], ], ], ]; */ $iblockId = $this->iblockId; $sections = $this->ProductsSections->getAllSections($iblockId); $sectionsAsTree = $this->ProductsSections->getSectionTreeFromList($sections); return $sectionsAsTree; } }