fix
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount\Agents;
|
||||
|
||||
use Wt\Core\Agent\AAgentCase;
|
||||
|
||||
class SpiderAgentManager extends AAgentCase
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getAgentConfig()
|
||||
{
|
||||
return [
|
||||
'exec' => [
|
||||
'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);";
|
||||
}
|
||||
}
|
||||
+14
-1
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount;
|
||||
|
||||
use Wt\Core\Entity\AgentEntity;
|
||||
|
||||
class ElementSpider
|
||||
{
|
||||
protected $agent;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function next()
|
||||
{
|
||||
if(!$this->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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount\Handlers;
|
||||
|
||||
use Bitrix\Main\Entity\Event;
|
||||
use Bx\Discount\Tools;
|
||||
|
||||
class ADiscount
|
||||
{
|
||||
protected static array $lang = [
|
||||
'EMPTY_PRODUCT_TYPE' => 'Не выбран тип визуального представления скидки на сайте'
|
||||
];
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount\Handlers;
|
||||
|
||||
use Bx\Discount\Tools;
|
||||
|
||||
class CatalogDiscount extends ADiscount
|
||||
{
|
||||
|
||||
public static function onBeforeAdd(&$arFields)
|
||||
{
|
||||
if(!Tools::isEnable()){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if(!static::isValidData()){
|
||||
global $APPLICATION;
|
||||
$APPLICATION->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount\Handlers;
|
||||
|
||||
use Bitrix\Main\Entity\Event;
|
||||
use Bx\Discount\Tools;
|
||||
|
||||
class SaleDiscount extends ADiscount
|
||||
{
|
||||
|
||||
public static function onBeforeAdd(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();
|
||||
|
||||
$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();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount\Handlers;
|
||||
|
||||
use Bitrix\Main\Page\Asset;
|
||||
use Bitrix\Main\Page\AssetLocation;
|
||||
use Bx\Discount\Tools;
|
||||
use Wt\Core\Templater\Shell;
|
||||
|
||||
class SaleDiscountEditPage
|
||||
{
|
||||
public static function onPageStartHandler()
|
||||
{
|
||||
if(!Tools::isEnable()){
|
||||
return;
|
||||
}
|
||||
|
||||
$isSaleDiscountEditPage = Tools::isSaleDiscountEditPage();
|
||||
if(!$isSaleDiscountEditPage){
|
||||
return;
|
||||
}
|
||||
|
||||
$discountID = 0;
|
||||
$copy = false;
|
||||
if (isset($_REQUEST['ID']))
|
||||
{
|
||||
$discountID = (int)$_REQUEST['ID'];
|
||||
if ($discountID < 0)
|
||||
$discountID = 0;
|
||||
}
|
||||
if ($discountID > 0)
|
||||
{
|
||||
$copy = (isset($_REQUEST['action']) && (string)$_REQUEST['action'] == 'copy');
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount\Handlers;
|
||||
|
||||
class SpiderHandler
|
||||
{
|
||||
|
||||
public static function saleDiscountOnAfterAdd()
|
||||
{
|
||||
static::resetSpider();
|
||||
}
|
||||
|
||||
public static function saleDiscountOnAfterUpdate()
|
||||
{
|
||||
static::resetSpider();
|
||||
}
|
||||
|
||||
public static function saleDiscountOnAfterDelete()
|
||||
{
|
||||
static::resetSpider();
|
||||
}
|
||||
|
||||
public static function resetSpider()
|
||||
{
|
||||
discountApp()->service()->spider()->reset();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace Bx\Discount;
|
||||
|
||||
use Bitrix\Main\Loader;
|
||||
use CSite;
|
||||
|
||||
class Tools
|
||||
{
|
||||
public static function isSaleDiscountEditPage()
|
||||
{
|
||||
$isAdminSection = defined('ADMIN_SECTION') && ADMIN_SECTION;
|
||||
|
||||
if(!$isAdminSection) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return CSite::InDir('/bitrix/admin/sale_discount_edit.php')
|
||||
// || CSite::InDir('/bitrix/admin/sale_discount.php') // деактивация
|
||||
|| CSite::InDir('/bitrix/admin/cat_discount_edit.php')
|
||||
|| CSite::InDir('/bitrix/admin/cat_discount.php');
|
||||
}
|
||||
|
||||
public static function isValidRequestData()
|
||||
{
|
||||
return isset($_POST['PRODUCT_TYPE']);
|
||||
}
|
||||
|
||||
public static function isEnable()
|
||||
{
|
||||
if(defined('SALE_DISCOUNT_MIGRATE_MODE') && SALE_DISCOUNT_MIGRATE_MODE){
|
||||
return false;
|
||||
}
|
||||
return discountApp()->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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user