Files
crm.clientright.ru/modules/ITS4YouGoogleCalendarSync/models/Calendar.php
Fedor ac7467f0b4 Major CRM updates: AI Assistant, Court Status API, S3 integration improvements, and extensive file storage system
- 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.
2025-10-16 11:17:21 +03:00

505 lines
14 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/models/SyncRecordModel.php');
class ITS4YouGoogleCalendarSync_Calendar_Model extends WSAPP_SyncRecordModel
{
protected $startUTC;
protected $endUTC;
protected $googleStartDate;
protected $googleEndDate;
protected $activityType;
protected $assigned_user_id;
public $summary;
public $visibility;
public $start_date;
public $end_date;
/**
* @param $recordValues array
* @return ITS4YouGoogleCalendarSync_Calendar_Model
*/
public static function getInstanceFromValues($recordValues)
{
$model = new ITS4YouGoogleCalendarSync_Calendar_Model($recordValues);
return $model;
}
/**
* return id of Google Record
* @return <string> id
*/
function getId()
{
return $this->data['entity']->getId();
}
/**
* return modified time of Google Record
* @return <date> modified time
*/
public function getModifiedTime()
{
return $this->vtigerFormat($this->data['entity']->getUpdated());
}
/**
* converts the Google Format date to
* @param <date> $date Google Date
* @return <date> Vtiger date Format
*/
public static function vtigerFormat($date)
{
$origDate = $date;
list($date, $timestring) = explode('T', $date);
list($time, $tz) = explode('.', $timestring);
// EDIT - if this is UTC lets change it to correct system time
if (substr($tz, -1) == 'Z') {
$date = new DateTime($origDate);
$timeZone = new DateTimeZone(date_default_timezone_get());
$date->setTimezone($timeZone);
$date = $date->format('Y-m-d H:i:s');
return $date;
}
return $date . " " . $time;
}
/**
* return Subject of Google Record
* @return <string> Subject
*/
function getSubject()
{
return $this->data['entity']->getSummary();
}
/**
* @return bool
*/
public function isAllDay()
{
$start = $this->getEntity()->getStart();
$end = $this->getEntity()->getEnd();
return ($start->getDate() && $end->getDate());
}
/**
* @return object
*/
public function getEntity()
{
return $this->data['entity'];
}
/**
* return Start time time in UTC of Google Record
* @return string <date> start time
*/
function getStartTimeUTC($user = false)
{
if (isset($this->startUTC)) {
return $this->startUTC;
}
if (!$user) {
$user = Users_Record_Model::getCurrentUserModel();
}
$when = $this->data['entity']->getStart();
if (empty($when)) {
$gStart = "00:00";
} else {
if ($when->getDateTime()) {
$gStart = $when->getDateTime();
} else {
$gStart = "00:00";
}
}
$start = new DateTime($gStart);
$timeZone = new DateTimeZone(date_default_timezone_get());
$start->setTimezone($timeZone);
$startUTC = $start->format('H:i:s');
$gDateTime = $when->getDateTime();
if ($startUTC == '00:00:00' && empty($gDateTime)) {
$userTimezone = $user->get('time_zone');
$startUTCObj = DateTimeField::convertTimeZone($startUTC, $userTimezone, DateTimeField::getDBTimeZone());
$startUTC = $startUTCObj->format('H:i:s');
}
$this->startUTC = $startUTC;
return $startUTC;
}
/**
* return End time in UTC of Google Record
* @return string <date> end time
* @throws Exception
*/
function getEndTimeUTC($user = false)
{
if (isset($this->endUTC)) {
return $this->endUTC;
}
if (!$user) {
$user = Users_Record_Model::getCurrentUserModel();
}
$when = $this->data['entity']->getEnd();
if (empty($when)) {
$gEnd = "00:00";
} else {
if ($when->getDateTime()) {
$gEnd = $when->getDateTime();
} else {
$gEnd = "00:00";
}
}
$end = new DateTime($gEnd);
$timeZone = new DateTimeZone(date_default_timezone_get());
$end->setTimezone($timeZone);
$endUTC = $end->format('H:i:s');
$gDateTime = $when->getDateTime();
if ($endUTC == '00:00:00' && empty($gDateTime)) {
$userTimezone = $user->get('time_zone');
$startUTCObj = DateTimeField::convertTimeZone($endUTC, $userTimezone, DateTimeField::getDBTimeZone());
$endUTC = $startUTCObj->format('H:i:s');
}
$this->endUTC = $endUTC;
return $endUTC;
}
/**
* @return string
* @throws Exception
*/
public function getTaskStartDate()
{
$startDate = $this->data['entity']->getStart();
$date = $this->getDateFromObject($startDate);
return $this->getUTCDate($date);
}
/**
* @param object $value
* @param string $date
* @return string
*/
public function getDateFromObject($value, $date = '')
{
if (!empty($value)) {
if ($value->getDateTime()) {
$date = $value->getDateTime();
} else {
if ($value->getDate()) {
$date = $value->getDate();
}
}
}
return $date;
}
/**
* @param string $date
* @param string $format
* @return string
* @throws Exception
*/
public function getUTCDate($date, $format = 'Y-m-d')
{
$timeZone = new DateTimeZone('UTC');
$start = new DateTime($date, $timeZone);
return $start->format($format);
}
/**
* @return string
* @throws Exception
*/
public function getTaskEndDate()
{
$startDate = $this->data['entity']->getEnd();
$date = $this->getDateFromObject($startDate);
return $this->getUTCDate($date);
}
/**
* @retrun Google_Service_Calendar_Event
*/
public function getDataEntity()
{
return $this->data['entity'];
}
/**
* @param Google_Service_Calendar_EventDateTime $when
* @return string
*/
public function getWhenDate($when)
{
if (!$when) {
return date('Y-m-d');
}
if ($when->getDateTime()) {
return $when->getDateTime();
}
if ($when->getDate()) {
return $when->getDate();
}
return date('Y-m-d');
}
/**
* return start date in UTC of Google Record
* @return string <date> start date
* @throws Exception
*/
function getStartDate($user = false)
{
if (isset($this->googleStartDate)) {
return $this->googleStartDate;
}
if (!$user) {
$user = Users_Record_Model::getCurrentUserModel();
}
$when = $this->getDataEntity()->getStart();
$gStart = $this->getWhenDate($when);
$timeZone = new DateTimeZone('UTC');
$start = new DateTime($gStart);
$start->setTimezone($timeZone);
$startDate = $start->format('Y-m-d');
if ($start->format('H:i:s') == '00:00:00' && empty($when->getDateTime())) {
$userTimezone = $user->get('time_zone');
$startUTCObj = DateTimeField::convertTimeZone($startDate, $userTimezone, DateTimeField::getDBTimeZone());
$startDate = $startUTCObj->format('Y-m-d');
}
$this->googleStartDate = $startDate;
return $startDate;
}
/**
* @param Google_Service_Calendar_EventDateTime $when
* @param Users_Record_Model $user
* @return string
* @throws Exception
*/
public function getUserDatetime($when, $user = null)
{
$whenDate = $this->getWhenDate($when);
if (!$user) {
$user = Users_Record_Model::getCurrentUserModel();
}
$timeZone = new DateTimeZone($user->get('time_zone'));
$date = new DateTime($whenDate);
$date->setTimezone($timeZone);
return $date->format('Y-m-d H:i:s');
}
/**
* @param Google_Service_Calendar_EventDateTime $when
* @param Users_Record_Model $user
* @return string
* @throws Exception
*/
public function getUserDate($when, $user = null)
{
$whenDate = $this->getWhenDate($when);
if (!$user) {
$user = Users_Record_Model::getCurrentUserModel();
}
$timeZone = new DateTimeZone($user->get('time_zone'));
$date = new DateTime($whenDate);
$date->setTimezone($timeZone);
return $date->format('Y-m-d');
}
public function getUserDateStart($user = null)
{
$when = $this->getDataEntity()->getStart();
return $this->getUserDate($when, $user);
}
public function getUserDateEnd($user = null)
{
$when = $this->getDataEntity()->getEnd();
$date = $this->getUserDate($when, $user);
return date('Y-m-d', strtotime('-1 day', strtotime($date)));
}
public function getUserDatetimeStart($user = null)
{
$when = $this->getDataEntity()->getStart();
return $this->getUserDatetime($when, $user);
}
public function getUserDatetimeEnd($user = null)
{
$when = $this->getDataEntity()->getEnd();
return $this->getUserDatetime($when, $user);
}
/**
* return End date in UTC of Google Record
* @return string <date> end date
* @throws Exception
*/
function getEndDate($user = false)
{
if (isset($this->googleEndDate)) {
return $this->googleEndDate;
}
if (!$user) {
$user = Users_Record_Model::getCurrentUserModel();
}
$when = $this->getDataEntity()->getEnd();
$gEnd = $this->getWhenDate($when);
$timeZone = new DateTimeZone('UTC');
$end = new DateTime($gEnd);
$end->setTimezone($timeZone);
$endDate = $end->format('Y-m-d');
if ($end->format('H:i:s') == '00:00:00' && empty($when->getDateTime())) {
$userTimezone = $user->get('time_zone');
$endUTCObj = DateTimeField::convertTimeZone($endDate, $userTimezone, DateTimeField::getDBTimeZone());
$endDate = $endUTCObj->format('Y-m-d');
}
$this->googleEndDate = $endDate;
return $endDate;
}
/**
* return tilte of Google Record
* @return <string> title
*/
function getTitle()
{
$title = $this->data['entity']->getSummary();
return empty($title) ? null : $title;
}
/**
* function to get Visibility of google calendar event
* @return <string> visibility of google event (Private or Public)
* @return <null> if google event visibility is default
*/
public function getVisibility($user)
{
$visibility = $this->data['entity']->getVisibility();
if (strpos($visibility, 'private') !== false) {
return 'Private';
} elseif (strpos($visibility, 'public') !== false) {
return 'Public';
} else {
$sharedType = $user->get('calendarsharedtype');
if ($sharedType == 'selectedusers' || $sharedType == 'public') {
return 'Public';
}
return 'Private';
}
}
/**
* return discription of Google Record
* @return <string> Discription
*/
function getDescription()
{
return $this->data['entity']->getDescription();
}
/**
* return location of Google Record
* @return <string> location
*/
function getWhere()
{
$where = $this->data['entity']->getLocation();
return $where;
}
/**
* @return mixed
*/
public function getAssignedUserId()
{
return $this->assigned_user_id;
}
/**
* @param mixed $assigned_user_id
*/
public function setAssignedUserId($assigned_user_id)
{
$this->assigned_user_id = $assigned_user_id;
}
/**
* @return mixed
*/
public function getActivityType()
{
return $this->activityType;
}
/**
* @param mixed $activityType
*/
public function setActivityType($activityType)
{
$this->activityType = $activityType;
}
/**
* return array Attendees of Google Record
*/
public function getAttendees()
{
$attendeeDetails = array();
$attendees = $this->data['entity']->getAttendees();
if (is_array($attendees)) {
foreach ($attendees as $attendee) {
$attendeeDetails[] = $attendee->getEmail();
}
}
return $attendeeDetails;
}
}