403Webshell
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/grain.iiko/lib/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/grain.iiko/lib/restaurants.php
<?php

namespace Grain\Iiko;

use \Bitrix\Main\Localization\Loc;	
use \Bitrix\Main\Config\Option;
use \Grain\Iiko\Rest;
use \Grain\Iiko\Tools;
use \Grain\Iiko\Modifier;
use \Bitrix\Main\Loader;
use \Bitrix\Iblock\PropertyTable;
use \Bitrix\Iblock\IblockTable;

Loc::loadMessages(__FILE__);

class Restaurants
{
	const basketPropPrefix = 'GRAIN.IIKO.R.';
	const moduleName = 'grain.iiko';
	
	public static function sync() 
	{
		if(
			Option::get('grain.iiko','sync_restaurants')!='Y'
			|| intval($restaurantsIblockId=Option::get('grain.iiko','restaurants_iblock_id'))<=0
			|| !Loader::includeModule('iblock')
			|| !static::checkAndCreateProperties($restaurantsIblockId)

		)
			return '\\'.__METHOD__.'();';

		$doNotChangeCode = Option::get(static::moduleName,'sync_restaurants_do_not_change_code')=='Y';

		$organizationsByAccounts = Rest::getOrganizations();

		foreach($organizationsByAccounts as $organizations)
		{
			if(!(is_array($organizations['result']) && count($organizations['result'])>0))
			{
				\CEventLog::Add(Array(
					"SEVERITY" => "CUSTOM",
					"AUDIT_TYPE_ID" => "GRAIN_IIKO_RESTAURANTS_SYNC",
					"MODULE_ID" => "grain.iiko",
					"ITEM_ID" => $organizations['account']['LOGIN'],
					"DESCRIPTION" => Loc::getMessage("GRAIN_IIKO_RESTAURANTS_SYNC_ERROR",array(
						'#APILOGIN#' => $organizations['account']['LOGIN'],
						'#ERROR#' => $organizations['error']?$organizations['error']:Loc::getMessage('GRAIN_IIKO_RESTAURANTS_SYNC_ERROR_NO_DATA'),
					)),
				));
				return '\\'.__METHOD__.'();';			
			}
			
			$stat = array(
				'modified' => 0,
				'added' => 0,
			);
					
			foreach($organizations['result'] as $organization)
			{
				if(!$xmlId=$organization['id'])
					continue;
						
				$rsElements = \CIBlockElement::GetList(
					array(),
					array("IBLOCK_ID"=>$restaurantsIblockId,"=XML_ID"=>$xmlId),
					false,
					array("nTopCount"=>1),
					array("ID","IBLOCK_ID","NAME","PROPERTY_*")
				);
			
				if($obElement=$rsElements->GetNextElement()) 
				{
					$arElement = $obElement->GetFields();
					$arElement['PROPERTIES'] = $obElement->GetProperties();
					$elementId = $arElement['ID'];
					if(Option::get(static::moduleName,'sync_restaurants_name')=='Y')
					{
						$el = new \CIBlockElement;
						$updateRestaurantFields = array(
							'NAME' => $organization['name'],
						);
						if(!$doNotChangeCode)
							$updateRestaurantFields['CODE'] = Tools::getCode($organization['name'],$restaurantsIblockId,'element',$arElement['ID']);
						if($el->Update($elementId,$updateRestaurantFields))
						{
							$event = new \Bitrix\Main\Event('grain.iiko', 'OnRestaurantUpdate', array(
								'ID' => $elementId,
								'ACCOUNT_ID' => $organizations['account']['ID'],
								'IBLOCK_ID' => $restaurantsIblockId,
								'FIELDS' => $updateRestaurantFields,
							));
							$event->send();
						}
					}
					$stat['modified']++;
				}
				else
				{
					$el = new \CIBlockElement;
					$newRestaurantFields = array(
						'ACTIVE' => Option::get(static::moduleName,'sync_restaurants_inactive')=='Y'?'N':'Y',
						'NAME' => $organization['name'],
						'XML_ID' => $organization['id'],
						'IBLOCK_ID' => $restaurantsIblockId,
						'CODE' => Tools::getCode($organization['name'],$restaurantsIblockId,'element'),
					);
					if($elementId=$el->Add($newRestaurantFields))
					{
						$stat['added']++;
						$event = new \Bitrix\Main\Event('grain.iiko', 'OnRestaurantAdd', array(
							'ID' => $elementId,
							'ACCOUNT_ID' => $organizations['account']['ID'],
							'IBLOCK_ID' => $restaurantsIblockId,
							'FIELDS' => $newRestaurantFields,
						));
						$event->send();
					}
					else
					{
						\CEventLog::Add(Array(
							"SEVERITY" => "CUSTOM",
							"AUDIT_TYPE_ID" => "GRAIN_IIKO_RESTAURANTS_SYNC",
							"MODULE_ID" => "grain.iiko",
							"ITEM_ID" => $organizations['account']['LOGIN'],
							"DESCRIPTION" => Loc::getMessage("GRAIN_IIKO_RESTAURANTS_SYNC_ERROR_ADD",array(
								'#RESTID#' => $organization['id'],
								'#ERROR#' => $el->LAST_ERROR,
							)),
						));
						continue;
					}
				}
				
				$propToValue = array(
					'address' => $organization['address'],
					'latitude' => $organization['latitude'],
					'longitude' => $organization['longitude'],
				);
				
				foreach($propToValue as $propCode=>$value)
					\CIBlockElement::SetPropertyValueCode($elementId, $propCode, ($arElement && $arElement['PROPERTIES'][$propCode]['PROPERTY_VALUE_ID'])?array($arElement['PROPERTIES'][$propCode]['PROPERTY_VALUE_ID']=>array('VALUE'=>$value)):$value);
					
			}
			
			\CEventLog::Add(Array(
				"SEVERITY" => "CUSTOM",
				"AUDIT_TYPE_ID" => "GRAIN_IIKO_RESTAURANTS_SYNC",
				"MODULE_ID" => "grain.iiko",
				"ITEM_ID" => $organizations['account']['LOGIN'],
				"DESCRIPTION" => Loc::getMessage("GRAIN_IIKO_RESTAURANTS_SYNC_SUCCESS",array(
					'#ADDED#' => $stat['added'],
					'#MODIFIED#' => $stat['modified'],
				)),
			));
		}

		return '\\'.__METHOD__.'();';
	}
	
