diff --git a/.module.json b/.module.json index abfc28a..aa389ad 100644 --- a/.module.json +++ b/.module.json @@ -3,7 +3,7 @@ "prefix": "bx_discount", "namespace": "Bx\\Discount", "gitUrl": "https://gitea.ixrs.ru/Bellord/bx.discount.git", - "version": "1.0.0", - "date": "2026-02-04 00:00:00", + "version": "1.0.1", + "date": "2026-02-05 00:00:00", "deps": ["wt.core"] } \ No newline at end of file diff --git a/admin/main/submenu/discount_table/menu.php b/admin/main/submenu/discount_table/menu.php index 2cc0625..c8d2f22 100644 --- a/admin/main/submenu/discount_table/menu.php +++ b/admin/main/submenu/discount_table/menu.php @@ -7,7 +7,7 @@ Loc::loadMessages(__FILE__); return (new GlobalMenuItem()) - ->setVisible(false) + //->setVisible(false) ->setSort(1300) ->setTitle('Discount Table') ->setIcon('iblock_menu_icon_iblocks') diff --git a/admin/main/submenu/settings/config.php b/admin/main/submenu/settings/config.php index fd74c4c..d6a40d1 100644 --- a/admin/main/submenu/settings/config.php +++ b/admin/main/submenu/settings/config.php @@ -27,6 +27,13 @@ return [ 'SHOW_EDIT_BUTTON' => 'N', 'DEFAULT' => '', ], + 'SPIDERS' => [ + 'TITLE' => 'Spiders', + 'TYPE' => 'content', + 'INCLUDEFILE' => Tools::removeDocRoot(__DIR__ . '/include/spiders.php'), + 'SHOW_EDIT_BUTTON' => 'N', + 'DEFAULT' => '', + ], ], ], 'DOCS' => [ diff --git a/admin/main/submenu/settings/include/agents.php b/admin/main/submenu/settings/include/agents.php index 2b3b07c..eec6563 100644 --- a/admin/main/submenu/settings/include/agents.php +++ b/admin/main/submenu/settings/include/agents.php @@ -3,9 +3,9 @@ * @var Wt\Core\Admin\Templater $this */ -use Bx\Discount\Agents\SpiderAgentManager; +use Bx\Discount\Agents\SpiderAgentCase; $moduleId = $this->getAdminPage()->getModuleId(); -$agentCase = new SpiderAgentManager(); +$agentCase = new SpiderAgentCase(); $this->renderAgentCase($agentCase); \ No newline at end of file diff --git a/admin/main/submenu/settings/include/config.php b/admin/main/submenu/settings/include/config.php index 9b33b8d..78748cc 100644 --- a/admin/main/submenu/settings/include/config.php +++ b/admin/main/submenu/settings/include/config.php @@ -8,69 +8,94 @@ $system = new System(); $config = discountApp()->config(); $data = []; $data['env'] = discountApp()->env(); -$data['ENABLE'] = $this->getCheckTemplate($config->get('ENABLE')); -$data['UPDATE_ELEMENT_INDEX'] = $this->getCheckTemplate($config->get('UPDATE_ELEMENT_INDEX')); -$data['DISCOUNT_TYPE_IBLOCK_ID'] = $discountTypeIBlockId = $config->get('DISCOUNT_TYPE_IBLOCK_ID'); -if($discountTypeIBlockId){ - $blockEntity = app()->factory()->main()->getIBlockFactory()->getEntityById($discountTypeIBlockId); - if($blockEntity){ - $data['DISCOUNT_TYPE_IBLOCK_ID'] = "[$discountTypeIBlockId] {$blockEntity->getName()}"; - } -} -$data['CATALOG_IBLOCK_ID'] = $catalogIBlockId = $config->get('CATALOG_IBLOCK_ID'); -if($catalogIBlockId){ - $blockEntity = app()->factory()->main()->getIBlockFactory()->getEntityById($catalogIBlockId); - if($blockEntity){ - $data['CATALOG_IBLOCK_ID'] = "[$catalogIBlockId] {$blockEntity->getName()}"; - } -} -$data['TICKET_PROP_ID'] = $ticketPropId = $config->get('TICKET_PROP_ID'); -$propertyEntity = null; -if($ticketPropId){ - $blockAFactory = app()->factory()->iBlock()->getEntityById($catalogIBlockId); - if($blockAFactory){ - $propertyEntity = $blockAFactory->getPropertyFactory()->getEntityById($ticketPropId); - $linkTemplate = "$ticketPropId"; - $data['TICKET_PROP_ID'] = "[$linkTemplate] {$propertyEntity->getName()} ({$propertyEntity->getFullType()})"; - } -} -$data['CATALOG_GROUP_ID'] = $priceTypeId = $config->get('CATALOG_GROUP_ID'); -if($priceTypeId){ +$data['enable'] = $this->getCheckTemplate($config->get('enable')); +$data['update_element_index'] = $this->getCheckTemplate($config->get('update_element_index')); + + +$priceTypeIdList = $config->get('catalog_group_id_list', []); +$data['catalog_group_id_list'] = $priceTypeIdList->join(', '); +foreach ($priceTypeIdList as $priceTypeId){ $priceTypeEntity = app()->factory()->shop()->getPriceTypeFactory()->getEntityById($priceTypeId); if($priceTypeEntity){ - $linkTemplate = "$priceTypeId"; - $data['CATALOG_GROUP_ID'] = "[$linkTemplate] {$priceTypeEntity->getName()}"; + $linkTemplate = "getUrl()}\" target=\"_blank\">$priceTypeId"; + $data["catalog_group_id_list_$priceTypeId"] = "[$linkTemplate] {$priceTypeEntity->getName()}"; } } -$data['PAGE_SIZE'] = $config->get('PAGE_SIZE'); -$data['RENEWAL_PERIOD'] = $config->get('RENEWAL_PERIOD'); +$data['page_size'] = $config->get('page_size'); ?> - - $value) { - ?> + +
MAPget('MAP', [])->count()){ + + $value) { + ?> +
+
- ?> - - get('MAP', []) as $type => $id){?> - - -
typeticket item
getAdapter()->getValueFactory(); - $entity = $valueFactory->getEntityById($id); - if($entity){ - echo "[{$entity->getId()}] {$entity->getName()}"; - } - } - ?>
emptyИнфоблоки + + + + + + + + + config('catalog_iblock_id', 0); + $catalogTitle = "".($catalogIBlockId?:'Undefined'). ''; + $catalogEntity = app()->factory()->main()->getIBlockFactory()->getEntityById($catalogIBlockId); + if($catalogEntity){ + $catalogTitle = "[{$catalogEntity->getId()}] {$catalogEntity->getName()}"; + $catalogTitle = "[getAdminUrl()}\" target=\"_blank\">{$catalogEntity->getId()}] {$catalogEntity->getName()}"; } - ?>
Config NameTitleisSectionProperty
\ No newline at end of file + ?> +
isSectionProperty()?'Да':'Нет'?>
+
+ +

