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/cvetdv.ru/bitrix/modules/sproduction.datasync/lib/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/cvetdv.ru/bitrix/modules/sproduction.datasync/lib/iblockprofilesbgrsync.php
<?php
/**
 *    Background Synchronization
 *
 * @mail support@s-production.online
 * @link s-production.online
 */

namespace SProduction\Datasync;

use Bitrix\Main,
    Bitrix\Main\DB\Exception,
    Bitrix\Main\Config\Option,
	Bitrix\Main\Localization\Loc;

class IblockProfilesBgrSync
{
	protected int $profile_id = 0;
	protected array $options;
	protected int $start_time;

	const STEP_TIME = 5;
	const STATE_STOP = 0;
	const STATE_PROCESS = 1;
	const STEP_SECT_SYNC = 0;
	const STEP_SECT_DEL = 1;
	const STEP_PROD_SYNC = 2;
	const STEP_PRICE_SYNC = 3;
	const STEP_PROD_DEL = 4;

	public function __construct($profile_id) {
		$this->setProfileId($profile_id);
		$this->loadProfileOptions();
	}

	/**
	 * Get profile_id
	 */
	public function getProfileId() {
		return $this->profile_id;
	}

	/**
	 * Set profile_id
	 */
	public function setProfileId($profile_id) {
		$this->profile_id = $profile_id;
	}

	/**
	 * Set profile options of synchronization
	 */
	public function loadProfileOptions() {
		$profile = SettingsIblocksProfiles::get($this->getProfileId());
		$this->options = $profile['sync'];
	}

	/**
	 * Get option of sync period
	 */
	public function getOptionPeriod() {
		return $this->options['man_sync_period'];
	}

	/**
	 * Get flag of only new export
	 */
	public function getOptionOnlynew() {
		return boolval($this->options['man_sync_syncprod_onlynew']);
	}

	/**
	 * Get flag of only new export
	 */
	public function getOptionSteps() {
		return $this->options['man_sync_steps'];
	}