	public static function checkAndCreateProperties($restaurantsIblockId)
	{
		$rsIblock = IblockTable::getList(array(
			'filter' => array(
				'=ID'=>$restaurantsIblockId,
				'ACTIVE'=>'Y',
			),
			'select' => array(
				'ID',
				'CODE',
			),
			'limit' => 1,
		));
		

		if(!($iblock=$rsIblock->fetch()))
			return false;
		
		$propertyList = array(
			'address' => PropertyTable::TYPE_STRING,
			'latitude' => PropertyTable::TYPE_STRING,
			'longitude' => PropertyTable::TYPE_STRING,
		);
		$sort = 100;
		foreach($propertyList as $propertyCode=>$propertyType)
		{
			$rsProperty = PropertyTable::getList(array(
				'filter' => array(
					'IBLOCK_ID'=>$restaurantsIblockId,
					'=CODE'=>$propertyCode,
				),
				'select' => array(
					'ID',
					'CODE',
				),
				'limit' => 1,
			));
			if(!$property=$rsProperty->fetch())
			{
				$newPropertyFields = array(
					'IBLOCK_ID' => $restaurantsIblockId,
					'CODE' => $propertyCode,
					'NAME' => Loc::getMessage('GRAIN_IIKO_RESTAURANTS_SYNC_PROP_'.strtoupper($propertyCode)),
					'PROPERTY_TYPE' => $propertyType,
					'ACTIVE' => 'Y',
					'SORT' => $sort,
				);
				PropertyTable::add($newPropertyFields);
			}			
			$sort+=10;
		}
		
		return true;
	}
	
	public static function getRestaurantByProductId($productId)
	{
		$productId = intval($productId);
		if(
			!Loader::includeModule('iblock')
		)
			return false;
		$resultProduct = \Bitrix\Iblock\ElementTable::getList(array(
			'filter' => array('=ID'=>$productId),
			'select' => array('IBLOCK_ID'),
			'limit' => 1,
		));
		if(!$product=$resultProduct->fetch())
			return false;
		return static::getRestaurantByProductIblockId($product['IBLOCK_ID']);
	}