Mapping

+factory()->iBlock()->getEntityById($catalogIBlockId); +$propertyFactory = $af->getPropertyFactory(); +$map = $config->get('MAP', [])->toArray(); +?> + + + $typeMap){ + foreach ($typeMap as $propertyCode => $propertyValue) { + \Bx\Discount\Tools::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); + + $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); + if($propertyEntity?->getBaseType() != 'L'){ + continue; + } + /** + * @var \Wt\Core\Collection\IBlock\PropertyEnumCollection $valueCollection + */ + $valueCollection = $propertyEntity->getValueCollection(); + $valueEntity = $valueCollection->getByXmlId($propertyValue); + ?> + + + + + + +
TypePropertyPropertyValue
getUrl(); + $linkTemplate = "getUrl()}\" target=\"_blank\">{$propertyEntity->getId()}"; + echo "[$linkTemplate] {$propertyEntity->getName()}"; + ?>getId()}] {$valueEntity->getName()}"; + } + ?>
diff --git a/admin/main/submenu/settings/include/spiders.php b/admin/main/submenu/settings/include/spiders.php new file mode 100644 index 0000000..837e877 --- /dev/null +++ b/admin/main/submenu/settings/include/spiders.php @@ -0,0 +1,9 @@ +getAdminPage()->getModuleId(); +$spider = discountApp()->service()->spider(); + +$this->renderSpider($spider); diff --git a/default_config.php b/default_config.php index b16a882..a542cc8 100644 --- a/default_config.php +++ b/default_config.php @@ -2,19 +2,27 @@ //$config['events']['main:OnPageStart'][] = ['Bx\\Discount\\Handlers\\SaleDiscountEditPage', 'onPageStartHandler']; return [ - 'ENABLE' => false, - 'UPDATE_ELEMENT_INDEX' => false, - 'PAGE_SIZE' => 100, - 'RENEWAL_PERIOD' => 604800, + 'enable' => false, + 'update_element_index' => false, + 'page_size' => 100, - 'CATALOG_IBLOCK_ID' => 0, - 'DISCOUNT_TYPE_IBLOCK_ID' => 0, - 'CATALOG_GROUP_ID' => 0, + 'catalog_iblock_id' => 0, + 'catalog_group_id_list' => [], - 'TICKET_PROP_ID' => 0, + 'spider' => [ + 'micro_task_period' => 60, + 'expire' => 300, + 'period' => 86400, + ], + + /** + * содержит пары: Тип скидки => [ propertyCode.propertyValueXmlId, ... ] + * Тип скидки = '0' по умолчанию, если о скидке нет данных в таблице + */ 'MAP' => [ - 'A' => 0, - 'R' => 0, +// '0' => [ +// 'HIT.STOCK', +// ], ], diff --git a/lib/Agents/SpiderAgentManager.php b/lib/Agents/SpiderAgentCase.php similarity index 77% rename from lib/Agents/SpiderAgentManager.php rename to lib/Agents/SpiderAgentCase.php index b7af16f..ef37429 100644 --- a/lib/Agents/SpiderAgentManager.php +++ b/lib/Agents/SpiderAgentCase.php @@ -2,9 +2,10 @@ namespace Bx\Discount\Agents; +use Bx\Discount\Tools; use Wt\Core\Agent\AAgentCase; -class SpiderAgentManager extends AAgentCase +class SpiderAgentCase extends AAgentCase { /** * @return array @@ -33,9 +34,11 @@ class SpiderAgentManager extends AAgentCase $agent = app()->factory()->main()->getAgentFactory()->getEntityById($arAgent['ID']); } - $spider = discountApp()->service()->spider(); - $spider->setAgent($agent); - $spider->next(); + if(Tools::isEnable()){ + $spider = discountApp()->service()->spider(); + $spider->setContext($agent); + $spider->next(); + } return static::class . "::exec(\$arAgent);"; } diff --git a/lib/Ajax/BxDiscount.php b/lib/Ajax/BxDiscount.php index 4153cd4..b736c3c 100644 --- a/lib/Ajax/BxDiscount.php +++ b/lib/Ajax/BxDiscount.php @@ -2,7 +2,6 @@ namespace Bx\Discount\Ajax; -use Bx\Discount\Agents\SpiderAgentManager; use Wt\Core\Ajax\AAjaxHandler; class BxDiscount extends AAjaxHandler @@ -12,18 +11,6 @@ class BxDiscount extends AAjaxHandler $this->getResponse()->addData(['method' => 'ajaxStart']); } - public function spiderAgentDelete() - { - $agentManager = new SpiderAgentManager(); - $agentManager->delete(); - } - - public function spiderAgentAdd() - { - $agentManager = new SpiderAgentManager(); - $agentManager->add(); - } - public function test() { $this->getResponse()->addData(['method' => 'test', 'version' => discountApp()->module()->getVersion()]); diff --git a/lib/ElementSpider.php b/lib/ElementSpider.php index 32b2944..e69de29 100644 --- a/lib/ElementSpider.php +++ b/lib/ElementSpider.php @@ -1,122 +0,0 @@ -canDo()){ - if($this->isDataExpired()){ - $this->reset(); - } - return false; - } - $lastId = $this->getLastId(); - if($lastId == 0){ - $this->setBeginTimestamp(); - } - $iBlockId = discountApp()->config('CATALOG_IBLOCK_ID'); - $ids = Tools::getElementList($lastId); - if(count($ids) == 0){ - $this->setEndTimestamp(); - } - $__lastId = 0; - foreach ($ids as $id){ - $__lastId = $id; - Tools::setTicketValueFromDiscount($id); - } - $this->setLastId($__lastId); - return true; - } - - public function canDo() - { - $resetTimestamp = $this->getResetTimestamp(); - $spiderBeginTimestamp = $this->getBeginTimestamp(); - $spiderEndTimestamp = $this->getEndTimestamp(); - - return ($resetTimestamp > $spiderEndTimestamp) - || ($resetTimestamp > $spiderBeginTimestamp); - } - - public function isDataExpired() - { - $spiderEndTimestamp = $this->getEndTimestamp(); - $renewalPeriod = discountApp()->config('RENEWAL_PERIOD'); - return $spiderEndTimestamp + $renewalPeriod < time(); - } - - public function getLastId() - { - return (int)discountApp()->setting()->get('last_id', 0); - } - - public function setLastId($lastId) - { - discountApp()->setting()->set('last_id', (int)$lastId); - return $this; - } - - public function setBeginTimestamp() - { - discountApp()->setting()->set('spider_begin_timestamp', time()); - return $this; - } - - public function getBeginTimestamp() - { - return (int)discountApp()->setting()->get('spider_begin_timestamp', 0); - } - - public function setEndTimestamp() - { - discountApp()->setting()->set('spider_end_timestamp', time()); - return $this; - } - - public function getEndTimestamp() - { - return (int)discountApp()->setting()->get('spider_end_timestamp', 0); - } - - public function setResetTimestamp() - { - discountApp()->setting()->set('spider_reset_timestamp', time()); - return $this; - } - - public function getResetTimestamp() - { - return (int)discountApp()->setting()->get('spider_reset_timestamp', 0); - } - - public function reset() - { - $this->setLastId(0); - $this->setResetTimestamp(); - return $this; - } - - public function setAgent($agent) - { - $this->agent = $agent; - } - - /** - * @return AgentEntity|null - */ - public function getAgent() - { - return $this->agent; - } -} \ No newline at end of file diff --git a/lib/Entity/DiscountEntity.php b/lib/Entity/DiscountEntity.php index 6737c7b..d52c909 100644 --- a/lib/Entity/DiscountEntity.php +++ b/lib/Entity/DiscountEntity.php @@ -16,6 +16,14 @@ class DiscountEntity extends AEntity return parent::getFactory(); // TODO: Change the autogenerated stub } + /** + * @return string + */ + public function getType() + { + return $this->getRaw('TYPE', '0'); + } + /** * @return DiscountFields */ diff --git a/lib/Facade/DefaultServiceFacade.php b/lib/Facade/DefaultServiceFacade.php index d3771cd..3503e28 100644 --- a/lib/Facade/DefaultServiceFacade.php +++ b/lib/Facade/DefaultServiceFacade.php @@ -2,7 +2,7 @@ namespace Bx\Discount\Facade; -use Bx\Discount\ElementSpider; +use Bx\Discount\Spiders\ElementSpider; use Wt\Core\Facade\AFacade; class DefaultServiceFacade extends AFacade @@ -13,7 +13,13 @@ class DefaultServiceFacade extends AFacade */ public function spider() { - return $this->app->container()->make(ElementSpider::class); + return $this->app->container()->make(ElementSpider::class, [ + discountApp()->module()->getId(), + 'element', + discountApp()->config('spider.micro_task_period', 60), + discountApp()->config('spider.expire', 3600), + discountApp()->config('spider.period', 86400), + ]); } } \ No newline at end of file diff --git a/lib/Factory/DiscountFactory.php b/lib/Factory/DiscountFactory.php index 1035cf6..92cc8ea 100644 --- a/lib/Factory/DiscountFactory.php +++ b/lib/Factory/DiscountFactory.php @@ -43,6 +43,15 @@ class DiscountFactory extends AORMFactory return parent::getEntityById($id); } + /** + * @param array $arResult + * @return DiscountEntity + */ + public function getEntityByResult(array $arResult) + { + return parent::getEntityByResult($arResult); // TODO: Change the autogenerated stub + } + /** * @param array $filter * @param array $order diff --git a/lib/Handlers/ADiscount.php b/lib/Handlers/ADiscount.php index c848eaf..0f9a15b 100644 --- a/lib/Handlers/ADiscount.php +++ b/lib/Handlers/ADiscount.php @@ -24,61 +24,26 @@ class ADiscount protected static function updateAdditionalDiscountData($id) { - - $depsLoaded = \Bitrix\Main\Loader::includeModule('iblock'); - if(!$depsLoaded) { - return; - } - - $res_element = \CIBlockElement::GetList( - ['SORT' => 'ASC'], - ['IBLOCK_ID' => discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID'), 'NAME' => $id], - false, - false, - ['ID', 'NAME', 'PROPERTY_TYPE'] - ); - $res_element_count = $res_element->SelectedRowsCount(); - if($res_element_count){ - $obElement = $res_element->GetNext(); - \CIBlockElement::SetPropertyValuesEx( - $obElement['ID'], - discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID'), - [ - 'TYPE' => $_POST['PRODUCT_TYPE'], - 'MIN_COUNT_PROD' => $_POST['MIN_COUNT_PROD'] - ] - ); - } else { - $el = new \CIBlockElement(); - $arTypeField = [ - 'IBLOCK_ID' => discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID'), - 'NAME' => $id, - 'PROPERTY_VALUES' => [ - 'TYPE' => $_POST['PRODUCT_TYPE'], - 'MIN_COUNT_PROD' => $_POST['MIN_COUNT_PROD'], - ], - ]; - $el->Add($arTypeField); - } + $discountFactory = discountApp()->factory()->main()->getDiscountFactory(); + return $discountFactory->updateOrCreate([ + '=DISCOUNT_ID' => $id, + ], [ + 'DISCOUNT_ID' => $id, + 'PRODUCT_TYPE' => (string)$_POST['PRODUCT_TYPE'], + 'MIN_COUNT_PROD' => (int)$_POST['MIN_COUNT_PROD'], + ]); } protected static function addAdditionalDiscountData($id) { - $depsLoaded = \Bitrix\Main\Loader::includeModule('iblock'); - if(!$depsLoaded) { - return; - } - - $el = new \CIBlockElement(); - $arTypeField = [ - 'IBLOCK_ID' => discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID'), - 'NAME' => $id, - 'PROPERTY_VALUES' => [ - 'TYPE' => $_POST['PRODUCT_TYPE'], - 'MIN_COUNT_PROD' => $_POST['MIN_COUNT_PROD'], - ], - ]; - $r = $el->Add($arTypeField); + $discountFactory = discountApp()->factory()->main()->getDiscountFactory(); + return $discountFactory->updateOrCreate([ + '=DISCOUNT_ID' => $id, + ], [ + 'DISCOUNT_ID' => $id, + 'PRODUCT_TYPE' => (string)$_POST['PRODUCT_TYPE'], + 'MIN_COUNT_PROD' => (int)$_POST['MIN_COUNT_PROD'], + ]); } /** @@ -104,7 +69,7 @@ class ADiscount if(count($arResult) > $counter){ foreach($arResult as $key => $discount){ - $discountViewType = Tools::getProductTypeArray($discount['ID']); + $discountViewType = Tools::getDiscountEntity($discount['ID']); if($discountViewType['PRODUCT_TYPE'] === 'Q'){ unset($arResult[$key]); } diff --git a/lib/ORM/DiscountTable.php b/lib/ORM/DiscountTable.php index f4a3a43..df201fc 100644 --- a/lib/ORM/DiscountTable.php +++ b/lib/ORM/DiscountTable.php @@ -38,6 +38,12 @@ class DiscountTable extends ADataManager 'autocomplete' => true, 'title' => 'Id', ]), + 'DISCOUNT_ID' => new \Bitrix\Main\Entity\IntegerField('DISCOUNT_ID', [ + 'title' => 'DiscountId', + ]), + 'TYPE' => new \Bitrix\Main\Entity\StringField('TYPE', [ + 'title' => 'Type', + ]), 'CREATED_AT' => new \Bitrix\Main\Entity\DatetimeField('CREATED_AT', [ 'title' => 'CreatedAt', 'user_edit' => false, diff --git a/lib/Providers/DefaultServiceProvider.php b/lib/Providers/DefaultServiceProvider.php index 35b1eeb..57195d8 100644 --- a/lib/Providers/DefaultServiceProvider.php +++ b/lib/Providers/DefaultServiceProvider.php @@ -2,7 +2,7 @@ namespace Bx\Discount\Providers; -use Bx\Discount\ElementSpider; +use Bx\Discount\Spiders\ElementSpider; use Wt\Core\Interfaces\IApp; use Wt\Core\Providers\ADefaultServiceProvider; diff --git a/lib/Spiders/ElementSpider.php b/lib/Spiders/ElementSpider.php new file mode 100644 index 0000000..5d7250e --- /dev/null +++ b/lib/Spiders/ElementSpider.php @@ -0,0 +1,30 @@ +config('catalog_iblock_id', 0); + if(!$catalogIBlockId){ + return; + } + $lastId = $this->getLastId(); + $ids = Tools::getElementList($lastId); + $__lastId = 0; + foreach ($ids as $id){ + $__lastId = $id; + Tools::setTicketValueFromDiscount($id); + } + + if($ids){ + $this->setLastId($__lastId); + } + } +} \ No newline at end of file diff --git a/lib/Tools.php b/lib/Tools.php index e78ab26..5e2aea2 100644 --- a/lib/Tools.php +++ b/lib/Tools.php @@ -31,111 +31,151 @@ class Tools if(defined('SALE_DISCOUNT_MIGRATE_MODE') && SALE_DISCOUNT_MIGRATE_MODE){ return false; } - return discountApp()->config('ENABLE'); + return discountApp()->config('enable', false); } - public static function getProductTypeArray($discountID) + /** + * @param $discountId + * @return Entity\DiscountEntity + */ + public static function getDiscountEntity($discountId) { - $arProductType = []; - $arProductType['PRODUCT_TYPE'] = '0'; - $arProductType['MIN_COUNT_PROD'] = 0; + $discountFactory = discountApp()->factory()->main()->getDiscountFactory(); + $discountEntity = $discountFactory->getEntityById($discountId); - if(empty($discountID) || ($discountID < 0)){ - return $arProductType; + if(!$discountEntity){ + return $discountFactory->getEntityByResult([]); } - $depsLoaded = Loader::includeModule('iblock'); - - if(!$depsLoaded) { - return $arProductType; - } - - $iBlockId = discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID'); - - if(!$iBlockId){ - return $arProductType; - } - - $res_element = \CIBlockElement::GetList( - ['SORT' => 'ASC'], - ['IBLOCK_ID' => $iBlockId, '=NAME' => $discountID], - false, - false, - ['ID', 'NAME', 'PROPERTY_TYPE', 'PROPERTY_MIN_COUNT_PROD'] - ); - $res_element_count = $res_element->SelectedRowsCount(); - if($res_element_count){ - $obElement = $res_element->GetNext(true, false); - $arProductType['PRODUCT_TYPE'] = (string)$obElement['PROPERTY_TYPE_VALUE']; - $arProductType['MIN_COUNT_PROD'] = (int)$obElement['PROPERTY_MIN_COUNT_PROD_VALUE']; - } - return $arProductType; + return $discountEntity; } public static function setTicketValueFromDiscount($elementId) { $includeModule = Loader::includeModule('catalog') - && Loader::includeModule('iblock'); + && Loader::includeModule('iblock') + && Loader::includeModule('sale'); if(!$includeModule) { return false; } - $iBlockId = discountApp()->config('CATALOG_IBLOCK_ID'); - $ticketPropId = discountApp()->config('TICKET_PROP_ID'); + $iBlockId = discountApp()->config('catalog_iblock_id'); $map = discountApp()->config('MAP')->toArray(); - $rawDiscountList = \CCatalogDiscount::GetDiscount( + $discountList = \CCatalogDiscount::GetDiscount( $elementId, $iBlockId, - [], //CATALOG_GROUP_ID + [], //catalog_group_id_list [2], 'N', // renewal 's1', false ); - $discountList = []; - foreach ($rawDiscountList as $i => $discount){ - $discountId = $discount['ID']; - $arProductType = static::getProductTypeArray($discountId); - $discountList[$discountId]['PAN_SETTINGS'] = $arProductType; + if(!is_array($discountList)){ + $discountList = []; } - // pre($discountList, 0); - - $iterator = \CIBlockElement::GetPropertyValues( - $iBlockId, - ['ID' => $elementId], - false, - ['ID' => $ticketPropId] - ); - $ticketPropValueList = []; - $arTicketPropValue = $iterator->Fetch(); - if($arTicketPropValue && $arTicketPropValue[$ticketPropId]){ - $ticketPropValueList = $arTicketPropValue[$ticketPropId]; + $af = app()->factory()->iBlock()->getEntityById($iBlockId); + $propertyFactory = $af->getPropertyFactory(); + $elementFactory = $af->getElementFactory(); + $elementEntity = $elementFactory->getEntityById($elementId); + if(!$elementEntity){ + return false; } - sort($ticketPropValueList); - $newTicketValue = array_diff($ticketPropValueList, $map); + $pvc = $elementEntity->getPropertyValueCollection(); + $currentPropertyValueList = []; + foreach ($map as $type => $typeMap){ + foreach ($typeMap as $propertyCode => $propertyValue) { + static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); - foreach ($discountList as $discountId => $discount){ - $ticketType = $discount['PAN_SETTINGS']['PRODUCT_TYPE']; - if($ticketType && $map[$ticketType]){ - $newTicketValue[] = $map[$ticketType]; + $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); + if($propertyEntity?->getBaseType() != 'L'){ + continue; + } + + if(!isset($currentPropertyValueList[$propertyEntity->getId()])){ + $pve = $pvc->getByCode($propertyCode); + $currentValue = $pve->getValue(); + $currentValueCollection = collect($currentValue); + $currentPropertyValueList[$propertyEntity->getId()] = $currentValueCollection->values()->sort()->toArray(); + } } } - sort($newTicketValue); + $currentPropertyValueList = collect($currentPropertyValueList)->sort()->toArray(); + pre($currentPropertyValueList); - if($newTicketValue != $ticketPropValueList) { - \CIBlockElement::SetPropertyValuesEx($elementId, $iBlockId, [ - $ticketPropId => $newTicketValue?:false - ]); + $newPropertyValueList = $currentPropertyValueList; + foreach ($map as $type => $typeMap){ + foreach ($typeMap as $propertyCode => $propertyValue) { + static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); - if(discountApp()->config('UPDATE_ELEMENT_INDEX')){ + $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); + if($propertyEntity?->getBaseType() != 'L'){ + continue; + } + + /** + * @var \Wt\Core\Collection\IBlock\PropertyEnumCollection $valueCollection + */ + $valueCollection = $propertyEntity->getValueCollection(); + $valueEntity = $valueCollection->getByXmlId($propertyValue); + $newPropertyValueList[$propertyEntity->getId()] + = collect($newPropertyValueList[$propertyEntity->getId()]) + ->deleteValue($valueEntity->getId()) + ->values()->toArray(); + } + } + + foreach ($discountList as $discountId => $discount){ + $discountEntity = static::getDiscountEntity($discountId); + $type = $discountEntity->getType(); + if(isset($map[$type]) && is_array($map[$type])){ + $typeMap = $map[$type]; + foreach ($typeMap as $propertyCode => $propertyValue) { + static::prepareMapPropertyCodeAndValue($propertyCode, $propertyValue); + + $propertyEntity = $propertyFactory->getEntityByCode($propertyCode); + if($propertyEntity?->getBaseType() != 'L'){ + continue; + } + + /** + * @var \Wt\Core\Collection\IBlock\PropertyEnumCollection $valueCollection + */ + $valueCollection = $propertyEntity->getValueCollection(); + $valueEntity = $valueCollection->getByXmlId($propertyValue); + $newPropertyValueList[$propertyEntity->getId()] + = collect($newPropertyValueList[$propertyEntity->getId()]) + ->push($valueEntity->getId()) + ->values()->toArray(); + } + } + } + pre($newPropertyValueList); + + if($newPropertyValueList != $currentPropertyValueList) { + pre('set'); + foreach ($newPropertyValueList as $propertyId => $propertyValue){ + if(is_array($propertyValue) && !$propertyValue){ + $newPropertyValueList[$propertyId] = false; + } + } + \CIBlockElement::SetPropertyValuesEx($elementId, $iBlockId, $newPropertyValueList); + + if(discountApp()->config('update_element_index')){ \Bitrix\Iblock\PropertyIndex\Manager::updateElementIndex($iBlockId, $elementId); } } return true; } + public static function prepareMapPropertyCodeAndValue(&$propertyCode, &$propertyValue) + { + if (is_int($propertyCode)) { + [$propertyCode, $propertyValue] = explode('.', $propertyValue); + } + } + public static function getElementList($lastId = 0) { $includeModule = Loader::includeModule('iblock'); @@ -143,7 +183,10 @@ class Tools return []; } - $iBlockId = discountApp()->config('CATALOG_IBLOCK_ID'); + $iBlockId = discountApp()->config('catalog_iblock_id', 0); + if(!$iBlockId) { + return []; + } $rs = \CIBlockElement::GetList( ['ID' => 'ASC'], @@ -155,7 +198,7 @@ class Tools ], false, [ - 'nTopCount' => discountApp()->config('PAGE_SIZE', 50), + 'nTopCount' => discountApp()->config('page_size', 50), ], ['ID', 'IBLOCK_ID'] );