    /**
     * Get state
     */
	public function getState() {
		$value = SettingsIblocksProfiles::getParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'state');
		return $value ? : self::STATE_STOP;
	}

	/**
	 * Set state
	 */
	public function setState($value) {
		SettingsIblocksProfiles::saveParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'state', $value);
	}

    /**
     * Get step
     */
	public function getStep() {
		$value = SettingsIblocksProfiles::getParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'step');
		return $value ? : self::STEP_SECT_SYNC;
	}

	/**
	 * Set step
	 */
	public function setStep($value) {
		SettingsIblocksProfiles::saveParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'step', $value);
	}

    /**
     * Get progress
     */
	public function getStepProgress() {
		$value = SettingsIblocksProfiles::getParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'progress');
		return $value ? : 0;
	}

	/**
	 * Set progress
	 */
	public function setStepProgress($value) {
		SettingsIblocksProfiles::saveParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'progress', $value);
	}

	/**
	 * Set progress
	 */
	public function incStepProgress($value) {
		$this->setStepProgress($this->getStepProgress() + $value);
	}

    /**
     * Get step count
     */
	public function getStepCount() {
		$value = SettingsIblocksProfiles::getParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'count');
		return $value;
	}

	/**
	 * Set step count
	 */
	public function setStepCount($value) {
		SettingsIblocksProfiles::saveParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'count', $value);
	}

    /**
     * Get step last id
     */
	public function getStepLastId() {
		$value = SettingsIblocksProfiles::getParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'last_id');
		return $value;
	}

	/**
	 * Set step last id
	 */
	public function setStepLastId($value) {
		SettingsIblocksProfiles::saveParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'last_id', $value);
	}

    /**
     * Get timestamp of last step
     */
	public function getStepTs() {
		$value = SettingsIblocksProfiles::getParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'ts');
		return $value;
	}

	/**
	 * Set timestamp of last step
	 */
	public function setStepTs($value) {
		SettingsIblocksProfiles::saveParam($this->getProfileId(), SettingsIblocksProfiles::BLOCK_SYNC_PROCESS, 'ts', $value);
	}

    /**
     * Get start time
     */
	public function getStartTime() {
		return $this->start_time;
	}

	/**
	 * Set start time
	 */
	public function setStartTime($value) {
		$this->start_time = $value;
	}

    /**
     * Get process hint
     */
	public function getProcessHint() {
		$lang_code = '';
		switch ($this->getStep()) {
			case self::STEP_SECT_SYNC:
				$lang_code = 'SP_DS_IBPROFILESBGRSYNC_HINT_SECT_SYNC';
				break;
			case self::STEP_SECT_DEL:
				$lang_code = 'SP_DS_IBPROFILESBGRSYNC_HINT_SECT_DEL';
				break;
			case self::STEP_PROD_SYNC:
				$lang_code = 'SP_DS_IBPROFILESBGRSYNC_HINT_PROD_SYNC';
				break;
			case self::STEP_PRICE_SYNC:
				$lang_code = 'SP_DS_IBPROFILESBGRSYNC_HINT_PRICE_SYNC';
				break;
			case self::STEP_PROD_DEL:
				$lang_code = 'SP_DS_IBPROFILESBGRSYNC_HINT_PROD_DEL';
				break;
		}
		$text = Loc::getMessage($lang_code, ['#PROGRESS#' => $this->getStepProgress(), '#COUNT#' => $this->getStepCount()]);
		return $text;
	}

	/**
	 * Start stop synchronization
	 */
	public function startSync($bgr_script) {
		FileLog::put('(BgrSync::startSync)');
		$this->setState(self::STATE_PROCESS);
		$this->setStep($this->getAllowStep(self::STEP_SECT_SYNC));
		$this->setStepProgress(0);
		$this->setStepCount(0);
		$this->setStepLastId(0);
		$this->setStepTs(time());
		if ($bgr_script) {
			Rest::sendBgrRequest($bgr_script, []);
		}
	}

	/**
	 * Start stop synchronization
	 */
	public function resumeSync($bgr_script) {
		FileLog::put('(BgrSync::resumeSync)');
		if ($bgr_script) {
			Rest::sendBgrRequest($bgr_script, []);
		}
	}

	/**
	 * Start stop synchronization
	 */
	public function stopSync() {
		FileLog::put('(BgrSync::stopSync)');
		$this->setState(self::STATE_STOP);
		$this->setStep(self::STEP_SECT_SYNC);
	}

	/**
	 * Run synchronization step
	 */
	public function runIteration($bgr_script) {
		FileLog::put('(BgrSync::runIteration) ' . $bgr_script);
		FileLog::put('(BgrSync::runIteration) step number ' . $this->getStep());
		$allow_steps = $this->getOptionSteps();
		// Process
		FileLog::put('(BgrSync::runIteration) start process');
		if ($allow_steps[$this->getStep()]) {
			if ($this->getStep() == self::STEP_SECT_SYNC) {
				$this->runSectSync();
			} elseif ($this->getStep() == self::STEP_SECT_DEL) {
				$this->runSectDel();
			} elseif ($this->getStep() == self::STEP_PROD_SYNC) {
				$this->runProdSync();
			} elseif ($this->getStep() == self::STEP_PRICE_SYNC) {
				$this->runPriceSync();
			} elseif ($this->getStep() == self::STEP_PROD_DEL) {
				$this->runProdDel();
			}
			FileLog::put('(BgrSync::runIteration) end process');
			FileLog::put('(BgrSync::runIteration) step count ' . $this->getStepCount());
			FileLog::put('(BgrSync::runIteration) step progress ' . $this->getStepProgress());
		}
		// If step progress ended
		if ($this->getStepProgress() >= $this->getStepCount()) {
			// Switch next step
			$next_step = $this->getAllowStep($this->getStep() + 1);
			if ($next_step === false) {
				$this->stopSync();
			}
			else {
				$this->setStep($next_step);
			}
			// Reset params
			$this->setStepProgress(0);
			$this->setStepCount(0);
			$this->setStepLastId(0);
			$this->setStepTs(time());
			sleep(1);
		}
		// Run next iteration
		if ($this->getState() == self::STATE_PROCESS) {
			if ($bgr_script) {
				FileLog::put('(BgrSync::runIteration) bgr request ' . $bgr_script);
				Rest::sendBgrRequest($bgr_script, []);
			}
		}
		else {
			$this->setStepProgress(0);
			$this->setStepCount(0);
		}
	}

	/**
	 * Get next allow step
	 */

	public function getAllowStep($step) {
		$allow_steps = $this->getOptionSteps();
		if ($step == self::STEP_SECT_SYNC && !$allow_steps[self::STEP_SECT_SYNC]) {
			$step = self::STEP_SECT_DEL;
		}
		if ($step == self::STEP_SECT_DEL && !$allow_steps[self::STEP_SECT_DEL]) {
			$step = self::STEP_PROD_SYNC;
		}
		if ($step == self::STEP_PROD_SYNC && !$allow_steps[self::STEP_PROD_SYNC]) {
			$step = self::STEP_PRICE_SYNC;
		}
		if ($step == self::STEP_PRICE_SYNC && !$allow_steps[self::STEP_PRICE_SYNC]) {
			$step = self::STEP_PROD_DEL;
		}
		if (($step == self::STEP_PROD_DEL && !$allow_steps[self::STEP_PROD_DEL]) || $step > self::STEP_PROD_DEL) {
			$step = false;
		}
		return $step;
	}

	/**
	 * Time control
	 */
	public function isRunoutTime() {
		$result    = false;
		$exec_time = time() - $this->getStartTime();
		$step_time = Settings::get(Settings::FLD_IBLOCKS_SYNC_STEP_TIME) ? : self::STEP_TIME;
		if ($step_time && $exec_time > $step_time) {
			$result = true;
		}
		return $result;
	}

	/**
	 * Run sections synchronization
	 */
	public function runSectSync() {
		FileLog::put('(IblockProfilesBgrSync::runSectSync)');
		// Get sections count
		$count = StoreSections::getCount($this->getProfileId());
		$this->setStepCount($count);
		// Get next sections from position
		$this->setStartTime(time());
		do {
			$id_list = [];
			$list = StoreSections::getList($this->getProfileId(), $this->getStepLastId(), ['ID'], 5);
			foreach ($list as $item) {
				$id_list[] = $item['ID'];
			}
			// Send section to CRM
			FileLog::put('(IblockProfilesBgrSync::runSectSync) ids ' . print_r($id_list, true));
			try {
				SyncPortal::syncSections($id_list);
			}
			catch (\Exception $e) {
				FileLog::put('(IblockProfilesBgrSync::runSectSync) error ' . $e->getMessage());
				// TODO: Save error to report
			}
			// Fix current position
			if (!empty($id_list)) {
				$this->setStepLastId($id_list[count($id_list)-1]);
				$this->setStepTs(time());
			}
			$this->incStepProgress(count($id_list));
		} while (!empty($id_list) && !$this->isRunoutTime());
	}

	/**
	 * Run sections clear
	 */
	public function runSectDel() {
		FileLog::put('(IblockProfilesBgrSync::runSectDel)');
		$crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($this->getProfileId());
		// Get sections count
		$count = PortalSections::getCount($crm_iblock_id);
		$this->setStepCount($count);
		// Get next sections from position
		$this->setStartTime(time());
		do {
			$id_list = [];
			$list = PortalSections::getList($crm_iblock_id, $this->getStepLastId(), [PortalSections::SEARCH_FIELD_DEFAULT], 10);
			foreach ($list as $item) {
				$search_value = $item[PortalSections::SEARCH_FIELD_DEFAULT];
				if ($search_value && !StoreSections::find($this->getProfileId(), $search_value)) {
					$id_list[] = $item['id'];
				}
			}
			// Delete sections from CRM
			FileLog::put('(IblockProfilesBgrSync::runSectDel) ids ' . print_r($id_list, true));
			foreach ($id_list as $id) {
				try {
					PortalSections::delete($id);
				}
				catch (\Exception $e) {
					FileLog::put('(IblockProfilesBgrSync::runSectDel) error ' . $e->getMessage());
					// TODO: Save error to report
				}
			}
			// Fix current position
			if (!empty($id_list)) {
				$this->setStepLastId($id_list[count($id_list)-1]);
				$this->setStepTs(time());
			}
			$this->incStepProgress(count($list));
		} while (!empty($id_list) && !$this->isRunoutTime());
	}

	/**
	 * Run products synchronization
	 */
	public function runProdSync() {
		FileLog::put('(IblockProfilesBgrSync::runProdSync)');
		// Get products count
		$count = StoreProducts::getCount($this->getProfileId());
		$this->setStepCount($count);
		// Get next sections from position
		$this->setStartTime(time());
		do {
			$id_list = [];
			$list = StoreProducts::getList($this->getProfileId(), $this->getStepLastId(), ['ID'], 5);
			foreach ($list as $item) {
				$id_list[] = $item['ID'];
			}
			// Send section to CRM
			FileLog::put('(IblockProfilesBgrSync::runProdSync) ids ' . print_r($id_list, true));
			try {
				SyncPortal::syncProducts($id_list, $this->getOptionOnlynew());
			}
			catch (\Exception $e) {
				FileLog::put('(IblockProfilesBgrSync::runProdSync) error ' . $e->getMessage());
				// TODO: Save error to report
			}
			// Fix current position
			if (!empty($id_list)) {
				$this->setStepLastId($id_list[count($id_list)-1]);
				$this->setStepTs(time());
			}
			$this->incStepProgress(count($id_list));
		} while (!empty($id_list) && !$this->isRunoutTime());
	}

	/**
	 * Run products synchronization
	 */
	public function runPriceSync() {
		FileLog::put('(IblockProfilesBgrSync::runPriceSync)');
		// Get products count
		$count = StoreProducts::getCount($this->getProfileId());
		$this->setStepCount($count);
		// Get next sections from position
		$this->setStartTime(time());
		do {
			$id_list = [];
			$list = StoreProducts::getList($this->getProfileId(), $this->getStepLastId(), ['ID'], 10);
			foreach ($list as $item) {
				$id_list[] = $item['ID'];
			}
			// Send section to CRM
			FileLog::put('(IblockProfilesBgrSync::runPriceSync) ids ' . print_r($id_list, true));
			try {
				SyncPortal::syncPrices($id_list);
			}
			catch (\Exception $e) {
				FileLog::put('(IblockProfilesBgrSync::runPriceSync) error ' . $e->getMessage());
				// TODO: Save error to report
			}
			// Fix current position
			if (!empty($id_list)) {
				$this->setStepLastId($id_list[count($id_list)-1]);
				$this->setStepTs(time());
			}
			$this->incStepProgress(count($id_list));
		} while (!empty($id_list) && !$this->isRunoutTime());
	}

	/**
	 * Run products clear
	 */
	public function runProdDel() {
		FileLog::put('(IblockProfilesBgrSync::runProdDel)');
		$crm_iblock_id = SettingsIblocksProfiles::getCrmIblockId($this->getProfileId());
		$crm_search_field = SettingsIblocksProfiles::getCrmSearchField($this->getProfileId());
		$store_search_field = SettingsIblocksProfiles::getStoreSearchField($this->getProfileId());
		// Get products count
		$count = PortalProducts::getCount($crm_iblock_id);
		$this->setStepCount($count);
		// Get next products from position
		$this->setStartTime(time());
		do {
			$id_list = [];
			$list = PortalProducts::getList($crm_iblock_id, $this->getStepLastId(), [$crm_search_field], 10);
			foreach ($list as $item) {
				$search_value = $item[$crm_search_field];
				if ($search_value && !StoreProducts::find($this->getProfileId(), $search_value, $store_search_field)) {
					$id_list[] = $item['id'];
				}
			}
			// Delete products from CRM
			FileLog::put('(IblockProfilesBgrSync::runProdDel) ids ' . print_r($id_list, true));
			foreach ($id_list as $id) {
				try {
					PortalProducts::delete($id);
				}
				catch (\Exception $e) {
					FileLog::put('(IblockProfilesBgrSync::runProdDel) error ' . $e->getMessage());
					// TODO: Save error to report
				}
			}
			// Fix current position
			if (!empty($id_list)) {
				$this->setStepLastId($id_list[count($id_list)-1]);
				$this->setStepTs(time());
			}
			$this->incStepProgress(count($list));
		} while (!empty($id_list) && !$this->isRunoutTime());
	}

}

Youez - 2016 - github.com/yon3zu
LinuXploit