Moved the register-appointment function to a new ajax callback and added extra installation checks to the installation.php controller.

This commit is contained in:
Alex Tselegidis 2015-10-18 20:00:01 +02:00
parent 1075855e04
commit 056e9c1165
2 changed files with 199 additions and 181 deletions

View file

@ -17,16 +17,28 @@
* @package Controllers * @package Controllers
*/ */
class Appointments extends CI_Controller { class Appointments extends CI_Controller {
/**
* Class Constructor
*/
public function __construct() { public function __construct() {
parent::__construct(); parent::__construct();
$this->load->library('session'); $this->load->library('session');
// Set user's selected language. $this->load->helper('installation');
// Set user's selected language.
if ($this->session->userdata('language')) { if ($this->session->userdata('language')) {
$this->config->set_item('language', $this->session->userdata('language')); $this->config->set_item('language', $this->session->userdata('language'));
$this->lang->load('translations', $this->session->userdata('language')); $this->lang->load('translations', $this->session->userdata('language'));
} else { } else {
$this->lang->load('translations', $this->config->item('language')); // default $this->lang->load('translations', $this->config->item('language')); // default
} }
// Create a new captcha builder instance and store it to the session vars.
if ($this->session->userdata('captcha_builder') === FALSE) {
$this->session->userdata('captcha_builder', new Gregwar\Captcha\CaptchaBuilder());
}
} }
/** /**
@ -40,7 +52,7 @@ class Appointments extends CI_Controller {
* record. * record.
*/ */
public function index($appointment_hash = '') { public function index($appointment_hash = '') {
if (!$this->check_installation()) { if (!is_ea_installed()) {
redirect('installation/index'); redirect('installation/index');
return; return;
} }
@ -51,179 +63,64 @@ class Appointments extends CI_Controller {
$this->load->model('customers_model'); $this->load->model('customers_model');
$this->load->model('settings_model'); $this->load->model('settings_model');
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') { try {
try { $available_services = $this->services_model->get_available_services();
$available_services = $this->services_model->get_available_services(); $available_providers = $this->providers_model->get_available_providers();
$available_providers = $this->providers_model->get_available_providers(); $company_name = $this->settings_model->get_setting('company_name');
$company_name = $this->settings_model->get_setting('company_name');
// If an appointment hash is provided then it means that the customer // If an appointment hash is provided then it means that the customer
// is trying to edit a registered appointment record. // is trying to edit a registered appointment record.
if ($appointment_hash !== ''){ if ($appointment_hash !== ''){
// Load the appointments data and enable the manage mode of the page. // Load the appointments data and enable the manage mode of the page.
$manage_mode = TRUE; $manage_mode = TRUE;
$results = $this->appointments_model->get_batch(array('hash' => $appointment_hash)); $results = $this->appointments_model->get_batch(array('hash' => $appointment_hash));
if (count($results) === 0) { if (count($results) === 0) {
// The requested appointment doesn't exist in the database. Display // The requested appointment doesn't exist in the database. Display
// a message to the customer. // a message to the customer.
$view = array( $view = array(
'message_title' => $this->lang->line('appointment_not_found'), 'message_title' => $this->lang->line('appointment_not_found'),
'message_text' => $this->lang->line('appointment_does_not_exist_in_db'), 'message_text' => $this->lang->line('appointment_does_not_exist_in_db'),
'message_icon' => $this->config->item('base_url') 'message_icon' => $this->config->item('base_url')
. '/assets/img/error.png' . '/assets/img/error.png'
); );
$this->load->view('appointments/message', $view); $this->load->view('appointments/message', $view);
return; return;
}
$appointment = $results[0];
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
} else {
// The customer is going to book a new appointment so there is no
// need for the manage functionality to be initialized.
$manage_mode = FALSE;
$appointment = array();
$provider = array();
$customer = array();
} }
$google_analytics_code = $this->settings_model->get_setting('google_analytics_code'); $appointment = $results[0];
// Load the book appointment view.
$view = array (
'available_services' => $available_services,
'available_providers' => $available_providers,
'company_name' => $company_name,
'manage_mode' => $manage_mode,
'appointment_data' => $appointment,
'provider_data' => $provider,
'customer_data' => $customer,
'google_analytics_code' => $google_analytics_code
);
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
}
$this->load->view('appointments/book', $view);
} else {
// The page is a post-back. Register the appointment and send notification emails
// to the provider and the customer that are related to the appointment. If google
// sync is enabled then add the appointment to the provider's account.
try {
$view = array();
$post_data = json_decode($_POST['post_data'], true);
$appointment = $post_data['appointment'];
$customer = $post_data['customer'];
if ($this->customers_model->exists($customer))
$customer['id'] = $this->customers_model->find_record_id($customer);
$customer_id = $this->customers_model->add($customer);
$appointment['id_users_customer'] = $customer_id;
$appointment['id'] = $this->appointments_model->add($appointment);
$appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']);
$provider = $this->providers_model->get_row($appointment['id_users_provider']); $provider = $this->providers_model->get_row($appointment['id_users_provider']);
$service = $this->services_model->get_row($appointment['id_services']); $customer = $this->customers_model->get_row($appointment['id_users_customer']);
$company_settings = array( } else {
'company_name' => $this->settings_model->get_setting('company_name'), // The customer is going to book a new appointment so there is no
'company_link' => $this->settings_model->get_setting('company_link'), // need for the manage functionality to be initialized.
'company_email' => $this->settings_model->get_setting('company_email') $manage_mode = FALSE;
); $appointment = array();
$provider = array();
// :: SYNCHRONIZE APPOINTMENT WITH PROVIDER'S GOOGLE CALENDAR $customer = array();
// The provider must have previously granted access to his google calendar account
// in order to sync the appointment.
try {
$google_sync = $this->providers_model->get_setting('google_sync',
$appointment['id_users_provider']);
if ($google_sync == TRUE) {
$google_token = json_decode($this->providers_model
->get_setting('google_token', $appointment['id_users_provider']));
$this->load->library('google_sync');
$this->google_sync->refresh_token($google_token->refresh_token);
if ($post_data['manage_mode'] === FALSE) {
// Add appointment to Google Calendar.
$google_event = $this->google_sync->add_appointment($appointment, $provider,
$service, $customer, $company_settings);
$appointment['id_google_calendar'] = $google_event->id;
$this->appointments_model->add($appointment);
} else {
// Update appointment to Google Calendar.
$appointment['id_google_calendar'] = $this->appointments_model
->get_value('id_google_calendar', $appointment['id']);
$this->google_sync->update_appointment($appointment, $provider,
$service, $customer, $company_settings);
}
}
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
}
// :: SEND NOTIFICATION EMAILS TO BOTH CUSTOMER AND PROVIDER
try {
$this->load->library('Notifications');
$send_provider = $this->providers_model
->get_setting('notifications', $provider['id']);
if (!$post_data['manage_mode']) {
$customer_title = $this->lang->line('appointment_booked');
$customer_message = $this->lang->line('thank_you_for_appointment');
$customer_link = $this->config->item('base_url') . '/index.php/appointments/index/'
. $appointment['hash'];
$provider_title = $this->lang->line('appointment_added_to_your_plan');
$provider_message = $this->lang->line('appointment_link_description');
$provider_link = $this->config->item('base_url') . '/index.php/backend/index/'
. $appointment['hash'];
} else {
$customer_title = $this->lang->line('appointment_changes_saved');
$customer_message = '';
$customer_link = $this->config->item('base_url') . '/index.php/appointments/index/'
. $appointment['hash'];
$provider_title = $this->lang->line('appointment_details_changed');
$provider_message = '';
$provider_link = $this->config->item('base_url') . '/index.php/backend/index/'
. $appointment['hash'];
}
$this->notifications->send_appointment_details($appointment, $provider,
$service, $customer,$company_settings, $customer_title,
$customer_message, $customer_link, $customer['email']);
if ($send_provider == TRUE) {
$this->notifications->send_appointment_details($appointment, $provider,
$service, $customer, $company_settings, $provider_title,
$provider_message, $provider_link, $provider['email']);
}
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
}
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
} }
// Save any exceptions to the session and redirect to another page so that the user $google_analytics_code = $this->settings_model->get_setting('google_analytics_code');
// will not add a new appointment on page reload.
$view['exceptions'] = (!empty($view['exceptions'])) ? $view['exceptions'] : array(); // Load the book appointment view.
$this->session->set_flashdata('book_exceptions', $view['exceptions']); $view = array (
redirect('appointments/book_success/'.$appointment['id']); 'available_services' => $available_services,
'available_providers' => $available_providers,
'company_name' => $company_name,
'manage_mode' => $manage_mode,
'appointment_data' => $appointment,
'provider_data' => $provider,
'customer_data' => $customer,
'google_analytics_code' => $google_analytics_code
);
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
} }
$this->load->view('appointments/book', $view);
} }
/** /**
@ -607,20 +504,6 @@ class Appointments extends CI_Controller {
return array_values($available_periods_with_appointments); return array_values($available_periods_with_appointments);
} }
/**
* This method checks whether the application is installed.
*
* This method resides in this controller because the "index()" function will
* be the first to be launched after the files are on the server. NOTE that the
* "config.php" file must be already set because we won't be able to connect to
* the database otherwise.
*
* @return bool Returns false if an installation is required.
*/
public function check_installation() {
return $this->db->table_exists('ea_users');
}
/** /**
* GET an specific appointment book and redirect to the success screen. * GET an specific appointment book and redirect to the success screen.
* *
@ -654,6 +537,120 @@ class Appointments extends CI_Controller {
} }
$this->load->view('appointments/book_success', $view); $this->load->view('appointments/book_success', $view);
} }
/**
* [AJAX] Register the appointment to the database.
*/
public function ajax_register_appointment() {
try {
$view = array();
$post_data = json_decode($_POST['post_data'], true);
$appointment = $post_data['appointment'];
$customer = $post_data['customer'];
if ($this->customers_model->exists($customer))
$customer['id'] = $this->customers_model->find_record_id($customer);
$customer_id = $this->customers_model->add($customer);
$appointment['id_users_customer'] = $customer_id;
$appointment['id'] = $this->appointments_model->add($appointment);
$appointment['hash'] = $this->appointments_model->get_value('hash', $appointment['id']);
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
$service = $this->services_model->get_row($appointment['id_services']);
$company_settings = array(
'company_name' => $this->settings_model->get_setting('company_name'),
'company_link' => $this->settings_model->get_setting('company_link'),
'company_email' => $this->settings_model->get_setting('company_email')
);
// :: SYNCHRONIZE APPOINTMENT WITH PROVIDER'S GOOGLE CALENDAR
// The provider must have previously granted access to his google calendar account
// in order to sync the appointment.
try {
$google_sync = $this->providers_model->get_setting('google_sync',
$appointment['id_users_provider']);
if ($google_sync == TRUE) {
$google_token = json_decode($this->providers_model
->get_setting('google_token', $appointment['id_users_provider']));
$this->load->library('google_sync');
$this->google_sync->refresh_token($google_token->refresh_token);
if ($post_data['manage_mode'] === FALSE) {
// Add appointment to Google Calendar.
$google_event = $this->google_sync->add_appointment($appointment, $provider,
$service, $customer, $company_settings);
$appointment['id_google_calendar'] = $google_event->id;
$this->appointments_model->add($appointment);
} else {
// Update appointment to Google Calendar.
$appointment['id_google_calendar'] = $this->appointments_model
->get_value('id_google_calendar', $appointment['id']);
$this->google_sync->update_appointment($appointment, $provider,
$service, $customer, $company_settings);
}
}
} catch(Exception $exc) {
log_message('error', $exc->getMessage());
log_message('error', $exc->getTraceAsString());
}
// :: SEND NOTIFICATION EMAILS TO BOTH CUSTOMER AND PROVIDER
try {
$this->load->library('Notifications');
$send_provider = $this->providers_model
->get_setting('notifications', $provider['id']);
if (!$post_data['manage_mode']) {
$customer_title = $this->lang->line('appointment_booked');
$customer_message = $this->lang->line('thank_you_for_appointment');
$customer_link = $this->config->item('base_url') . '/index.php/appointments/index/'
. $appointment['hash'];
$provider_title = $this->lang->line('appointment_added_to_your_plan');
$provider_message = $this->lang->line('appointment_link_description');
$provider_link = $this->config->item('base_url') . '/index.php/backend/index/'
. $appointment['hash'];
} else {
$customer_title = $this->lang->line('appointment_changes_saved');
$customer_message = '';
$customer_link = $this->config->item('base_url') . '/index.php/appointments/index/'
. $appointment['hash'];
$provider_title = $this->lang->line('appointment_details_changed');
$provider_message = '';
$provider_link = $this->config->item('base_url') . '/index.php/backend/index/'
. $appointment['hash'];
}
$this->notifications->send_appointment_details($appointment, $provider,
$service, $customer,$company_settings, $customer_title,
$customer_message, $customer_link, $customer['email']);
if ($send_provider == TRUE) {
$this->notifications->send_appointment_details($appointment, $provider,
$service, $customer, $company_settings, $provider_title,
$provider_message, $provider_link, $provider['email']);
}
} catch(Exception $exc) {
log_message('error', $exc->getMessage());
log_message('error', $exc->getTraceAsString());
}
echo AJAX_SUCCESS;
} catch(Exception $exc) {
echo json_encode(array(
'exceptions' => array(exceptionToJavaScript($exc))
));
}
}
} }
/* End of file appointments.php */ /* End of file appointments.php */

View file

@ -20,12 +20,27 @@
*/ */
class Installation extends CI_Controller { class Installation extends CI_Controller {
/**
* Class Constructor
*/
public function __construct() {
parent::__construct();
$this->load->helper('installation');
}
/** /**
* Display the installation page. * Display the installation page.
*/ */
public function index() { public function index() {
$view['base_url'] = $this->config->item('base_url'); if (is_ea_installed()) {
$this->load->view('general/installation', $view); redirect('appointments/index');
return;
}
$this->load->view('general/installation', array(
'base_url' => $this->config->item('base_url')
));
} }
/** /**
@ -37,6 +52,10 @@ class Installation extends CI_Controller {
*/ */
public function ajax_install() { public function ajax_install() {
try { try {
if (is_ea_installed()) {
return;
}
// Create E!A database structure. // Create E!A database structure.
$file_contents = file_get_contents(BASEPATH . 'assets/sql/structure.sql'); $file_contents = file_get_contents(BASEPATH . 'assets/sql/structure.sql');
$sql_queries = explode(';', $file_contents); $sql_queries = explode(';', $file_contents);
@ -75,6 +94,8 @@ class Installation extends CI_Controller {
} }
} }
} }
/* End of file installation.php */ /* End of file installation.php */