Your IP : 3.134.247.163


Current Path : /home/bitrix/ext_www/ballu.in.ua/local/modules/rusklimat.exchange/classes/general/
Upload File :
Current File : /home/bitrix/ext_www/ballu.in.ua/local/modules/rusklimat.exchange/classes/general/main.php

<?php
/*
 * RusKlimat 
 * 
 * 
 * @author  Alex
 * @date   05/12/2017
*/
use Bitrix\Main\Diag;
use Bitrix\Main\Diag\Debug;

IncludeModuleLangFile(__FILE__);

class RusklimatMain
{
	protected $connection = array(); // соединение с шлюзом
	protected $errors = array(); // ошибки
	protected $result = array(); // итоговые данные
	
	public function __construct()
    {
		CModule::IncludeModule('iblock');
		try {
			$this->connection = new RusklimatConnect;
		} catch (Exception $e) {
			$this->setError($e->GetMessage());
		}
	}	
	
	/*
	 * Устанавливаем результирующие данные, если есть готовый массив
	 */
	function setResultArray($result)
	{
		if($result["ERROR"])
			foreach($result["ERROR"] as $error)
				$this->result["ERROR"][] = $error;
				
		if($result["SUCCESS"])
			foreach($result["SUCCESS"] as $success)
				$this->result["SUCCESS"][] = $success;
	}
	
	/*
	 * Устанавливаем результирующие данные по 1 строчке
	 */
	function setResult($result, $is_error = false)
	{
		if($is_error)
			$this->result["ERROR"][] = $result;
		else
			$this->result["SUCCESS"][] = $result;
	}
	
	/*
	 * Выводим результаты
	 */
	function showResult()
	{
		if(!empty($this->result))
		{
			// сначала выведим ошибки
			if(!empty($this->result["ERROR"]))
			{
				echo '<h2>Errors</h2>';
				foreach($this->result["ERROR"] as $error)
				{
					echo '<pre>'.print_r($error, true).'</pre>';
				}
			}
			
			// потом удачные результаты
			if(!empty($this->result["SUCCESS"]))
			{
				echo '<h2>Success</h2>';
				foreach($this->result["SUCCESS"] as $success){
					echo '<pre>'.print_r($success, true).'</pre>';
				}
			}
		} else {
			echo 'Empty Result';
		}
	}
	
	/*
	 * Показать время работы
	 */
	function showTimeline()
	{
		echo '<pre>'.print_r(Debug::getTimeLabels(), true).'</pre>';
	}	
	
	/*
	 * Устанавливаем ошибки
	 */
	function setError($error) 
	{
		$this->errors[] = $error;
	}
	
	/*
	 * показ ошибки и запись в лог
	 */
	public static function showAndWriteError ($data, $file = false)
	{
		// если указан путь для файла запишем в логи
		if($file)
			RusklimatMain::writeLog($file, "ERROR: ".$error);
		
		foreach($data as $error){
			// выведем ошибку
			echo '<pre>'.print_r("ERROR: ".$error, true).'</pre>';
		}
	}
	
	/*
	 * Запись логов в файл
	 */
	public static function writeLog ($file = "log.txt", $data)
	{
		$file_log = $_SERVER['DOCUMENT_ROOT'].'/upload/rusklimat.exchange/logs/'.$file;
		$current = file_get_contents($file_log);
		$current .= print_r(date("d-m-Y H:i:s"), true);
		$current .= print_r("\r\n", true);
		$current .= print_r($data, true);
		$current .= print_r("--------------------------------------------------", true);
		$current .= print_r("\r\n", true);
		file_put_contents($file_log, $current);
	}
	
	/*
	 * показ ошибки и запись в лог
	 */
	public function writeTimelineLog ($scriptName = "")
	{
		$text = GetMessage("RK_EXCHANGE_MAIN_FINISH").$scriptName." | ".date("d-m-Y H:i:s");
		$file_log = $_SERVER['DOCUMENT_ROOT'].'/upload/rusklimat.exchange/logs/timeline.txt';
		$current = file_get_contents($file_log);
		$current .= print_r(date("d-m-Y H:i:s"), true);
		$current .= print_r("\r\n", true);
		$current .= print_r($data, true);
		$current .= print_r("\r\n", true);
		$current .= print_r("--------------------------------------------------", true);
		$current .= print_r("\r\n", true);
		file_put_contents($file_log, $current);
	}
}