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.
|
2013-05-17 16:09:10 +03:00
|
|
|
require_once dirname(__FILE__) . '/external/google-api-php-client/Google_Client.php';
|
|
|
|
require_once dirname(__FILE__) . '/external/google-api-php-client/contrib/Google_CalendarService.php';
|
2013-05-04 00:26:04 +03:00
|
|
|
require_once dirname(dirname(dirname(__FILE__))) . '/configuration.php';
|
|
|
|
|
2013-06-10 18:51:23 +03:00
|
|
|
/**
|
|
|
|
* Google Synchronization Class
|
|
|
|
*
|
|
|
|
* This class implements all the core synchronization between the Google Calendar
|
|
|
|
* and the Easy!Appointments system.
|
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
class Google_Sync {
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
session_start();
|
|
|
|
|
|
|
|
$CI =& get_instance();
|
|
|
|
|
|
|
|
// Initialize google client and calendar service.
|
|
|
|
$this->client = new Google_Client();
|
|
|
|
$this->client->setApplicationName(SystemConfiguration::$google_product_name);
|
|
|
|
$this->client->setClientId(SystemConfiguration::$google_client_id);
|
|
|
|
$this->client->setClientSecret(SystemConfiguration::$google_client_secret);
|
|
|
|
$this->client->setDeveloperKey(SystemConfiguration::$google_api_key);
|
2013-06-10 18:51:23 +03:00
|
|
|
$this->client->setRedirectUri('http://localhost/oauth_callback');
|
2013-05-04 00:26:04 +03:00
|
|
|
$this->service = new Google_CalendarService($this->client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-12 18:31:16 +03:00
|
|
|
* Authenticate the Google API usage.
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
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.
|
|
|
|
* @return bool Returns the authenticate operation result.
|
2013-05-04 00:26:04 +03:00
|
|
|
*/
|
2013-06-12 18:31:16 +03:00
|
|
|
public function authenticate($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-06-10 18:51:23 +03:00
|
|
|
* <strong>IMPORTANT!</strong> If the access token is not valid anymore the
|
|
|
|
* appointment cannot be added to the Google Calendar. A notification warning
|
|
|
|
* must be sent to the provider in order to authorize the E!A again, and store
|
|
|
|
* the new access token to the database.
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
2013-06-10 18:51:23 +03:00
|
|
|
* @param int $appointment_id The record id of the appointment that is going to
|
|
|
|
* be added to the database.
|
2013-05-04 00:26:04 +03:00
|
|
|
* @return Google_Event Returns the Google_Event class object.
|
|
|
|
*/
|
2013-06-10 18:51:23 +03:00
|
|
|
public function add_appointment($appointment_id) {
|
2013-05-04 00:26:04 +03:00
|
|
|
$CI =& get_instance();
|
|
|
|
|
|
|
|
$CI->load->model('Appointments_Model');
|
2013-06-10 18:51:23 +03:00
|
|
|
$CI->load->model('Service_Model');
|
|
|
|
$CI->load->model('Provider_Model');
|
2013-05-04 00:26:04 +03:00
|
|
|
$CI->load->model('Customers_Model');
|
|
|
|
$CI->load->model('Settings_Model');
|
|
|
|
|
2013-06-10 18:51:23 +03:00
|
|
|
$appointment_data = $CI->Appointments_Model->get_row($appointment_id);
|
|
|
|
$provider_data = $CI->Providers_Model->get_row($appointment_data['id_users_provider']);
|
|
|
|
$customer_data = $CI->Customers_Model->get_row($appointment_data['id_users_customer']);
|
|
|
|
$service_data = $CI->Services_Model->get_row($appointment_data['id_services']);
|
|
|
|
$company_name = $CI->Settings_Model->get_setting('company_name');
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
$CI->load->helper('general');
|
|
|
|
|
|
|
|
$event = new Google_Event();
|
2013-06-10 18:51:23 +03:00
|
|
|
$event->setSummary($service_data['name']);
|
|
|
|
$event->setLocation($company_name);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
$start = new Google_EventDateTime();
|
2013-06-10 18:51:23 +03:00
|
|
|
$start->setDateTime(date3339(strtotime($appointment_data['start_datetime'])));
|
2013-05-04 00:26:04 +03:00
|
|
|
$event->setStart($start);
|
|
|
|
|
|
|
|
$end = new Google_EventDateTime();
|
2013-06-10 18:51:23 +03:00
|
|
|
$end->setDateTime(date3339(strtotime($appointment_data['end_datetime'])));
|
2013-05-04 00:26:04 +03:00
|
|
|
$event->setEnd($end);
|
|
|
|
|
|
|
|
$eventProvider = new Google_EventAttendee();
|
2013-06-10 18:51:23 +03:00
|
|
|
$eventProvider->setDisplayName($provider_data['first_name'] . ' '
|
|
|
|
. $provider_data['last_name']);
|
|
|
|
$eventProvider->setEmail($provider_data['email']);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
$eventCustomer = new Google_EventAttendee();
|
2013-06-10 18:51:23 +03:00
|
|
|
$eventCustomer->setDisplayName($customer_data['first_name'] . ' '
|
|
|
|
. $customer_data['last_name']);
|
|
|
|
$eventCustomer->setEmail($customer_data['email']);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-06-10 18:51:23 +03:00
|
|
|
$event->attendees = array(
|
|
|
|
$eventProvider,
|
|
|
|
$eventCustomer
|
|
|
|
);
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-06-10 18:51:23 +03:00
|
|
|
// Add the new event to the "primary" calendar.
|
2013-05-04 00:26:04 +03:00
|
|
|
$created_event = $this->service->events->insert('primary', $event);
|
|
|
|
|
|
|
|
return $created_event;
|
|
|
|
}
|
2013-06-10 18:51:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing appointment that is already synced with Google Calendar.
|
|
|
|
*
|
|
|
|
* @param int $appointment_id
|
|
|
|
*/
|
|
|
|
public function update_appointment($appointment_id) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing appointment from Google Calendar.
|
|
|
|
*
|
|
|
|
* @param type $appointment_id
|
|
|
|
*/
|
|
|
|
public function delete_appointment($appointment_id) {
|
|
|
|
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file google_sync.php */
|
|
|
|
/* Location: ./application/libraries/google_sync.php */
|