- Added comprehensive AI Assistant system (aiassist/ directory): * Vector search and embedding capabilities * Typebot proxy integration * Elastic search functionality * Message classification and chat history * MCP proxy for external integrations - Implemented Court Status API (GetCourtStatus.php): * Real-time court document status checking * Integration with external court systems * Comprehensive error handling and logging - Enhanced S3 integration: * Improved file backup system with metadata * Batch processing capabilities * Enhanced error logging and recovery * Copy operations with URL fixing - Added Telegram contact creation API - Improved error logging across all modules - Enhanced callback system for AI responses - Extensive backup file storage with timestamps - Updated documentation and README files - File storage improvements: * Thousands of backup files with proper metadata * Fix operations for broken file references * Project-specific backup and recovery systems * Comprehensive file integrity checking Total: 26,461+ files added/modified including AWS SDK, vendor dependencies, and extensive backup system.
689 lines
27 KiB
PHP
689 lines
27 KiB
PHP
<?php
|
|
/*+**********************************************************************************
|
|
* The content of this file is subject to the ITS4GoogleCalendarSync license.
|
|
* ("License"); You may not use this file except in compliance with the License
|
|
* The Initial Developer of the Original Code is IT-Solutions4You s.r.o.
|
|
* Portions created by IT-Solutions4You s.r.o. are Copyright(C) IT-Solutions4You s.r.o.
|
|
* All Rights Reserved.
|
|
************************************************************************************/
|
|
|
|
vimport('~~/modules/WSAPP/synclib/connectors/TargetConnector.php');
|
|
|
|
class ITS4YouGoogleCalendarSync_Calendar_Connector extends WSAPP_TargetConnector
|
|
{
|
|
const maxBatchRequestCount = 50;
|
|
|
|
/**
|
|
* @var ITS4YouGoogleCalendarSync_Oauth2_Connector
|
|
*/
|
|
protected $apiConnection;
|
|
protected $totalRecords;
|
|
protected $maxResults = 100;
|
|
protected $createdRecords;
|
|
|
|
protected $client;
|
|
protected $service;
|
|
|
|
public function __construct($oauth2Connection)
|
|
{
|
|
$this->apiConnection = $oauth2Connection;
|
|
$this->client = new Google_Client();
|
|
$this->client->setClientId($oauth2Connection->getClientId());
|
|
$this->client->setClientSecret($oauth2Connection->getClientSecret());
|
|
$this->client->setRedirectUri($oauth2Connection->getRedirectUri());
|
|
$this->client->setScopes($oauth2Connection->getScope());
|
|
$this->client->setAccessType($oauth2Connection->getAccessType());
|
|
$this->client->setApprovalPrompt($oauth2Connection->getApprovalPrompt());
|
|
try {
|
|
$this->client->setAccesstoken($oauth2Connection->getAccessToken());
|
|
} catch (Exception $e) {
|
|
} //suppressing invalid access-token exception
|
|
$this->service = new Google_Service_Calendar($this->client);
|
|
}
|
|
|
|
public function getName()
|
|
{
|
|
return 'GoogleCalendar';
|
|
}
|
|
|
|
/**
|
|
* Tarsform Google Records to Vtiger Records
|
|
* @param array $targetRecords
|
|
* @return array tranformed Google Records
|
|
* @throws Exception
|
|
*/
|
|
public function transformToSourceRecord($targetRecords, $user = false)
|
|
{
|
|
$calendarArray = array();
|
|
$activityType = '';
|
|
|
|
/** @var ITS4YouGoogleCalendarSync_Calendar_Model $googleRecord */
|
|
foreach ($targetRecords as $googleRecord) {
|
|
$entity = array();
|
|
|
|
if ($googleRecord->getMode() == WSAPP_SyncRecordModel::WSAPP_DELETE_MODE) {
|
|
$googleRecord->summary = vtranslate('Deleted record');
|
|
$crmId = $this->getCalendarIdForDeleteRecord($googleRecord->get('entity')->id);
|
|
$userId = $this->getUserIdForDeleteRecord($crmId);
|
|
|
|
if ($userId) {
|
|
$user = Users_Record_Model::getInstanceById($userId, 'Users');
|
|
$activityType = ITS4YouGoogleCalendarSync_GoogleToVtigerMapping_Model::getActivityTypeByCalendarId($googleRecord->get('calendarId'), $user);
|
|
}
|
|
} else {
|
|
$activityType = ITS4YouGoogleCalendarSync_GoogleToVtigerMapping_Model::getActivityTypeByCalendarId($googleRecord->get('calendarId'), $user);
|
|
$function = 'getEntityDataFor' . explode('_', $activityType)[0];
|
|
|
|
if (method_exists($this, $function)) {
|
|
$entity = $this->$function($googleRecord, $activityType);
|
|
} else {
|
|
$entity = $this->getEntityDataForCalendar($googleRecord, $activityType);
|
|
}
|
|
}
|
|
|
|
$calendar = $this->getSynchronizeController()->getSourceRecordModel($entity);
|
|
$calendar = $this->performBasicTransformations($googleRecord, $calendar);
|
|
$calendar = $this->performBasicTransformationsToSourceRecords($calendar, $googleRecord);
|
|
|
|
if ($this->isSyncTypeAllowed($activityType)) {
|
|
$calendarArray[] = $calendar;
|
|
}
|
|
}
|
|
|
|
return $calendarArray;
|
|
}
|
|
|
|
/**
|
|
* @param ITS4YouGoogleCalendarSync_Calendar_Model $googleRecord
|
|
* @param string $activityType
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function getEntityDataForCalendar($googleRecord, $activityType)
|
|
{
|
|
$user = $this->apiConnection->getLoginUser();
|
|
$todayDatetime = new DateTime();
|
|
$activityType = str_replace('_', ' ', $activityType);
|
|
|
|
if ($googleRecord->isAllDay()) {
|
|
$activityType = 'Task';
|
|
}
|
|
|
|
$entityModule = ('Task' === $activityType) ? 'Calendar' : 'Events';
|
|
|
|
if (method_exists('Vtiger_Functions', 'getMandatoryReferenceFields')) {
|
|
$entity = Vtiger_Functions::getMandatoryReferenceFields($entityModule);
|
|
}
|
|
|
|
$entity['assigned_user_id'] = vtws_getWebserviceEntityId('Users', $user->getId());
|
|
$entity['subject'] = $googleRecord->getSubject();
|
|
$entity['location'] = $googleRecord->getWhere();
|
|
$datetime = new DateTime($googleRecord->getStartDate($user));
|
|
$entity['eventstatus'] = $datetime >= $todayDatetime ? 'Planned' : 'Held';
|
|
$entity['activitytype'] = $activityType;
|
|
$entity['description'] = $googleRecord->getDescription();
|
|
$entity['duration_hours'] = '00:00';
|
|
$entity['visibility'] = $googleRecord->getVisibility($user);
|
|
|
|
$entity['its4you_synchronization'] = true;
|
|
|
|
if (empty($entity['subject'])) {
|
|
$entity['subject'] = 'Google Event';
|
|
}
|
|
|
|
if ('Task' === $activityType) {
|
|
$entity['date_start'] = $googleRecord->getTaskStartDate();
|
|
$entity['due_date'] = $googleRecord->getTaskEndDate();
|
|
} else {
|
|
$entity['due_date'] = $googleRecord->getEndDate($user);
|
|
$entity['date_start'] = $googleRecord->getStartDate($user);
|
|
|
|
if (empty($entity['date_start'])) {
|
|
$entity['date_start'] = $entity['due_date'];
|
|
}
|
|
|
|
$entity['time_start'] = $googleRecord->getStartTimeUTC($user);
|
|
$entity['time_end'] = $googleRecord->getEndTimeUTC($user);
|
|
}
|
|
|
|
$attendees = $googleRecord->getAttendees();
|
|
$entity['contactidlist'] = '';
|
|
|
|
if (count($attendees)) {
|
|
$contactIds = $this->emailLookUp($attendees);
|
|
if (count($contactIds)) {
|
|
$entity['contactidlist'] = implode(';', array_unique($contactIds));
|
|
$_REQUEST['contactidlist'] = $entity['contactidlist'];
|
|
}
|
|
}
|
|
|
|
$googleRecord->summary = $entity['subject'];
|
|
$googleRecord->visibility = $entity['visibility'];
|
|
$googleRecord->start_date = trim($entity['date_start'] . ' ' . $entity['time_start']);
|
|
$googleRecord->end_date = trim($entity['due_date'] . ' ' . $entity['time_end']);
|
|
|
|
return $entity;
|
|
}
|
|
|
|
/**
|
|
* @param ITS4YouGoogleCalendarSync_Calendar_Model $googleRecord
|
|
* @param string $activityType
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function getEntityDataForITS4YouCalendar($googleRecord, $activityType)
|
|
{
|
|
$user = $this->apiConnection->getLoginUser();
|
|
$todayDatetime = new DateTime();
|
|
$datetime = new DateTime($googleRecord->getStartDate($user));
|
|
$entityModule = 'ITS4YouCalendar';
|
|
$activityType = str_replace(['ITS4YouCalendar_', '_'], ['', ' '], $activityType);
|
|
|
|
if (method_exists('Vtiger_Functions', 'getMandatoryReferenceFields')) {
|
|
$entity = Vtiger_Functions::getMandatoryReferenceFields($entityModule);
|
|
}
|
|
|
|
if ($googleRecord->isAllDay()) {
|
|
$entity['is_all_day'] = '1';
|
|
$entity['datetime_start'] = $googleRecord->getUserDateStart($user) . ' 00:00';
|
|
$entity['datetime_end'] = $googleRecord->getUserDateEnd($user) . ' 23:59';
|
|
} else {
|
|
$entity['datetime_start'] = $googleRecord->getUserDatetimeStart($user);
|
|
$entity['datetime_end'] = $googleRecord->getUserDatetimeEnd($user);
|
|
}
|
|
|
|
$entity['module'] = $entityModule;
|
|
$entity['assigned_user_id'] = vtws_getWebserviceEntityId('Users', $user->getId());
|
|
$entity['subject'] = $googleRecord->getSubject();
|
|
$entity['location'] = $googleRecord->getWhere();
|
|
$entity['calendar_status'] = $datetime >= $todayDatetime ? 'Planned' : 'Completed';
|
|
$entity['calendar_type'] = $activityType;
|
|
$entity['description'] = $googleRecord->getDescription();
|
|
$entity['its4you_synchronization'] = true;
|
|
$entity['visibility'] = $googleRecord->getVisibility($user);
|
|
|
|
if (empty($entity['subject'])) {
|
|
$entity['subject'] = 'Google Event';
|
|
}
|
|
|
|
$attendees = $googleRecord->getAttendees();
|
|
$entity['attendees_contact_id'] = '';
|
|
|
|
if (count($attendees)) {
|
|
$contactIds = $this->emailLookUp($attendees);
|
|
|
|
if (count($contactIds)) {
|
|
$entity['attendees_contact_id'] = implode(';', array_unique($contactIds));
|
|
}
|
|
}
|
|
|
|
$googleRecord->summary = $entity['subject'];
|
|
$googleRecord->visibility = $entity['visibility'];
|
|
$googleRecord->start_date = $entity['datetime_start'];
|
|
$googleRecord->end_date = $entity['datetime_end'];
|
|
|
|
return $entity;
|
|
}
|
|
|
|
/**
|
|
* @param array $emailIds
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function emailLookUp($emailIds)
|
|
{
|
|
$db = PearDatabase::getInstance();
|
|
$sql = 'SELECT crmid FROM vtiger_emailslookup WHERE setype = "Contacts" AND value IN (' . generateQuestionMarks($emailIds) . ')';
|
|
$result = $db->pquery($sql, $emailIds);
|
|
$crmIds = array();
|
|
for ($i = 0; $i < $db->num_rows($result); $i++) {
|
|
$crmIds[] = $db->query_result($result, $i, 'crmid');
|
|
}
|
|
return $crmIds;
|
|
}
|
|
|
|
/**
|
|
* Pull the events from google
|
|
* @param <object> $SyncState
|
|
* @return <array> google Records
|
|
*/
|
|
public function pull($SyncState, $user = false)
|
|
{
|
|
try {
|
|
return $this->getCalendar($SyncState, $user);
|
|
} catch (Exception $e) {
|
|
return array();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Pull the events from google
|
|
* @param <object> $SyncState
|
|
* @return <array> google Records
|
|
*/
|
|
public function getCalendar($SyncState, $user = false)
|
|
{
|
|
$sourceType = $this->getSynchronizeController()->getSourceType();
|
|
|
|
if ($this->apiConnection->isTokenExpired()) {
|
|
$this->apiConnection->refreshToken();
|
|
$this->client->setAccessToken($this->apiConnection->getAccessToken());
|
|
$this->service = new Google_Service_Calendar($this->client);
|
|
}
|
|
|
|
$datetime = date("Y-m-d H:i:s", strtotime("-1 months"));
|
|
|
|
$query = array(
|
|
'maxResults' => $this->maxResults,
|
|
'orderBy' => 'updated',
|
|
'singleEvents' => true,
|
|
'updatedMin' => $this->googleFormat($datetime),
|
|
//showDeleted
|
|
//showHiddenInvitations
|
|
);
|
|
|
|
if (ITS4YouGoogleCalendarSync_Utils_Helper::getSyncTime($sourceType, $user)) {
|
|
$query['updatedMin'] = $this->googleFormat(ITS4YouGoogleCalendarSync_Utils_Helper::getSyncTime($sourceType, $user));
|
|
//shows deleted by default
|
|
} else {
|
|
// v pripade, ze ide o prvu synchronizaciu, nesynchronizuj cancelled udalosti
|
|
$query['showDeleted'] = false;
|
|
}
|
|
|
|
$user = $this->apiConnection->getLoginUser();
|
|
$googleToVtigerMapping = ITS4YouGoogleCalendarSync_GoogleToVtigerMapping_Model::getInstance($user);
|
|
$calendarIds = $googleToVtigerMapping ? $googleToVtigerMapping->getUsedCalendarIds() : [];
|
|
|
|
$calendarRecords = [];
|
|
$googleRecords = [];
|
|
$feed = [];
|
|
|
|
foreach ($calendarIds as $calendarId) {
|
|
try {
|
|
$feed[$calendarId] = $this->service->events->listEvents($calendarId, $query);
|
|
} catch (Exception $e) {
|
|
if ($e->getCode() == 410) {
|
|
$query['showDeleted'] = false;
|
|
$feed[$calendarId] = $this->service->events->listEvents($calendarId, $query);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @var $calendarEvents Google_Service_Calendar_Events
|
|
*/
|
|
$calendarEvents = $feed[$calendarId];
|
|
|
|
if ($feed[$calendarId]) {
|
|
$calendarRecords = $calendarEvents->getItems();
|
|
|
|
if ($calendarEvents->getNextPageToken()) {
|
|
$this->totalRecords = $this->maxResults + 1;
|
|
}
|
|
}
|
|
|
|
if (count($calendarRecords) > 0) {
|
|
$maxModifiedTime = date('Y-m-d H:i:s', strtotime(ITS4YouGoogleCalendarSync_Calendar_Model::vtigerFormat(end($calendarRecords)->getUpdated())) + 1);
|
|
}
|
|
|
|
foreach ($calendarRecords as $i => $calendar) {
|
|
$recordModel = ITS4YouGoogleCalendarSync_Calendar_Model::getInstanceFromValues(array('entity' => $calendar));
|
|
$deleted = false;
|
|
|
|
if ($calendar->getStatus() == 'cancelled') {
|
|
$deleted = true;
|
|
}
|
|
|
|
if (!$deleted) {
|
|
$recordModel->setType($sourceType)->setMode(WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE);
|
|
} else {
|
|
$recordModel->setType($sourceType)->setMode(WSAPP_SyncRecordModel::WSAPP_DELETE_MODE);
|
|
}
|
|
|
|
$recordModel->set('calendarId', $calendarId);
|
|
$googleRecords[$calendar->getId()] = $recordModel;
|
|
}
|
|
|
|
$this->createdRecords = $this->createdRecords + count((array)$googleRecords[$calendarId]);
|
|
}
|
|
|
|
//odkomentovat nasledujuci if else kvoli updatovaniu poslednej synchronizacie
|
|
if (isset($maxModifiedTime)) {
|
|
ITS4YouGoogleCalendarSync_Utils_Helper::updateSyncTime($sourceType, $maxModifiedTime, $user);
|
|
} else {
|
|
ITS4YouGoogleCalendarSync_Utils_Helper::updateSyncTime($sourceType, false, $user);
|
|
}
|
|
|
|
return $googleRecords;
|
|
}
|
|
|
|
/**
|
|
* Function to convert datetime to RFC 3339 timestamp
|
|
* @param <String> $date
|
|
* @return <DateTime>
|
|
*/
|
|
function googleFormat($date)
|
|
{
|
|
$datTime = new DateTime($date);
|
|
$timeZone = new DateTimeZone('UTC');
|
|
$datTime->setTimezone($timeZone);
|
|
$googleFormat = $datTime->format('Y-m-d\TH:i:s\Z');
|
|
return $googleFormat;
|
|
}
|
|
|
|
/**
|
|
* @param string $date
|
|
* @param string $update
|
|
* @return false|string
|
|
* @throws Exception
|
|
*/
|
|
public function googleFormatUpdate($date, $update = '+1 day')
|
|
{
|
|
$dateTime = new DateTime($date);
|
|
|
|
return date('Y-m-d', strtotime($update, $dateTime->getTimestamp()));
|
|
}
|
|
|
|
/**
|
|
* Push the vtiger records to google
|
|
* @param <array> $records vtiger records to be pushed to google
|
|
* @return <array> pushed records
|
|
*/
|
|
public function push($records, $state = null)
|
|
{
|
|
//TODO : use batch requests
|
|
foreach ($records as $record) {
|
|
$entity = $record->get('entity');
|
|
if ($this->apiConnection->isTokenExpired()) {
|
|
$this->apiConnection->refreshToken();
|
|
$this->client->setAccessToken($this->apiConnection->getAccessToken());
|
|
$this->service = new Google_Service_Calendar($this->client);
|
|
}
|
|
$userInfo = explode("x", $record->getAssignedUserId());
|
|
$userId = $userInfo[1];
|
|
|
|
$activityType = $record->getActivityType();
|
|
$calendarId = ITS4YouGoogleCalendarSync_VtigerToGoogleMapping_Model::getCalendaridByActivityType($activityType, $userId);
|
|
|
|
if (!empty($calendarId)) {
|
|
try {
|
|
if ($record->getMode() == WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE) {
|
|
$newEntity = $this->service->events->update($calendarId, $entity->getId(), $entity);
|
|
$record->set('entity', $newEntity);
|
|
} elseif ($record->getMode() == WSAPP_SyncRecordModel::WSAPP_DELETE_MODE) {
|
|
$record->set('entity', $entity);
|
|
$this->service->events->delete($calendarId, $entity->getId());
|
|
} else {
|
|
$newEntity = $this->service->events->insert($calendarId, $entity);
|
|
$record->set('entity', $newEntity);
|
|
}
|
|
} catch (Exception $e) {
|
|
$record->set('error_trace', $e->getTraceAsString());
|
|
$record->set('error_message', $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
return $records;
|
|
}
|
|
|
|
/**
|
|
* Return crmid for deleted record from vtiger_wsapp_recordmapping table
|
|
* @param $clientId
|
|
* @return mixed
|
|
*/
|
|
private function getCalendarIdForDeleteRecord($clientId)
|
|
{
|
|
$adb = PearDatabase::getInstance();
|
|
|
|
$sql = "SELECT serverid FROM vtiger_wsapp_recordmapping WHERE clientid = ?";
|
|
$result = $adb->pquery($sql, array($clientId));
|
|
|
|
$serverid = $adb->query_result($result, 0, 'serverid');
|
|
|
|
$info = explode('x', $serverid);
|
|
return $info[1];
|
|
}
|
|
|
|
public function getUserIdForDeleteRecord($crmId)
|
|
{
|
|
$adb = PearDatabase::getInstance();
|
|
$result = $adb->pquery('SELECT smownerid FROM vtiger_crmentity WHERE crmid=?', [$crmId]);
|
|
$userId = intval($adb->query_result($result, 0, 'smownerid'));
|
|
|
|
return Vtiger_Functions::isUserExist($userId) ? $userId : null;
|
|
}
|
|
|
|
/**
|
|
* Tarsform Vtiger Records to Google Records
|
|
* @param <array> $vtEvents
|
|
* @return <array> tranformed vtiger Records
|
|
*/
|
|
public function transformToTargetRecord($vtEvents, $user = false)
|
|
{
|
|
$records = array();
|
|
|
|
foreach ($vtEvents as $vtEvent) {
|
|
$newEvent = new Google_Service_Calendar_Event();
|
|
$activityType = $this->getActivityType($vtEvent);
|
|
|
|
if ($vtEvent->getMode() == WSAPP_SyncRecordModel::WSAPP_DELETE_MODE) {
|
|
$crmId = $this->getCalendarIdForDeleteRecord($vtEvent->get('_id'));
|
|
$activityType = Vtiger_Cache::get('deleteInGoogle', $crmId);
|
|
$userId = Vtiger_Cache::get('deleteInGoogleUserId', $crmId);
|
|
$calendarId = ITS4YouGoogleCalendarSync_VtigerToGoogleMapping_Model::getCalendaridByActivityType($activityType, $userId);
|
|
$vtEvent->set('assigned_user_id', implode('x', [getTabid('Users'), $userId]));
|
|
|
|
try {
|
|
$newEvent = $this->service->events->get($calendarId, $vtEvent->get('_id'));
|
|
} catch (Exception $e) {
|
|
continue;
|
|
}
|
|
} elseif ($vtEvent->getMode() == WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE && $vtEvent->get('_id')) {
|
|
if ($this->apiConnection->isTokenExpired()) {
|
|
$this->apiConnection->refreshToken();
|
|
$this->client->setAccessToken($this->apiConnection->getAccessToken());
|
|
$this->service = new Google_Service_Calendar($this->client);
|
|
}
|
|
$userInfo = explode("x", $vtEvent->get('assigned_user_id'));
|
|
$userId = $userInfo[1];
|
|
|
|
$calendarId = ITS4YouGoogleCalendarSync_VtigerToGoogleMapping_Model::getCalendaridByActivityType($activityType, $userId);
|
|
|
|
try {
|
|
$newEvent = $this->service->events->get($calendarId, $vtEvent->get('_id'));
|
|
} catch (Exception $e) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$vtEvent->set('activity_type', $activityType);
|
|
$vtEvent->setType($this->getSynchronizeController()->getSourceType());
|
|
|
|
$function = 'retrieveDataFor' . $this->getActivityTypeModule($activityType);
|
|
|
|
if (method_exists($this, $function)) {
|
|
$this->$function($newEvent, $vtEvent);
|
|
} else {
|
|
$this->retrieveDataForCalendar($newEvent, $vtEvent);
|
|
}
|
|
|
|
$recordModel = ITS4YouGoogleCalendarSync_Calendar_Model::getInstanceFromValues(array('entity' => $newEvent));
|
|
$recordModel = $this->performBasicTransformations($vtEvent, $recordModel);
|
|
$recordModel = $this->performTransformationsToTargetRecords($recordModel, $vtEvent);
|
|
|
|
if ($this->isSyncTypeAllowed($activityType)) {
|
|
$records[] = $recordModel;
|
|
}
|
|
}
|
|
|
|
return $records;
|
|
}
|
|
|
|
public function getActivityTypeModule($activityType)
|
|
{
|
|
$module = explode('_', $activityType)[0];
|
|
|
|
if (ITS4YouGoogleCalendarSync_Utils_Helper::isModuleActive($module)) {
|
|
return $module;
|
|
}
|
|
|
|
return 'Task' === $activityType ? 'Calendar' : 'Events';
|
|
}
|
|
|
|
public function getActivityType($vtEvent)
|
|
{
|
|
if (!$vtEvent->isEmpty('calendar_type')) {
|
|
return 'ITS4YouCalendar_' . str_replace(' ', '_', $vtEvent->get('calendar_type'));
|
|
}
|
|
|
|
return $vtEvent->get('activitytype');
|
|
}
|
|
|
|
/**
|
|
* @param Google_Service_Calendar_Event $newEvent
|
|
* @param $vtEvent
|
|
* @throws Exception
|
|
*/
|
|
public function retrieveDataForITS4YouCalendar(&$newEvent, &$vtEvent)
|
|
{
|
|
$visibility = strtolower($vtEvent->get('calendar_visibility'));
|
|
|
|
$newEvent->setSummary($vtEvent->get('subject'));
|
|
$newEvent->setLocation($vtEvent->get('location'));
|
|
$newEvent->setDescription($vtEvent->get('description'));
|
|
$newEvent->setVisibility($visibility ? $visibility : 'private');
|
|
|
|
$start = new Google_Service_Calendar_EventDateTime();
|
|
$end = new Google_Service_Calendar_EventDateTime();
|
|
|
|
if (!$vtEvent->isEmpty('is_all_day')) {
|
|
$user = $this->getEventUser($vtEvent);
|
|
$startDate = $this->googleFormatAllDay($vtEvent->get('datetime_start'), $user);
|
|
$endDate = $this->googleFormatAllDay($vtEvent->get('datetime_end'), $user);
|
|
|
|
$start->setDate($startDate);
|
|
$end->setDate(date('Y-m-d', strtotime($endDate . ' + 1 day')));
|
|
} else {
|
|
$startDate = $this->googleFormat($vtEvent->get('datetime_start'));
|
|
$endDate = $this->googleFormat($vtEvent->get('datetime_end'));
|
|
|
|
$start->setDateTime($startDate);
|
|
$end->setDateTime($endDate);
|
|
}
|
|
|
|
$newEvent->setStart($start);
|
|
$newEvent->setEnd($end);
|
|
$newEvent->setAttendees(ITS4YouGoogleCalendarSync_Utils_Helper::getAttendeesForGoogle($vtEvent));
|
|
|
|
$vtEvent->summary = $newEvent->getSummary();
|
|
$vtEvent->visibility = $newEvent->getVisibility();
|
|
$vtEvent->start_date = $startDate;
|
|
$vtEvent->end_date = $endDate;
|
|
}
|
|
|
|
public function getEventUser($vtEvent)
|
|
{
|
|
list($tabId, $userId) = explode('x', $vtEvent->get('assigned_user_id'));
|
|
|
|
return Users_Record_Model::getInstanceById($userId, 'Users');
|
|
}
|
|
|
|
/**
|
|
* @param Google_Service_Calendar_Event $newEvent
|
|
* @param $vtEvent
|
|
* @throws Exception
|
|
*/
|
|
public function retrieveDataForCalendar(&$newEvent, &$vtEvent)
|
|
{
|
|
$visibility = strtolower($vtEvent->get('visibility'));
|
|
|
|
$newEvent->setSummary($vtEvent->get('subject'));
|
|
$newEvent->setLocation($vtEvent->get('location'));
|
|
$newEvent->setDescription($vtEvent->get('description'));
|
|
$newEvent->setVisibility($visibility ? $visibility : 'private');
|
|
|
|
$startDate = $vtEvent->get('date_start');
|
|
$startTime = $vtEvent->get('time_start');
|
|
$endDate = $vtEvent->get('due_date');
|
|
$endTime = !$vtEvent->isEmpty('time_end') ? $vtEvent->get('time_end') : $startTime;
|
|
|
|
$start = new Google_Service_Calendar_EventDateTime();
|
|
$end = new Google_Service_Calendar_EventDateTime();
|
|
|
|
if ('Task' === $vtEvent->get('activitytype')) {
|
|
$user = $this->getEventUser($vtEvent);
|
|
$startDate = $this->googleFormatAllDay($startDate . ' ' . $startTime, $user);
|
|
$endDate = $this->googleFormatAllDay($endDate . ' ' . $endTime, $user);
|
|
|
|
$start->setDate($startDate);
|
|
$end->setDate($endDate);
|
|
} else {
|
|
$startDate = $this->googleFormat($startDate . ' ' . $startTime);
|
|
$endDate = $this->googleFormat($endDate . ' ' . $endTime);
|
|
|
|
$start->setDateTime($startDate);
|
|
$end->setDateTime($endDate);
|
|
}
|
|
|
|
$newEvent->setStart($start);
|
|
$newEvent->setEnd($end);
|
|
$newEvent->setAttendees(ITS4YouGoogleCalendarSync_Utils_Helper::getAttendeesForGoogle($vtEvent));
|
|
|
|
$vtEvent->summary = $newEvent->getSummary();
|
|
$vtEvent->visibility = $newEvent->getVisibility();
|
|
$vtEvent->start_date = $startDate;
|
|
$vtEvent->end_date = $endDate;
|
|
}
|
|
|
|
/**
|
|
* @param string $activityType
|
|
* @return bool
|
|
*/
|
|
public function isSyncTypeAllowed($activityType)
|
|
{
|
|
$activityModule = $this->getActivityTypeModule($activityType);
|
|
$sourceType = vglobal('GOOGLE_SYNC_SOURCE_TYPE');
|
|
|
|
if (ITS4YouGoogleCalendarSync_Utils_Helper::isModuleActive($activityModule)) {
|
|
return $activityModule === $sourceType;
|
|
}
|
|
|
|
if ('Task' === $activityType) {
|
|
return 'Calendar' == $sourceType;
|
|
}
|
|
|
|
return 'Events' == $sourceType;
|
|
}
|
|
|
|
/**
|
|
* @param string $datetime
|
|
* @param null $user
|
|
* @return string
|
|
*/
|
|
public function googleFormatAllDay($datetime, $user = null)
|
|
{
|
|
$userDate = DateTimeField::convertTimeZone($datetime, DateTimeField::getDBTimeZone(), $user->get('time_zone'));
|
|
|
|
return $userDate->format('Y-m-d');
|
|
}
|
|
|
|
public function performTransformationsToTargetRecords(ITS4YouGoogleCalendarSync_Calendar_Model $sourceRecord, WSAPP_SyncRecordModel $targetRecord)
|
|
{
|
|
$sourceRecord->setId($targetRecord->get('_id'))
|
|
->setModifiedTime($targetRecord->get('_modifiedtime'));
|
|
|
|
$sourceRecord->setActivityType($targetRecord->get('activity_type'));
|
|
$sourceRecord->setAssignedUserId($targetRecord->get('assigned_user_id'));
|
|
|
|
return $sourceRecord;
|
|
}
|
|
|
|
/**
|
|
* returns if more records exits or not
|
|
* @return <boolean> true or false
|
|
*/
|
|
public function moreRecordsExits()
|
|
{
|
|
return ($this->totalRecords - $this->createdRecords > 0) ? true : false;
|
|
}
|
|
} |