Υλοποίηση της μεθοδου διαγραφής ραντεβού από το backend και το Google Calendar.

This commit is contained in:
alextselegidis@gmail.com 2013-06-24 06:04:30 +00:00
parent a481c6747d
commit 82d458fe1d
9 changed files with 125 additions and 90 deletions

View file

@ -159,6 +159,8 @@ class Appointments extends CI_Controller {
$this->load->model('Appointments_Model'); $this->load->model('Appointments_Model');
$this->load->model('Providers_Model'); $this->load->model('Providers_Model');
$this->load->model('Customers_Model'); $this->load->model('Customers_Model');
$this->load->model('Services_Model');
$this->load->model('Settings_Model');
// Check whether the appointment hash exists in the database. // Check whether the appointment hash exists in the database.
$records = $this->Appointments_Model->get_batch(array('hash' => $appointment_hash)); $records = $this->Appointments_Model->get_batch(array('hash' => $appointment_hash));
@ -167,40 +169,42 @@ class Appointments extends CI_Controller {
} }
$appointment_data = $records[0]; $appointment_data = $records[0];
$provider_data = $this->Providers_Model->get_row($appointment_data['id_users_provider']);
$customer_data = $this->Customers_Model->get_row($appointment_data['id_users_customer']);
$service_data = $this->Services_Model->get_row($appointment_data['id_services']);
$company_settings = array(
'company_name' => $this->Settings_Model->get_setting('company_name'),
'company_email' => $this->Settings_Model->get_setting('company_email'),
'company_link' => $this->Settings_Model->get_setting('company_link')
);
// Delete the appointment from the database. // :: DELETE APPOINTMENT RECORD FROM THE DATABASE.
if (!$this->Appointments_Model->delete($appointment_data['id'])) { if (!$this->Appointments_Model->delete($appointment_data['id'])) {
throw new Exception('Appointment could not be deleted from the database.'); throw new Exception('Appointment could not be deleted from the database.');
} }
// Send notification emails to the customer and provider. // :: SYNC APPOINTMENT REMOVAL WITH GOOGLE CALENDAR
$provider_email = $this->Providers_Model->get_value('email', if ($appointment_data['id_google_calendar'] != NULL) {
$appointment_data['id_users_provider']);
$customer_email = $this->Customers_Model->get_value('email',
$appointment_data['id_users_customer']);
$this->load->library('Notifications');
$this->notifications->send_cancel_appointment($appointment_data, $provider_email);
$this->notifications->send_cancel_appointment($appointment_data, $customer_email);
// Delete the appointment from Google Calendar, if it is synced.
if ($appointment_data['id_google_calendar'] != NULL) {
$google_sync = $this->Providers_Model->get_setting('google_sync', $google_sync = $this->Providers_Model->get_setting('google_sync',
$appointment_data['id_users_provider']); $appointment_data['id_users_provider']);
if ($google_sync == TRUE) { if ($google_sync == TRUE) {
$this->load->library('google_sync'); $google_token = json_decode($this->Providers_Model
->get_setting('google_token', $provider_data['id']));
// Get the provider's refresh token and try to authenticate the
// Google Calendar API usage. $this->load->library('Google_Sync');
$google_token = $this->Providers_Model->get_setting('google_token', $this->google_sync->refresh_token($google_token->refresh_token);
$appointment_data['id_users_provider']); $this->google_sync->delete_appointment($appointment_data['id_google_calendar']);
if ($this->google_sync->authendicate($google_token) === TRUE) {
$this->google_sync->delete_appointment($appointment_data['id']);
}
} }
} }
// :: SEND NOTIFICATION EMAILS TO CUSTOMER AND PROVIDER
$this->load->library('Notifications');
$this->notifications->send_remove_appointment($appointment_data, $provider_data,
$service_data, $customer_data, $company_settings, $provider_data['email']);
$this->notifications->send_remove_appointment($appointment_data, $provider_data,
$service_data, $customer_data, $company_settings, $customer_data['email']);
} catch(Exception $exc) { } catch(Exception $exc) {
// Display the error message to the customer. // Display the error message to the customer.
$view_data['error_message'] = $exc->getMessage(); $view_data['error_message'] = $exc->getMessage();

View file

@ -102,9 +102,15 @@ class Backend extends CI_Controller {
*/ */
public function ajax_save_appointment_changes() { public function ajax_save_appointment_changes() {
try { try {
$this->load->model('Appointments_Model');
$this->load->model('Providers_Model');
$this->load->model('Services_Model');
$this->load->model('Customers_Model');
$this->load->model('Settings_Model');
// :: SAVE APPOINTMENT CHANGES TO DATABASE
if (isset($_POST['appointment_data'])) { if (isset($_POST['appointment_data'])) {
$appointment_data = json_decode(stripcslashes($_POST['appointment_data']), true); $appointment_data = json_decode(stripcslashes($_POST['appointment_data']), true);
$this->load->model('Appointments_Model');
$this->Appointments_Model->add($appointment_data); $this->Appointments_Model->add($appointment_data);
if ($appointment_data['id_google_calendar'] != NULL) { if ($appointment_data['id_google_calendar'] != NULL) {
@ -120,12 +126,42 @@ class Backend extends CI_Controller {
} }
} }
// :: SAVE CUSTOMER CHANGES TO DATABASE
if (isset($_POST['customer_data'])) { if (isset($_POST['customer_data'])) {
$customer_data = json_decode(stripcslashes($_POST['customer_data']), true); $customer_data = json_decode(stripcslashes($_POST['customer_data']), true);
$this->load->model('Customers_Model');
$this->Customers_Model->add($customer_data); $this->Customers_Model->add($customer_data);
} }
// :: SYNC APPOINTMENT CHANGES WITH GOOGLE CALENDAR
if ($appointment_data['id_google_calendar'] != NULL) {
$google_sync = $this->Providers_Model
->get_setting('google_sync', $appointment_data['id_users_provider']);
if ($google_sync == TRUE) {
$google_token = $this->Providers_Model
->get_setting('google_token', $appointment_data['id_users_provider']);
$this->load->library('Google_Sync');
$this->google_sync->refresh_token($google_token->refresh_token);
$this->google_sync->update_appointment($appointment_data, $provider_data,
$service_data, $customer_data);
}
}
// :: SEND EMAIL NOTIFICATIONS TO PROVIDER AND CUSTOMER
$this->load->library('Notifications');
try {
$customer_title = 'Appointment Changes Saved Successfully!';
$provider_title = 'Appointment Details Have Changed';
$this->notifications->send_book_success(
$customer_data, $appointment_data, $customer_title);
$this->notifications->send_new_appointment(
$customer_data, $appointment_data, $provider_title);
} catch(NotificationException $nt_exc) {
// @task Display message to the user that the notification messages could
// not be sent.
}
echo json_encode('SUCCESS'); echo json_encode('SUCCESS');
} catch(Exception $exc) { } catch(Exception $exc) {
@ -144,9 +180,6 @@ class Backend extends CI_Controller {
* account of the provider, if the "google_sync" setting is enabled. * account of the provider, if the "google_sync" setting is enabled.
* *
* @param int $_POST['appointment_id'] The appointment id to be deleted. * @param int $_POST['appointment_id'] The appointment id to be deleted.
*
* @task Sync action with GCal.
* @task Send email notifications to provider and customer.
*/ */
public function ajax_delete_appointment() { public function ajax_delete_appointment() {
try { try {
@ -159,11 +192,17 @@ class Backend extends CI_Controller {
$this->load->model('Providers_Model'); $this->load->model('Providers_Model');
$this->load->model('Customers_Model'); $this->load->model('Customers_Model');
$this->load->model('Services_Model'); $this->load->model('Services_Model');
$this->load->model('Settings_Model');
$appointment_data = $this->Appointments_Model->get_row($_POST['appointment_id']); $appointment_data = $this->Appointments_Model->get_row($_POST['appointment_id']);
$provider_data = $this->Providers_Model->get_row($appointment_data['id_users_provider']); $provider_data = $this->Providers_Model->get_row($appointment_data['id_users_provider']);
$customer_data = $this->Customers_Model->get_row($appointment_data['id_users_customer']); $customer_data = $this->Customers_Model->get_row($appointment_data['id_users_customer']);
$service_data = $this->services_Model->get_row($appointment_data['id_services']); $service_data = $this->Services_Model->get_row($appointment_data['id_services']);
$company_settings = array(
'company_name' => $this->Settings_Model->get_setting('company_name'),
'company_email' => $this->Settings_Model->get_setting('company_email'),
'company_link' => $this->Settings_Model->get_setting('company_link')
);
// :: DELETE APPOINTMENT RECORD FROM DATABASE. // :: DELETE APPOINTMENT RECORD FROM DATABASE.
$this->Appointments_Model->delete($_POST['appointment_id']); $this->Appointments_Model->delete($_POST['appointment_id']);
@ -183,8 +222,8 @@ class Backend extends CI_Controller {
// :: SEND NOTIFICATION EMAILS TO PROVIDER AND CUSTOMER. // :: SEND NOTIFICATION EMAILS TO PROVIDER AND CUSTOMER.
$this->load->library('Notifications'); $this->load->library('Notifications');
$this->notification->send_delete_appointment($appointment_data, $provider_data, $this->notification->send_remove_appointment($appointment_data, $provider_data,
$service_data, $customer_data); $service_data, $customer_data, $company_settings);
echo json_encode('SUCCESS'); echo json_encode('SUCCESS');

View file

@ -158,22 +158,16 @@ class Google_Sync {
/** /**
* Update an existing appointment that is already synced with Google Calendar. * Update an existing appointment that is already synced with Google Calendar.
* *
* @param int $appointment_id The appointment record id. * This method updates the google calendar event item that is connected with the
* provided appointment record of Easy!Appointments.
*
* @param array $appointment_data Contains the appointment record data.
* @param array $provider_data Contains the provider record data.
* @param array $service_data Contains the service record data.
* @param array $customer_data Contains the customer recod data.
*/ */
public function update_appointment($appointment_data, $provider_data, public function update_appointment($appointment_data, $provider_data,
$service_data, $customer_data) { $service_data, $customer_data) {
$this->CI->load->model('Appointments_Model');
$this->CI->load->model('Services_Model');
$this->CI->load->model('Providers_Model');
$this->CI->load->model('Customers_Model');
$this->CI->load->model('Settings_Model');
$appointment_data = $this->CI->Appointments_Model->get_row($appointment_id);
$provider_data = $this->CI->Providers_Model->get_row($appointment_data['id_users_provider']);
$customer_data = $this->CI->Customers_Model->get_row($appointment_data['id_users_customer']);
$service_data = $this->CI->Services_Model->get_row($appointment_data['id_services']);
$company_name = $this->CI->Settings_Model->get_setting('company_name');
$this->CI->load->helper('general'); $this->CI->load->helper('general');
} }

View file

@ -157,38 +157,45 @@ class Notifications {
} }
/** /**
* Send an email notification to a user when an appointment is cancelled. * Send an email notification to both provider and customer on appointment removal.
* *
* @param array $appointment_data The record data of the cancelled appointment. * Whenever an appointment is cancelled or removed, both the provider and customer
* @param string $user_email The user email where the email notification is going * need to be informed. This method sends the same email twice.
* to be send. *
* <strong>IMPORTANT!</strong> This method's arguments should be taken
* from database before the appointment record is deleted.
*
* @param array $appointment_data The record data of the removed appointment.
* @param array $provider_data The record data of the appointment provider.
* @param array $service_data The record data of the appointment service.
* @param array $customer_data The record data of the appointment customer.
* @param array $company_settings Some settings that are required for this function.
* By now this array must contain the following values: "company_link",
* "company_name", "company_email".
* @param string $to_address The email address of the email receiver.
*/ */
public function send_cancel_appointment($appointment_data, $user_email) { public function send_remove_appointment($appointment_data, $provider_data,
$this->CI->load->model('Providers_Model'); $service_data, $customer_data, $company_settings, $to_address) {
$this->CI->load->model('Services_Model'); // :: PREPARE EMAIL REPLACE ARRAY
$this->CI->load->model('Settings_Model');
$provider_data = $this->CI->Providers_Model->get_row($appointment_data['id_users_provider']);
$service_data = $this->CI->Services_Model->get_row($appointment_data['id_services']);
$replace_array = array( $replace_array = array(
'$email_title' => 'Appointment Cancelled', '$email_title' => 'Appointment Cancelled',
'$appointment_service' => $service_data['name'], '$appointment_service' => $service_data['name'],
'$appointment_provider' => $provider_data['first_name'] . ' ' . $provider_data['last_name'], '$appointment_provider' => $provider_data['first_name'] . ' ' . $provider_data['last_name'],
'$appointment_date' => date('d/m/Y H:i', strtotime($appointment_data['start_datetime'])), '$appointment_date' => date('d/m/Y H:i', strtotime($appointment_data['start_datetime'])),
'$appointment_duration' => $service_data['duration'] . ' minutes', '$appointment_duration' => $service_data['duration'] . ' minutes',
'$company_link' => $this->CI->Settings_Model->get_setting('company_link'), '$company_link' => $company_settings('company_link'),
'$company_name' => $this->CI->Settings_Model->get_setting('company_name') '$company_name' => $company_settings('company_name')
); );
$email_html = file_get_contents(dirname(dirname(__FILE__)) $email_html = file_get_contents(dirname(dirname(__FILE__))
. '/views/emails/cancel_appointment.php'); . '/views/emails/cancel_appointment.php');
$email_html = $this->replace_template_variables($replace_array, $email_html); $email_html = $this->replace_template_variables($replace_array, $email_html);
// :: SETUP EMAIL OBJECT AND SEND NOTIFICATION
$mail = new PHPMailer(); $mail = new PHPMailer();
$mail->From = $this->CI->Settings_Model->get_setting('company_email'); $mail->From = $company_settings['company_email'];
$mail->FromName = $this->CI->Settings_Model->get_setting('company_name'); $mail->FromName = $company_settings['$company_name'];
$mail->AddAddress($user_email); // "Name" argument crushes the phpmailer class. $mail->AddAddress($to_address); // "Name" argument crushes the phpmailer class.
$mail->IsHTML(true); $mail->IsHTML(true);
$mail->CharSet = 'UTF-8'; $mail->CharSet = 'UTF-8';
$mail->Subject = 'Appointment Cancelled'; $mail->Subject = 'Appointment Cancelled';
@ -201,21 +208,7 @@ class Notifications {
return TRUE; return TRUE;
} }
/**
* Send delete appointment notification.
*
* This method should be called after the appointment has been deleted.
*
* <strong>IMPORTANT!</strong> This method's arguments should be taken
* from database before the appointment record is deleted.
*/
public function send_delete_appointment($appointment_data, $provider_data,
$service_data, $customer_data) {
// @task Implement sending delete appointment notification.
}
} }
/* End of file notifications.php */ /* End of file notifications.php */
/* Location: ./application/libraries/notifications.php */ /* Location: ./application/libraries/notifications.php */

View file

@ -75,7 +75,13 @@ class Services_Model extends CI_Model {
* database services. * database services.
*/ */
function get_available_services() { function get_available_services() {
return $this->db->get('ea_services')->result_array(); $this->db->distinct();
return $this->db
->select('ea_services.*')
->from('ea_services')
->join('ea_services_providers',
'ea_services_providers.id_services = ea_services.id', 'inner')
->get()->result_array();
} }
} }

View file

@ -66,8 +66,6 @@ var Backend = {
notificationHtml += '</div>'; notificationHtml += '</div>';
var leftValue = window.innerWidth / 2 - $('body .notification').width() - 200;
$('#notification').html(notificationHtml); $('#notification').html(notificationHtml);
$('#notification').show('blind'); $('#notification').show('blind');
} }

View file

@ -69,7 +69,7 @@ var BackendCalendar = {
provider['first_name'] + ' ' + provider['last_name'] + '</option>'; provider['first_name'] + ' ' + provider['last_name'] + '</option>';
}); });
optgroupHtml += '</optgroup>'; optgroupHtml += '</optgroup>';
$('#select-filter-item').append(optgroupHtml) $('#select-filter-item').append(optgroupHtml);
optgroupHtml = '<optgroup label="Services">'; optgroupHtml = '<optgroup label="Services">';
$.each(GlobalVariables.availableServices, function(index, service) { $.each(GlobalVariables.availableServices, function(index, service) {
@ -78,7 +78,7 @@ var BackendCalendar = {
service['name'] + '</option>'; service['name'] + '</option>';
}); });
optgroupHtml += '</optgroup>'; optgroupHtml += '</optgroup>';
$('#select-filter-item').append(optgroupHtml) $('#select-filter-item').append(optgroupHtml);
// :: BIND THE DEFAULT EVENT HANDLERS // :: BIND THE DEFAULT EVENT HANDLERS
if (defaultEventHandlers === true) { if (defaultEventHandlers === true) {
@ -239,18 +239,19 @@ var BackendCalendar = {
$.post(postUrl, postData, function(response) { $.post(postUrl, postData, function(response) {
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
console.log('Delete Appointment Response :', response); //console.log('Delete Appointment Response :', response);
///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////
$('#message_box').dialog('close');
if (response.error) { if (response.error) {
GeneralFunctions.displayMessageBox('Delete Appointment Error', GeneralFunctions.displayMessageBox('Delete Appointment Error',
'An unexpected error occured during the deletion of the ' 'An unexpected error occured during the deletion of the ' +
+ 'appointment. Please try again.'); 'appointment. Please try again.');
return; return;
} }
// Close dialog and refresh calendar events. // Refresh calendar event items.
$('#message_box').dialog('close');
$('#select-filter-item').trigger('change'); $('#select-filter-item').trigger('change');
}, 'json'); }, 'json');

View file

@ -111,14 +111,14 @@ var GeneralFunctions = {
* @returns {String} Returns the transformed string. * @returns {String} Returns the transformed string.
*/ */
ISODateString: function(dt){ ISODateString: function(dt){
function pad(n){return n<10 ? '0'+n : n} function pad(n){return n<10 ? '0'+n : n;}
return dt.getUTCFullYear()+'-' return dt.getUTCFullYear()+'-'
+ pad(dt.getUTCMonth()+1)+'-' + pad(dt.getUTCMonth()+1)+'-'
+ pad(dt.getUTCDate())+'T' + pad(dt.getUTCDate())+'T'
+ pad(dt.getUTCHours())+':' + pad(dt.getUTCHours())+':'
+ pad(dt.getUTCMinutes())+':' + pad(dt.getUTCMinutes())+':'
+ pad(dt.getUTCSeconds())+'Z' + pad(dt.getUTCSeconds())+'Z';
}, },
/** /**