2015-07-20 22:41:24 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
2016-04-02 13:02:17 +03:00
|
|
|
*
|
2015-07-20 22:41:24 +03:00
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, Alex Tselegidis
|
2016-04-02 13:02:17 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
2015-07-20 22:41:24 +03:00
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2015-04-26 16:28:52 +03:00
|
|
|
$(document).ready(function() {
|
|
|
|
/**
|
|
|
|
* Event: Add Appointment to Google Calendar "Click"
|
2016-04-02 13:02:17 +03:00
|
|
|
*
|
2016-05-14 13:16:11 +03:00
|
|
|
* This event handler adds the appointment to the users Google Calendar Account. The event is going to
|
|
|
|
* be added to the "primary" calendar. In order to use the API the javascript client library provided by
|
|
|
|
* Google is necessary.
|
2015-04-26 16:28:52 +03:00
|
|
|
*/
|
|
|
|
$('#add-to-google-calendar').click(function() {
|
|
|
|
gapi.client.setApiKey(GlobalVariables.googleApiKey);
|
|
|
|
gapi.auth.authorize({
|
2016-05-14 13:16:11 +03:00
|
|
|
client_id: GlobalVariables.googleClientId,
|
|
|
|
scope: GlobalVariables.googleApiScope,
|
|
|
|
immediate: false
|
2015-04-26 16:28:52 +03:00
|
|
|
}, handleAuthResult);
|
|
|
|
});
|
2016-04-02 13:02:17 +03:00
|
|
|
|
2015-04-26 16:28:52 +03:00
|
|
|
/**
|
2016-05-14 13:16:11 +03:00
|
|
|
* Handle Authorization Result
|
2016-04-02 13:02:17 +03:00
|
|
|
*
|
2016-05-14 13:16:11 +03:00
|
|
|
* This method handles the authorization result. If the user granted access to his data, then the
|
|
|
|
* appointment is going to be added to his calendar.
|
|
|
|
*
|
|
|
|
* @param {Boolean} authResult The user's authorization result.
|
2015-04-26 16:28:52 +03:00
|
|
|
*/
|
|
|
|
function handleAuthResult(authResult) {
|
|
|
|
try {
|
|
|
|
if (authResult.error) {
|
|
|
|
throw 'Could not authorize user.';
|
|
|
|
}
|
2016-04-02 13:02:17 +03:00
|
|
|
|
2015-04-26 16:28:52 +03:00
|
|
|
// The user has granted access, add the appointment to his calendar.
|
|
|
|
// Before making the event.insert request the the event resource data
|
|
|
|
// must be prepared.
|
|
|
|
var appointmentData = GlobalVariables.appointmentData;
|
2016-04-02 13:02:17 +03:00
|
|
|
|
2017-11-02 16:19:54 +03:00
|
|
|
appointmentData.start_datetime = GeneralFunctions.ISODateString(
|
|
|
|
Date.parseExact(appointmentData.start_datetime,
|
2015-04-26 16:28:52 +03:00
|
|
|
'yyyy-MM-dd HH:mm:ss'));
|
2017-11-02 16:19:54 +03:00
|
|
|
appointmentData.end_datetime = GeneralFunctions.ISODateString(
|
|
|
|
Date.parseExact(appointmentData.end_datetime,
|
2015-04-26 16:28:52 +03:00
|
|
|
'yyyy-MM-dd HH:mm:ss'));
|
|
|
|
|
|
|
|
// Create a valid Google Calendar API resource for the new event.
|
|
|
|
var resource = {
|
2017-11-02 16:19:54 +03:00
|
|
|
summary: GlobalVariables.serviceData.name,
|
2016-05-14 13:16:11 +03:00
|
|
|
location: GlobalVariables.companyName,
|
|
|
|
start: {
|
2017-11-02 16:19:54 +03:00
|
|
|
dateTime: appointmentData.start_datetime
|
2015-04-26 16:28:52 +03:00
|
|
|
},
|
2016-05-14 13:16:11 +03:00
|
|
|
end: {
|
2017-11-02 16:19:54 +03:00
|
|
|
dateTime: appointmentData.end_datetime
|
2015-04-26 16:28:52 +03:00
|
|
|
},
|
2016-05-14 13:16:11 +03:00
|
|
|
attendees: [
|
2015-04-26 16:28:52 +03:00
|
|
|
{
|
2017-11-02 16:19:54 +03:00
|
|
|
email: GlobalVariables.providerData.email,
|
|
|
|
displayName: GlobalVariables.providerData.first_name + ' '
|
|
|
|
+ GlobalVariables.providerData.last_name
|
2015-04-26 16:28:52 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
gapi.client.load('calendar', 'v3', function() {
|
|
|
|
var request = gapi.client.calendar.events.insert({
|
2016-05-14 13:16:11 +03:00
|
|
|
calendarId: 'primary',
|
|
|
|
resource: resource
|
2015-04-26 16:28:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
request.execute(function(response) {
|
2016-05-14 13:16:11 +03:00
|
|
|
if (response.error) {
|
2015-04-26 16:28:52 +03:00
|
|
|
throw 'Could not add the event to Google Calendar.';
|
|
|
|
}
|
2016-05-14 13:16:11 +03:00
|
|
|
|
|
|
|
$('#success-frame').append(
|
|
|
|
'<br><br>' +
|
|
|
|
'<div class="alert alert-success col-xs-12">' +
|
2017-09-11 17:09:15 +03:00
|
|
|
'<h4>' + EALang.success + '</h4>' +
|
2016-05-14 13:16:11 +03:00
|
|
|
'<p>' +
|
2017-09-11 17:09:15 +03:00
|
|
|
EALang.appointment_added_to_google_calendar +
|
2016-05-14 13:16:11 +03:00
|
|
|
'<br>' +
|
|
|
|
'<a href="' + response.htmlLink + '" target="_blank">' +
|
2017-09-11 17:09:15 +03:00
|
|
|
EALang.view_appointment_in_google_calendar +
|
2016-05-14 13:16:11 +03:00
|
|
|
'</a>' +
|
|
|
|
'</p>' +
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
$('#add-to-google-calendar').hide();
|
2015-04-26 16:28:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch(exc) {
|
2016-05-14 13:16:11 +03:00
|
|
|
// The user denied access or something else happened, display corresponding message on the screen.
|
2015-04-26 16:28:52 +03:00
|
|
|
$('#success-frame').append(
|
2016-04-02 13:02:17 +03:00
|
|
|
'<div class="alert alert-danger col-xs-12">' +
|
2017-09-11 17:09:15 +03:00
|
|
|
'<h4>' + EALang.oops_something_went_wrong + '</h4>' +
|
2016-04-02 13:02:17 +03:00
|
|
|
'<p>' +
|
2017-09-11 17:09:15 +03:00
|
|
|
EALang.could_not_add_to_google_calendar +
|
2015-04-26 16:28:52 +03:00
|
|
|
'<pre>' + exc + '</pre>' +
|
|
|
|
'</p>' +
|
|
|
|
'</div>');
|
|
|
|
}
|
2016-04-02 13:02:17 +03:00
|
|
|
}
|
|
|
|
});
|