mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-22 07:52:29 +03:00
Finished the google calendar selection for the synchronization feature.
This commit is contained in:
parent
de29ef1ef4
commit
982a9abb23
8 changed files with 52 additions and 40 deletions
|
@ -263,7 +263,7 @@ class Appointments extends CI_Controller {
|
||||||
->get_setting('google_token', $provider['id']));
|
->get_setting('google_token', $provider['id']));
|
||||||
$this->load->library('Google_Sync');
|
$this->load->library('Google_Sync');
|
||||||
$this->google_sync->refresh_token($google_token->refresh_token);
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
||||||
$this->google_sync->delete_appointment($appointment['id_google_calendar']);
|
$this->google_sync->delete_appointment($provider, $appointment['id_google_calendar']);
|
||||||
}
|
}
|
||||||
} catch(Exception $exc) {
|
} catch(Exception $exc) {
|
||||||
$exceptions[] = $exc;
|
$exceptions[] = $exc;
|
||||||
|
|
|
@ -279,7 +279,7 @@ class Backend_api extends CI_Controller {
|
||||||
->get_setting('google_token', $provider['id']));
|
->get_setting('google_token', $provider['id']));
|
||||||
$this->load->library('Google_Sync');
|
$this->load->library('Google_Sync');
|
||||||
$this->google_sync->refresh_token($google_token->refresh_token);
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
||||||
$this->google_sync->delete_appointment($appointment['id_google_calendar']);
|
$this->google_sync->delete_appointment($provider, $appointment['id_google_calendar']);
|
||||||
}
|
}
|
||||||
} catch(Exception $exc) {
|
} catch(Exception $exc) {
|
||||||
$warnings[] = exceptionToJavaScript($exc);
|
$warnings[] = exceptionToJavaScript($exc);
|
||||||
|
@ -430,6 +430,8 @@ class Backend_api extends CI_Controller {
|
||||||
$this->load->model('appointments_model');
|
$this->load->model('appointments_model');
|
||||||
$this->load->model('providers_model');
|
$this->load->model('providers_model');
|
||||||
|
|
||||||
|
$provider = $this->providers_model->get_row($unavailable['id_users_provider']);
|
||||||
|
|
||||||
// Add appointment
|
// Add appointment
|
||||||
$unavailable['id'] = $this->appointments_model->add_unavailable($unavailable);
|
$unavailable['id'] = $this->appointments_model->add_unavailable($unavailable);
|
||||||
$unavailable = $this->appointments_model->get_row($unavailable['id']); // fetch all inserted data
|
$unavailable = $this->appointments_model->get_row($unavailable['id']); // fetch all inserted data
|
||||||
|
@ -447,11 +449,11 @@ class Backend_api extends CI_Controller {
|
||||||
$this->google_sync->refresh_token($google_token->refresh_token);
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
||||||
|
|
||||||
if ($unavailable['id_google_calendar'] == NULL) {
|
if ($unavailable['id_google_calendar'] == NULL) {
|
||||||
$google_event = $this->google_sync->add_unavailable($unavailable);
|
$google_event = $this->google_sync->add_unavailable($provider, $unavailable);
|
||||||
$unavailable['id_google_calendar'] = $google_event->id;
|
$unavailable['id_google_calendar'] = $google_event->id;
|
||||||
$this->appointments_model->add_unavailable($unavailable);
|
$this->appointments_model->add_unavailable($unavailable);
|
||||||
} else {
|
} else {
|
||||||
$google_event = $this->google_sync->update_unavailable($unavailable);
|
$google_event = $this->google_sync->update_unavailable($provider, $unavailable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(Exception $exc) {
|
} catch(Exception $exc) {
|
||||||
|
@ -500,7 +502,7 @@ class Backend_api extends CI_Controller {
|
||||||
$google_token = json_decode($this->providers_model->get_setting('google_token', $provider['id']));
|
$google_token = json_decode($this->providers_model->get_setting('google_token', $provider['id']));
|
||||||
$this->load->library('google_sync');
|
$this->load->library('google_sync');
|
||||||
$this->google_sync->refresh_token($google_token->refresh_token);
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
||||||
$this->google_sync->delete_unavailable($unavailable['id_google_calendar']);
|
$this->google_sync->delete_unavailable($provider, $unavailable['id_google_calendar']);
|
||||||
}
|
}
|
||||||
} catch(Exception $exc) {
|
} catch(Exception $exc) {
|
||||||
$warnings[] = $exc;
|
$warnings[] = $exc;
|
||||||
|
|
|
@ -12,8 +12,7 @@ class Google extends CI_Controller {
|
||||||
* a refresh token from the Google API service, this method is going to authorize
|
* a refresh token from the Google API service, this method is going to authorize
|
||||||
* the given provider.
|
* the given provider.
|
||||||
*
|
*
|
||||||
* @param int $provider_id The provider id, for whom the sync authorization is
|
* @param int $provider_id The provider id, for whom the sync authorization is made.
|
||||||
* made.
|
|
||||||
*/
|
*/
|
||||||
public function oauth($provider_id) {
|
public function oauth($provider_id) {
|
||||||
// Store the provider id for use on the callback function.
|
// Store the provider id for use on the callback function.
|
||||||
|
@ -54,11 +53,11 @@ class Google extends CI_Controller {
|
||||||
if (isset($_SESSION['oauth_provider_id'])) {
|
if (isset($_SESSION['oauth_provider_id'])) {
|
||||||
$this->load->model('providers_model');
|
$this->load->model('providers_model');
|
||||||
$this->providers_model->set_setting('google_sync', TRUE,
|
$this->providers_model->set_setting('google_sync', TRUE,
|
||||||
$_SESSION['oauth_provider_id']);
|
$_SESSION['oauth_provider_id']);
|
||||||
$this->providers_model->set_setting('google_token', $token,
|
$this->providers_model->set_setting('google_token', $token,
|
||||||
$_SESSION['oauth_provider_id']);
|
$_SESSION['oauth_provider_id']);
|
||||||
$this->providers_model->set_setting('google_calendar', 'primary',
|
$this->providers_model->set_setting('google_calendar', 'primary',
|
||||||
$_SESSION['oauth_provider_id']);
|
$_SESSION['oauth_provider_id']);
|
||||||
} else {
|
} else {
|
||||||
echo '<h1>Sync provider id not specified!</h1>';
|
echo '<h1>Sync provider id not specified!</h1>';
|
||||||
}
|
}
|
||||||
|
@ -75,7 +74,6 @@ class Google extends CI_Controller {
|
||||||
* be necessary and this will lead to consuming the Google limit for the Calendar API usage.
|
* be necessary and this will lead to consuming the Google limit for the Calendar API usage.
|
||||||
*
|
*
|
||||||
* @param numeric $provider_id Provider record to be synced.
|
* @param numeric $provider_id Provider record to be synced.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public function sync($provider_id = NULL) {
|
public function sync($provider_id = NULL) {
|
||||||
try {
|
try {
|
||||||
|
@ -146,7 +144,7 @@ class Google extends CI_Controller {
|
||||||
} else {
|
} else {
|
||||||
// Appointment is synced with google calendar.
|
// Appointment is synced with google calendar.
|
||||||
try {
|
try {
|
||||||
$google_event = $this->google_sync->get_event($appointment['id_google_calendar']);
|
$google_event = $this->google_sync->get_event($provider, $appointment['id_google_calendar']);
|
||||||
|
|
||||||
if ($google_event->status == 'cancelled') {
|
if ($google_event->status == 'cancelled') {
|
||||||
throw new Exception('Event is cancelled, remove the record from Easy!Appointments.');
|
throw new Exception('Event is cancelled, remove the record from Easy!Appointments.');
|
||||||
|
@ -178,7 +176,8 @@ class Google extends CI_Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
// :: ADD GCAL EVENTS THAT ARE NOT PRESENT ON E!A
|
// :: ADD GCAL EVENTS THAT ARE NOT PRESENT ON E!A
|
||||||
$events = $this->google_sync->get_sync_events($start, $end);
|
$google_calendar = $provider['settings']['google_calendar'];
|
||||||
|
$events = $this->google_sync->get_sync_events($google_calendar, $start, $end);
|
||||||
|
|
||||||
foreach($events->getItems() as $event) {
|
foreach($events->getItems() as $event) {
|
||||||
$results = $this->appointments_model->get_batch(array('id_google_calendar' => $event->getId()));
|
$results = $this->appointments_model->get_batch(array('id_google_calendar' => $event->getId()));
|
||||||
|
|
|
@ -60,6 +60,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
||||||
'notifications' => TRUE,
|
'notifications' => TRUE,
|
||||||
'google_sync' => 0,
|
'google_sync' => 0,
|
||||||
'google_token' => NULL,
|
'google_token' => NULL,
|
||||||
|
'google_calendar' => NULL,
|
||||||
'sync_past_days' => '5',
|
'sync_past_days' => '5',
|
||||||
'sync_future_days' => '5'
|
'sync_future_days' => '5'
|
||||||
)
|
)
|
||||||
|
@ -117,6 +118,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
||||||
'notifications' => TRUE,
|
'notifications' => TRUE,
|
||||||
'google_sync' => 0,
|
'google_sync' => 0,
|
||||||
'google_token' => NULL,
|
'google_token' => NULL,
|
||||||
|
'google_calendar' => NULL,
|
||||||
'sync_past_days' => '5',
|
'sync_past_days' => '5',
|
||||||
'sync_future_days' => '5'
|
'sync_future_days' => '5'
|
||||||
);
|
);
|
||||||
|
@ -144,6 +146,7 @@ class Unit_tests_providers_model extends CI_Driver {
|
||||||
'notifications' => TRUE,
|
'notifications' => TRUE,
|
||||||
'google_sync' => 0,
|
'google_sync' => 0,
|
||||||
'google_token' => NULL,
|
'google_token' => NULL,
|
||||||
|
'google_calendar' => NULL,
|
||||||
'sync_past_days' => '9', // changed
|
'sync_past_days' => '9', // changed
|
||||||
'sync_future_days' => '8' // changed
|
'sync_future_days' => '8' // changed
|
||||||
)
|
)
|
||||||
|
|
|
@ -105,7 +105,6 @@ class Google_Sync {
|
||||||
* @return Google_Event Returns the Google_Event class object.
|
* @return Google_Event Returns the Google_Event class object.
|
||||||
*/
|
*/
|
||||||
public function add_appointment($appointment, $provider, $service, $customer, $company_settings) {
|
public function add_appointment($appointment, $provider, $service, $customer, $company_settings) {
|
||||||
|
|
||||||
$this->CI->load->helper('general');
|
$this->CI->load->helper('general');
|
||||||
|
|
||||||
$event = new Google_Event();
|
$event = new Google_Event();
|
||||||
|
@ -136,8 +135,8 @@ class Google_Sync {
|
||||||
$event->attendees[] = $event_customer;
|
$event->attendees[] = $event_customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the new event to the "primary" calendar.
|
// Add the new event to the google calendar.
|
||||||
$created_event = $this->service->events->insert('primary', $event);
|
$created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event);
|
||||||
|
|
||||||
return $created_event;
|
return $created_event;
|
||||||
}
|
}
|
||||||
|
@ -160,7 +159,7 @@ class Google_Sync {
|
||||||
public function update_appointment($appointment, $provider, $service, $customer, $company_settings) {
|
public function update_appointment($appointment, $provider, $service, $customer, $company_settings) {
|
||||||
$this->CI->load->helper('general');
|
$this->CI->load->helper('general');
|
||||||
|
|
||||||
$event = $this->service->events->get('primary', $appointment['id_google_calendar']);
|
$event = $this->service->events->get($provider['settings']['google_calendar'], $appointment['id_google_calendar']);
|
||||||
|
|
||||||
$event->setSummary($service['name']);
|
$event->setSummary($service['name']);
|
||||||
$event->setLocation($company_settings['company_name']);
|
$event->setLocation($company_settings['company_name']);
|
||||||
|
@ -189,7 +188,8 @@ class Google_Sync {
|
||||||
$event->attendees[] = $event_customer;
|
$event->attendees[] = $event_customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
$updated_event = $this->service->events->update('primary', $event->getId(), $event);
|
$updated_event = $this->service->events->update($provider['settings']['google_calendar'],
|
||||||
|
$event->getId(), $event);
|
||||||
|
|
||||||
return $updated_event;
|
return $updated_event;
|
||||||
}
|
}
|
||||||
|
@ -197,24 +197,26 @@ class Google_Sync {
|
||||||
/**
|
/**
|
||||||
* Delete an existing appointment from Google Calendar.
|
* Delete an existing appointment from Google Calendar.
|
||||||
*
|
*
|
||||||
* @param string $google_calendar_id The Google Calendar event id to
|
* @param array $provider Contains the provider record data.
|
||||||
|
* @param string $google_event_id The Google Calendar event id to
|
||||||
* be deleted.
|
* be deleted.
|
||||||
*/
|
*/
|
||||||
public function delete_appointment($google_calendar_id) {
|
public function delete_appointment($provider, $google_event_id) {
|
||||||
$this->service->events->delete('primary', $google_calendar_id);
|
$this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add unavailable period event to Google Calendar.
|
* Add unavailable period event to Google Calendar.
|
||||||
*
|
*
|
||||||
|
* @param array $provider Contains the provider record data.
|
||||||
* @param array $unavailable Contains unavailable period's data.
|
* @param array $unavailable Contains unavailable period's data.
|
||||||
* @return Google_Event Returns the google event's object.
|
* @return Google_Event Returns the google event's object.
|
||||||
*/
|
*/
|
||||||
public function add_unavailable($unavailable) {
|
public function add_unavailable($provider, $unavailable) {
|
||||||
$this->CI->load->helper('general');
|
$this->CI->load->helper('general');
|
||||||
|
|
||||||
$event = new Google_Event();
|
$event = new Google_Event();
|
||||||
$event->setSummary('Unavailalbe');
|
$event->setSummary('Unavailable');
|
||||||
$event->setDescription($unavailable['notes']);
|
$event->setDescription($unavailable['notes']);
|
||||||
|
|
||||||
$start = new Google_EventDateTime();
|
$start = new Google_EventDateTime();
|
||||||
|
@ -225,8 +227,8 @@ class Google_Sync {
|
||||||
$end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
|
$end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
|
||||||
$event->setEnd($end);
|
$event->setEnd($end);
|
||||||
|
|
||||||
// Add the new event to the "primary" calendar.
|
// Add the new event to the google calendar.
|
||||||
$created_event = $this->service->events->insert('primary', $event);
|
$created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event);
|
||||||
|
|
||||||
return $created_event;
|
return $created_event;
|
||||||
|
|
||||||
|
@ -235,13 +237,14 @@ class Google_Sync {
|
||||||
/**
|
/**
|
||||||
* Update Google Calendar unavailable period event.
|
* Update Google Calendar unavailable period event.
|
||||||
*
|
*
|
||||||
|
* @param array $provider Contains the provider record data.
|
||||||
* @param array $unavailable Contains the unavailable period data.
|
* @param array $unavailable Contains the unavailable period data.
|
||||||
* @return Google_Event Returns the Google_Event object.
|
* @return Google_Event Returns the Google_Event object.
|
||||||
*/
|
*/
|
||||||
public function update_unavailable($unavailable) {
|
public function update_unavailable($provider, $unavailable) {
|
||||||
$this->CI->load->helper('general');
|
$this->CI->load->helper('general');
|
||||||
|
|
||||||
$event = $this->service->events->get('primary', $unavailable['id_google_calendar']);
|
$event = $this->service->events->get($provider['settings']['google_calendar'], $unavailable['id_google_calendar']);
|
||||||
$event->setDescription($unavailable['notes']);
|
$event->setDescription($unavailable['notes']);
|
||||||
|
|
||||||
$start = new Google_EventDateTime();
|
$start = new Google_EventDateTime();
|
||||||
|
@ -252,7 +255,8 @@ class Google_Sync {
|
||||||
$end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
|
$end->setDateTime(date3339(strtotime($unavailable['end_datetime'])));
|
||||||
$event->setEnd($end);
|
$event->setEnd($end);
|
||||||
|
|
||||||
$updated_event = $this->service->events->update('primary', $event->getId(), $event);
|
$updated_event = $this->service->events->update($provider['settings']['google_calendar'],
|
||||||
|
$event->getId(), $event);
|
||||||
|
|
||||||
return $updated_event;
|
return $updated_event;
|
||||||
}
|
}
|
||||||
|
@ -260,31 +264,34 @@ class Google_Sync {
|
||||||
/**
|
/**
|
||||||
* Delete unavailable period event from Google Calendar.
|
* Delete unavailable period event from Google Calendar.
|
||||||
*
|
*
|
||||||
* @param string $google_calendar_id Google Calendar event id to be deleted.
|
* @param array $provider Contains the provider record data.
|
||||||
|
* @param string $google_event_id Google Calendar event id to be deleted.
|
||||||
*/
|
*/
|
||||||
public function delete_unavailable($google_calendar_id) {
|
public function delete_unavailable($provider, $google_event_id) {
|
||||||
$this->service->events->delete('primary', $google_calendar_id);
|
$this->service->events->delete($provider['settings']['google_calendar'], $google_event_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an event object from gcal
|
* Get an event object from gcal
|
||||||
*
|
*
|
||||||
* @param string $google_calendar_id Id of the google calendar event
|
* @param array $provider Contains the provider record data.
|
||||||
|
* @param string $google_event_id Id of the google calendar event
|
||||||
* @return Google_Event Returns the google event object.
|
* @return Google_Event Returns the google event object.
|
||||||
*/
|
*/
|
||||||
public function get_event($google_calendar_id) {
|
public function get_event($provider, $google_event_id) {
|
||||||
return $this->service->events->get('primary', $google_calendar_id);
|
return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all the events between the sync period.
|
* Get all the events between the sync period.
|
||||||
*
|
*
|
||||||
|
* @param string $google_calendar The name of the google calendar to be used.
|
||||||
* @param date $start The start date of sync period.
|
* @param date $start The start date of sync period.
|
||||||
* @param date $end The end 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
|
* @return object Returns an array with Google_Event objects that belong on the given
|
||||||
* sync period (start, end).
|
* sync period (start, end).
|
||||||
*/
|
*/
|
||||||
public function get_sync_events($start, $end) {
|
public function get_sync_events($google_calendar, $start, $end) {
|
||||||
$this->CI->load->helper('general');
|
$this->CI->load->helper('general');
|
||||||
|
|
||||||
$params = array(
|
$params = array(
|
||||||
|
@ -292,7 +299,7 @@ class Google_Sync {
|
||||||
'timeMax' => date3339($end)
|
'timeMax' => date3339($end)
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->service->events->listEvents('primary', $params);
|
return $this->service->events->listEvents($google_calendar, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
* 'working_plan'
|
* 'working_plan'
|
||||||
* 'google_sync'
|
* 'google_sync'
|
||||||
* 'google_token'
|
* 'google_token'
|
||||||
|
* 'google_calendar'
|
||||||
* 'sync_past_days'
|
* 'sync_past_days'
|
||||||
* 'sync_future_days'
|
* 'sync_future_days'
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -194,7 +194,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<label for="notes" class="control-label"><?php echo $this->lang->line('notes'); ?></label>
|
<label for="appointment-notes" class="control-label"><?php echo $this->lang->line('notes'); ?></label>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
<textarea id="appointment-notes" class="span4" rows="3"></textarea>
|
<textarea id="appointment-notes" class="span4" rows="3"></textarea>
|
||||||
</div>
|
</div>
|
||||||
|
@ -282,7 +282,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<label for="notes" class="control-label">
|
<label for="customer-notes" class="control-label">
|
||||||
<?php echo $this->lang->line('notes'); ?></label>
|
<?php echo $this->lang->line('notes'); ?></label>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
<textarea id="customer-notes" rows="3"></textarea>
|
<textarea id="customer-notes" rows="3"></textarea>
|
||||||
|
@ -306,7 +306,7 @@
|
||||||
<?php
|
<?php
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// MANAGE UNAVAILALBE
|
// MANAGE UNAVAILABLE
|
||||||
//
|
//
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -636,7 +636,7 @@ var BackendCalendar = {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display success message to the user.
|
// Display success message to the user.
|
||||||
$dialog.find('.modal-message').text(EALang['unavailabled_saved']);
|
$dialog.find('.modal-message').text(EALang['unavailable_saved']);
|
||||||
$dialog.find('.modal-message').removeClass('alert-error');
|
$dialog.find('.modal-message').removeClass('alert-error');
|
||||||
$dialog.find('.modal-message').addClass('alert-success');
|
$dialog.find('.modal-message').addClass('alert-success');
|
||||||
$dialog.find('.modal-message').fadeIn();
|
$dialog.find('.modal-message').fadeIn();
|
||||||
|
|
Loading…
Reference in a new issue