mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-25 17:33:19 +03:00
Moved google-sync related code to a new javascript file.
This commit is contained in:
parent
a9edbda7a7
commit
8649983c6f
2 changed files with 206 additions and 167 deletions
|
@ -34,43 +34,6 @@ window.BackendCalendar = window.BackendCalendar || {};
|
|||
function _bindEventHandlers() {
|
||||
|
||||
var $calendarPage = $('#calendar-page');
|
||||
|
||||
/**
|
||||
* Event: Google Sync Button "Click"
|
||||
*
|
||||
* Trigger the synchronization algorithm.
|
||||
*/
|
||||
$('#google-sync').click(function() {
|
||||
var url = GlobalVariables.baseUrl + '/index.php/google/sync/' + $('#select-filter-item').val();
|
||||
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: 'GET',
|
||||
dataType: 'json'
|
||||
})
|
||||
.done(function(response) {
|
||||
if (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) {
|
||||
response.warnings = GeneralFunctions.parseExceptions(response.warnings);
|
||||
GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE,
|
||||
GeneralFunctions.WARNINGS_MESSAGE);
|
||||
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings));
|
||||
}
|
||||
|
||||
Backend.displayNotification(EALang['google_sync_completed']);
|
||||
$('#reload-appointments').trigger('click');
|
||||
})
|
||||
.fail(function(jqXHR, textStatus, errorThrown) {
|
||||
Backend.displayNotification(EALang['google_sync_failed']);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Reload Button "Click"
|
||||
|
@ -418,84 +381,7 @@ window.BackendCalendar = window.BackendCalendar || {};
|
|||
$('#manage-unavailable').modal('hide');
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Enable - Disable Synchronization Button "Click"
|
||||
*
|
||||
* When the user clicks on the "Enable Sync" button, a popup should appear
|
||||
* that is going to follow the web server authorization flow of OAuth.
|
||||
*/
|
||||
$('#enable-sync').click(function() {
|
||||
if ($('#enable-sync').hasClass('enabled') === false) {
|
||||
// Enable synchronization for selected provider.
|
||||
var authUrl = GlobalVariables.baseUrl + '/index.php/google/oauth/'
|
||||
+ $('#select-filter-item').val();
|
||||
|
||||
var redirectUrl = GlobalVariables.baseUrl + '/index.php/google/oauth_callback';
|
||||
|
||||
var windowHandle = window.open(authUrl, 'Authorize Easy!Appointments',
|
||||
'width=800, height=600');
|
||||
|
||||
var authInterval = window.setInterval(function() {
|
||||
// When the browser redirects to the google user consent page the "window.document" variable
|
||||
// becomes "undefined" and when it comes back to the redirect URL it changes back. So check
|
||||
// whether the variable is undefined to avoid javascript errors.
|
||||
if (windowHandle.document !== undefined) {
|
||||
if (windowHandle.document.URL.indexOf(redirectUrl) !== -1) {
|
||||
// The user has granted access to his data.
|
||||
windowHandle.close();
|
||||
window.clearInterval(authInterval);
|
||||
$('#enable-sync').addClass('btn-danger enabled');
|
||||
$('#enable-sync span:eq(1)').text(EALang['disable_sync']);
|
||||
$('#google-sync').prop('disabled', false);
|
||||
$('#select-filter-item option:selected').attr('google-sync', 'true');
|
||||
|
||||
// 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 + '/index.php/backend_api/ajax_get_google_calendars';
|
||||
var postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
provider_id: $('#select-filter-item').val()
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(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').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
|
||||
} else {
|
||||
// Disable synchronization for selected provider.
|
||||
// Update page elements and make an AJAX call to remove the google sync setting of the
|
||||
// selected provider.
|
||||
$.each(GlobalVariables.availableProviders, function(index, provider) {
|
||||
if (provider['id'] == $('#select-filter-item').val()) {
|
||||
provider['settings']['google_sync'] = '0';
|
||||
provider['settings']['google_token'] = null;
|
||||
|
||||
_disableProviderSync(provider['id']);
|
||||
|
||||
$('#enable-sync').removeClass('btn-danger enabled');
|
||||
$('#enable-sync span:eq(1)').text(EALang['enable_sync']);
|
||||
$('#google-sync').prop('disabled', true);
|
||||
$('#select-filter-item option:selected').attr('google-sync', 'false');
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Event: Insert Appointment Button "Click"
|
||||
|
@ -727,32 +613,6 @@ window.BackendCalendar = window.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 + '/index.php/backend_api/ajax_select_google_calendar';
|
||||
var postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
provider_id: $('#select-filter-item').val(),
|
||||
calendar_id: $('#google-calendar').val()
|
||||
};
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
Backend.displayNotification(EALang['google_calendar_selected']);
|
||||
$('#select-google-calendar').modal('hide');
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Close Google Calendar "Click"
|
||||
*/
|
||||
$('#close-calendar').click(function() {
|
||||
$('#select-google-calendar').modal('hide');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -818,32 +678,6 @@ window.BackendCalendar = window.BackendCalendar || {};
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable Provider Sync
|
||||
*
|
||||
* This method disables the google synchronization for a specific provider.
|
||||
*
|
||||
* @param {Number} providerId The selected provider record ID.
|
||||
*/
|
||||
function _disableProviderSync(providerId) {
|
||||
// Make an ajax call to the server in order to disable the setting
|
||||
// from the database.
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_disable_provider_sync';
|
||||
var postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
provider_id: providerId
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (response.exceptions) {
|
||||
response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
|
||||
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE);
|
||||
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
|
||||
return;
|
||||
}
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset Apppointment Dialog
|
||||
*
|
||||
|
@ -1130,6 +964,8 @@ window.BackendCalendar = window.BackendCalendar || {};
|
|||
BackendCalendarDefaultView.initialize();
|
||||
}
|
||||
|
||||
BackendCalendarGoogleSync.initialize();
|
||||
|
||||
_bindEventHandlers(); // General event handling.
|
||||
};
|
||||
|
||||
|
|
203
src/assets/js/backend_calendar_google_sync.js
Normal file
203
src/assets/js/backend_calendar_google_sync.js
Normal file
|
@ -0,0 +1,203 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* Easy!Appointments - Open Source Web Scheduler
|
||||
*
|
||||
* @package EasyAppointments
|
||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
||||
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
||||
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
||||
* @link http://easyappointments.org
|
||||
* @since v1.2.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Backend Calendar Google Sync
|
||||
*
|
||||
* This module implements the Google Calendar sync operations.
|
||||
*
|
||||
* @module BackendCalendarGoogleSync
|
||||
*/
|
||||
window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {};
|
||||
|
||||
(function(exports) {
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Bind event handlers.
|
||||
*/
|
||||
function _bindEventHandlers() {
|
||||
/**
|
||||
* Event: Enable - Disable Synchronization Button "Click"
|
||||
*
|
||||
* When the user clicks on the "Enable Sync" button, a popup should appear
|
||||
* that is going to follow the web server authorization flow of OAuth.
|
||||
*/
|
||||
$('#enable-sync').click(function() {
|
||||
if ($('#enable-sync').hasClass('enabled') === false) {
|
||||
// Enable synchronization for selected provider.
|
||||
var authUrl = GlobalVariables.baseUrl + '/index.php/google/oauth/'
|
||||
+ $('#select-filter-item').val();
|
||||
|
||||
var redirectUrl = GlobalVariables.baseUrl + '/index.php/google/oauth_callback';
|
||||
|
||||
var windowHandle = window.open(authUrl, 'Authorize Easy!Appointments',
|
||||
'width=800, height=600');
|
||||
|
||||
var authInterval = window.setInterval(function() {
|
||||
// When the browser redirects to the google user consent page the "window.document" variable
|
||||
// becomes "undefined" and when it comes back to the redirect URL it changes back. So check
|
||||
// whether the variable is undefined to avoid javascript errors.
|
||||
if (windowHandle.document !== undefined) {
|
||||
if (windowHandle.document.URL.indexOf(redirectUrl) !== -1) {
|
||||
// The user has granted access to his data.
|
||||
windowHandle.close();
|
||||
window.clearInterval(authInterval);
|
||||
$('#enable-sync').addClass('btn-danger enabled');
|
||||
$('#enable-sync span:eq(1)').text(EALang['disable_sync']);
|
||||
$('#google-sync').prop('disabled', false);
|
||||
$('#select-filter-item option:selected').attr('google-sync', 'true');
|
||||
|
||||
// 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 + '/index.php/backend_api/ajax_get_google_calendars';
|
||||
var postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
provider_id: $('#select-filter-item').val()
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(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').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
|
||||
} else {
|
||||
// Disable synchronization for selected provider.
|
||||
// Update page elements and make an AJAX call to remove the google sync setting of the
|
||||
// selected provider.
|
||||
$.each(GlobalVariables.availableProviders, function(index, provider) {
|
||||
if (provider['id'] == $('#select-filter-item').val()) {
|
||||
provider['settings']['google_sync'] = '0';
|
||||
provider['settings']['google_token'] = null;
|
||||
|
||||
_disableProviderSync(provider['id']);
|
||||
|
||||
$('#enable-sync').removeClass('btn-danger enabled');
|
||||
$('#enable-sync span:eq(1)').text(EALang['enable_sync']);
|
||||
$('#google-sync').prop('disabled', true);
|
||||
$('#select-filter-item option:selected').attr('google-sync', 'false');
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Select Google Calendar "Click"
|
||||
*/
|
||||
$('#select-calendar').click(function() {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_select_google_calendar';
|
||||
var postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
provider_id: $('#select-filter-item').val(),
|
||||
calendar_id: $('#google-calendar').val()
|
||||
};
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
Backend.displayNotification(EALang['google_calendar_selected']);
|
||||
$('#select-google-calendar').modal('hide');
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Close Google Calendar "Click"
|
||||
*/
|
||||
$('#close-calendar').click(function() {
|
||||
$('#select-google-calendar').modal('hide');
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Google Sync Button "Click"
|
||||
*
|
||||
* Trigger the synchronization algorithm.
|
||||
*/
|
||||
$('#google-sync').click(function() {
|
||||
var url = GlobalVariables.baseUrl + '/index.php/google/sync/' + $('#select-filter-item').val();
|
||||
|
||||
$.ajax({
|
||||
url: url,
|
||||
type: 'GET',
|
||||
dataType: 'json'
|
||||
})
|
||||
.done(function(response) {
|
||||
if (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) {
|
||||
response.warnings = GeneralFunctions.parseExceptions(response.warnings);
|
||||
GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE,
|
||||
GeneralFunctions.WARNINGS_MESSAGE);
|
||||
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings));
|
||||
}
|
||||
|
||||
Backend.displayNotification(EALang['google_sync_completed']);
|
||||
$('#reload-appointments').trigger('click');
|
||||
})
|
||||
.fail(function(jqXHR, textStatus, errorThrown) {
|
||||
Backend.displayNotification(EALang['google_sync_failed']);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable Provider Sync
|
||||
*
|
||||
* This method disables the google synchronization for a specific provider.
|
||||
*
|
||||
* @param {Number} providerId The selected provider record ID.
|
||||
*/
|
||||
function _disableProviderSync(providerId) {
|
||||
// Make an ajax call to the server in order to disable the setting
|
||||
// from the database.
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_disable_provider_sync';
|
||||
var postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
provider_id: providerId
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (response.exceptions) {
|
||||
response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
|
||||
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE);
|
||||
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
|
||||
return;
|
||||
}
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
}
|
||||
|
||||
|
||||
exports.initialize = function() {
|
||||
_bindEventHandlers();
|
||||
};
|
||||
|
||||
})(window.BackendCalendarGoogleSync);
|
Loading…
Reference in a new issue