From 982a9abb23b02c6a7a25592f71a13bb10303e603 Mon Sep 17 00:00:00 2001 From: "alextselegidis@gmail.com" Date: Thu, 26 Dec 2013 13:32:25 +0000 Subject: [PATCH] Finished the google calendar selection for the synchronization feature. --- src/application/controllers/appointments.php | 2 +- src/application/controllers/backend_api.php | 10 ++-- src/application/controllers/google.php | 15 +++--- .../drivers/Unit_tests_providers_model.php | 3 ++ src/application/libraries/google_sync.php | 53 +++++++++++-------- src/application/models/providers_model.php | 1 + src/application/views/backend/calendar.php | 6 +-- src/assets/js/backend_calendar.js | 2 +- 8 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/application/controllers/appointments.php b/src/application/controllers/appointments.php index 93e67e1c..b8b73847 100644 --- a/src/application/controllers/appointments.php +++ b/src/application/controllers/appointments.php @@ -263,7 +263,7 @@ class Appointments extends CI_Controller { ->get_setting('google_token', $provider['id'])); $this->load->library('Google_Sync'); $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) { $exceptions[] = $exc; diff --git a/src/application/controllers/backend_api.php b/src/application/controllers/backend_api.php index ee8872f0..71d37b66 100644 --- a/src/application/controllers/backend_api.php +++ b/src/application/controllers/backend_api.php @@ -279,7 +279,7 @@ class Backend_api extends CI_Controller { ->get_setting('google_token', $provider['id'])); $this->load->library('Google_Sync'); $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) { $warnings[] = exceptionToJavaScript($exc); @@ -430,6 +430,8 @@ class Backend_api extends CI_Controller { $this->load->model('appointments_model'); $this->load->model('providers_model'); + $provider = $this->providers_model->get_row($unavailable['id_users_provider']); + // Add appointment $unavailable['id'] = $this->appointments_model->add_unavailable($unavailable); $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); 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; $this->appointments_model->add_unavailable($unavailable); } else { - $google_event = $this->google_sync->update_unavailable($unavailable); + $google_event = $this->google_sync->update_unavailable($provider, $unavailable); } } } 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'])); $this->load->library('google_sync'); $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) { $warnings[] = $exc; diff --git a/src/application/controllers/google.php b/src/application/controllers/google.php index 3ac1976b..01409797 100644 --- a/src/application/controllers/google.php +++ b/src/application/controllers/google.php @@ -12,8 +12,7 @@ class Google extends CI_Controller { * a refresh token from the Google API service, this method is going to authorize * the given provider. * - * @param int $provider_id The provider id, for whom the sync authorization is - * made. + * @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. @@ -54,11 +53,11 @@ class Google extends CI_Controller { if (isset($_SESSION['oauth_provider_id'])) { $this->load->model('providers_model'); $this->providers_model->set_setting('google_sync', TRUE, - $_SESSION['oauth_provider_id']); + $_SESSION['oauth_provider_id']); $this->providers_model->set_setting('google_token', $token, - $_SESSION['oauth_provider_id']); + $_SESSION['oauth_provider_id']); $this->providers_model->set_setting('google_calendar', 'primary', - $_SESSION['oauth_provider_id']); + $_SESSION['oauth_provider_id']); } else { echo '

Sync provider id not specified!

'; } @@ -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. * * @param numeric $provider_id Provider record to be synced. - * */ public function sync($provider_id = NULL) { try { @@ -146,7 +144,7 @@ class Google extends CI_Controller { } else { // Appointment is synced with google calendar. 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') { 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 - $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) { $results = $this->appointments_model->get_batch(array('id_google_calendar' => $event->getId())); diff --git a/src/application/libraries/Unit_tests/drivers/Unit_tests_providers_model.php b/src/application/libraries/Unit_tests/drivers/Unit_tests_providers_model.php index 55ab6f1c..36ff8503 100644 --- a/src/application/libraries/Unit_tests/drivers/Unit_tests_providers_model.php +++ b/src/application/libraries/Unit_tests/drivers/Unit_tests_providers_model.php @@ -60,6 +60,7 @@ class Unit_tests_providers_model extends CI_Driver { 'notifications' => TRUE, 'google_sync' => 0, 'google_token' => NULL, + 'google_calendar' => NULL, 'sync_past_days' => '5', 'sync_future_days' => '5' ) @@ -117,6 +118,7 @@ class Unit_tests_providers_model extends CI_Driver { 'notifications' => TRUE, 'google_sync' => 0, 'google_token' => NULL, + 'google_calendar' => NULL, 'sync_past_days' => '5', 'sync_future_days' => '5' ); @@ -144,6 +146,7 @@ class Unit_tests_providers_model extends CI_Driver { 'notifications' => TRUE, 'google_sync' => 0, 'google_token' => NULL, + 'google_calendar' => NULL, 'sync_past_days' => '9', // changed 'sync_future_days' => '8' // changed ) diff --git a/src/application/libraries/google_sync.php b/src/application/libraries/google_sync.php index 7325aee1..62e18656 100644 --- a/src/application/libraries/google_sync.php +++ b/src/application/libraries/google_sync.php @@ -105,7 +105,6 @@ class Google_Sync { * @return Google_Event Returns the Google_Event class object. */ public function add_appointment($appointment, $provider, $service, $customer, $company_settings) { - $this->CI->load->helper('general'); $event = new Google_Event(); @@ -136,8 +135,8 @@ class Google_Sync { $event->attendees[] = $event_customer; } - // Add the new event to the "primary" calendar. - $created_event = $this->service->events->insert('primary', $event); + // Add the new event to the google calendar. + $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event); return $created_event; } @@ -160,7 +159,7 @@ class Google_Sync { public function update_appointment($appointment, $provider, $service, $customer, $company_settings) { $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->setLocation($company_settings['company_name']); @@ -189,7 +188,8 @@ class Google_Sync { $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; } @@ -197,24 +197,26 @@ class Google_Sync { /** * 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. */ - public function delete_appointment($google_calendar_id) { - $this->service->events->delete('primary', $google_calendar_id); + public function delete_appointment($provider, $google_event_id) { + $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id); } /** * Add unavailable period event to Google Calendar. * + * @param array $provider Contains the provider record data. * @param array $unavailable Contains unavailable period's data. * @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'); $event = new Google_Event(); - $event->setSummary('Unavailalbe'); + $event->setSummary('Unavailable'); $event->setDescription($unavailable['notes']); $start = new Google_EventDateTime(); @@ -225,8 +227,8 @@ class Google_Sync { $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); $event->setEnd($end); - // Add the new event to the "primary" calendar. - $created_event = $this->service->events->insert('primary', $event); + // Add the new event to the google calendar. + $created_event = $this->service->events->insert($provider['settings']['google_calendar'], $event); return $created_event; @@ -235,13 +237,14 @@ class Google_Sync { /** * Update Google Calendar unavailable period event. * + * @param array $provider Contains the provider record data. * @param array $unavailable Contains the unavailable period data. * @return Google_Event Returns the Google_Event object. */ - public function update_unavailable($unavailable) { + public function update_unavailable($provider, $unavailable) { $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']); $start = new Google_EventDateTime(); @@ -252,7 +255,8 @@ class Google_Sync { $end->setDateTime(date3339(strtotime($unavailable['end_datetime']))); $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; } @@ -260,31 +264,34 @@ class Google_Sync { /** * 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) { - $this->service->events->delete('primary', $google_calendar_id); + public function delete_unavailable($provider, $google_event_id) { + $this->service->events->delete($provider['settings']['google_calendar'], $google_event_id); } /** * 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. */ - public function get_event($google_calendar_id) { - return $this->service->events->get('primary', $google_calendar_id); + public function get_event($provider, $google_event_id) { + return $this->service->events->get($provider['settings']['google_calendar'], $google_event_id); } /** * 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 $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). */ - public function get_sync_events($start, $end) { + public function get_sync_events($google_calendar, $start, $end) { $this->CI->load->helper('general'); $params = array( @@ -292,7 +299,7 @@ class Google_Sync { 'timeMax' => date3339($end) ); - return $this->service->events->listEvents('primary', $params); + return $this->service->events->listEvents($google_calendar, $params); } /** diff --git a/src/application/models/providers_model.php b/src/application/models/providers_model.php index efa87ea3..bd0faaee 100644 --- a/src/application/models/providers_model.php +++ b/src/application/models/providers_model.php @@ -26,6 +26,7 @@ * 'working_plan' * 'google_sync' * 'google_token' + * 'google_calendar' * 'sync_past_days' * 'sync_future_days' */ diff --git a/src/application/views/backend/calendar.php b/src/application/views/backend/calendar.php index 084c80c8..1312d9a6 100644 --- a/src/application/views/backend/calendar.php +++ b/src/application/views/backend/calendar.php @@ -194,7 +194,7 @@
- +
@@ -282,7 +282,7 @@
-