forked from mirrors/easyappointments
Apply the new add-to-google button for 1.5
This commit is contained in:
parent
ce028607c2
commit
2f67141d75
4 changed files with 68 additions and 199 deletions
|
@ -30,6 +30,8 @@ class Booking_confirmation extends EA_Controller {
|
||||||
$this->load->model('providers_model');
|
$this->load->model('providers_model');
|
||||||
$this->load->model('services_model');
|
$this->load->model('services_model');
|
||||||
$this->load->model('customers_model');
|
$this->load->model('customers_model');
|
||||||
|
|
||||||
|
$this->load->library('google_sync');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,44 +52,13 @@ class Booking_confirmation extends EA_Controller {
|
||||||
|
|
||||||
$appointment = $occurrences[0];
|
$appointment = $occurrences[0];
|
||||||
|
|
||||||
unset($appointment['notes']);
|
$add_to_google_url = $this->google_sync->get_add_to_google_url($appointment['id']);
|
||||||
|
|
||||||
$provider = $this->providers_model->find($appointment['id_users_provider']);
|
|
||||||
|
|
||||||
$this->providers_model->only($provider, [
|
|
||||||
'id',
|
|
||||||
'first_name',
|
|
||||||
'last_name',
|
|
||||||
'email',
|
|
||||||
'timezone'
|
|
||||||
]);
|
|
||||||
|
|
||||||
$service = $this->services_model->find($appointment['id_services']);
|
|
||||||
|
|
||||||
$this->services_model->only($service, [
|
|
||||||
'id',
|
|
||||||
'first_name',
|
|
||||||
'last_name',
|
|
||||||
'email',
|
|
||||||
'timezone'
|
|
||||||
]);
|
|
||||||
|
|
||||||
$company_name = setting('company_name');
|
|
||||||
|
|
||||||
script_vars([
|
|
||||||
'appointment_data' => $appointment,
|
|
||||||
'provider_data' => $provider,
|
|
||||||
'service_data' => $service,
|
|
||||||
'company_name' => $company_name,
|
|
||||||
'google_api_scope' => 'https://www.googleapis.com/auth/calendar',
|
|
||||||
'google_api_key' => config('google_api_key'),
|
|
||||||
'google_client_id' => config('google_api_key'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
html_vars([
|
html_vars([
|
||||||
'page_title' => lang('success'),
|
'page_title' => lang('success'),
|
||||||
'google_analytics_code' => setting('google_analytics_code'),
|
'google_analytics_code' => setting('google_analytics_code'),
|
||||||
'matomo_analytics_url' => setting('matomo_analytics_url'),
|
'matomo_analytics_url' => setting('matomo_analytics_url'),
|
||||||
|
'add_to_google_url' => $add_to_google_url,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->load->view('pages/booking_confirmation');
|
$this->load->view('pages/booking_confirmation');
|
||||||
|
|
|
@ -46,8 +46,13 @@ class Google_sync {
|
||||||
{
|
{
|
||||||
$this->CI =& get_instance();
|
$this->CI =& get_instance();
|
||||||
|
|
||||||
|
$this->CI->load->model('appointments_model');
|
||||||
|
$this->CI->load->model('services_model');
|
||||||
|
$this->CI->load->model('providers_model');
|
||||||
|
$this->CI->load->model('customers_model');
|
||||||
|
|
||||||
$http = new GuzzleHttp\Client([
|
$http = new GuzzleHttp\Client([
|
||||||
'verify' => false
|
'verify' => FALSE
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->client = new Google_Client();
|
$this->client = new Google_Client();
|
||||||
|
@ -62,6 +67,7 @@ class Google_sync {
|
||||||
$this->client->addScope([
|
$this->client->addScope([
|
||||||
Google_Service_Calendar::CALENDAR,
|
Google_Service_Calendar::CALENDAR,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->service = new Google_Service_Calendar($this->client);
|
$this->service = new Google_Service_Calendar($this->client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -369,4 +375,56 @@ class Google_sync {
|
||||||
|
|
||||||
return $calendars;
|
return $calendars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Add-To-Google-URL, that can be used by anyone to quickly add the event to Google Calendar (no API needed).
|
||||||
|
*
|
||||||
|
* @param int $appointment_id
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function get_add_to_google_url(int $appointment_id): string
|
||||||
|
{
|
||||||
|
$appointment = $this->CI->appointments_model->find($appointment_id);
|
||||||
|
|
||||||
|
$service = $this->CI->services_model->find($appointment['id_services']);
|
||||||
|
|
||||||
|
$provider = $this->CI->providers_model->find($appointment['id_users_provider']);
|
||||||
|
|
||||||
|
$customer = $this->CI->customers_model->find($appointment['id_users_customer']);
|
||||||
|
|
||||||
|
$provider_timezone_instance = new DateTimeZone($provider['timezone']);
|
||||||
|
|
||||||
|
$utc_timezone_instance = new DateTimeZone('UTC');
|
||||||
|
|
||||||
|
$appointment_start_instance = new DateTime($appointment['start_datetime'], $provider_timezone_instance);
|
||||||
|
|
||||||
|
$appointment_start_instance->setTimezone($utc_timezone_instance);
|
||||||
|
|
||||||
|
$appointment_end_instance = new DateTime($appointment['end_datetime'], $provider_timezone_instance);
|
||||||
|
|
||||||
|
$appointment_end_instance->setTimezone($utc_timezone_instance);
|
||||||
|
|
||||||
|
$add = [
|
||||||
|
$provider['email']
|
||||||
|
];
|
||||||
|
|
||||||
|
if ( ! empty($customer['email']))
|
||||||
|
{
|
||||||
|
$add[] = $customer['email'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$add_to_google_url_params = [
|
||||||
|
'action' => 'TEMPLATE',
|
||||||
|
'text' => $service['name'],
|
||||||
|
'dates' => $appointment_start_instance->format('Ymd\THis\Z') . '/' . $appointment_end_instance->format('Ymd\THis\Z'),
|
||||||
|
'location' => setting('company_name'),
|
||||||
|
'details' => 'View/Change Appointment: ' . site_url('appointments/index/' . $appointment['hash']),
|
||||||
|
'add' => implode(', ', $add)
|
||||||
|
];
|
||||||
|
|
||||||
|
return 'https://calendar.google.com/calendar/render?' . http_build_query($add_to_google_url_params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,23 +24,10 @@
|
||||||
<?= lang('go_to_booking_page') ?>
|
<?= lang('go_to_booking_page') ?>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<?php if (config('google_sync_feature')): ?>
|
<a href="<?= vars('add_to_google_url') ?>" id="add-to-google-calendar" class="btn btn-primary" target="_blank">
|
||||||
<button id="add-to-google-calendar" class="btn btn-outline-secondary">
|
|
||||||
<i class="fas fa-plus me-2"></i>
|
<i class="fas fa-plus me-2"></i>
|
||||||
<?= lang('add_to_google_calendar') ?>
|
<?= lang('add_to_google_calendar') ?>
|
||||||
</button>
|
</a>
|
||||||
<?php endif ?>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<?php section('content') ?>
|
<?php section('content') ?>
|
||||||
|
|
||||||
<?php section('scripts') ?>
|
|
||||||
|
|
||||||
<script src="<?= asset_url('assets/js/utils/date.js') ?>"></script>
|
|
||||||
<script src="<?= asset_url('assets/js/utils/message.js') ?>"></script>
|
|
||||||
<script src="<?= asset_url('assets/js/utils/validation.js') ?>"></script>
|
|
||||||
<script src="<?= asset_url('assets/js/utils/url.js') ?>"></script>
|
|
||||||
<script src="<?= asset_url('assets/js/pages/booking_confirmation.js') ?>"></script>
|
|
||||||
|
|
||||||
<?php section('scripts') ?>
|
|
||||||
|
|
||||||
|
|
|
@ -1,147 +0,0 @@
|
||||||
/* ----------------------------------------------------------------------------
|
|
||||||
* Easy!Appointments - Online Appointment Scheduler
|
|
||||||
*
|
|
||||||
* @package EasyAppointments
|
|
||||||
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
||||||
* @copyright Copyright (c) Alex Tselegidis
|
|
||||||
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
||||||
* @link https://easyappointments.org
|
|
||||||
* @since v1.5.0
|
|
||||||
* ---------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Booking confirmation page.
|
|
||||||
*
|
|
||||||
* This module implements the functionality of the booking confirmation page.
|
|
||||||
*/
|
|
||||||
App.Pages.BookingConfirmation = (function () {
|
|
||||||
const $addToGoogleCalendar = $('#add-to-google-calendar');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle Authorization Result
|
|
||||||
*
|
|
||||||
* 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 {Object} authResult The user's authorization result.
|
|
||||||
*/
|
|
||||||
function handleAuthResult(authResult) {
|
|
||||||
try {
|
|
||||||
if (authResult.error) {
|
|
||||||
throw new Error('Could not authorize user.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// The user has granted access, add the appointment to his calendar. Before making the event.insert request
|
|
||||||
// the event resource data must be prepared.
|
|
||||||
const providerData = vars('provider_data');
|
|
||||||
|
|
||||||
const appointmentData = vars('appointment_data');
|
|
||||||
|
|
||||||
// Create a valid Google Calendar API resource for the new event.
|
|
||||||
const resource = {
|
|
||||||
summary: vars('service_data').name,
|
|
||||||
location: vars('company_name'),
|
|
||||||
start: {
|
|
||||||
dateTime: moment.tz(appointmentData.start_datetime, providerData.timezone).format()
|
|
||||||
},
|
|
||||||
end: {
|
|
||||||
dateTime: moment.tz(appointmentData.end_datetime, providerData.timezone).format()
|
|
||||||
},
|
|
||||||
attendees: [
|
|
||||||
{
|
|
||||||
email: vars('provider_data').email,
|
|
||||||
displayName: vars('provider_data').first_name + ' ' + vars('provider_data').last_name
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
gapi.client.load('calendar', 'v3', () => {
|
|
||||||
const request = gapi.client.calendar.events.insert({
|
|
||||||
calendarId: 'primary',
|
|
||||||
resource: resource
|
|
||||||
});
|
|
||||||
|
|
||||||
request.execute((response) => {
|
|
||||||
if (response.error) {
|
|
||||||
throw new Error('Could not add the event to Google Calendar.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$('#success-frame').append(
|
|
||||||
$('<br/>'),
|
|
||||||
$('<div/>', {
|
|
||||||
'class': 'alert alert-success col-xs-12',
|
|
||||||
'html': [
|
|
||||||
$('<h4/>', {
|
|
||||||
'text': lang('success')
|
|
||||||
}),
|
|
||||||
$('<p/>', {
|
|
||||||
'text': lang('appointment_added_to_google_calendar')
|
|
||||||
}),
|
|
||||||
$('<a/>', {
|
|
||||||
'href': response.htmlLink,
|
|
||||||
'text': lang('view_appointment_in_google_calendar')
|
|
||||||
})
|
|
||||||
]
|
|
||||||
})
|
|
||||||
);
|
|
||||||
$addToGoogleCalendar.hide();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
// The user denied access or something else happened, display corresponding message on the screen.
|
|
||||||
$('#success-frame').append(
|
|
||||||
$('<br/>'),
|
|
||||||
$('<div/>', {
|
|
||||||
'class': 'alert alert-danger col-xs-12',
|
|
||||||
'html': [
|
|
||||||
$('<h4/>', {
|
|
||||||
'text': lang('oops_something_went_wrong')
|
|
||||||
}),
|
|
||||||
$('<p/>', {
|
|
||||||
'text': lang('could_not_add_to_google_calendar')
|
|
||||||
}),
|
|
||||||
$('<pre/>', {
|
|
||||||
'text': error.message
|
|
||||||
})
|
|
||||||
]
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add the page event listeners.
|
|
||||||
*/
|
|
||||||
function addEventListeners() {
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
$addToGoogleCalendar.on('click', () => {
|
|
||||||
gapi.client.setApiKey(vars('google_api_key'));
|
|
||||||
|
|
||||||
gapi.auth.authorize(
|
|
||||||
{
|
|
||||||
client_id: vars('google_client_id'),
|
|
||||||
scope: vars('google_api_scope'),
|
|
||||||
immediate: false
|
|
||||||
},
|
|
||||||
handleAuthResult
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the module.
|
|
||||||
*/
|
|
||||||
function initialize() {
|
|
||||||
addEventListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', initialize);
|
|
||||||
|
|
||||||
return {};
|
|
||||||
})();
|
|
Loading…
Reference in a new issue