From de29ef1ef4dbce6284257d5d8ab2621c0b4c1d4b Mon Sep 17 00:00:00 2001 From: "alextselegidis@gmail.com" Date: Thu, 26 Dec 2013 00:57:59 +0000 Subject: [PATCH] Added calendar selection when the user enables the google calendar synchronization for a provider. --- doc/translations.xls | Bin 59392 -> 60928 bytes src/application/controllers/appointments.php | 6 +- src/application/controllers/backend_api.php | 92 ++++++++++++------ src/application/controllers/google.php | 29 +++--- .../language/english/translations_lang.php | 3 + .../language/german/translations_lang.php | 5 +- .../language/greek/translations_lang.php | 5 +- src/application/libraries/google_sync.php | 16 ++- src/application/views/backend/calendar.php | 35 ++++++- src/assets/js/backend_calendar.js | 57 ++++++++++- 10 files changed, 188 insertions(+), 60 deletions(-) diff --git a/doc/translations.xls b/doc/translations.xls index 27b5869e716c147662c6cb89d9be0aa08db91161..5fffa7d870832d1fa6abf48e32c72490aa9ed5ae 100644 GIT binary patch delta 1245 zcmaJ=OGp(_82;z}+QVxmfju(2sEGv81f95_is0PlQ`Tp~L|Kt4sv^>X_8Eike z$%53A4{)ykNDNCfGU&9LX#HGWT@}krecNL1s3{%DIqL{8tpK%LUyKf*)dt=T1Yl_IcM^AI_kdMID;u1!bixUlA`I*2r6(W z1gKkx5SP^|JS{%%iMeqp2awimI{(&TNy10=Gcw#0eb1 zG3><$IE8~$%oHZ*J%slOJ*4k3e1j*Y>x;UfY;y9ClhOi)zc$k;~Kf!DQQPTaIV@khRw4cb&IwQv3(xc#;BG@&P1~1O@OhgM5TOrI2t`0LvL9n4dF9Fu$ab z-(!BwAi-S8Ai?}bXwfO2)%<=*WGjJR@;hC#XWi=gZfW3M4dx!A5=74RT`wa}{oL;C boA0Mjzi?aUmVA=o*b5hZg){ET!aDo~q3(sH delta 208 zcmZp;!`yIzd4d6NAp;Z$Ffin8v@K^A{P+L=e-JJJN`UC1$rqXRCnoT0V)0{Va+x|g zfV+uRmYIR!^XBW^oefOW`8Fr@Oy|@7&!EMyhv6100|O6G<8uWD21#iKhFP`@3@gML z7}SCGyyaqGm^+z)!G80Zc}o}>IX4$BxM{b!?S2*G}|E z^dyj($B2Q!0VvpD%jS_jIrh0c>n@k%mHe9customers_model->exists($customer)) - $customer['id'] = $this->customers_model->find_record_id($customer); + if ($this->customers_model->exists($customer)) + $customer['id'] = $this->customers_model->find_record_id($customer); $customer_id = $this->customers_model->add($customer); $appointment['id_users_customer'] = $customer_id; - $appointment['id'] = $this->appointments_model->add($appointment); + $appointment['id'] = $this->appointments_model->add($appointment); $appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']); $provider = $this->providers_model->get_row($appointment['id_users_provider']); diff --git a/src/application/controllers/backend_api.php b/src/application/controllers/backend_api.php index 51f335b3..ee8872f0 100644 --- a/src/application/controllers/backend_api.php +++ b/src/application/controllers/backend_api.php @@ -332,9 +332,9 @@ class Backend_api extends CI_Controller { */ public function ajax_disable_provider_sync() { try { - if (!isset($_POST['provider_id'])) { + if (!isset($_POST['provider_id'])) throw new Exception('Provider id not specified.'); - } + if ($this->privileges[PRIV_USERS]['edit'] == FALSE && $this->session->userdata('user_id') != $_POST['provider_id']) { @@ -1049,25 +1049,25 @@ class Backend_api extends CI_Controller { */ public function ajax_change_language() { try { - // Check if language exists in the available languages. - $found = false; - foreach($this->config->item('available_languages') as $lang) { - if ($lang == $_POST['language']) { - $found = true; - break; - } - } - - if (!$found) - throw new Exception('Translations for the given language does not exist (' . $_POST['language'] . ').'); - - $this->session->set_userdata('language', $_POST['language']); - $this->config->set_item('language', $_POST['language']); - - echo json_encode(AJAX_SUCCESS); + // Check if language exists in the available languages. + $found = false; + foreach($this->config->item('available_languages') as $lang) { + if ($lang == $_POST['language']) { + $found = true; + break; + } + } + + if (!$found) + throw new Exception('Translations for the given language does not exist (' . $_POST['language'] . ').'); + + $this->session->set_userdata('language', $_POST['language']); + $this->config->set_item('language', $_POST['language']); + + echo json_encode(AJAX_SUCCESS); } catch(Exception $exc) { - echo json_encode(array( + echo json_encode(array( 'exceptions' => array(exceptionToJavaScript($exc)) )); } @@ -1084,18 +1084,54 @@ class Backend_api extends CI_Controller { */ public function ajax_get_google_calendars() { try { - $this->load->library('google_sync'); - $this->load->model('providers_model'); - - $calendars = $this->google_sync->get_google_calendars($_POST['provider_id']); - echo json_encode($calendars); - + $this->load->library('google_sync'); + $this->load->model('providers_model'); + + if (!isset($_POST['provider_id'])) + throw new Exception('Provider id is required in order to fetch the google calendars.'); + + // Check if selected provider has sync enabled. + $google_sync = $this->providers_model->get_setting('google_sync', $_POST['provider_id']); + if ($google_sync) { + $google_token = json_decode($this->providers_model->get_setting('google_token', $_POST['provider_id'])); + $this->google_sync->refresh_token($google_token->refresh_token); + $calendars = $this->google_sync->get_google_calendars(); + echo json_encode($calendars); + } else { + echo json_encode(AJAX_FAILURE); + } } catch(Exception $exc) { - echo json_encode(array( - 'exceptions' => array(exceptionToJavaScript($exc)) - )); + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); } } + + /** + * Select a specific google calendar for a provider. + * + * All the appointments will be synced with this particular calendar. + * + * @param numeric $_POST['provider_id'] Provider record id. + * @param string $_POST['calendar_id'] Google calendar's id. + */ + public function ajax_select_google_calendar() { + try { + if ($this->privileges[PRIV_USERS]['edit'] == FALSE + && $this->session->userdata('user_id') != $_POST['provider_id']) { + throw new Exception('You do not have the required privileges for this task.'); + } + + $this->load->model('providers_model'); + $result = $this->providers_model->set_setting('google_calendar', $_POST['calendar_id'], $_POST['provider_id']); + echo json_encode(($result) ? AJAX_SUCCESS : AJAX_FAILURE); + + } catch (Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } } /* End of file backend_api.php */ diff --git a/src/application/controllers/google.php b/src/application/controllers/google.php index 1bb5f520..3ac1976b 100644 --- a/src/application/controllers/google.php +++ b/src/application/controllers/google.php @@ -43,30 +43,27 @@ class Google extends CI_Controller { */ public function oauth_callback() { if (isset($_GET['code'])) { - $this->load->library('Google_Sync'); - $token = $this->google_sync->authenticate($_GET['code']); + $this->load->library('Google_Sync'); + $token = $this->google_sync->authenticate($_GET['code']); // Store the token into the database for future reference. if (!isset($_SESSION)) { @session_start(); } - if (isset($_SESSION['oauth_provider_id'])) { - $this->load->model('providers_model'); - + if (isset($_SESSION['oauth_provider_id'])) { + $this->load->model('providers_model'); $this->providers_model->set_setting('google_sync', TRUE, - $_SESSION['oauth_provider_id']); - $this->providers_model->set_setting('google_token', $token, - $_SESSION['oauth_provider_id']); - $this->providers_model->set_setting('google_calendar', 'primary', - $_SESSION['oath_provider_id']); - - } else { - echo '

Sync provider id not specified!

'; - } - + $_SESSION['oauth_provider_id']); + $this->providers_model->set_setting('google_token', $token, + $_SESSION['oauth_provider_id']); + $this->providers_model->set_setting('google_calendar', 'primary', + $_SESSION['oauth_provider_id']); + } else { + echo '

Sync provider id not specified!

'; + } } else { - echo '

Authorization Failed!

'; + echo '

Authorization Failed!

'; } } diff --git a/src/application/language/english/translations_lang.php b/src/application/language/english/translations_lang.php index f735c1d9..d0913178 100644 --- a/src/application/language/english/translations_lang.php +++ b/src/application/language/english/translations_lang.php @@ -264,3 +264,6 @@ $lang['hour'] = 'Hour'; $lang['minute'] = 'Minute'; $lang['google_sync_completed'] = 'Google synchronization completed successfully!'; $lang['google_sync_failed'] = 'Google synchronization failed: Could not establish server connection.'; +$lang['select_google_calendar'] = 'Select Google Calendar'; +$lang['select_google_calendar_prompt'] = 'Select the calendar that you want to sync your appointments. If you do not want to select a specific calendar the default one will be used.'; +$lang['google_calendar_selected'] = 'Google calendar has been successfully selected!'; \ No newline at end of file diff --git a/src/application/language/german/translations_lang.php b/src/application/language/german/translations_lang.php index a45d5344..378332cb 100644 --- a/src/application/language/german/translations_lang.php +++ b/src/application/language/german/translations_lang.php @@ -263,4 +263,7 @@ $lang['time'] = 'Zeit'; $lang['hour'] = 'Uhr'; $lang['minute'] = 'Minute'; $lang['google_sync_completed'] = 'Google Synchronisation erfolgreich beendet!'; -$lang['google_sync_failed'] = 'Google Synchronisation fehlgeschlagen: Serververbindung konnte nicht hergestellt werden.'; \ No newline at end of file +$lang['google_sync_failed'] = 'Google Synchronisation fehlgeschlagen: Serververbindung konnte nicht hergestellt werden.'; +$lang['select_google_calendar'] = 'Wählen Sie Google Kalender'; +$lang['select_google_calendar_prompt'] = 'Wählen Sie den Kalender, den Sie Ihre Termine synchronisieren möchten. Wenn Sie nicht wollen, um einen bestimmten Kalender wählen den Standard, verwendet.'; +$lang['google_calendar_selected'] = 'Google-Kalender ausgewählt wurde erfolgreich!'; \ No newline at end of file diff --git a/src/application/language/greek/translations_lang.php b/src/application/language/greek/translations_lang.php index 1cb838e1..4dfa8ff4 100644 --- a/src/application/language/greek/translations_lang.php +++ b/src/application/language/greek/translations_lang.php @@ -263,4 +263,7 @@ $lang['time'] = 'Χρόνος'; $lang['hour'] = 'Ώρα'; $lang['minute'] = 'Λεπτά'; $lang['google_sync_completed'] = 'Ο συγχρονισμός με την Google ολοκληρώθηκε επιτυχώς!'; -$lang['google_sync_failed'] = 'Ο συγχρονισμός με την Google απέτυχε: Δεν μπόρεσε να πραγματοποιηθεί σύνδεση με τον server.'; \ No newline at end of file +$lang['google_sync_failed'] = 'Ο συγχρονισμός με την Google απέτυχε: Δεν μπόρεσε να πραγματοποιηθεί σύνδεση με τον server.'; +$lang['select_google_calendar'] = 'Επιλογή Ημερολογίου της Google'; +$lang['select_google_calendar_prompt'] = 'Επιλέξτε το ημερολόγιο στο οποίο θέλετε να συγχρονίζεται τα ραντεβού σας. Εάν δεν θελήσετε να επιλέξετε ένα συγκεκριμένο ημερολόγιο θα χρησιμοποιηθεί το προεπιλεγμένο.'; +$lang['google_calendar_selected'] = 'Το ημερολόγιο της Google επιλέχθηκε επιτυχώς!'; \ No newline at end of file diff --git a/src/application/libraries/google_sync.php b/src/application/libraries/google_sync.php index fce96097..7325aee1 100644 --- a/src/application/libraries/google_sync.php +++ b/src/application/libraries/google_sync.php @@ -13,7 +13,7 @@ require_once dirname(dirname(dirname(__FILE__))) . '/configuration.php'; * library. */ class Google_Sync { - private $CI; + private $CI; private $client; private $service; @@ -301,11 +301,19 @@ class Google_Sync { * The given user's token must already exist in db in order to get access to his * Google Calendar account. * - * @param numeric $provider_id The provider's record id. + * @param string $google_token The user's token will be used to grant access to google calendar. * @return array Returns an array with the available calendars. */ - public function get_google_calendars($provider_id) { - + 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; } } diff --git a/src/application/views/backend/calendar.php b/src/application/views/backend/calendar.php index 4102b081..084c80c8 100644 --- a/src/application/views/backend/calendar.php +++ b/src/application/views/backend/calendar.php @@ -315,7 +315,6 @@

lang->line('new_unavailable_title'); ?>

- - \ No newline at end of file + + + + + diff --git a/src/assets/js/backend_calendar.js b/src/assets/js/backend_calendar.js index fd609aad..cd58cfbf 100644 --- a/src/assets/js/backend_calendar.js +++ b/src/assets/js/backend_calendar.js @@ -708,8 +708,29 @@ var BackendCalendar = { $('#google-sync').prop('disabled', false); $('#select-filter-item option:selected').attr('google-sync', 'true'); - // Display the calendar selection dialog. - + // Display the calendar selection dialog. First we will get a list + // of the available user's calendars and then we will display a selection + // modal so the user can select the sync calendar. + var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_get_google_calendars'; + var postData = { + 'provider_id': $('#select-filter-item').val() + }; + $.post(postUrl, postData, function(response) { + /////////////////////////////////////////////////////////////////// + console.log('Get Available Google Calendars Response', response); + /////////////////////////////////////////////////////////////////// + + if (!GeneralFunctions.handleAjaxExceptions(response)) return; + + $('#google-calendar').empty(); + $.each(response, function() { + var option = ''; + $('#google-calendar').append(option); + }); + + $('#select-google-calendar').modal('show'); + + }, 'json'); } } }, 100); @@ -922,6 +943,32 @@ var BackendCalendar = { $('#manage-appointment').find('#customer-id, #first-name, #last-name, #email, ' + '#phone-number, #address, #city, #zip-code, #customer-notes').val(''); }); + + /** + * Event: Select Google Calendar "Click" + */ + $('#select-calendar').click(function() { + var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_select_google_calendar'; + var postData = { + 'provider_id': $('#select-filter-item').val(), + 'calendar_id': $('#google-calendar').val() + }; + $.post(postUrl, postData, function(response){ + /////////////////////////////////////////////////////////// + console.log('Select Google Calendar Response', response); + /////////////////////////////////////////////////////////// + if (!GeneralFunctions.handleAjaxExceptions(response)) return; + Backend.displayNotification(EALang['google_calendar_selected']); + $('#select-google-calendar').modal('hide'); + }); + }); + + /** + * Event: Close Google Calendar "Click" + */ + $('#close-calendar').click(function() { + $('#select-google-calendar').modal('hide'); + }); }, /** @@ -1087,7 +1134,7 @@ var BackendCalendar = { // on the calendar, even if the provider won't work on that day). $.each(response.unavailables, function(index, unavailable) { if (currDateStart.toString('dd/MM/yyyy') - === Date.parse(unavailable.start_datetime).toString('dd/MM/yyyy')) { + === Date.parse(unavailable.start_datetime).toString('dd/MM/yyyy')) { var unavailablePeriod = { 'title': EALang['unavailable'] + '
' + ((unavailable.notes.length > 30) ? unavailable.notes.substring(0, 30) + '...' @@ -1630,14 +1677,14 @@ var BackendCalendar = { console.log('Drop Unavailable Event Response:', response); if (response.exceptions) { - reponse.exceptions = GeneralFunctions.parseExceptions(response.exceptions); + response.exceptions = GeneralFunctions.parseExceptions(response.exceptions); GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE); $('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions)); return; } if (response.warnings) { - reponse.warnings = GeneralFunctions.parseExceptions(response.warnings); + response.warnings = GeneralFunctions.parseExceptions(response.warnings); GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE, GeneralFunctions.WARNINGS_MESSAGE); $('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings)); }