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/calendar/lib/sync/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/calendar/lib/sync/googleapibatch.php
<?php

namespace Bitrix\Calendar\Sync;

use Bitrix\Calendar\Sync\Google\Dictionary;
use Bitrix\Calendar\Util;
use Bitrix\Main\ORM\Query\Query;
use Bitrix\Main\Type;
use Bitrix\Calendar\Internals;
use Bitrix\Main\Web\HttpClient;
use CCalendarEvent;

/**
 * @deprecated
 */
class GoogleApiBatch
{
	protected const FINISH = true;
	protected const NEXT = false;

	/**
	 * @return GoogleApiBatch
	 */
	public static function createInstance(): GoogleApiBatch
	{
		return new self();
	}

	/**
	 * @throws \Bitrix\Main\ObjectException
	 */
	public function __construct()
	{
	}

	public function syncLocalEvents(array $events, int $userId, string $gApiCalendarId): array
	{
		return $this->syncEvents($events, $userId, $gApiCalendarId);
	}


	private function syncEvents($events, $userId, $gApiCalendarId): array
	{
		return $this->mergeLocalEventWithExternalData(
			$events, $this->getEventsExternalFields($userId, $events, $gApiCalendarId));
	}

	/**
	 * @return bool
	 * @throws \Bitrix\Main\ObjectException
	 */
	public function syncLocalInstances(array $instances, int $userId, string $gApiCalendarId): array
	{
		return $this->mergeLocalEventWithExternalData(
			$instances,
			$this->getInstancesExternalFields($userId, $instances, $gApiCalendarId)
		);
	}

	/**
	 * @param int $eventId
	 * @param string $exDates
	 * @return string
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectPropertyException
	 * @throws \Bitrix\Main\SystemException
	 */
	private function calculateExDate(int $eventId, string $exDates): string
	{
		return implode(';', array_diff(
			explode(';', $exDates), $this->getExDatesByInstances($eventId)
		));
	}

	/**
	 * @param $eventId
	 * @return array
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectPropertyException
	 * @throws \Bitrix\Main\SystemException
	 */
	private function getInstanceByRecurrenceId(int $eventId): array
	{
		$instancesDb = Internals\EventTable::getList([
			'filter' => Query::filter()->where('RECURRENCE_ID', $eventId),
		]);

		$instances =[];
		while ($instance = $instancesDb->fetch())
		{
			$instances[] = $instance;
		}

		return $instances;
	}


	/**
	 * @param array $events
	 * @return array
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectPropertyException
	 * @throws \Bitrix\Main\SystemException
	 */
	private function preparedEventToBatch(array $events): array
	{
		return $events;
	}

	/**
	 * @param $originalEventId
	 * @return array
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectPropertyException
	 * @throws \Bitrix\Main\SystemException
	 */
	private function getExDatesByInstances(int $originalEventId): array
	{
		$instances = $this->getInstanceByRecurrenceId($originalEventId);

		$excludeDates = [];
		foreach ($instances as $instance)
		{
			$excludeDates[] = \CCalendar::Date(\CCalendar::Timestamp($instance['DATE_FROM']), false);
		}

		return $excludeDates;
	}

	/**
	 * @param array $instances
	 * @return array
	 * @throws \Bitrix\Main\ObjectException
	 */
	private function prepareInstanceToBatch(array $instances): array
	{
		$batch = [];
		foreach ($instances as $instance)
		{
			$currentInstance = $instance;
			if (empty($instance['ORIGINAL_DATE_FROM']))
			{
				$instanceDate = \CCalendar::GetOriginalDate(
					$instance['PARENT_DATE_FROM'],
					$instance['DATE_FROM'],
					$instance['PARENT_TZ_FROM']
				);
			}
			else
			{
				$instanceDate = $instance['ORIGINAL_DATE_FROM'];
			}
			/** @var Type\DateTime $eventOriginalStart */
			$eventOriginalStart = Util::getDateObject($instanceDate, false, $instance['PARENT_TZ_FROM']);
			$currentInstance['ORIGINAL_DATE_FROM'] =
				$eventOriginalStart->format(Type\Date::convertFormatToPhp(FORMAT_DATETIME));
			$currentInstance['DAV_XML_ID'] = $instance['PARENT_DAV_XML_ID'];
			$currentInstance['gEventId'] = $instance['PARENT_G_EVENT_ID'] . '_'
				. $eventOriginalStart->setTimeZone(Util::prepareTimezone())->format('Ymd\THis\Z');
			$currentInstance['G_EVENT_ID'] = $currentInstance['gEventId'];
			$batch[] = $currentInstance;
		}

		return $batch;
	}

	/**
	 * @param array $events
	 * @param array $externalFields
	 * @return array
	 */
	private function mergeLocalEventWithExternalData(array $events, array $externalFields): array
	{
		$resultEvents = [];

		foreach ($events as $event)
		{
			if (isset($externalFields[$event['ID']]))
			{
				$event['SYNC_STATUS'] = Dictionary::SYNC_STATUS['success'];
				$resultEvents[] = array_merge($event, $externalFields[$event['ID']]);
			}
			else
			{
				\CCalendarEvent::updateSyncStatus($event['ID'], Dictionary::SYNC_STATUS['undefined']);
			}
		}

		return $resultEvents;
	}

	/**
	 * @param int $userId
	 * @param array $instances
	 * @param string $gApiCalendarId
	 * @return array
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectException
	 */
	private function getInstancesExternalFields(int $userId, array $instances, string $gApiCalendarId): array
	{
		return (new GoogleApiSync($userId))
			->saveBatchEvents($this->prepareInstanceToBatch($instances), $gApiCalendarId, ['method' => HttpClient::HTTP_PUT])
		;
	}

	/**
	 * @param int $userId
	 * @param array $events
	 * @param string $gApiCalendarId
	 * @return array
	 * @throws \Bitrix\Main\ArgumentException
	 * @throws \Bitrix\Main\ObjectPropertyException
	 * @throws \Bitrix\Main\SystemException
	 */
	private function getEventsExternalFields(int $userId, array $events, string $gApiCalendarId): array
	{
		return (new GoogleApiSync($userId))
			->saveBatchEvents($this->preparedEventToBatch($events), $gApiCalendarId, ['method' => HttpClient::HTTP_POST])
		;
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit