From 9c10b29f465b2513d52fad6c681d767cde036242 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sun, 26 Apr 2015 15:28:52 +0200 Subject: [PATCH] moved book_success.php javascript code to frontend_book_success.js --- .../views/appointments/book_success.php | 116 +----------------- src/assets/js/frontend_book_success.js | 111 +++++++++++++++++ 2 files changed, 115 insertions(+), 112 deletions(-) create mode 100644 src/assets/js/frontend_book_success.js diff --git a/src/application/views/appointments/book_success.php b/src/application/views/appointments/book_success.php index 5c1637c4..6fadd9ce 100644 --- a/src/application/views/appointments/book_success.php +++ b/src/application/views/appointments/book_success.php @@ -102,120 +102,12 @@ }; var EALang = lang->language); ?>; - - $(document).ready(function() { - /** - * Event: Add Appointment to Google Calendar "Click" - * - * 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. - */ - $('#add-to-google-calendar').click(function() { - gapi.client.setApiKey(GlobalVariables.googleApiKey); - gapi.auth.authorize({ - 'client_id' : GlobalVariables.googleClientId, - 'scope' : GlobalVariables.googleApiScope, - 'immediate' : false - }, handleAuthResult); - }); - - /** - * 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 {bool} authResult The user's auth result. - */ - function handleAuthResult(authResult) { - try { - /////////////////////////////////////////////////////////// - console.log('Google Authorization Result: ', authResult); - /////////////////////////////////////////////////////////// - - if (authResult.error) { - throw 'Could not authorize user.'; - } - - // 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; - - appointmentData['start_datetime'] = GeneralFunctions.ISODateString( - Date.parseExact(appointmentData['start_datetime'], - 'yyyy-MM-dd HH:mm:ss')); - appointmentData['end_datetime'] = GeneralFunctions.ISODateString( - Date.parseExact(appointmentData['end_datetime'], - 'yyyy-MM-dd HH:mm:ss')); - - // Create a valid Google Calendar API resource for the new event. - var resource = { - 'summary' : GlobalVariables.serviceData['name'], - 'location' : GlobalVariables.companyName, - 'start' : { - 'dateTime': appointmentData['start_datetime'] - }, - 'end' : { - 'dateTime': appointmentData['end_datetime'] - }, - 'attendees' : [ - { - 'email' : GlobalVariables.providerData['email'], - 'displayName' : GlobalVariables.providerData['first_name'] + ' ' - + GlobalVariables.providerData['last_name'] - } - ] - }; - - gapi.client.load('calendar', 'v3', function() { - var request = gapi.client.calendar.events.insert({ - 'calendarId' : 'primary', - 'resource' : resource - }); - - request.execute(function(response) { - ///////////////////////////////////////////////// - console.log('Google API Response:', response); - ///////////////////////////////////////////////// - - if (!response.error) { - $('#success-frame').append( - '

' + - '
' + - '

' + EALang['success'] + '

' + - '

' + - EALang['appointment_added_to_google_calendar'] + - '
' + - '' + - EALang['view_appointment_in_google_calendar'] + - '' + - '

' + - '
' - ); - $('#add-to-google-calendar').hide(); - } else { - throw 'Could not add the event to Google Calendar.'; - } - }); - }); - } catch(exc) { - // The user denied access or something else happened, display - // corresponding message on the screen. - $('#success-frame').append( - '
' + - '

' + EALang['oops_something_went_wrong'] + '

' + - '

' + - EALang['could_not_add_to_google_calendar'] + - '

' + exc + '
' + - '

' + - '
'); - console.log('Add To Google Calendar Exception', exc); - } - } - }); + + diff --git a/src/assets/js/frontend_book_success.js b/src/assets/js/frontend_book_success.js new file mode 100644 index 00000000..ff04328c --- /dev/null +++ b/src/assets/js/frontend_book_success.js @@ -0,0 +1,111 @@ +$(document).ready(function() { + /** + * Event: Add Appointment to Google Calendar "Click" + * + * 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. + */ + $('#add-to-google-calendar').click(function() { + gapi.client.setApiKey(GlobalVariables.googleApiKey); + gapi.auth.authorize({ + 'client_id' : GlobalVariables.googleClientId, + 'scope' : GlobalVariables.googleApiScope, + 'immediate' : false + }, handleAuthResult); + }); + + /** + * 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 {bool} authResult The user's auth result. + */ + function handleAuthResult(authResult) { + try { + /////////////////////////////////////////////////////////// + console.log('Google Authorization Result: ', authResult); + /////////////////////////////////////////////////////////// + + if (authResult.error) { + throw 'Could not authorize user.'; + } + + // 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; + + appointmentData['start_datetime'] = GeneralFunctions.ISODateString( + Date.parseExact(appointmentData['start_datetime'], + 'yyyy-MM-dd HH:mm:ss')); + appointmentData['end_datetime'] = GeneralFunctions.ISODateString( + Date.parseExact(appointmentData['end_datetime'], + 'yyyy-MM-dd HH:mm:ss')); + + // Create a valid Google Calendar API resource for the new event. + var resource = { + 'summary' : GlobalVariables.serviceData['name'], + 'location' : GlobalVariables.companyName, + 'start' : { + 'dateTime': appointmentData['start_datetime'] + }, + 'end' : { + 'dateTime': appointmentData['end_datetime'] + }, + 'attendees' : [ + { + 'email' : GlobalVariables.providerData['email'], + 'displayName' : GlobalVariables.providerData['first_name'] + ' ' + + GlobalVariables.providerData['last_name'] + } + ] + }; + + gapi.client.load('calendar', 'v3', function() { + var request = gapi.client.calendar.events.insert({ + 'calendarId' : 'primary', + 'resource' : resource + }); + + request.execute(function(response) { + ///////////////////////////////////////////////// + console.log('Google API Response:', response); + ///////////////////////////////////////////////// + + if (!response.error) { + $('#success-frame').append( + '

' + + '
' + + '

' + EALang['success'] + '

' + + '

' + + EALang['appointment_added_to_google_calendar'] + + '
' + + '' + + EALang['view_appointment_in_google_calendar'] + + '' + + '

' + + '
' + ); + $('#add-to-google-calendar').hide(); + } else { + throw 'Could not add the event to Google Calendar.'; + } + }); + }); + } catch(exc) { + // The user denied access or something else happened, display + // corresponding message on the screen. + $('#success-frame').append( + '
' + + '

' + EALang['oops_something_went_wrong'] + '

' + + '

' + + EALang['could_not_add_to_google_calendar'] + + '

' + exc + '
' + + '

' + + '
'); + console.log('Add To Google Calendar Exception', exc); + } + } +}); \ No newline at end of file