2016-07-17 15:23:18 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2018-03-27 10:23:09 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2018, Alex Tselegidis
|
2016-07-17 15:23:18 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backend Calendar Google Sync
|
|
|
|
*
|
2018-01-23 12:08:37 +03:00
|
|
|
* This module implements the Google Calendar sync operations.
|
2016-07-17 15:23:18 +03:00
|
|
|
*
|
|
|
|
* @module BackendCalendarGoogleSync
|
|
|
|
*/
|
|
|
|
window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {};
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
(function (exports) {
|
2016-07-17 15:23:18 +03:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
2018-01-23 12:08:37 +03:00
|
|
|
* Bind event handlers.
|
2016-07-17 15:23:18 +03:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
$('#enable-sync').click(function () {
|
2016-07-17 15:23:18 +03:00
|
|
|
if ($('#enable-sync').hasClass('enabled') === false) {
|
|
|
|
// Enable synchronization for selected provider.
|
|
|
|
var authUrl = GlobalVariables.baseUrl + '/index.php/google/oauth/'
|
2018-01-23 12:08:37 +03:00
|
|
|
+ $('#select-filter-item').val();
|
2016-07-17 15:23:18 +03:00
|
|
|
|
|
|
|
var redirectUrl = GlobalVariables.baseUrl + '/index.php/google/oauth_callback';
|
|
|
|
|
|
|
|
var windowHandle = window.open(authUrl, 'Authorize Easy!Appointments',
|
2018-01-23 12:08:37 +03:00
|
|
|
'width=800, height=600');
|
2016-07-17 15:23:18 +03:00
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
var authInterval = window.setInterval(function () {
|
2016-07-17 15:23:18 +03:00
|
|
|
// 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.
|
2017-03-10 11:23:31 +03:00
|
|
|
try {
|
|
|
|
if (windowHandle.document !== undefined) {
|
2018-01-23 12:08:37 +03:00
|
|
|
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);
|
|
|
|
}
|
2016-07-17 15:23:18 +03:00
|
|
|
}
|
2018-01-23 12:08:37 +03:00
|
|
|
} catch (Error) {
|
2017-03-10 11:23:31 +03:00
|
|
|
// Accessing the document object before the window is loaded throws an error, but
|
|
|
|
// it will only happen during the initialization of the window. Attaching "load"
|
|
|
|
// event handling is not possible due to CORS restrictions.
|
|
|
|
}
|
2016-07-17 15:23:18 +03:00
|
|
|
}, 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.
|
2018-01-23 12:08:37 +03:00
|
|
|
$.each(GlobalVariables.availableProviders, function (index, provider) {
|
2017-11-02 16:19:54 +03:00
|
|
|
if (provider.id == $('#select-filter-item').val()) {
|
|
|
|
provider.settings.google_sync = '0';
|
|
|
|
provider.settings.google_token = null;
|
2016-07-17 15:23:18 +03:00
|
|
|
|
2017-11-02 16:19:54 +03:00
|
|
|
_disableProviderSync(provider.id);
|
2016-07-17 15:23:18 +03:00
|
|
|
|
|
|
|
$('#enable-sync').removeClass('btn-danger enabled');
|
2017-09-11 17:09:15 +03:00
|
|
|
$('#enable-sync span:eq(1)').text(EALang.enable_sync);
|
2016-07-17 15:23:18 +03:00
|
|
|
$('#google-sync').prop('disabled', true);
|
|
|
|
$('#select-filter-item option:selected').attr('google-sync', 'false');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Select Google Calendar "Click"
|
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
$('#select-calendar').click(function () {
|
2016-07-17 15:23:18 +03:00
|
|
|
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()
|
|
|
|
};
|
2018-01-23 12:08:37 +03:00
|
|
|
$.post(postUrl, postData, function (response) {
|
2016-07-17 15:23:18 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.google_calendar_selected);
|
2016-07-17 15:23:18 +03:00
|
|
|
$('#select-google-calendar').modal('hide');
|
|
|
|
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Close Google Calendar "Click"
|
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
$('#close-calendar').click(function () {
|
2016-07-17 15:23:18 +03:00
|
|
|
$('#select-google-calendar').modal('hide');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Google Sync Button "Click"
|
|
|
|
*
|
|
|
|
* Trigger the synchronization algorithm.
|
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
$('#google-sync').click(function () {
|
2016-07-17 15:23:18 +03:00
|
|
|
var url = GlobalVariables.baseUrl + '/index.php/google/sync/' + $('#select-filter-item').val();
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json'
|
|
|
|
})
|
2018-01-23 12:08:37 +03:00
|
|
|
.done(function (response) {
|
2016-07-17 15:23:18 +03:00
|
|
|
if (response.exceptions) {
|
|
|
|
response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
|
|
|
|
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE,
|
2018-01-23 12:08:37 +03:00
|
|
|
GeneralFunctions.EXCEPTIONS_MESSAGE);
|
2016-07-17 15:23:18 +03:00
|
|
|
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.warnings) {
|
|
|
|
response.warnings = GeneralFunctions.parseExceptions(response.warnings);
|
|
|
|
GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE,
|
2018-01-23 12:08:37 +03:00
|
|
|
GeneralFunctions.WARNINGS_MESSAGE);
|
2016-07-17 15:23:18 +03:00
|
|
|
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings));
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.google_sync_completed);
|
2016-07-17 15:23:18 +03:00
|
|
|
$('#reload-appointments').trigger('click');
|
|
|
|
})
|
2018-01-23 12:08:37 +03:00
|
|
|
.fail(function (jqXHR, textStatus, errorThrown) {
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.google_sync_failed);
|
2016-07-17 15:23:18 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
};
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
$.post(postUrl, postData, function (response) {
|
2016-07-17 15:23:18 +03:00
|
|
|
if (response.exceptions) {
|
|
|
|
response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
|
|
|
|
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE);
|
|
|
|
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
|
|
|
|
}
|
|
|
|
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
exports.initialize = function () {
|
|
|
|
_bindEventHandlers();
|
2016-07-17 15:23:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
})(window.BackendCalendarGoogleSync);
|