Here's how I was able to make Google sync occur automatically so that I never accidentally double-book. This may not work for servers with heavy traffic, but I imagine it will work just fine for most people. The way it works is that anytime someone tries to book an appointment google sync runs in the background before the list of available times is displayed.
Change 1
in application/controllers/google.php comment out lines 82 & 83 so that no one needs to be logged into the system for Google sync to occur. It should look like this when you are done:
//$this->load->library('session');
//if ($this->session->userdata('user_id') == FALSE) return;
note: this is the change I understood the least. There may be a good reason to be sure the provider is logged in before you sync their calendar, I just can't think of what it is.
Change 2
Next you need to tell the application to sync once someone chooses their service provider on the booking screen
so in assets/js/frontend_book.js around line 103 change
$('#select-provider').change(function() {
FrontendBook.getAvailableHours(Date.today().toString('dd-MM-yyyy'));
FrontendBook.updateConfirmFrame();
$('#select-provider').change(function() {
FrontendBook.googleSync(); // this will make the google calendar for the newly selected provider sync
FrontendBook.getAvailableHours(Date.today().toString('dd-MM-yyyy'));
FrontendBook.updateConfirmFrame();
Change 3
next you need to tell the application to do a sync upon loading in case the client does not change the default provider
a few lines down in the same file assets/js/frontend_book.js add this line around line 130:
it is currently
FrontendBook.getAvailableHours($('#select-date').val());
FrontendBook.updateConfirmFrame();
change it to
FrontendBook.googleSync(); // this will sync the calendar for the default provider
FrontendBook.getAvailableHours($('#select-date').val());
FrontendBook.updateConfirmFrame();
Change 4
finally you need to add the googleSync function.
Later in the same file assets/js/frontend_book.js add the function googleSync in the list of parameters to the initialize method. It can be anywhere, I guess. I added it at line 286 right above the call to getAvailableHours. It should look like this when you are done.
provider_id= $('#select-provider').val();
getUrl = GlobalVariables.baseUrl + 'google/sync/' + provider_id;
jQuery.get(getUrl,provider_id, console.log('Google sync successful'),'json');
/**
* This function makes an ajax call and returns the available
* hours for the selected service, provider and date.
*
* @param {string} selDate The selected date of which the available
* hours we need to receive.
*/
getAvailableHours: function(selDate) {
coding note: I'm pretty sure including provider_id in the parameter list to jQuery.get is unnecessary - I just typed it that way and things work so I'm leaving it
Change 5
Summary & Limitations
Now whenever a potential client accesses your Easy!Appointments website the provider's calendar will be automatically updated.
Some limitations:
- I don't do any checking to be sure that the provider has enabled google sync. I just assume that everyone does. The sync function in application/controllers/google.php does check, but if Google sync is not enabled it throws an exception.
- This is not very efficient for heavily-trafficked sites (you will call sync way more often than needed), but for a less-visited site it may be more efficient than a recurring cron job. I don't think this will matter much as the sync seems very quick.
- The relevant google limitations are 5 requests per second per account and one million requests per day. Those are large enough limits that I don't think you need to worry.
- Also, I suppose I might have introduced some kind of security vulnerability with the first change (not checking for user being logged in), but I can't see what it might be. If you see a way to be nefarious please let me know.
I think those are all the changes I made. If you try it and it doesn't work let me know and I'll see if I changed anything else.