Added calendar selection when the user enables the google calendar synchronization for a provider.

This commit is contained in:
alextselegidis@gmail.com 2013-12-26 00:57:59 +00:00
parent 05416823f6
commit de29ef1ef4
10 changed files with 188 additions and 60 deletions

Binary file not shown.

View file

@ -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']);

View file

@ -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 */

View file

@ -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 '<h1>Sync provider id not specified!</h1>';
}
$_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 '<h1>Sync provider id not specified!</h1>';
}
} else {
echo '<h1>Authorization Failed!</h1>';
echo '<h1>Authorization Failed!</h1>';
}
}

View file

@ -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!';

View file

@ -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.';
$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!';

View file

@ -263,4 +263,7 @@ $lang['time'] = 'Χρόνος';
$lang['hour'] = 'Ώρα';
$lang['minute'] = 'Λεπτά';
$lang['google_sync_completed'] = 'Ο συγχρονισμός με την Google ολοκληρώθηκε επιτυχώς!';
$lang['google_sync_failed'] = 'Ο συγχρονισμός με την Google απέτυχε: Δεν μπόρεσε να πραγματοποιηθεί σύνδεση με τον server.';
$lang['google_sync_failed'] = 'Ο συγχρονισμός με την Google απέτυχε: Δεν μπόρεσε να πραγματοποιηθεί σύνδεση με τον server.';
$lang['select_google_calendar'] = 'Επιλογή Ημερολογίου της Google';
$lang['select_google_calendar_prompt'] = 'Επιλέξτε το ημερολόγιο στο οποίο θέλετε να συγχρονίζεται τα ραντεβού σας. Εάν δεν θελήσετε να επιλέξετε ένα συγκεκριμένο ημερολόγιο θα χρησιμοποιηθεί το προεπιλεγμένο.';
$lang['google_calendar_selected'] = 'Το ημερολόγιο της Google επιλέχθηκε επιτυχώς!';

View file

@ -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;
}
}

View file

@ -315,7 +315,6 @@
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h3><?php echo $this->lang->line('new_unavailable_title'); ?></h3>
</div>
<div class="modal-body">
@ -363,4 +362,36 @@
<?php echo $this->lang->line('cancel'); ?>
</button>
</div>
</div>
</div>
<?php
// --------------------------------------------------------------------
//
// SELECT GOOGLE CALENDAR
//
// --------------------------------------------------------------------
?>
<div id="select-google-calendar" class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h3><?php echo $this->lang->line('select_google_calendar'); ?></h3>
</div>
<div class="modal-body">
<p>
<?php echo $this->lang->line('select_google_calendar_prompt'); ?>
</p>
<select id="google-calendar"></select>
</div>
<div class="modal-footer">
<button id="select-calendar" class="btn btn-primary">
<?php echo $this->lang->line('select'); ?>
</button>
<button id="close-calendar" class="btn">
<?php echo $this->lang->line('close'); ?>
</button>
</div>
</div>

View file

@ -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 = '<option value="' + this.id + '">' + this.summary + '</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'] + ' <br><small>' + ((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));
}