MaketRandevu/application/controllers/Google.php

321 lines
13 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
2015-10-11 23:21:45 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, 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
* ---------------------------------------------------------------------------- */
/**
* Google Controller
*
2015-12-30 13:02:14 +02:00
* This controller handles the Google Calendar synchronization operations.
*
2020-09-23 12:47:18 +03:00
* @property CI_Session $session
* @property CI_Loader $load
* @property CI_Input $input
* @property CI_Output $output
* @property CI_Config $config
* @property CI_Lang $lang
* @property CI_Cache $cache
* @property CI_DB_query_builder $db
* @property CI_Security $security
* @property Google_Sync $google_sync
* @property Ics_file $ics_file
* @property Appointments_model $appointments_model
* @property Providers_model $providers_model
* @property Services_model $services_model
* @property Customers_model $customers_model
* @property Settings_model $settings_model
2020-09-23 12:47:18 +03:00
* @property Timezones $timezones
* @property Roles_model $roles_model
* @property Secretaries_model $secretaries_model
* @property Admins_model $admins_model
* @property User_model $user_model
*
2015-10-11 23:21:45 +03:00
* @package Controllers
*/
class Google extends CI_Controller {
/**
* Class Constructor
*/
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
2015-10-11 23:21: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.
2015-10-11 23:21:45 +03:00
*
* @param int $provider_id The provider id, for whom the sync authorization is made.
*/
public function oauth($provider_id)
{
// Store the provider id for use on the callback function.
$this->session->set_userdata('oauth_provider_id', $provider_id);
2015-10-11 23:21:45 +03:00
// Redirect browser to google user content page.
$this->load->library('google_sync');
2015-10-11 23:21:45 +03:00
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.
2015-10-11 23:21:45 +03:00
*
* 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
* api console account, the "http://path-to-Easy!Appointments/google/oauth_callback" should be included in an allowed redirect URL.
2015-10-11 23:21:45 +03:00
*/
public function oauth_callback()
{
$code = $this->input->get('code');
if (empty($code))
{
$this->output->set_output('Code authorization failed.');
return;
}
2015-10-11 23:21:45 +03:00
$this->load->library('Google_sync');
$token = $this->google_sync->authenticate($code);
if (empty($token))
{
$this->output->set_output('Token authorization failed.');
return;
}
// Store the token into the database for future reference.
$oauth_provider_id = $this->session->userdata('oauth_provider_id');
if ($oauth_provider_id)
{
$this->load->model('providers_model');
$this->providers_model->set_setting('google_sync', TRUE, $oauth_provider_id);
$this->providers_model->set_setting('google_token', json_encode($token), $oauth_provider_id);
$this->providers_model->set_setting('google_calendar', 'primary', $oauth_provider_id);
2018-01-23 12:08:37 +03:00
}
else
{
$this->output->set_output('Sync provider id not specified.');
}
2015-10-11 23:21:45 +03:00
}
/**
* Complete synchronization of appointments between Google Calendar and Easy!Appointments.
*
* 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
*
* @param int $provider_id Provider record to be synced.
2015-10-11 23:21:45 +03:00
*/
public static function sync($provider_id = NULL)
{
try
{
$framework = get_instance();
2015-10-11 23:21:45 +03:00
// The user must be logged in.
$framework->load->library('session');
if ($framework->session->userdata('user_id') == FALSE && is_cli() === FALSE)
{
return;
}
2015-10-11 23:21:45 +03:00
if ($provider_id === NULL)
{
2015-10-11 23:21:45 +03:00
throw new Exception('Provider id not specified.');
}
$framework->load->model('appointments_model');
$framework->load->model('providers_model');
$framework->load->model('services_model');
$framework->load->model('customers_model');
$framework->load->model('settings_model');
2015-10-11 23:21:45 +03:00
$provider = $framework->providers_model->get_row($provider_id);
2015-10-11 23:21:45 +03:00
// Check whether the selected provider has google sync enabled.
$google_sync = $framework->providers_model->get_setting('google_sync', $provider['id']);
if ( ! $google_sync)
{
throw new Exception('The selected provider has not the google synchronization setting enabled.');
2015-10-11 23:21:45 +03:00
}
$google_token = json_decode($framework->providers_model->get_setting('google_token', $provider['id']));
$framework->load->library('google_sync');
$framework->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.
$sync_past_days = $framework->providers_model->get_setting('sync_past_days', $provider['id']);
$sync_future_days = $framework->providers_model->get_setting('sync_future_days', $provider['id']);
2015-10-11 23:21:45 +03:00
$start = strtotime('-' . $sync_past_days . ' days', strtotime(date('Y-m-d')));
$end = strtotime('+' . $sync_future_days . ' days', strtotime(date('Y-m-d')));
$where_clause = [
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),
'id_users_provider' => $provider['id']
];
2015-10-11 23:21:45 +03:00
$appointments = $framework->appointments_model->get_batch($where_clause);
2015-10-11 23:21:45 +03:00
$company_settings = [
'company_name' => $framework->settings_model->get_setting('company_name'),
'company_link' => $framework->settings_model->get_setting('company_link'),
'company_email' => $framework->settings_model->get_setting('company_email')
];
2015-10-11 23:21:45 +03:00
$provider_timezone = new DateTimeZone($provider['timezone']);
// Sync each appointment with Google Calendar by following the project's sync protocol (see documentation).
foreach ($appointments as $appointment)
{
if ($appointment['is_unavailable'] == FALSE)
{
$service = $framework->services_model->get_row($appointment['id_services']);
$customer = $framework->customers_model->get_row($appointment['id_users_customer']);
2018-01-23 12:08:37 +03:00
}
else
{
2015-10-11 23:21:45 +03:00
$service = NULL;
$customer = NULL;
}
// If current appointment not synced yet, add to Google Calendar.
if ($appointment['id_google_calendar'] == NULL)
{
$google_event = $framework->google_sync->add_appointment($appointment, $provider,
$service, $customer, $company_settings);
2015-10-11 23:21:45 +03:00
$appointment['id_google_calendar'] = $google_event->id;
$framework->appointments_model->add($appointment); // Save the Google Calendar ID.
2018-01-23 12:08:37 +03:00
}
else
{
2015-10-11 23:21:45 +03:00
// Appointment is synced with google calendar.
try
{
$google_event = $framework->google_sync->get_event($provider, $appointment['id_google_calendar']);
2015-10-11 23:21:45 +03:00
if ($google_event->status == 'cancelled')
{
2015-10-11 23:21:45 +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.
2015-10-11 23:21:45 +03:00
$is_different = FALSE;
$appt_start = strtotime($appointment['start_datetime']);
$appt_end = strtotime($appointment['end_datetime']);
$event_start = new DateTime($google_event->getStart()->getDateTime());
$event_start->setTimezone($provider_timezone);
$event_end = new DateTime($google_event->getEnd()->getDateTime());
$event_end->setTimezone($provider_timezone);
2015-10-11 23:21:45 +03:00
if ($appt_start != $event_start->getTimestamp() || $appt_end != $event_end->getTimestamp()
|| $appointment['notes'] !== $google_event->getDescription())
{
2015-10-11 23:21:45 +03:00
$is_different = TRUE;
}
if ($is_different)
{
$appointment['start_datetime'] = $event_start->format('Y-m-d H:i:s');
$appointment['end_datetime'] = $event_end->format('Y-m-d H:i:s');
$appointment['notes'] = $google_event->getDescription();
$framework->appointments_model->add($appointment);
2015-10-11 23:21:45 +03:00
}
2017-09-23 02:30:22 +03:00
}
catch (Exception $exception)
{
// Appointment not found on Google Calendar, delete from Easy!Appoinmtents.
$framework->appointments_model->delete($appointment['id']);
2015-10-11 23:21:45 +03:00
$appointment['id_google_calendar'] = NULL;
}
}
}
// 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'];
$google_events = $framework->google_sync->get_sync_events($google_calendar, $start, $end);
2015-10-11 23:21:45 +03:00
foreach ($google_events->getItems() as $google_event)
{
if ($google_event->getStatus() === 'cancelled')
{
continue;
}
if ($google_event->getStart() === NULL || $google_event->getEnd() === NULL)
{
continue;
}
$results = $framework->appointments_model->get_batch(['id_google_calendar' => $google_event->getId()]);
if ( ! empty($results))
{
continue;
2015-10-11 23:21:45 +03:00
}
$event_start = new DateTime($google_event->getStart()->getDateTime());
$event_start->setTimezone($provider_timezone);
$event_end = new DateTime($google_event->getEnd()->getDateTime());
$event_end->setTimezone($provider_timezone);
// Record doesn't exist in the Easy!Appointments, so add the event now.
$appointment = [
'start_datetime' => $event_start->format('Y-m-d H:i:s'),
'end_datetime' => $event_end->format('Y-m-d H:i:s'),
'is_unavailable' => TRUE,
'location' => $google_event->getLocation(),
'notes' => $google_event->getSummary() . ' ' . $google_event->getDescription(),
'id_users_provider' => $provider_id,
'id_google_calendar' => $google_event->getId(),
'id_users_customer' => NULL,
'id_services' => NULL,
];
$framework->appointments_model->add($appointment);
2015-10-11 23:21:45 +03:00
}
$response = AJAX_SUCCESS;
2017-09-23 02:30:22 +03:00
}
catch (Exception $exception)
{
$framework->output->set_status_header(500);
$response = [
'message' => $exception->getMessage(),
'trace' => config('debug') ? $exception->getTrace() : []
];
2015-10-11 23:21:45 +03:00
}
$framework->output
->set_content_type('application/json')
->set_output(json_encode($response));
2015-10-11 23:21:45 +03:00
}
2015-10-11 23:21:45 +03:00
}