diff --git a/doc/translations.xls b/doc/translations.xls index 27b5869e..5fffa7d8 100644 Binary files a/doc/translations.xls and b/doc/translations.xls differ diff --git a/src/application/controllers/appointments.php b/src/application/controllers/appointments.php index 564a96e7..93e67e1c 100644 --- a/src/application/controllers/appointments.php +++ b/src/application/controllers/appointments.php @@ -100,13 +100,13 @@ class Appointments extends CI_Controller { $appointment = $post_data['appointment']; $customer = $post_data['customer']; - if ($this->customers_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)); }