diff --git a/admin/main/lang/ru/config.php b/admin/main/lang/ru/config.php
index a9f0ebd..9532459 100644
--- a/admin/main/lang/ru/config.php
+++ b/admin/main/lang/ru/config.php
@@ -1,2 +1,2 @@
-
+setSort(10)
- ->setTitle('Discount Main');
\ No newline at end of file
+ ->setTitle('Discount');
\ No newline at end of file
diff --git a/admin/main/submenu/discount_table/submenu/row/index.php b/admin/main/submenu/discount_table/submenu/row/index.php
index 47176da..205eea6 100644
--- a/admin/main/submenu/discount_table/submenu/row/index.php
+++ b/admin/main/submenu/discount_table/submenu/row/index.php
@@ -3,7 +3,6 @@
use Bx\Discount\ORM\DiscountTable;
use Wt\Core\Admin\TableRowAdminPage;
-
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_before.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_admin_after.php');
/**
@@ -15,6 +14,4 @@ $AdminPageTableRow = new TableRowAdminPage($ormTable, $_REQUEST, '/bitrix/admin/
$AdminPageTableRow->checkPermission();
$AdminPageTableRow->start();
-\CJSCore::Init('jquery');
-
require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/epilog_admin.php');
\ No newline at end of file
diff --git a/admin/main/submenu/settings/config.php b/admin/main/submenu/settings/config.php
index 307dcd5..fd74c4c 100644
--- a/admin/main/submenu/settings/config.php
+++ b/admin/main/submenu/settings/config.php
@@ -1,6 +1,7 @@
[
'TITLE' => 'Общие сведения',
'OPTIONS' => [
- 'ENABLE' => [
- 'TITLE' => 'Enable module',
- 'TYPE' => 'checkbox',
- 'DEFAULT' => 'N',
- 'HINT' => '',
- ],
'CONFIG' => [
- 'TITLE' => 'Конфигурационные данные',
- 'HINT' => '',
+ 'TITLE' => 'Config',
'TYPE' => 'content',
- 'INCLUDEFILE' => \Wt\Core\Tools::removeDocRoot(__DIR__ . '/include/config.php'),
+ 'INCLUDEFILE' => Tools::removeDocRoot(__DIR__ . '/include/config.php'),
+ 'SHOW_EDIT_BUTTON' => 'N',
+ 'DEFAULT' => '',
+ ],
+ 'AGENTS' => [
+ 'TITLE' => 'Агенты',
+ 'TYPE' => 'content',
+ 'INCLUDEFILE' => Tools::removeDocRoot(__DIR__ . '/include/agents.php'),
'SHOW_EDIT_BUTTON' => 'N',
'DEFAULT' => '',
],
@@ -34,7 +35,7 @@ return [
'DOCUMENTATION' => [
'TITLE' => 'Documentation',
'TYPE' => 'content',
- 'INCLUDEFILE' => \Wt\Core\Tools::removeDocRoot(__DIR__ . '/include/documentation.php'),
+ 'INCLUDEFILE' => Tools::removeDocRoot(__DIR__ . '/include/documentation.php'),
'SHOW_EDIT_BUTTON' => 'N',
'DEFAULT' => '',
],
diff --git a/admin/main/submenu/settings/include/agents.php b/admin/main/submenu/settings/include/agents.php
new file mode 100644
index 0000000..2b3b07c
--- /dev/null
+++ b/admin/main/submenu/settings/include/agents.php
@@ -0,0 +1,11 @@
+getAdminPage()->getModuleId();
+$agentCase = new SpiderAgentManager();
+
+$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 3cfe04b..9b33b8d 100644
--- a/admin/main/submenu/settings/include/config.php
+++ b/admin/main/submenu/settings/include/config.php
@@ -2,48 +2,75 @@
/**
* @var Wt\Core\Admin\Templater $this
*/
-$moduleId = $this->getAdminPage()->getModuleId();
-$app = app()::getInstance($moduleId);
+use Wt\Core\Support\System;
+
+$system = new System();
+$config = discountApp()->config();
$data = [];
-$data['env'] = $app->env();
+$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){
+ $priceTypeEntity = app()->factory()->shop()->getPriceTypeFactory()->getEntityById($priceTypeId);
+ if($priceTypeEntity){
+ $linkTemplate = "$priceTypeId";
+ $data['CATALOG_GROUP_ID'] = "[$linkTemplate] {$priceTypeEntity->getName()}";
+ }
+}
+$data['PAGE_SIZE'] = $config->get('PAGE_SIZE');
+$data['RENEWAL_PERIOD'] = $config->get('RENEWAL_PERIOD');
?>
-
- $value) {
- ?>| = $key ?> | = $value ?> |
-
+ $value) {
+ ?>| = $key ?> | = $value ?> |
| MAP | get('MAP', [])->count()){
-?>
-
- module()->getSrc();
- $html = "if(file_exists(\$_SERVER['DOCUMENT_ROOT'] . '$src/init.php'))
- include_once \$_SERVER['DOCUMENT_ROOT'] . '$src/init.php'; ";
- ?>
-
- =$this->getCheckTemplate($app->isIncludeFileToInitPhp())?>
- |
-
- Модуль подключен в init.php
- |
-
-
- =$initPhpSrc?>
- init.php отсутствует
-
- =$html?>
- |
-
-
-
\ No newline at end of file
+ ?>
+ | type | ticket item |
+ get('MAP', []) as $type => $id){?>
+ | = $type ?> | getAdapter()->getValueFactory();
+ $entity = $valueFactory->getEntityById($id);
+ if($entity){
+ echo "[{$entity->getId()}] {$entity->getName()}";
+ }
+ }
+ ?> |
+
+ empty |
\ No newline at end of file
diff --git a/admin/main/submenu/settings/include/documentation.php b/admin/main/submenu/settings/include/documentation.php
index 45d092e..8b9c52c 100644
--- a/admin/main/submenu/settings/include/documentation.php
+++ b/admin/main/submenu/settings/include/documentation.php
@@ -1 +1,26 @@
-Документация
\ No newline at end of file
+Catalog: Скидки
+
+Sale: Скидки
+
+Таблица с типами скидкок
+
+Таблица c настройками паука
+
+
+
+
+ Перед
миграцией важно сохранить ID скидок
+ bitrix/modules/sale/admin/discount_catalog_migrator.php:377
+ DiscountCatalogMigrator::migrateDiscounts
+ в $newData добавить ID
+
+
+
+
+$newData = array(
+ 'ID' => $discount['ID'],
+ 'XML_ID' => $discount['XML_ID'],
+ ...
+);
+
+
diff --git a/admin/main/submenu/settings/lang/ru/index.php b/admin/main/submenu/settings/lang/ru/index.php
index c090dcf..bd9dc32 100644
--- a/admin/main/submenu/settings/lang/ru/index.php
+++ b/admin/main/submenu/settings/lang/ru/index.php
@@ -1,2 +1,2 @@
-
+setSort(5)
- ->setTitle('Settings')
+ ->setTitle('settings')
->setUrl();
\ No newline at end of file
diff --git a/default_config.php b/default_config.php
index f3a1e4e..abfa4dc 100644
--- a/default_config.php
+++ b/default_config.php
@@ -1,21 +1,61 @@
[
- 'Bx\Discount\Factory\DiscountFactory' => ['cache' => ['hit'], 'is_collection_query' => true],
+ 'ENABLE' => false,
+ 'UPDATE_ELEMENT_INDEX' => false,
+ 'PAGE_SIZE' => 100,
+ 'RENEWAL_PERIOD' => 604800,
+
+ 'CATALOG_IBLOCK_ID' => 0,
+ 'DISCOUNT_TYPE_IBLOCK_ID' => 0,
+ 'CATALOG_GROUP_ID' => 0,
+
+ 'TICKET_PROP_ID' => 0,
+ 'MAP' => [
+ 'A' => 0,
+ 'R' => 0,
],
+
+
'events' => [
- 'main:OnPageStart' => [
- ['Bx\Discount\Handlers\Autoloader', 'onPageStart'],
+ 'main:OnBeforeProlog' => ['Bx\\Discount\\Handlers\\SaleDiscountEditPage', 'onPageStartHandler'],
+
+
+ 'sale:DiscountOnBeforeAdd' => ['Bx\Discount\Handlers\SaleDiscount', 'onBeforeAdd'],
+ 'sale:DiscountOnAfterAdd' => [
+ ['Bx\Discount\Handlers\SaleDiscount', 'onAfterAdd'],
+ ['Bx\Discount\Handlers\SpiderHandler', 'saleDiscountOnAfterAdd'],
],
- 'main:OnProlog' => [
- ['Bx\Discount\Handlers\Autoloader', 'onProlog'],
+ 'sale:DiscountOnBeforeUpdate' => ['Bx\Discount\Handlers\SaleDiscount', 'onBeforeUpdate'],
+ 'sale:DiscountOnAfterUpdate' => [
+ ['Bx\Discount\Handlers\SaleDiscount', 'onAfterUpdate'],
+ ['Bx\Discount\Handlers\SpiderHandler', 'saleDiscountOnAfterUpdate'],
],
- 'main:OnEpilog' => [
- ['Bx\Discount\Handlers\Autoloader', 'onEpilog'],
+ 'sale:DiscountOnBeforeDelete' => ['Bx\Discount\Handlers\SaleDiscount', 'onBeforeDelete'],
+ 'sale:DiscountOnAfterDelete' => [
+ ['Bx\Discount\Handlers\SaleDiscount', 'onAfterDelete'],
+ ['Bx\Discount\Handlers\SpiderHandler', 'saleDiscountOnAfterDelete'],
],
- 'main:OnBeforeProlog' => [
- ['Bx\Discount\Handlers\Autoloader', 'onBeforeProlog'],
+
+
+ 'catalog:OnBeforeDiscountAdd' => ['Bx\Discount\Handlers\CatalogDiscount', 'onBeforeAdd'],
+ 'catalog:OnDiscountAdd' => [
+ ['Bx\Discount\Handlers\CatalogDiscount', 'onAfterAdd'],
+ ['Bx\Discount\Handlers\SpiderHandler', 'saleDiscountOnAfterAdd'],
],
- ],
+ 'catalog:OnBeforeDiscountUpdate' => ['Bx\Discount\Handlers\CatalogDiscount', 'onBeforeUpdate'],
+ 'catalog:OnDiscountUpdate' => [
+ ['Bx\Discount\Handlers\CatalogDiscount', 'onAfterUpdate'],
+ ['Bx\Discount\Handlers\SpiderHandler', 'saleDiscountOnAfterUpdate'],
+ ],
+ 'catalog:OnBeforeDiscountDelete' => ['Bx\Discount\Handlers\CatalogDiscount', 'onBeforeDelete'],
+ 'catalog:OnDiscountDelete' => [
+ ['Bx\Discount\Handlers\CatalogDiscount', 'onAfterDelete'],
+ ['Bx\Discount\Handlers\SpiderHandler', 'saleDiscountOnAfterDelete'],
+ ],
+ 'catalog:OnGetDiscountResult' => [
+ ['Bx\Discount\Handlers\CatalogDiscount', 'onGetDiscountResult'],
+ ],
+ ]
];
\ No newline at end of file
diff --git a/lib/Agents/SpiderAgentManager.php b/lib/Agents/SpiderAgentManager.php
new file mode 100644
index 0000000..b7af16f
--- /dev/null
+++ b/lib/Agents/SpiderAgentManager.php
@@ -0,0 +1,42 @@
+ [
+ 'NEXT_EXEC' => $this->getNextExecLater(1),
+ 'MODULE_ID' => discountApp()->module()->getId(),
+ 'AGENT_INTERVAL' => 60,
+ 'IS_PERIOD' => 'Y',
+ ],
+ ];
+ }
+
+ public static function exec($arAgent)
+ {
+ ini_set('max_execution_time', 0);
+ ini_set('memory_limit', -1);
+ @set_time_limit(0);
+
+
+ $agent = null;
+ if (is_array($arAgent) && $arAgent['ID']) {
+ $agent = app()->factory()->main()->getAgentFactory()->getEntityById($arAgent['ID']);
+ }
+
+ $spider = discountApp()->service()->spider();
+ $spider->setAgent($agent);
+ $spider->next();
+
+ return static::class . "::exec(\$arAgent);";
+ }
+}
\ No newline at end of file
diff --git a/lib/Ajax/BxDiscount.php b/lib/Ajax/BxDiscount.php
index 021eb7a..4153cd4 100644
--- a/lib/Ajax/BxDiscount.php
+++ b/lib/Ajax/BxDiscount.php
@@ -2,6 +2,7 @@
namespace Bx\Discount\Ajax;
+use Bx\Discount\Agents\SpiderAgentManager;
use Wt\Core\Ajax\AAjaxHandler;
class BxDiscount extends AAjaxHandler
@@ -11,9 +12,21 @@ 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' => app()::getInstance('bx.discount')->module()->getVersion()]);
+ $this->getResponse()->addData(['method' => 'test', 'version' => discountApp()->module()->getVersion()]);
return $this->getResponse();
}
}
\ No newline at end of file
diff --git a/lib/ElementSpider.php b/lib/ElementSpider.php
new file mode 100644
index 0000000..32b2944
--- /dev/null
+++ b/lib/ElementSpider.php
@@ -0,0 +1,122 @@
+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/Facade/DefaultServiceFacade.php b/lib/Facade/DefaultServiceFacade.php
index 3d44f79..d3771cd 100644
--- a/lib/Facade/DefaultServiceFacade.php
+++ b/lib/Facade/DefaultServiceFacade.php
@@ -2,9 +2,18 @@
namespace Bx\Discount\Facade;
+use Bx\Discount\ElementSpider;
use Wt\Core\Facade\AFacade;
class DefaultServiceFacade extends AFacade
{
+ /**
+ * @return ElementSpider
+ */
+ public function spider()
+ {
+ return $this->app->container()->make(ElementSpider::class);
+ }
+
}
\ No newline at end of file
diff --git a/lib/Handlers/ADiscount.php b/lib/Handlers/ADiscount.php
new file mode 100644
index 0000000..c848eaf
--- /dev/null
+++ b/lib/Handlers/ADiscount.php
@@ -0,0 +1,114 @@
+ 'Не выбран тип визуального представления скидки на сайте'
+ ];
+
+ public static function getLang($code)
+ {
+ return static::$lang[$code];
+ }
+
+ protected static function isValidData()
+ {
+ return isset($_POST['PRODUCT_TYPE']) && $_POST['PRODUCT_TYPE'] !== '0' && $_POST['PRODUCT_TYPE'] !== '';
+ }
+
+
+ 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);
+ }
+ }
+
+ 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);
+ }
+
+ /**
+ * Немного бреда с седых времен
+ * я хз что тут происходит
+ * коммент от старого разработчика: при поиске оптимальной цены на товар не учитываем скидки типа "От кол-ва"
+ * @see /local/php_interface/include/events.php [git history]
+ * @see BxDiscount [old functions]
+ *
+ * Желательно избавиться от функционала Q !!!
+ *
+ * @param $arResult
+ * @return void
+ */
+ public static function onGetDiscountResult(&$arResult)
+ {
+ global $scriptType;
+ $counter = 1;
+ if($scriptType === 'XML_EXPORT'){
+ $counter = 0;
+ }
+
+ if(count($arResult) > $counter){
+ foreach($arResult as $key => $discount){
+
+ $discountViewType = Tools::getProductTypeArray($discount['ID']);
+ if($discountViewType['PRODUCT_TYPE'] === 'Q'){
+ unset($arResult[$key]);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/lib/Handlers/CatalogDiscount.php b/lib/Handlers/CatalogDiscount.php
new file mode 100644
index 0000000..cc85e7d
--- /dev/null
+++ b/lib/Handlers/CatalogDiscount.php
@@ -0,0 +1,96 @@
+ThrowException(
+ static::getLang('EMPTY_PRODUCT_TYPE')
+ );
+ return false;
+ }
+ return true;
+ }
+
+ public static function onAfterAdd($id, $arFields)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ if(!Tools::isSaleDiscountEditPage()){
+ return;
+ }
+ if(!Tools::isValidRequestData()){
+ return;
+ }
+
+ static::updateAdditionalDiscountData($id);
+ }
+
+ public static function onBeforeUpdate($id, &$arFields)
+ {
+ if(!Tools::isEnable()){
+ return true;
+ }
+
+ $isSaleDiscountEditPage = Tools::isSaleDiscountEditPage();
+ if(!$isSaleDiscountEditPage){
+ return true;
+ }
+
+ if(!static::isValidData()){
+ global $APPLICATION;
+ $APPLICATION->ThrowException(static::$lang['EMPTY_PRODUCT_TYPE'], $id);
+ return false;
+ }
+ return true;
+ }
+
+ public static function onAfterUpdate($id, $arFields)
+ {
+ if(!Tools::isEnable()){
+ return true;
+ }
+
+ if(!Tools::isSaleDiscountEditPage()){
+ return true;
+ }
+ if(!Tools::isValidRequestData()){
+ return true;
+ }
+
+ static::updateAdditionalDiscountData($id);
+
+ return true;
+ }
+
+ public static function onBeforeDelete($id)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ }
+
+ public static function onAfterDelete($id)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ }
+
+}
\ No newline at end of file
diff --git a/lib/Handlers/SaleDiscount.php b/lib/Handlers/SaleDiscount.php
new file mode 100644
index 0000000..91be249
--- /dev/null
+++ b/lib/Handlers/SaleDiscount.php
@@ -0,0 +1,160 @@
+getParameter("id");
+
+ if(is_array($id))
+ $id = $id["ID"];
+
+ $entity = $event->getEntity();
+ $entityDataClass = $entity->GetDataClass();
+ $eventType = $event->getEventType();
+ $arParameters = $event->getParameters();
+
+ $result = new \Bitrix\Main\Entity\EventResult();
+ if(!static::isValidData()){
+ $result->addError(
+ new \Bitrix\Main\Entity\EntityError(static::$lang['EMPTY_PRODUCT_TYPE'])
+ );
+ return $result;
+ }
+ }
+
+ public static function onAfterAdd(Event $event)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ $id = $event->getParameter('id');
+
+ if(is_array($id))
+ $id = $id['ID'];
+
+ $entity = $event->getEntity();
+ $entityDataClass = $entity->GetDataClass();
+ $eventType = $event->getEventType();
+ $arParameters = $event->getParameters();
+
+ if(!Tools::isSaleDiscountEditPage()){
+ return;
+ }
+ if(!Tools::isValidRequestData()){
+ return;
+ }
+
+
+ static::addAdditionalDiscountData($id);
+ }
+
+ public static function onBeforeUpdate(Event $event)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ $id = $event->getParameter('id');
+
+ if(is_array($id))
+ $id = $id['ID'];
+
+ $entity = $event->getEntity();
+ $entityDataClass = $entity->GetDataClass();
+ $eventType = $event->getEventType();
+ $arParameters = $event->getParameters();
+ $arFields = $event->getParameter('fields');
+ $isSaleDiscountEditPage = Tools::isSaleDiscountEditPage();
+ if(!$isSaleDiscountEditPage){
+ return;
+ }
+ //dd($id);
+ //$event->setParameter("fields",$arFields);
+
+ $result = new \Bitrix\Main\Entity\EventResult();
+ if(!static::isValidData()){
+ $result->addError(
+ new \Bitrix\Main\Entity\EntityError(static::$lang['EMPTY_PRODUCT_TYPE'])
+ );
+ return $result;
+ }
+ }
+
+ public static function onAfterUpdate(Event $event)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ $id = $event->getParameter('id');
+
+ if(is_array($id))
+ $id = $id['ID'];
+
+ $entity = $event->getEntity();
+ $entityDataClass = $entity->GetDataClass();
+ $eventType = $event->getEventType();
+ $arParameters = $event->getParameters();
+ $arFields = $event->getParameter('fields');
+
+ if(!Tools::isSaleDiscountEditPage()){
+ return;
+ }
+ if(!Tools::isValidRequestData()){
+ return;
+ }
+
+ static::updateAdditionalDiscountData($id);
+ }
+
+ public static function onBeforeDelete(Event $event)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ $id = $event->getParameter('id');
+
+ if(is_array($id))
+ $id = $id['ID'];
+
+ $entity = $event->getEntity();
+ $entityDataClass = $entity->GetDataClass();
+ $eventType = $event->getEventType();
+ $arParameters = $event->getParameters();
+
+
+ }
+
+ public static function onAfterDelete(Event $event)
+ {
+ if(!Tools::isEnable()){
+ return;
+ }
+
+ $id = $event->getParameter('id');
+
+ if(is_array($id))
+ $id = $id['ID'];
+
+ $entity = $event->getEntity();
+ $entityDataClass = $entity->GetDataClass();
+ $eventType = $event->getEventType();
+ $arParameters = $event->getParameters();
+
+
+ }
+
+}
\ No newline at end of file
diff --git a/lib/Handlers/SaleDiscountEditPage.php b/lib/Handlers/SaleDiscountEditPage.php
new file mode 100644
index 0000000..d235ed0
--- /dev/null
+++ b/lib/Handlers/SaleDiscountEditPage.php
@@ -0,0 +1,37 @@
+ 0)
+ {
+ $copy = (isset($_REQUEST['action']) && (string)$_REQUEST['action'] == 'copy');
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/lib/Handlers/SpiderHandler.php b/lib/Handlers/SpiderHandler.php
new file mode 100644
index 0000000..fd74227
--- /dev/null
+++ b/lib/Handlers/SpiderHandler.php
@@ -0,0 +1,27 @@
+service()->spider()->reset();
+ }
+}
\ No newline at end of file
diff --git a/lib/Providers/DefaultServiceProvider.php b/lib/Providers/DefaultServiceProvider.php
index b2b4f9a..35b1eeb 100644
--- a/lib/Providers/DefaultServiceProvider.php
+++ b/lib/Providers/DefaultServiceProvider.php
@@ -2,6 +2,7 @@
namespace Bx\Discount\Providers;
+use Bx\Discount\ElementSpider;
use Wt\Core\Interfaces\IApp;
use Wt\Core\Providers\ADefaultServiceProvider;
@@ -15,5 +16,6 @@ class DefaultServiceProvider extends ADefaultServiceProvider
$container = $this->app->container();
$config = $this->app->config();
+ $container->singleton(ElementSpider::class);
}
}
\ No newline at end of file
diff --git a/lib/Tools.php b/lib/Tools.php
new file mode 100644
index 0000000..e78ab26
--- /dev/null
+++ b/lib/Tools.php
@@ -0,0 +1,222 @@
+config('ENABLE');
+ }
+
+ public static function getProductTypeArray($discountID)
+ {
+ $arProductType = [];
+ $arProductType['PRODUCT_TYPE'] = '0';
+ $arProductType['MIN_COUNT_PROD'] = 0;
+
+ if(empty($discountID) || ($discountID < 0)){
+ return $arProductType;
+ }
+
+ $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;
+ }
+
+ public static function setTicketValueFromDiscount($elementId)
+ {
+ $includeModule = Loader::includeModule('catalog')
+ && Loader::includeModule('iblock');
+ if(!$includeModule) {
+ return false;
+ }
+
+ $iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
+ $ticketPropId = discountApp()->config('TICKET_PROP_ID');
+ $map = discountApp()->config('MAP')->toArray();
+
+ $rawDiscountList = \CCatalogDiscount::GetDiscount(
+ $elementId,
+ $iBlockId,
+ [], //CATALOG_GROUP_ID
+ [2],
+ 'N', // renewal
+ 's1',
+ false
+ );
+ $discountList = [];
+ foreach ($rawDiscountList as $i => $discount){
+ $discountId = $discount['ID'];
+ $arProductType = static::getProductTypeArray($discountId);
+ $discountList[$discountId]['PAN_SETTINGS'] = $arProductType;
+ }
+
+ // pre($discountList, 0);
+
+ $iterator = \CIBlockElement::GetPropertyValues(
+ $iBlockId,
+ ['ID' => $elementId],
+ false,
+ ['ID' => $ticketPropId]
+ );
+ $ticketPropValueList = [];
+ $arTicketPropValue = $iterator->Fetch();
+ if($arTicketPropValue && $arTicketPropValue[$ticketPropId]){
+ $ticketPropValueList = $arTicketPropValue[$ticketPropId];
+ }
+ sort($ticketPropValueList);
+ $newTicketValue = array_diff($ticketPropValueList, $map);
+
+ foreach ($discountList as $discountId => $discount){
+ $ticketType = $discount['PAN_SETTINGS']['PRODUCT_TYPE'];
+ if($ticketType && $map[$ticketType]){
+ $newTicketValue[] = $map[$ticketType];
+ }
+ }
+ sort($newTicketValue);
+
+ if($newTicketValue != $ticketPropValueList) {
+ \CIBlockElement::SetPropertyValuesEx($elementId, $iBlockId, [
+ $ticketPropId => $newTicketValue?:false
+ ]);
+
+ if(discountApp()->config('UPDATE_ELEMENT_INDEX')){
+ \Bitrix\Iblock\PropertyIndex\Manager::updateElementIndex($iBlockId, $elementId);
+ }
+ }
+ return true;
+ }
+
+ public static function getElementList($lastId = 0)
+ {
+ $includeModule = Loader::includeModule('iblock');
+ if(!$includeModule) {
+ return [];
+ }
+
+ $iBlockId = discountApp()->config('CATALOG_IBLOCK_ID');
+
+ $rs = \CIBlockElement::GetList(
+ ['ID' => 'ASC'],
+ [
+ 'IBLOCK_ID' => $iBlockId,
+ '>ID' => $lastId,
+ 'ACTIVE' => 'Y',
+ 'GLOBAL_ACTIVE' => 'Y'
+ ],
+ false,
+ [
+ 'nTopCount' => discountApp()->config('PAGE_SIZE', 50),
+ ],
+ ['ID', 'IBLOCK_ID']
+ );
+ $elements = [];
+ while($ar = $rs->Fetch()){
+ $elements[$ar['ID']] = $ar['ID'];
+ }
+ return $elements;
+ }
+
+ /**
+ * Удаление записей которые ссылаются на отсутствующие элементы
+ *
+ * @return void
+ */
+ public static function removeOldTypeValuesFromCatalogDiscount()
+ {
+ $includeModule = Loader::includeModule('catalog')
+ && Loader::includeModule('iblock');
+ if(!$includeModule) {
+ return;
+ }
+
+ $typeIBlockId = discountApp()->config('DISCOUNT_TYPE_IBLOCK_ID');
+
+ $typeAFactory = app()->factory()->iBlock()->getEntityById($typeIBlockId);
+ $typeCollection = $typeAFactory->getElementFactory()->cache([])->getCollection([], [], ['ID', 'NAME']);
+
+
+ // \Bitrix\Sale\Internals\DiscountTable::getList()
+ $result = \CCatalogDiscount::GetList();
+ $list = [];
+ while($ar = $result->Fetch()){
+ /**
+ * Как пример не брать! Тут скидки дикие!
+ */
+ $list[$ar['ID']] = $ar;
+ }
+
+ $removeList = [];
+ foreach ($typeCollection as $entity){
+ $discountId = $entity->getName();
+ if(!$list[$discountId]){
+ /**
+ * remove iblock element - discount type
+ */
+ $removeList[] = $entity->getId();
+ $r = \CIBlockElement::Delete($entity->getId());
+ if(!$r){
+ global $APPLICATION;
+ pre("ERROR REMOVE {$entity->getId()}:" . $APPLICATION->LAST_ERROR?$APPLICATION->LAST_ERROR->GetString():'last_error_error');
+ }
+ }
+ }
+
+ $removeCount = count($removeList);
+ pre("remove {$removeCount} / {$typeCollection->count()} = " . ($typeCollection->count() - $removeCount));
+ pre('REMOVE ID LIST [TYPES]');
+ pre($removeList);
+
+ pre('DISCOUNT IDS ' . count($list));
+ pre(array_keys($list));
+ }
+}
\ No newline at end of file