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>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2015-07-20 22:41:24 +03:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
$(document).ready(function () {
|
2015-04-26 16:28:52 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2020-09-07 10:53:39 +03:00
|
|
|
$('#add-to-google-calendar').on('click', function () {
|
2015-04-26 16:28:52 +03:00
|
|
|
gapi.client.setApiKey(GlobalVariables.googleApiKey);
|
2021-11-06 19:38:37 +03:00
|
|
|
gapi.auth.authorize(
|
|
|
|
{
|
|
|
|
client_id: GlobalVariables.googleClientId,
|
|
|
|
scope: GlobalVariables.googleApiScope,
|
|
|
|
immediate: false
|
|
|
|
},
|
|
|
|
handleAuthResult
|
|
|
|
);
|
2015-04-26 16:28:52 +03:00
|
|
|
});
|
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) {
|
2020-05-06 20:15:11 +03:00
|
|
|
throw new Error('Could not authorize user.');
|
2015-04-26 16:28:52 +03:00
|
|
|
}
|
2016-04-02 13:02:17 +03:00
|
|
|
|
2020-12-14 21:37:08 +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 providerData = GlobalVariables.providerData;
|
2016-04-02 13:02:17 +03:00
|
|
|
|
2020-12-14 21:37:08 +03:00
|
|
|
var appointmentData = GlobalVariables.appointmentData;
|
2015-04-26 16:28:52 +03:00
|
|
|
|
|
|
|
// 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: {
|
2020-12-14 21:37:08 +03:00
|
|
|
dateTime: moment.tz(appointmentData.start_datetime, providerData.timezone).format()
|
2015-04-26 16:28:52 +03:00
|
|
|
},
|
2016-05-14 13:16:11 +03:00
|
|
|
end: {
|
2020-12-14 21:37:08 +03:00
|
|
|
dateTime: moment.tz(appointmentData.end_datetime, providerData.timezone).format()
|
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,
|
2021-11-06 19:38:37 +03:00
|
|
|
displayName:
|
|
|
|
GlobalVariables.providerData.first_name + ' ' + GlobalVariables.providerData.last_name
|
2015-04-26 16:28:52 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
gapi.client.load('calendar', 'v3', function () {
|
2015-04-26 16:28:52 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
request.execute(function (response) {
|
2016-05-14 13:16:11 +03:00
|
|
|
if (response.error) {
|
2020-05-06 20:15:11 +03:00
|
|
|
throw new Error('Could not add the event to Google Calendar.');
|
2015-04-26 16:28:52 +03:00
|
|
|
}
|
2016-05-14 13:16:11 +03:00
|
|
|
|
|
|
|
$('#success-frame').append(
|
2020-05-07 19:47:14 +03:00
|
|
|
$('<br/>'),
|
|
|
|
$('<div/>', {
|
|
|
|
'class': 'alert alert-success col-xs-12',
|
|
|
|
'html': [
|
|
|
|
$('<h4/>', {
|
2021-12-13 09:52:09 +03:00
|
|
|
'text': App.Lang.success
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<p/>', {
|
2021-12-13 09:52:09 +03:00
|
|
|
'text': App.Lang.appointment_added_to_google_calendar
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<a/>', {
|
|
|
|
'href': response.htmlLink,
|
2021-12-13 09:52:09 +03:00
|
|
|
'text': App.Lang.view_appointment_in_google_calendar
|
2020-05-07 19:47:14 +03:00
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
2016-05-14 13:16:11 +03:00
|
|
|
);
|
|
|
|
$('#add-to-google-calendar').hide();
|
2015-04-26 16:28:52 +03:00
|
|
|
});
|
|
|
|
});
|
2020-05-06 20:15:11 +03:00
|
|
|
} catch (error) {
|
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(
|
2020-05-07 19:47:14 +03:00
|
|
|
$('<br/>'),
|
|
|
|
$('<div/>', {
|
|
|
|
'class': 'alert alert-danger col-xs-12',
|
|
|
|
'html': [
|
|
|
|
$('<h4/>', {
|
2021-12-13 09:52:09 +03:00
|
|
|
'text': App.Lang.oops_something_went_wrong
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<p/>', {
|
2021-12-13 09:52:09 +03:00
|
|
|
'text': App.Lang.could_not_add_to_google_calendar
|
2020-05-07 19:47:14 +03:00
|
|
|
}),
|
|
|
|
$('<pre/>', {
|
|
|
|
'text': error.message
|
2021-11-06 19:38:37 +03:00
|
|
|
})
|
2020-05-07 19:47:14 +03:00
|
|
|
]
|
|
|
|
})
|
|
|
|
);
|
2015-04-26 16:28:52 +03:00
|
|
|
}
|
2016-04-02 13:02:17 +03:00
|
|
|
}
|
|
|
|
});
|