mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-26 01:43:06 +03:00
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:
parent
1075855e04
commit
056e9c1165
2 changed files with 199 additions and 181 deletions
|
@ -17,9 +17,16 @@
|
|||
* @package Controllers
|
||||
*/
|
||||
class Appointments extends CI_Controller {
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
$this->load->library('session');
|
||||
$this->load->helper('installation');
|
||||
|
||||
// Set user's selected language.
|
||||
if ($this->session->userdata('language')) {
|
||||
$this->config->set_item('language', $this->session->userdata('language'));
|
||||
|
@ -27,6 +34,11 @@ class Appointments extends CI_Controller {
|
|||
} else {
|
||||
$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.
|
||||
*/
|
||||
public function index($appointment_hash = '') {
|
||||
if (!$this->check_installation()) {
|
||||
if (!is_ea_installed()) {
|
||||
redirect('installation/index');
|
||||
return;
|
||||
}
|
||||
|
@ -51,7 +63,6 @@ class Appointments extends CI_Controller {
|
|||
$this->load->model('customers_model');
|
||||
$this->load->model('settings_model');
|
||||
|
||||
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') {
|
||||
try {
|
||||
$available_services = $this->services_model->get_available_services();
|
||||
$available_providers = $this->providers_model->get_available_providers();
|
||||
|
@ -110,120 +121,6 @@ class Appointments extends CI_Controller {
|
|||
}
|
||||
|
||||
$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']);
|
||||
$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) {
|
||||
$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
|
||||
// will not add a new appointment on page reload.
|
||||
$view['exceptions'] = (!empty($view['exceptions'])) ? $view['exceptions'] : array();
|
||||
$this->session->set_flashdata('book_exceptions', $view['exceptions']);
|
||||
redirect('appointments/book_success/'.$appointment['id']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -607,20 +504,6 @@ class Appointments extends CI_Controller {
|
|||
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.
|
||||
*
|
||||
|
@ -654,6 +537,120 @@ class Appointments extends CI_Controller {
|
|||
}
|
||||
$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 */
|
||||
|
|
|
@ -20,12 +20,27 @@
|
|||
*/
|
||||
class Installation extends CI_Controller {
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->load->helper('installation');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display the installation page.
|
||||
*/
|
||||
public function index() {
|
||||
$view['base_url'] = $this->config->item('base_url');
|
||||
$this->load->view('general/installation', $view);
|
||||
if (is_ea_installed()) {
|
||||
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() {
|
||||
try {
|
||||
if (is_ea_installed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create E!A database structure.
|
||||
$file_contents = file_get_contents(BASEPATH . 'assets/sql/structure.sql');
|
||||
$sql_queries = explode(';', $file_contents);
|
||||
|
@ -75,6 +94,8 @@ class Installation extends CI_Controller {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* End of file installation.php */
|
||||
|
|
Loading…
Reference in a new issue