2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2015-10-11 23:21:45 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2015-10-11 23:21:45 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
2020-11-14 22:36:25 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2015-10-11 23:21:45 +03:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Google controller.
|
2015-10-11 23:21:45 +03:00
|
|
|
*
|
2021-10-28 15:01:17 +03:00
|
|
|
* Handles the Google Calendar synchronization related operations.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2015-10-11 23:21:45 +03:00
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Google extends EA_Controller
|
|
|
|
{
|
2020-12-05 12:55:09 +03:00
|
|
|
/**
|
|
|
|
* Google constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2022-01-17 21:44:44 +03:00
|
|
|
|
2020-12-05 12:55:09 +03:00
|
|
|
$this->load->library('google_sync');
|
2022-01-17 21:44:44 +03:00
|
|
|
|
2021-11-18 09:30:31 +03:00
|
|
|
$this->load->model('appointments_model');
|
2020-12-05 12:55:09 +03:00
|
|
|
$this->load->model('providers_model');
|
2021-11-18 09:30:31 +03:00
|
|
|
$this->load->model('roles_model');
|
2020-12-05 12:55:09 +03:00
|
|
|
}
|
|
|
|
|
2015-10-11 23:21:45 +03:00
|
|
|
/**
|
|
|
|
* Complete synchronization of appointments between Google Calendar and Easy!Appointments.
|
|
|
|
*
|
2017-09-06 16:22:11 +03:00
|
|
|
* This method will completely sync the appointments of a provider with his Google Calendar account. The sync period
|
|
|
|
* needs to be relatively small, because a lot of API calls might be necessary and this will lead to consuming the
|
|
|
|
* Google limit for the Calendar API usage.
|
2015-10-11 23:21:45 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public static function sync(string $provider_id = null): void
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-10-28 15:01:17 +03:00
|
|
|
/** @var EA_Controller $CI */
|
2020-12-02 23:10:11 +03:00
|
|
|
$CI = get_instance();
|
2020-09-23 12:24:20 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$CI->load->library('google_sync');
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2023-10-23 12:21:30 +03:00
|
|
|
// Load the libraries as this method is called statically from the CLI command
|
|
|
|
|
2020-12-02 23:10:11 +03:00
|
|
|
$CI->load->model('appointments_model');
|
2023-07-11 08:46:28 +03:00
|
|
|
$CI->load->model('unavailabilities_model');
|
2020-12-02 23:10:11 +03:00
|
|
|
$CI->load->model('providers_model');
|
|
|
|
$CI->load->model('services_model');
|
|
|
|
$CI->load->model('customers_model');
|
|
|
|
$CI->load->model('settings_model');
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$user_id && !is_cli()) {
|
2021-10-28 15:01:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$provider_id) {
|
2021-10-28 15:01:17 +03:00
|
|
|
throw new InvalidArgumentException('No provider ID provided.');
|
|
|
|
}
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$provider = $CI->providers_model->find($provider_id);
|
|
|
|
|
|
|
|
// Check whether the selected provider has the Google Sync enabled.
|
|
|
|
$google_sync = $CI->providers_model->get_setting($provider['id'], 'google_sync');
|
2017-09-06 16:22:11 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$google_sync) {
|
2022-06-20 12:25:57 +03:00
|
|
|
return; // The selected provider does not have the Google Syncing enabled.
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$google_token = json_decode($provider['settings']['google_token'], true);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2022-06-20 12:36:44 +03:00
|
|
|
$CI->google_sync->refresh_token($google_token['refresh_token']);
|
2015-10-11 23:21:45 +03:00
|
|
|
|
|
|
|
// Fetch provider's appointments that belong to the sync time period.
|
2021-10-28 15:01:17 +03:00
|
|
|
$sync_past_days = $CI->providers_model->get_setting($provider['id'], 'sync_past_days');
|
|
|
|
|
|
|
|
$sync_future_days = $CI->providers_model->get_setting($provider['id'], 'sync_future_days');
|
|
|
|
|
2015-10-11 23:21:45 +03:00
|
|
|
$start = strtotime('-' . $sync_past_days . ' days', strtotime(date('Y-m-d')));
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2015-10-11 23:21:45 +03:00
|
|
|
$end = strtotime('+' . $sync_future_days . ' days', strtotime(date('Y-m-d')));
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$where = [
|
2015-10-11 23:21:45 +03:00
|
|
|
'start_datetime >=' => date('Y-m-d H:i:s', $start),
|
|
|
|
'end_datetime <=' => date('Y-m-d H:i:s', $end),
|
2023-12-22 13:35:41 +03:00
|
|
|
'id_users_provider' => $provider['id'],
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$appointments = $CI->appointments_model->get($where);
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2023-10-23 12:34:39 +03:00
|
|
|
$unavailabilities = $CI->unavailabilities_model->get($where);
|
|
|
|
|
|
|
|
$local_events = [...$appointments, ...$unavailabilities];
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$settings = [
|
|
|
|
'company_name' => setting('company_name'),
|
|
|
|
'company_link' => setting('company_link'),
|
2023-12-22 13:35:41 +03:00
|
|
|
'company_email' => setting('company_email'),
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$provider_timezone = new DateTimeZone($provider['timezone']);
|
2020-03-29 17:29:07 +03:00
|
|
|
|
2017-09-06 16:22:11 +03:00
|
|
|
// Sync each appointment with Google Calendar by following the project's sync protocol (see documentation).
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($local_events as $local_event) {
|
|
|
|
if (!$local_event['is_unavailability']) {
|
2023-10-23 12:34:39 +03:00
|
|
|
$service = $CI->services_model->find($local_event['id_services']);
|
|
|
|
$customer = $CI->customers_model->find($local_event['id_users_customer']);
|
|
|
|
$events_model = $CI->appointments_model;
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
|
|
|
$service = null;
|
|
|
|
$customer = null;
|
2023-10-23 12:34:39 +03:00
|
|
|
$events_model = $CI->unavailabilities_model;
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// If current appointment not synced yet, add to Google Calendar.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$local_event['id_google_calendar']) {
|
|
|
|
$google_event = $CI->google_sync->add_appointment(
|
|
|
|
$local_event,
|
|
|
|
$provider,
|
|
|
|
$service,
|
|
|
|
$customer,
|
2023-12-22 13:35:41 +03:00
|
|
|
$settings,
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-23 12:34:39 +03:00
|
|
|
|
|
|
|
$local_event['id_google_calendar'] = $google_event->getId();
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2023-10-23 12:34:39 +03:00
|
|
|
$events_model->save($local_event); // Save the Google Calendar ID.
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2023-10-23 12:34:39 +03:00
|
|
|
continue;
|
2018-01-23 12:08:37 +03:00
|
|
|
}
|
2023-10-23 12:34:39 +03:00
|
|
|
|
|
|
|
// Appointment is synced with Google Calendar.
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2023-10-23 12:34:39 +03:00
|
|
|
$google_event = $CI->google_sync->get_event($provider, $local_event['id_google_calendar']);
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($google_event->getStatus() == 'cancelled') {
|
2023-10-23 12:34:39 +03:00
|
|
|
throw new Exception('Event is cancelled, remove the record from Easy!Appointments.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Google Calendar event is different from Easy!Appointments appointment then update Easy!Appointments record.
|
|
|
|
$local_event_start = strtotime($local_event['start_datetime']);
|
|
|
|
$local_event_end = strtotime($local_event['end_datetime']);
|
2023-11-29 12:24:09 +03:00
|
|
|
$google_event_start = new DateTime(
|
2023-12-22 13:35:41 +03:00
|
|
|
$google_event->getStart()->getDateTime() ?? $google_event->getEnd()->getDate(),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-23 12:34:39 +03:00
|
|
|
$google_event_start->setTimezone($provider_timezone);
|
2023-11-29 12:24:09 +03:00
|
|
|
$google_event_end = new DateTime(
|
2023-12-22 13:35:41 +03:00
|
|
|
$google_event->getEnd()->getDateTime() ?? $google_event->getEnd()->getDate(),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2023-10-23 12:34:39 +03:00
|
|
|
$google_event_end->setTimezone($provider_timezone);
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$google_event_notes = $local_event['is_unavailability']
|
|
|
|
? $google_event->getSummary() . ' ' . $google_event->getDescription()
|
|
|
|
: $google_event->getDescription();
|
2023-10-23 12:34:39 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$is_different =
|
|
|
|
$local_event_start !== $google_event_start->getTimestamp() ||
|
|
|
|
$local_event_end !== $google_event_end->getTimestamp() ||
|
|
|
|
$local_event['notes'] !== $google_event_notes;
|
2023-10-23 12:34:39 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($is_different) {
|
2023-10-23 12:34:39 +03:00
|
|
|
$local_event['start_datetime'] = $google_event_start->format('Y-m-d H:i:s');
|
|
|
|
$local_event['end_datetime'] = $google_event_end->format('Y-m-d H:i:s');
|
|
|
|
$local_event['notes'] = $google_event_notes;
|
|
|
|
$events_model->save($local_event);
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable) {
|
2023-10-23 12:34:39 +03:00
|
|
|
// Appointment not found on Google Calendar, delete from Easy!Appointments.
|
|
|
|
$events_model->delete($local_event['id']);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$local_event['id_google_calendar'] = null;
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Add Google Calendar events that do not exist in Easy!Appointments.
|
2015-10-11 23:21:45 +03:00
|
|
|
$google_calendar = $provider['settings']['google_calendar'];
|
2021-10-28 15:01:17 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-20 12:25:57 +03:00
|
|
|
$google_events = $CI->google_sync->get_sync_events($google_calendar, $start, $end);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
if ($e->getCode() === 404) {
|
2022-06-20 12:25:57 +03:00
|
|
|
log_message('error', 'Google - Remote Calendar not found for provider ID: ' . $provider_id);
|
|
|
|
|
|
|
|
return; // The remote calendar was not found.
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2022-06-20 12:25:57 +03:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 23:21:45 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($google_events->getItems() as $google_event) {
|
|
|
|
if ($google_event->getStatus() === 'cancelled') {
|
2020-03-29 17:29:07 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($google_event->getStart() === null || $google_event->getEnd() === null) {
|
2020-03-29 17:29:07 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($google_event->getStart()->getDateTime() === $google_event->getEnd()->getDateTime()) {
|
2022-06-20 12:25:57 +03:00
|
|
|
continue;
|
2020-12-10 12:31:59 +03:00
|
|
|
}
|
|
|
|
|
2023-10-23 12:34:39 +03:00
|
|
|
$google_event_start = new DateTime($google_event->getStart()->getDateTime());
|
|
|
|
$google_event_start->setTimezone($provider_timezone);
|
|
|
|
$google_event_end = new DateTime($google_event->getEnd()->getDateTime());
|
|
|
|
$google_event_end->setTimezone($provider_timezone);
|
|
|
|
|
|
|
|
$appointment_results = $CI->appointments_model->get(['id_google_calendar' => $google_event->getId()]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($appointment_results)) {
|
2023-10-23 12:34:39 +03:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-20 12:25:57 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$unavailability_results = $CI->unavailabilities_model->get([
|
2023-12-22 13:35:41 +03:00
|
|
|
'id_google_calendar' => $google_event->getId(),
|
2023-11-29 12:24:09 +03:00
|
|
|
]);
|
2020-03-10 22:39:53 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($unavailability_results)) {
|
2020-03-10 22:39:53 +03:00
|
|
|
continue;
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
2020-03-10 22:39:53 +03:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
// Record doesn't exist in the Easy!Appointments, so add the event now.
|
2023-10-23 12:34:39 +03:00
|
|
|
$local_event = [
|
|
|
|
'start_datetime' => $google_event_start->format('Y-m-d H:i:s'),
|
|
|
|
'end_datetime' => $google_event_end->format('Y-m-d H:i:s'),
|
2023-11-29 12:24:09 +03:00
|
|
|
'is_unavailability' => true,
|
2020-04-22 22:48:56 +03:00
|
|
|
'location' => $google_event->getLocation(),
|
|
|
|
'notes' => $google_event->getSummary() . ' ' . $google_event->getDescription(),
|
2020-03-10 22:39:53 +03:00
|
|
|
'id_users_provider' => $provider_id,
|
2020-04-22 22:48:56 +03:00
|
|
|
'id_google_calendar' => $google_event->getId(),
|
2023-11-29 12:24:09 +03:00
|
|
|
'id_users_customer' => null,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id_services' => null,
|
2020-03-10 22:39:53 +03:00
|
|
|
];
|
|
|
|
|
2023-10-23 12:34:39 +03:00
|
|
|
$CI->unavailabilities_model->save($local_event);
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-10-28 15:01:17 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
log_message(
|
|
|
|
'error',
|
2023-12-22 13:35:41 +03:00
|
|
|
'Google - Sync completed with an error (provider ID "' . $provider_id . '"): ' . $e->getMessage(),
|
2023-11-29 12:24:09 +03:00
|
|
|
);
|
2022-06-20 12:25:57 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
json_exception($e);
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-23 12:24:20 +03:00
|
|
|
|
2020-12-09 15:17:45 +03:00
|
|
|
/**
|
|
|
|
* Authorize Google Calendar API usage for a specific provider.
|
|
|
|
*
|
|
|
|
* Since it is required to follow the web application flow, in order to retrieve a refresh token from the Google API
|
|
|
|
* service, this method is going to authorize the given provider.
|
|
|
|
*
|
2022-06-19 16:40:32 +03:00
|
|
|
* @param string $provider_id The provider id, for whom the sync authorization is made.
|
2020-12-09 15:17:45 +03:00
|
|
|
*/
|
2023-10-23 12:34:39 +03:00
|
|
|
public function oauth(string $provider_id): void
|
2020-12-09 15:17:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->session->userdata('user_id')) {
|
2022-02-23 16:54:41 +03:00
|
|
|
show_error('Forbidden', 403);
|
|
|
|
}
|
2022-06-19 16:40:32 +03:00
|
|
|
|
2020-12-09 15:17:45 +03:00
|
|
|
// Store the provider id for use on the callback function.
|
2021-10-28 15:01:17 +03:00
|
|
|
session(['oauth_provider_id' => $provider_id]);
|
2020-12-09 15:17:45 +03:00
|
|
|
|
|
|
|
// Redirect browser to google user content page.
|
|
|
|
header('Location: ' . $this->google_sync->get_auth_url());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback method for the Google Calendar API authorization process.
|
|
|
|
*
|
|
|
|
* Once the user grants consent with his Google Calendar data usage, the Google OAuth service will redirect him back
|
|
|
|
* in this page. Here we are going to store the refresh token, because this is what will be used to generate access
|
|
|
|
* tokens in the future.
|
|
|
|
*
|
|
|
|
* IMPORTANT: Because it is necessary to authorize the application using the web server flow (see official
|
|
|
|
* documentation of OAuth), every Easy!Appointments installation should use its own calendar api key. So in every
|
2023-10-23 12:21:30 +03:00
|
|
|
* api console account, the "http://path-to-Easy!Appointments/google/oauth_callback" should be included in an
|
2023-03-13 11:06:18 +03:00
|
|
|
* allowed redirect URL.
|
2023-10-23 12:21:30 +03:00
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws Exception
|
2020-12-09 15:17:45 +03:00
|
|
|
*/
|
2023-10-23 12:34:39 +03:00
|
|
|
public function oauth_callback(): void
|
2020-12-09 15:17:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!session('user_id')) {
|
2022-03-25 12:53:46 +03:00
|
|
|
abort(403, 'Forbidden');
|
2022-02-23 16:54:41 +03:00
|
|
|
}
|
2022-06-19 16:40:32 +03:00
|
|
|
|
2021-10-28 15:01:17 +03:00
|
|
|
$code = request('code');
|
2020-12-09 15:17:45 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($code)) {
|
2021-10-28 15:01:17 +03:00
|
|
|
response('Code authorization failed.');
|
|
|
|
|
2020-12-09 15:17:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->google_sync->authenticate($code);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($token)) {
|
2021-10-28 15:01:17 +03:00
|
|
|
response('Token authorization failed.');
|
|
|
|
|
2020-12-09 15:17:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the token into the database for future reference.
|
2021-10-28 15:01:17 +03:00
|
|
|
$oauth_provider_id = session('oauth_provider_id');
|
2020-12-09 15:17:45 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($oauth_provider_id) {
|
|
|
|
$this->providers_model->set_setting($oauth_provider_id, 'google_sync', true);
|
2021-10-28 15:01:17 +03:00
|
|
|
$this->providers_model->set_setting($oauth_provider_id, 'google_token', json_encode($token));
|
|
|
|
$this->providers_model->set_setting($oauth_provider_id, 'google_calendar', 'primary');
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2021-10-28 15:01:17 +03:00
|
|
|
response('Sync provider id not specified.');
|
2020-12-09 15:17:45 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-18 09:30:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will return a list of the available Google Calendars.
|
|
|
|
*
|
|
|
|
* The user will need to select a specific calendar from this list to sync his appointments with. Google access must
|
|
|
|
* be already granted for the specific provider.
|
|
|
|
*/
|
2023-10-23 12:34:39 +03:00
|
|
|
public function get_google_calendars(): void
|
2021-11-18 09:30:31 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
$provider_id = (int) request('provider_id');
|
2022-01-17 21:44:44 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($provider_id)) {
|
2021-11-18 09:30:31 +03:00
|
|
|
throw new Exception('Provider id is required in order to fetch the google calendars.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if selected provider has sync enabled.
|
|
|
|
$google_sync = $this->providers_model->get_setting($provider_id, 'google_sync');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$google_sync) {
|
2021-11-18 09:30:31 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => false,
|
2021-11-18 09:30:31 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$google_token = json_decode($this->providers_model->get_setting($provider_id, 'google_token'), true);
|
2021-11-18 09:30:31 +03:00
|
|
|
|
2022-06-20 12:36:44 +03:00
|
|
|
$this->google_sync->refresh_token($google_token['refresh_token']);
|
2021-11-18 09:30:31 +03:00
|
|
|
|
|
|
|
$calendars = $this->google_sync->get_google_calendars();
|
|
|
|
|
|
|
|
json_response($calendars);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:31 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a specific google calendar for a provider.
|
|
|
|
*
|
|
|
|
* All the appointments will be synced with this particular calendar.
|
|
|
|
*/
|
2023-10-23 12:34:39 +03:00
|
|
|
public function select_google_calendar(): void
|
2021-11-18 09:30:31 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-18 09:30:31 +03:00
|
|
|
$provider_id = request('provider_id');
|
|
|
|
|
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (cannot('edit', PRIV_USERS) && (int) $user_id !== (int) $provider_id) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$calendar_id = request('calendar_id');
|
|
|
|
|
|
|
|
$this->providers_model->set_setting($provider_id, 'google_calendar', $calendar_id);
|
|
|
|
|
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:31 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:31 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable a providers sync setting.
|
|
|
|
*
|
|
|
|
* This method deletes the "google_sync" and "google_token" settings from the database.
|
|
|
|
*
|
|
|
|
* After that the provider's appointments will be no longer synced with Google Calendar.
|
|
|
|
*/
|
2023-10-23 12:34:39 +03:00
|
|
|
public function disable_provider_sync(): void
|
2021-11-18 09:30:31 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2021-11-18 09:30:31 +03:00
|
|
|
$provider_id = request('provider_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$provider_id) {
|
2021-11-18 09:30:31 +03:00
|
|
|
throw new Exception('Provider id not specified.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (cannot('edit', PRIV_USERS) && (int) $user_id !== (int) $provider_id) {
|
2022-06-20 12:31:11 +03:00
|
|
|
throw new RuntimeException('You do not have the required permissions for this task.');
|
2021-11-18 09:30:31 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->providers_model->set_setting($provider_id, 'google_sync', false);
|
2021-11-18 09:30:31 +03:00
|
|
|
|
2023-03-13 11:06:18 +03:00
|
|
|
$this->providers_model->set_setting($provider_id, 'google_token');
|
2021-11-18 09:30:31 +03:00
|
|
|
|
|
|
|
$this->appointments_model->clear_google_sync_ids($provider_id);
|
|
|
|
|
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 09:30:31 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 09:30:31 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 23:21:45 +03:00
|
|
|
}
|