	public static function getRestaurantByProductIblockId($productIblockId)
	{
		$productIblockId = intval($productIblockId);
		if(
			!Loader::includeModule('iblock')
			|| intval($restaurantsIblockId=Option::get(static::moduleName,'restaurants_iblock_id'))<=0
		)
			return false;
		$resultRestaurants = \Bitrix\Iblock\ElementTable::getList(array(
			'filter' => array('IBLOCK_ID'=>$restaurantsIblockId),
			'select' => array('ID','XML_ID','NAME')
		));
		while($restaurant=$resultRestaurants->fetch())
		{
			if(Option::get(static::moduleName,'dishes_iblock_id_'.$restaurant['ID'])==$productIblockId)
				return $restaurant;
		}
		return false;
	}

	public static function getRestaurantByOrder(\Bitrix\Sale\Order $order)
	{
		foreach($order->getBasket() as $item)
		{
			return static::getRestaurantByBasketItem($item);
		}
		return false;
	}


	public static function getRestaurantByBasketItem(\Bitrix\Sale\BasketItem $item)
	{
		$basketProps = Modifier::getBasketPropertyValues($item);
		if($basketProps[static::basketPropPrefix."ID"])
		{
			return array(
				'ID' => $basketProps[static::basketPropPrefix."ID"],
				'NAME' => $basketProps[static::basketPropPrefix."NAME"],
				'XML_ID' => $basketProps[static::basketPropPrefix."XML_ID"],
			);
		}
		return false;
	}

	public static function getRestaurantById($id)
	{
		$id = intval($id);
		static $restaurants = array();
		if(array_key_exists($id, $restaurants))
			return $restaurants[$id];
		if(
			!Loader::includeModule('iblock')
			|| intval($restaurantsIblockId=Option::get(static::moduleName,'restaurants_iblock_id'))<=0
		)
			return false;
		$resultRestaurant = \Bitrix\Iblock\ElementTable::getList(array(
			'filter' => array('=ID'=>$id,'IBLOCK_ID'=>$restaurantsIblockId),
			'select' => array('ID','XML_ID','NAME'),
			'limit' => 1,
		));
		if($restaurant=$resultRestaurant->fetch())
		{
			$restaurants[$id] = $restaurant;
			return $restaurant;
		}
		return false;
	}

	public static function getRestaurantByXmlId($xmlId)
	{
		$xmlId = (string)$xmlId;
		if(strlen($xmlId)<=0)
			return false;
		static $restaurants = array();
		if(array_key_exists($xmlId, $restaurants))
			return $restaurants[$xmlId];
		if(
			!Loader::includeModule('iblock')
			|| intval($restaurantsIblockId=Option::get(static::moduleName,'restaurants_iblock_id'))<=0
		)
			return false;
		$resultRestaurant = \Bitrix\Iblock\ElementTable::getList(array(
			'filter' => array('=XML_ID'=>$xmlId,'IBLOCK_ID'=>$restaurantsIblockId),
			'select' => array('ID','XML_ID','NAME'),
			'limit' => 1,
		));
		if($restaurant=$resultRestaurant->fetch())
		{
			$restaurants[$xmlId] = $restaurant;
			return $restaurant;
		}
		return false;
	}
	
	public static function getRestaurantList($filter=false)
	{
		$restaurants = array();
		if(
			!Loader::includeModule('iblock')
			|| intval($restaurantsIblockId=Option::get(static::moduleName,'restaurants_iblock_id'))<=0
		)
			return $restaurants;
		if(!is_array($filter))
			$filter = array();
		$filter['IBLOCK_ID'] = $restaurantsIblockId;
		$result = \Bitrix\Iblock\ElementTable::getList(array(
			'filter' => $filter,
			'select' => array('NAME','ID','XML_ID'),
		));
		while($arRow = $result->Fetch())
			$restaurants[] = $arRow;
		return $restaurants;
	}
	
	public static function resetAgent() 
	{
		\CAgent::RemoveAgent('\\Grain\\Iiko\\Restaurants::sync();', "grain.iiko");
		\CAgent::AddAgent('\\Grain\\Iiko\\Restaurants::sync();', "grain.iiko", "N", 86400);
	}
}

?>

Youez - 2016 - github.com/yon3zu
LinuXploit