2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2016-01-09 23:29:28 +02:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
use Google\Service\Calendar\Events;
|
|
|
|
|
2016-01-09 23:29:28 +02:00
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Google sync library.
|
2020-12-14 21:37:30 +03:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* Handles Google Calendar API related functionality.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
|
|
|
* @package Libraries
|
|
|
|
*/
|
2020-12-14 21:37:30 +03:00
|
|
|
class Google_sync {
|
2016-04-27 09:21:40 +03:00
|
|
|
/**
|
2020-11-16 11:29:36 +03:00
|
|
|
* @var EA_Controller
|
2016-04-27 09:21:40 +03:00
|
|
|
*/
|
2020-11-16 11:29:36 +03:00
|
|
|
protected $CI;
|
2016-04-27 09:21:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Google_Client
|
|
|
|
*/
|
|
|
|
protected $client;
|
|
|
|
|
|
|
|
/**
|
2020-05-12 21:47:03 +03:00
|
|
|
* @var Google_Service_Calendar
|
2016-04-27 09:21:40 +03:00
|
|
|
*/
|
|
|
|
protected $service;
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-28 15:00:10 +03:00
|
|
|
* Google_sync constructor.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* This method initializes the Google client class and the Calendar service class so that they can be used by the
|
|
|
|
* other methods.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2020-11-16 11:29:36 +03:00
|
|
|
$this->CI =& get_instance();
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
$this->client = new Google_Client();
|
2020-12-02 23:12:54 +03:00
|
|
|
$this->client->setApplicationName(config('google_application_name'));
|
|
|
|
$this->client->setClientId(config('google_client_id'));
|
|
|
|
$this->client->setClientSecret(config('google_client_secret'));
|
|
|
|
$this->client->setDeveloperKey(config('google_api_key'));
|
2016-03-07 21:59:04 +02:00
|
|
|
$this->client->setRedirectUri(site_url('google/oauth_callback'));
|
2020-11-30 22:34:12 +03:00
|
|
|
$this->client->setPrompt('consent');
|
2020-09-24 10:26:29 +03:00
|
|
|
$this->client->setAccessType('offline');
|
|
|
|
$this->client->addScope([
|
|
|
|
Google_Service_Calendar::CALENDAR,
|
|
|
|
Google_Service_Calendar::CALENDAR_READONLY
|
|
|
|
]);
|
2020-05-12 21:47:03 +03:00
|
|
|
$this->service = new Google_Service_Calendar($this->client);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Google OAuth authorization url.
|
|
|
|
*
|
|
|
|
* This url must be used to redirect the user to the Google user consent page,
|
|
|
|
* where the user grants access to his data for the Easy!Appointments app.
|
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function get_auth_url(): string
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:00:10 +03:00
|
|
|
// The "max_auth_age" is needed because the user needs to always log in and not use an existing session.
|
2017-09-15 14:36:37 +03:00
|
|
|
return $this->client->createAuthUrl() . '&max_auth_age=0';
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate the Google API usage.
|
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* When the user grants consent for his data usage, Google is going to redirect the browser back to the given
|
|
|
|
* redirect URL. There an authentication code is provided. Using this code, we can authenticate the API usage and
|
|
|
|
* store the token information to the database.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param string $code
|
2020-09-24 10:26:29 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function authenticate(string $code): array
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-09-24 10:26:29 +03:00
|
|
|
$response = $this->client->fetchAccessTokenWithAuthCode($code);
|
|
|
|
|
|
|
|
if (isset($response['error']))
|
|
|
|
{
|
|
|
|
throw new Exception('Google Authentication Error (' . $response['error'] . '): ' . $response['error_description']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the Google Client access token.
|
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* This method must be executed every time we need to make actions on a provider's Google Calendar account. A new
|
|
|
|
* token is necessary and the only way to get it is to use the stored refresh token that was provided when the
|
|
|
|
* provider granted consent to Easy!Appointments for use his Google Calendar account.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param string $refresh_token The provider's refresh token. This value is stored in the database and used every
|
|
|
|
* time we need to make actions to his Google Calendar account.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function refresh_token(string $refresh_token)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
$this->client->refreshToken($refresh_token);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an appointment record to its providers Google Calendar account.
|
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* This method checks whether the appointment's provider has enabled the Google Sync utility of Easy!Appointments
|
|
|
|
* and the stored access token is still valid. If yes, the selected appointment record is going to be added to the
|
|
|
|
* Google Calendar account.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param array $service Service data.
|
|
|
|
* @param array $customer Customer data.
|
|
|
|
* @param array $settings Required settings.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2020-05-12 21:47:03 +03:00
|
|
|
* @return Google_Service_Calendar_Event Returns the Google_Event class object.
|
2021-10-28 15:00:10 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function add_appointment(array $appointment, array $provider, array $service, array $customer, array $settings): Google_Service_Calendar_Event
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-05-12 21:47:03 +03:00
|
|
|
$event = new Google_Service_Calendar_Event();
|
2021-10-28 15:00:10 +03:00
|
|
|
$event->setSummary(! empty($service) ? $service['name'] : 'Unavailable');
|
2019-06-08 17:43:21 +03:00
|
|
|
$event->setDescription($appointment['notes']);
|
2021-10-28 15:00:10 +03:00
|
|
|
$event->setLocation($appointment['location'] ?? $settings['company_name']);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-12-09 15:07:14 +03:00
|
|
|
$timezone = new DateTimeZone($provider['timezone']);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$start = new Google_Service_Calendar_EventDateTime();
|
2021-10-28 15:00:10 +03:00
|
|
|
$start->setDateTime((new DateTime($appointment['start_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setStart($start);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$end = new Google_Service_Calendar_EventDateTime();
|
2021-10-28 15:00:10 +03:00
|
|
|
$end->setDateTime((new DateTime($appointment['end_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$event->attendees = [];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$event_provider = new Google_Service_Calendar_EventAttendee();
|
2016-01-09 23:29:28 +02:00
|
|
|
$event_provider->setDisplayName($provider['first_name'] . ' '
|
2017-09-15 14:36:37 +03:00
|
|
|
. $provider['last_name']);
|
2016-01-09 23:29:28 +02:00
|
|
|
$event_provider->setEmail($provider['email']);
|
|
|
|
$event->attendees[] = $event_provider;
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
if ( ! empty($customer))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-05-12 21:47:03 +03:00
|
|
|
$event_customer = new Google_Service_Calendar_EventAttendee();
|
2020-12-09 15:07:14 +03:00
|
|
|
$event_customer->setDisplayName($customer['first_name'] . ' ' . $customer['last_name']);
|
2016-01-09 23:29:28 +02:00
|
|
|
$event_customer->setEmail($customer['email']);
|
|
|
|
$event->attendees[] = $event_customer;
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
// Add the new event to the Google Calendar.
|
|
|
|
return $this->service->events->insert($provider['settings']['google_calendar'], $event);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing appointment that is already synced with Google Calendar.
|
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* This method updates the Google Calendar event item that is connected with the provided appointment record of
|
|
|
|
* Easy!Appointments.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $appointment Appointment data.
|
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param array $service Service data.
|
|
|
|
* @param array $customer Customer data.
|
|
|
|
* @parma array $settings Required settings.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2020-05-12 21:47:03 +03:00
|
|
|
* @return Google_Service_Calendar_Event Returns the Google_Service_Calendar_Event class object.
|
2021-10-28 15:00:10 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function update_appointment(array $appointment, array $provider, array $service, array $customer, array $settings): Google_Service_Calendar_Event
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
$event = $this->service->events->get($provider['settings']['google_calendar'],
|
|
|
|
$appointment['id_google_calendar']);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
$event->setSummary($service['name']);
|
2019-06-08 17:43:21 +03:00
|
|
|
$event->setDescription($appointment['notes']);
|
2021-10-28 15:00:10 +03:00
|
|
|
$event->setLocation($appointment['location'] ?? $settings['company_name']);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-12-09 15:07:14 +03:00
|
|
|
$timezone = new DateTimeZone($provider['timezone']);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$start = new Google_Service_Calendar_EventDateTime();
|
2021-10-28 15:00:10 +03:00
|
|
|
$start->setDateTime((new DateTime($appointment['start_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setStart($start);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$end = new Google_Service_Calendar_EventDateTime();
|
2021-10-28 15:00:10 +03:00
|
|
|
$end->setDateTime((new DateTime($appointment['end_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$event->attendees = [];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$event_provider = new Google_Service_Calendar_EventAttendee();
|
2020-12-09 15:07:14 +03:00
|
|
|
$event_provider->setDisplayName($provider['first_name'] . ' ' . $provider['last_name']);
|
2016-01-09 23:29:28 +02:00
|
|
|
$event_provider->setEmail($provider['email']);
|
|
|
|
$event->attendees[] = $event_provider;
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
if ( ! empty($customer))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-05-12 21:47:03 +03:00
|
|
|
$event_customer = new Google_Service_Calendar_EventAttendee();
|
2016-01-09 23:29:28 +02:00
|
|
|
$event_customer->setDisplayName($customer['first_name'] . ' '
|
2017-09-15 14:36:37 +03:00
|
|
|
. $customer['last_name']);
|
2016-01-09 23:29:28 +02:00
|
|
|
$event_customer->setEmail($customer['email']);
|
|
|
|
$event->attendees[] = $event_customer;
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
return $this->service->events->update($provider['settings']['google_calendar'], $event->getId(), $event);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing appointment from Google Calendar.
|
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param string $google_event_id The Google Calendar event ID to be removed.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function delete_appointment(array $provider, string $google_event_id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:00:10 +03:00
|
|
|
$this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Add unavailability period event to Google Calendar.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $provider Provider data.
|
2022-01-18 14:54:41 +03:00
|
|
|
* @param array $unavailability Unavailable data.
|
2021-10-28 15:00:10 +03:00
|
|
|
*
|
|
|
|
* @return Google_Service_Calendar_Event Returns the Google event.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @throws Exception
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2022-01-18 14:54:41 +03:00
|
|
|
public function add_unavailability(array $provider, array $unavailability): Google_Service_Calendar_Event
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-05-12 21:47:03 +03:00
|
|
|
$event = new Google_Service_Calendar_Event();
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setSummary('Unavailable');
|
2022-01-18 14:54:41 +03:00
|
|
|
$event->setDescription($unavailability['notes']);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-12-09 15:07:14 +03:00
|
|
|
$timezone = new DateTimeZone($provider['timezone']);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$start = new Google_Service_Calendar_EventDateTime();
|
2022-01-18 14:54:41 +03:00
|
|
|
$start->setDateTime((new DateTime($unavailability['start_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setStart($start);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$end = new Google_Service_Calendar_EventDateTime();
|
2022-01-18 14:54:41 +03:00
|
|
|
$end->setDateTime((new DateTime($unavailability['end_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
// Add the new event to the Google Calendar.
|
|
|
|
return $this->service->events->insert($provider['settings']['google_calendar'], $event);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Update Google Calendar unavailability period event.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $provider Provider data.
|
2022-01-18 14:54:41 +03:00
|
|
|
* @param array $unavailability Unavailability data.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2020-05-12 21:47:03 +03:00
|
|
|
* @return Google_Service_Calendar_Event Returns the Google_Service_Calendar_Event object.
|
2021-10-28 15:00:10 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2022-01-18 14:54:41 +03:00
|
|
|
public function update_unavailability(array $provider, array $unavailability): Google_Service_Calendar_Event
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2022-01-18 14:54:41 +03:00
|
|
|
$event = $this->service->events->get($provider['settings']['google_calendar'], $unavailability['id_google_calendar']);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2022-01-18 14:54:41 +03:00
|
|
|
$event->setDescription($unavailability['notes']);
|
2016-01-09 23:29:28 +02:00
|
|
|
|
2020-12-09 15:07:14 +03:00
|
|
|
$timezone = new DateTimeZone($provider['timezone']);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$start = new Google_Service_Calendar_EventDateTime();
|
2022-01-18 14:54:41 +03:00
|
|
|
$start->setDateTime((new DateTime($unavailability['start_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setStart($start);
|
|
|
|
|
2020-05-12 21:47:03 +03:00
|
|
|
$end = new Google_Service_Calendar_EventDateTime();
|
2022-01-18 14:54:41 +03:00
|
|
|
$end->setDateTime((new DateTime($unavailability['end_datetime'], $timezone))->format(DateTimeInterface::RFC3339));
|
2016-01-09 23:29:28 +02:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
2021-10-28 15:00:10 +03:00
|
|
|
return $this->service->events->update($provider['settings']['google_calendar'], $event->getId(), $event);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-18 14:54:41 +03:00
|
|
|
* Delete unavailability period event from Google Calendar.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param string $google_event_id Google Calendar event ID to be removed.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2022-01-18 14:54:41 +03:00
|
|
|
public function delete_unavailability(array $provider, string $google_event_id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:00:10 +03:00
|
|
|
$this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-28 15:00:10 +03:00
|
|
|
* Get a Google Calendar event.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param array $provider Provider data.
|
|
|
|
* @param string $google_event_id Google Calendar event ID.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @return Google_Service_Calendar_Event Returns the Google Calendar event.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function get_event(array $provider, string $google_event_id): Google_Service_Calendar_Event
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-01-09 23:29:28 +02:00
|
|
|
return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the events between the sync period.
|
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @param string $google_calendar The name of the Google Calendar to be used.
|
2020-04-22 22:48:56 +03:00
|
|
|
* @param string $start The start date of sync period.
|
|
|
|
* @param string $end The end date of sync period.
|
2016-04-27 09:21:40 +03:00
|
|
|
*
|
2021-10-28 15:00:10 +03:00
|
|
|
* @return Events Returns a collection of events.
|
2016-01-09 23:29:28 +02:00
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function get_sync_events(string $google_calendar, string $start, string $end): Events
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
$params = [
|
2021-10-28 15:00:10 +03:00
|
|
|
'timeMin' => date(DateTimeInterface::RFC3339, $start),
|
|
|
|
'timeMax' => date(DateTimeInterface::RFC3339, $end),
|
2020-04-22 22:48:56 +03:00
|
|
|
'singleEvents' => TRUE,
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-01-09 23:29:28 +02:00
|
|
|
|
|
|
|
return $this->service->events->listEvents($google_calendar, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-28 15:00:10 +03:00
|
|
|
* Return available Google Calendars for specific user.
|
2016-01-09 23:29:28 +02:00
|
|
|
*
|
|
|
|
* The given user's token must already exist in db in order to get access to his
|
|
|
|
* Google Calendar account.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the available calendars.
|
|
|
|
*/
|
2021-10-28 15:00:10 +03:00
|
|
|
public function get_google_calendars(): array
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-28 15:00:10 +03:00
|
|
|
$calendar_list = $this->service->calendarList->listCalendarList();
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$calendars = [];
|
2021-10-28 15:00:10 +03:00
|
|
|
|
|
|
|
foreach ($calendar_list->items as $google_calendar)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
if ($google_calendar->getAccessRole() === 'reader')
|
|
|
|
{
|
2017-03-10 11:23:50 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$calendars[] = [
|
2016-01-09 23:29:28 +02:00
|
|
|
'id' => $google_calendar->id,
|
|
|
|
'summary' => $google_calendar->summary
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
2021-10-28 15:00:10 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
return $calendars;
|
2016-01-09 23:29:28 +02:00
|
|
|
}
|
|
|
|
}
|