2013-05-04 00:26:04 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
|
|
|
// Google API PHP Client is necessary to perform sync operations.
|
2015-04-09 22:47:00 +03:00
|
|
|
require_once __DIR__ . '/external/google-api-php-client/Google_Client.php';
|
|
|
|
require_once __DIR__ . '/external/google-api-php-client/contrib/Google_CalendarService.php';
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-06-10 18:51:23 +03:00
|
|
|
/**
|
|
|
|
* Google Synchronization Class
|
|
|
|
*
|
|
|
|
* This class implements all the core synchronization between the Google Calendar
|
2013-12-25 03:22:37 +02:00
|
|
|
* and the Easy!Appointments system. Do not place any model handling inside this
|
|
|
|
* library.
|
2015-07-08 01:40:04 +03:00
|
|
|
*
|
|
|
|
* @package Libraries
|
2013-06-10 18:51:23 +03:00
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
class Google_Sync {
|
2013-12-26 02:57:59 +02:00
|
|
|
private $CI;
|
2013-05-04 00:26:04 +03:00
|
|
|
private $client;
|
|
|
|
private $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*
|
2013-06-10 18:51:23 +03:00
|
|
|
* This method initializes the Google client class and the Calendar service
|
|
|
|
* class so that they can be used by the other methods.
|
2013-05-04 00:26:04 +03:00
|
|
|
*/
|
2013-06-19 22:29:00 +03:00
|
|
|
public function __construct() {
|
|
|
|
$this->CI =& get_instance();
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-06-19 22:29:00 +03:00
|
|
|
if (!isset($_SESSION)) {
|
|
|
|
@session_start();
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
// Initialize google client and calendar service.
|
|
|
|
$this->client = new Google_Client();
|
2013-06-24 12:52:23 +03:00
|
|
|
$this->client->setUseObjects(true);
|
|
|
|
|
2015-04-13 21:01:00 +03:00
|
|
|
$this->client->setApplicationName(Config::GOOGLE_PRODUCT_NAME);
|
|
|
|
$this->client->setClientId(Config::GOOGLE_CLIENT_ID);
|
|
|
|
$this->client->setClientSecret(Config::GOOGLE_CLIENT_SECRET);
|
|
|
|
$this->client->setDeveloperKey(Config::GOOGLE_API_KEY);
|
2015-05-21 00:01:21 +03:00
|
|
|
$this->client->setRedirectUri($this->CI->config->item('base_url') . '/index.php/google/oauth_callback');
|
2013-06-24 12:52:23 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
$this->service = new Google_CalendarService($this->client);
|
|
|
|
}
|
|
|
|
|
2013-06-19 22:29:00 +03: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.
|
|
|
|
*/
|
|
|
|
public function get_auth_url() {
|
|
|
|
// "max_auth_age" is needed because the user needs to always log in
|
|
|
|
// and not use an existing session.
|
|
|
|
return $this->client->createAuthUrl() . '&max_auth_age=0';
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
2013-06-12 18:31:16 +03:00
|
|
|
* Authenticate the Google API usage.
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
2013-06-19 22:29:00 +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 a authentication code is
|
|
|
|
* provided. Using this code, we can authenticate the API usage and store the
|
|
|
|
* token information to the database.
|
|
|
|
*
|
|
|
|
* @see Google Controller
|
|
|
|
*/
|
|
|
|
public function authenticate($auth_code) {
|
|
|
|
$this->client->authenticate($auth_code);
|
|
|
|
return $this->client->getAccessToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh the Google Client access token.
|
|
|
|
*
|
2013-06-12 18:31:16 +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.
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
2013-06-12 18:31:16 +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 Caledar account.
|
2013-05-04 00:26:04 +03:00
|
|
|
*/
|
2013-06-19 22:29:00 +03:00
|
|
|
public function refresh_token($refresh_token) {
|
|
|
|
$this->client->refreshToken($refresh_token);
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-10 18:51:23 +03:00
|
|
|
* Add an appointment record to its providers Google Calendar account.
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
2013-06-10 18:51:23 +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.
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
2013-07-09 17:46:48 +03:00
|
|
|
* @param array $appointment Contains the appointment record data.
|
|
|
|
* @param array $provider Contains the provider record data.
|
|
|
|
* @param array $service Contains the service record data.
|
|
|
|
* @param array $customer Contains the customer recod data.
|
2013-06-26 12:31:57 +03:00
|
|
|
* @parma array $company_settings Contains some company settings that are used
|
|
|
|
* by this method. By the time the following values must be in the array:
|
|
|
|
* 'company_name'.
|
2013-05-04 00:26:04 +03:00
|
|
|
* @return Google_Event Returns the Google_Event class object.
|
|
|
|
*/
|
2013-07-09 17:46:48 +03:00
|
|
|
public function add_appointment($appointment, $provider, $service, $customer, $company_settings) {
|
2013-06-19 22:29:00 +03:00
|
|
|
$this->CI->load->helper('general');
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
$event = new Google_Event();
|
2013-10-27 19:41:37 +02:00
|
|
|
$event->setSummary(($service != NULL) ? $service['name'] : 'Unavailable');
|
2013-06-26 12:31:57 +03:00
|
|
|
$event->setLocation($company_settings['company_name']);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
$start = new Google_EventDateTime();
|
2013-07-09 17:46:48 +03:00
|
|
|
$start->setDateTime(date3339(strtotime($appointment['start_datetime'])));
|
2013-05-04 00:26:04 +03:00
|
|
|
$event->setStart($start);
|
|
|
|
|
|
|
|
$end = new Google_EventDateTime();
|
2013-07-09 17:46:48 +03:00
|
|
|
$end->setDateTime(date3339(strtotime($appointment['end_datetime'])));
|
2013-05-04 00:26:04 +03:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
2013-10-27 19:41:37 +02:00
|
|
|
$event->attendees = array();
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-10-27 19:41:37 +02:00
|
|
|
$event_provider = new Google_EventAttendee();
|
|
|
|
$event_provider->setDisplayName($provider['first_name'] . ' '
|
|
|
|
. $provider['last_name']);
|
|
|
|
$event_provider->setEmail($provider['email']);
|
|
|
|
$event->attendees[] = $event_provider;
|
|
|
|
|
|
|
|
if ($customer != NULL) {
|
|
|
|
$event_customer = new Google_EventAttendee();
|
|
|
|
$event_customer->setDisplayName($customer['first_name'] . ' '
|
|
|
|
. $customer['last_name']);
|
|
|
|
$event_customer->setEmail($customer['email']);
|
|
|
|
$event->attendees[] = $event_customer;
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
// Add the new event to the google calendar.
|
|
|
|
$created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
return $created_event;
|
|
|
|
}
|
2013-06-10 18:51:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing appointment that is already synced with Google Calendar.
|
|
|
|
*
|
2013-06-24 09:04:30 +03:00
|
|
|
* This method updates the google calendar event item that is connected with the
|
|
|
|
* provided appointment record of Easy!Appointments.
|
|
|
|
*
|
2013-07-09 17:46:48 +03:00
|
|
|
* @param array $appointment Contains the appointment record data.
|
|
|
|
* @param array $provider Contains the provider record data.
|
|
|
|
* @param array $service Contains the service record data.
|
|
|
|
* @param array $customer Contains the customer recod data.
|
2013-06-24 12:52:23 +03:00
|
|
|
* @parma array $company_settings Contains some company settings that are used
|
|
|
|
* by this method. By the time the following values must be in the array:
|
|
|
|
* 'company_name'.
|
|
|
|
* @return Google_Event Returns the Google_Event class object.
|
2013-06-10 18:51:23 +03:00
|
|
|
*/
|
2013-07-09 17:46:48 +03:00
|
|
|
public function update_appointment($appointment, $provider, $service, $customer, $company_settings) {
|
2013-06-19 22:29:00 +03:00
|
|
|
$this->CI->load->helper('general');
|
2013-06-10 18:51:23 +03:00
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
$event = $this->service->events->get($provider['settings']['google_calendar'], $appointment['id_google_calendar']);
|
2013-06-24 12:52:23 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$event->setSummary($service['name']);
|
2013-06-24 12:52:23 +03:00
|
|
|
$event->setLocation($company_settings['company_name']);
|
|
|
|
|
|
|
|
$start = new Google_EventDateTime();
|
2013-07-09 17:46:48 +03:00
|
|
|
$start->setDateTime(date3339(strtotime($appointment['start_datetime'])));
|
2013-06-24 12:52:23 +03:00
|
|
|
$event->setStart($start);
|
|
|
|
|
|
|
|
$end = new Google_EventDateTime();
|
2013-07-09 17:46:48 +03:00
|
|
|
$end->setDateTime(date3339(strtotime($appointment['end_datetime'])));
|
2013-06-24 12:52:23 +03:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
2013-10-27 19:41:37 +02:00
|
|
|
$event->attendees = array();
|
|
|
|
|
2013-06-24 12:52:23 +03:00
|
|
|
$event_provider = new Google_EventAttendee();
|
2013-07-09 17:46:48 +03:00
|
|
|
$event_provider->setDisplayName($provider['first_name'] . ' '
|
|
|
|
. $provider['last_name']);
|
|
|
|
$event_provider->setEmail($provider['email']);
|
2013-10-27 19:41:37 +02:00
|
|
|
$event->attendees[] = $event_provider;
|
|
|
|
|
|
|
|
if ($customer != NULL) {
|
|
|
|
$event_customer = new Google_EventAttendee();
|
|
|
|
$event_customer->setDisplayName($customer['first_name'] . ' '
|
|
|
|
. $customer['last_name']);
|
|
|
|
$event_customer->setEmail($customer['email']);
|
|
|
|
$event->attendees[] = $event_customer;
|
|
|
|
}
|
2013-06-24 12:52:23 +03:00
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
$updated_event = $this->service->events->update($provider['settings']['google_calendar'],
|
|
|
|
$event->getId(), $event);
|
2013-06-24 12:52:23 +03:00
|
|
|
|
|
|
|
return $updated_event;
|
2013-06-10 18:51:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing appointment from Google Calendar.
|
|
|
|
*
|
2013-12-26 15:32:25 +02:00
|
|
|
* @param array $provider Contains the provider record data.
|
|
|
|
* @param string $google_event_id The Google Calendar event id to
|
2013-06-19 22:29:00 +03:00
|
|
|
* be deleted.
|
2013-06-10 18:51:23 +03:00
|
|
|
*/
|
2013-12-26 15:32:25 +02:00
|
|
|
public function delete_appointment($provider, $google_event_id) {
|
|
|
|
$this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
|
2013-06-10 18:51:23 +03:00
|
|
|
}
|
2013-07-09 17:46:48 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
/**
|
|
|
|
* Add unavailable period event to Google Calendar.
|
|
|
|
*
|
2013-12-26 15:32:25 +02:00
|
|
|
* @param array $provider Contains the provider record data.
|
2013-07-10 16:57:24 +03:00
|
|
|
* @param array $unavailable Contains unavailable period's data.
|
|
|
|
* @return Google_Event Returns the google event's object.
|
|
|
|
*/
|
2013-12-26 15:32:25 +02:00
|
|
|
public function add_unavailable($provider, $unavailable) {
|
2013-07-10 16:57:24 +03:00
|
|
|
$this->CI->load->helper('general');
|
|
|
|
|
|
|
|
$event = new Google_Event();
|
2013-12-26 15:32:25 +02:00
|
|
|
$event->setSummary('Unavailable');
|
2013-07-10 16:57:24 +03:00
|
|
|
$event->setDescription($unavailable['notes']);
|
|
|
|
|
|
|
|
$start = new Google_EventDateTime();
|
|
|
|
$start->setDateTime(date3339(strtotime($unavailable['start_datetime'])));
|
|
|
|
$event->setStart($start);
|
|
|
|
|
|
|
|
$end = new Google_EventDateTime();
|
|
|
|
$end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
|
|
|
|
$event->setEnd($end);
|
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
// Add the new event to the google calendar.
|
|
|
|
$created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event);
|
2013-07-10 16:57:24 +03:00
|
|
|
|
|
|
|
return $created_event;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update Google Calendar unavailable period event.
|
|
|
|
*
|
2013-12-26 15:32:25 +02:00
|
|
|
* @param array $provider Contains the provider record data.
|
2013-07-10 16:57:24 +03:00
|
|
|
* @param array $unavailable Contains the unavailable period data.
|
|
|
|
* @return Google_Event Returns the Google_Event object.
|
|
|
|
*/
|
2013-12-26 15:32:25 +02:00
|
|
|
public function update_unavailable($provider, $unavailable) {
|
2013-07-10 16:57:24 +03:00
|
|
|
$this->CI->load->helper('general');
|
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
$event = $this->service->events->get($provider['settings']['google_calendar'], $unavailable['id_google_calendar']);
|
2013-07-10 16:57:24 +03:00
|
|
|
$event->setDescription($unavailable['notes']);
|
|
|
|
|
|
|
|
$start = new Google_EventDateTime();
|
|
|
|
$start->setDateTime(date3339(strtotime($unavailable['start_datetime'])));
|
|
|
|
$event->setStart($start);
|
|
|
|
|
|
|
|
$end = new Google_EventDateTime();
|
|
|
|
$end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
|
|
|
|
$event->setEnd($end);
|
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
$updated_event = $this->service->events->update($provider['settings']['google_calendar'],
|
|
|
|
$event->getId(), $event);
|
2013-07-10 16:57:24 +03:00
|
|
|
|
|
|
|
return $updated_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete unavailable period event from Google Calendar.
|
|
|
|
*
|
2013-12-26 15:32:25 +02:00
|
|
|
* @param array $provider Contains the provider record data.
|
|
|
|
* @param string $google_event_id Google Calendar event id to be deleted.
|
2013-07-10 16:57:24 +03:00
|
|
|
*/
|
2013-12-26 15:32:25 +02:00
|
|
|
public function delete_unavailable($provider, $google_event_id) {
|
|
|
|
$this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
|
2013-07-10 16:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an event object from gcal
|
|
|
|
*
|
2013-12-26 15:32:25 +02:00
|
|
|
* @param array $provider Contains the provider record data.
|
|
|
|
* @param string $google_event_id Id of the google calendar event
|
2013-07-10 16:57:24 +03:00
|
|
|
* @return Google_Event Returns the google event object.
|
|
|
|
*/
|
2013-12-26 15:32:25 +02:00
|
|
|
public function get_event($provider, $google_event_id) {
|
|
|
|
return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id);
|
2013-07-09 17:46:48 +03:00
|
|
|
}
|
2013-07-15 10:32:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the events between the sync period.
|
|
|
|
*
|
2013-12-26 15:32:25 +02:00
|
|
|
* @param string $google_calendar The name of the google calendar to be used.
|
2013-07-15 10:32:19 +03:00
|
|
|
* @param date $start The start date of sync period.
|
|
|
|
* @param date $end The end date of sync period.
|
|
|
|
* @return object Returns an array with Google_Event objects that belong on the given
|
|
|
|
* sync period (start, end).
|
|
|
|
*/
|
2013-12-26 15:32:25 +02:00
|
|
|
public function get_sync_events($google_calendar, $start, $end) {
|
2013-07-15 10:32:19 +03:00
|
|
|
$this->CI->load->helper('general');
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
'timeMin' => date3339($start),
|
|
|
|
'timeMax' => date3339($end)
|
|
|
|
);
|
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
return $this->service->events->listEvents($google_calendar, $params);
|
2013-07-15 10:32:19 +03:00
|
|
|
}
|
2013-12-25 03:22:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return available google calendars for specific user.
|
|
|
|
*
|
|
|
|
* The given user's token must already exist in db in order to get access to his
|
|
|
|
* Google Calendar account.
|
|
|
|
*
|
2013-12-26 02:57:59 +02:00
|
|
|
* @param string $google_token The user's token will be used to grant access to google calendar.
|
2013-12-25 03:22:37 +02:00
|
|
|
* @return array Returns an array with the available calendars.
|
|
|
|
*/
|
2013-12-26 02:57:59 +02:00
|
|
|
public function get_google_calendars() {
|
|
|
|
$calendarList = $this->service->calendarList->listCalendarList();
|
|
|
|
$calendars = array();
|
|
|
|
foreach ($calendarList->items as $google_calendar) {
|
|
|
|
$calendars[] = array(
|
|
|
|
'id' => $google_calendar->id,
|
|
|
|
'summary' => $google_calendar->summary
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $calendars;
|
2013-12-25 03:22:37 +02:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file google_sync.php */
|
|
|
|
/* Location: ./application/libraries/google_sync.php */
|