2013-07-09 08:43:59 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
2015-07-20 22:41:24 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2015-07-20 22:41:24 +03:00
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2016-01-02 15:47:04 +02:00
|
|
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
2015-11-05 22:38:13 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
2015-07-20 22:41:24 +03:00
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-07-16 17:56:02 +03:00
|
|
|
use \EA\Engine\Types\String;
|
|
|
|
use \EA\Engine\Types\Email;
|
|
|
|
use \EA\Engine\Types\Url;
|
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
/**
|
2015-11-05 22:38:13 +03:00
|
|
|
* Backend API Controller
|
2015-07-08 01:33:33 +03:00
|
|
|
*
|
|
|
|
* Contains all the backend AJAX callbacks.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2015-07-08 01:33:33 +03:00
|
|
|
* @package Controllers
|
2013-07-09 08:43:59 +03:00
|
|
|
*/
|
|
|
|
class Backend_api extends CI_Controller {
|
2016-04-27 09:21:40 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $privileges;
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2016-04-27 09:21:40 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2013-09-26 19:06:57 +03:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
// All the methods in this class must be accessible through a POST request.
|
2015-05-28 01:12:44 +03:00
|
|
|
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') {
|
|
|
|
$this->security->csrf_show_error();
|
|
|
|
}
|
|
|
|
|
2013-09-26 19:06:57 +03:00
|
|
|
$this->load->library('session');
|
|
|
|
$this->load->model('roles_model');
|
|
|
|
$this->privileges = $this->roles_model->get_privileges($this->session->userdata('role_slug'));
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
// Set user's selected language.
|
2014-01-11 02:07:25 +02:00
|
|
|
if ($this->session->userdata('language')) {
|
|
|
|
$this->config->set_item('language', $this->session->userdata('language'));
|
|
|
|
$this->lang->load('translations', $this->session->userdata('language'));
|
|
|
|
} else {
|
|
|
|
$this->lang->load('translations', $this->config->item('language')); // default
|
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2016-07-17 19:51:46 +03:00
|
|
|
/**
|
|
|
|
* Get Calendar Events
|
|
|
|
*
|
|
|
|
* This method will return all the calendar events within a specified period.
|
|
|
|
*/
|
|
|
|
public function ajax_get_calendar_events() {
|
|
|
|
try {
|
|
|
|
$this->output->set_content_type('application/json');
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
|
|
|
|
$startDate = $_POST['startDate'] . ' 00:00:00';
|
|
|
|
$endDate = $_POST['endDate'] . ' 23:59:59';
|
|
|
|
|
|
|
|
$response = [
|
|
|
|
'appointments' => $this->appointments_model->get_batch([
|
|
|
|
'is_unavailable' => false,
|
|
|
|
'start_datetime >=' => $startDate,
|
|
|
|
'end_datetime <=' => $endDate
|
|
|
|
]),
|
|
|
|
'unavailabilities' => $this->appointments_model->get_batch([
|
|
|
|
'is_unavailable' => true,
|
|
|
|
'start_datetime >=' => $startDate,
|
|
|
|
'end_datetime <=' => $endDate
|
|
|
|
])
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach($response['appointments'] as &$appointment) {
|
|
|
|
$appointment['provider'] = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$appointment['service'] = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
$appointment['customer'] = $this->customers_model->get_row($appointment['id_users_customer']);
|
|
|
|
}
|
|
|
|
|
2016-07-20 23:33:24 +03:00
|
|
|
$userId = $this->session->userdata('user_id');
|
|
|
|
$roleSlug = $this->session->userdata('role_slug');
|
|
|
|
|
|
|
|
// If the current user is a provider he must only see his own appointments.
|
|
|
|
if ($roleSlug === DB_SLUG_PROVIDER) {
|
|
|
|
foreach($response['appointments'] as $index => $appointment) {
|
|
|
|
if ((int)$appointment['id_users_provider'] !== (int)$userId) {
|
|
|
|
unset($response['appointments'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($response['unavailabilities'] as $index => $unavailability) {
|
|
|
|
if ((int)$unavailability['id_users_provider'] !== (int)$userId) {
|
|
|
|
unset($response['unavailabilities'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current user is a secretary he must only see the appointments of his providers.
|
|
|
|
if ($roleSlug === DB_SLUG_SECRETARY) {
|
|
|
|
$this->load->model('secretaries_model');
|
|
|
|
$providers = $this->secretaries_model->get_row($userId)['providers'];
|
|
|
|
foreach($response['appointments'] as $index => $appointment) {
|
|
|
|
if (!in_array((int)$appointment['id_users_provider'], $providers)) {
|
|
|
|
unset($response['appointments'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($response['unavailabilities'] as $index => $unavailability) {
|
|
|
|
if (!in_array((int)$unavailability['id_users_provider'], $providers)) {
|
|
|
|
unset($response['unavailabilities'][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 19:51:46 +03:00
|
|
|
$this->output->set_output(json_encode($response));
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
$this->output->set_output(json_encode([
|
|
|
|
'exceptions' => [exceptionToJavaScript($exc)]
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Get the registered appointments for the given date period and record.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
|
|
|
* This method returns the database appointments and unavailable periods for the
|
|
|
|
* user selected date period and record type (provider or service).
|
|
|
|
*
|
2015-12-30 13:02:14 +02:00
|
|
|
* @param numeric $_POST['record_id'] Selected record id.
|
|
|
|
* @param string $_POST['filter_type'] Could be either FILTER_TYPE_PROVIDER or FILTER_TYPE_SERVICE.
|
|
|
|
* @param string $_POST['start_date'] The user selected start date.
|
|
|
|
* @param string $_POST['end_date'] The user selected end date.
|
2013-07-09 08:43:59 +03:00
|
|
|
*/
|
|
|
|
public function ajax_get_calendar_appointments() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_APPOINTMENTS]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
if (!isset($_POST['filter_type'])) {
|
|
|
|
echo json_encode(array( 'appointments' => array()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-26 19:06:57 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('customers_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
if ($_POST['filter_type'] == FILTER_TYPE_PROVIDER) {
|
|
|
|
$where_id = 'id_users_provider';
|
2015-11-05 22:38:13 +03:00
|
|
|
} else {
|
2013-07-09 08:43:59 +03:00
|
|
|
$where_id = 'id_services';
|
2015-11-05 22:38:13 +03:00
|
|
|
}
|
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
// Get appointments
|
2013-07-09 08:43:59 +03:00
|
|
|
$where_clause = array(
|
|
|
|
$where_id => $_POST['record_id'],
|
2014-01-15 17:48:58 +02:00
|
|
|
//'start_datetime >=' => $_POST['start_date'],
|
|
|
|
//'end_datetime <=' => $_POST['end_date'],
|
2013-07-10 16:57:24 +03:00
|
|
|
'is_unavailable' => FALSE
|
2013-07-09 08:43:59 +03:00
|
|
|
);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
$response['appointments'] = $this->appointments_model->get_batch($where_clause);
|
2013-07-09 08:43:59 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
foreach($response['appointments'] as &$appointment) {
|
|
|
|
$appointment['provider'] = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$appointment['service'] = $this->services_model->get_row($appointment['id_services']);
|
|
|
|
$appointment['customer'] = $this->customers_model->get_row($appointment['id_users_customer']);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
// Get unavailable periods (only for provider).
|
|
|
|
if ($_POST['filter_type'] == FILTER_TYPE_PROVIDER) {
|
|
|
|
$where_clause = array(
|
|
|
|
$where_id => $_POST['record_id'],
|
2014-01-15 17:48:58 +02:00
|
|
|
//'start_datetime >=' => $_POST['start_date'],
|
|
|
|
//'end_datetime <=' => $_POST['end_date'],
|
2013-07-10 16:57:24 +03:00
|
|
|
'is_unavailable' => TRUE
|
|
|
|
);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
$response['unavailables'] = $this->appointments_model->get_batch($where_clause);
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
echo json_encode($response);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-09 08:43:59 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
/**
|
2015-12-30 13:02:14 +02:00
|
|
|
* [AJAX] Save appointment changes that are made from the backend calendar page.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2015-12-30 13:02:14 +02:00
|
|
|
* @param array $_POST['appointment_data'] (OPTIONAL) Array with the appointment data.
|
|
|
|
* @param array $_POST['customer_data'] (OPTIONAL) Array with the customer data.
|
2013-07-09 08:43:59 +03:00
|
|
|
*/
|
|
|
|
public function ajax_save_appointment() {
|
|
|
|
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');
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SAVE CUSTOMER CHANGES TO DATABASE
|
|
|
|
if (isset($_POST['customer_data'])) {
|
2013-07-09 17:46:48 +03:00
|
|
|
$customer = json_decode(stripcslashes($_POST['customer_data']), true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($customer['id']))
|
|
|
|
? $this->privileges[PRIV_CUSTOMERS]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_CUSTOMERS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$customer['id'] = $this->customers_model->add($customer);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SAVE APPOINTMENT CHANGES TO DATABASE
|
|
|
|
if (isset($_POST['appointment_data'])) {
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment = json_decode(stripcslashes($_POST['appointment_data']), true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($appointment['id']))
|
|
|
|
? $this->privileges[PRIV_APPOINTMENTS]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_APPOINTMENTS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$manage_mode = isset($appointment['id']);
|
2015-11-05 22:38:13 +03:00
|
|
|
// If the appointment does not contain the customer record id, then it
|
2013-07-09 08:43:59 +03:00
|
|
|
// means that is is going to be inserted. Get the customer's record id.
|
2013-07-09 17:46:48 +03:00
|
|
|
if (!isset($appointment['id_users_customer'])) {
|
|
|
|
$appointment['id_users_customer'] = $customer['id'];
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id'] = $this->appointments_model->add($appointment);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment = $this->appointments_model->get_row($appointment['id']);
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
$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')
|
|
|
|
);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SYNC APPOINTMENT CHANGES WITH GOOGLE CALENDAR
|
|
|
|
try {
|
2015-11-05 22:38:13 +03:00
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync',
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id_users_provider']);
|
2013-07-09 08:43:59 +03:00
|
|
|
|
|
|
|
if ($google_sync == TRUE) {
|
2015-11-05 22:38:13 +03:00
|
|
|
$google_token = json_decode($this->providers_model->get_setting('google_token',
|
2013-07-09 17:46:48 +03:00
|
|
|
$appointment['id_users_provider']));
|
2013-07-09 08:43:59 +03:00
|
|
|
|
2016-07-20 23:33:24 +03:00
|
|
|
$this->load->library('Google_sync');
|
2013-07-09 08:43:59 +03:00
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
if ($appointment['id_google_calendar'] == NULL) {
|
2015-11-05 22:38:13 +03:00
|
|
|
$google_event = $this->google_sync->add_appointment($appointment, $provider,
|
2013-07-09 17:46:48 +03:00
|
|
|
$service, $customer, $company_settings);
|
|
|
|
$appointment['id_google_calendar'] = $google_event->id;
|
|
|
|
$this->appointments_model->add($appointment); // Store google calendar id.
|
2013-07-09 08:43:59 +03:00
|
|
|
} else {
|
2015-11-05 22:38:13 +03:00
|
|
|
$this->google_sync->update_appointment($appointment, $provider,
|
2013-07-09 17:46:48 +03:00
|
|
|
$service, $customer, $company_settings);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
2013-09-03 21:58:56 +03:00
|
|
|
$warnings[] = exceptionToJavaScript($exc);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SEND EMAIL NOTIFICATIONS TO PROVIDER AND CUSTOMER
|
|
|
|
try {
|
2016-07-16 17:56:02 +03:00
|
|
|
$this->config->load('email');
|
|
|
|
$email = new \EA\Engine\Notifications\Email($this, $this->config->config);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-24 16:09:04 +03:00
|
|
|
$send_provider = $this->providers_model
|
|
|
|
->get_setting('notifications', $provider['id']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
if (!$manage_mode) {
|
2016-07-16 17:56:02 +03:00
|
|
|
$customer_title = new String($this->lang->line('appointment_booked'));
|
|
|
|
$customer_message = new String($this->lang->line('thank_you_for_appointment'));
|
|
|
|
$provider_title = new String($this->lang->line('appointment_added_to_your_plan'));
|
|
|
|
$provider_message = new String($this->lang->line('appointment_link_description'));
|
2013-07-09 08:43:59 +03:00
|
|
|
} else {
|
2016-07-16 17:56:02 +03:00
|
|
|
$customer_title = new String($this->lang->line('appointment_changes_saved'));
|
|
|
|
$customer_message = new String('');
|
|
|
|
$provider_title = new String($this->lang->line('appointment_details_changed'));
|
|
|
|
$provider_message = new String('');
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2016-07-16 17:56:02 +03:00
|
|
|
$customer_link = new Url(site_url('appointments/index/' . $appointment['hash']));
|
|
|
|
$provider_link = new Url(site_url('backend/index/' . $appointment['hash']));
|
2015-11-25 00:12:37 +02:00
|
|
|
|
|
|
|
$send_customer = $this->settings_model->get_setting('customer_notifications');
|
|
|
|
|
|
|
|
if ((bool)$send_customer === TRUE) {
|
2016-07-16 17:56:02 +03:00
|
|
|
$email->sendAppointmentDetails($appointment, $provider,
|
2015-11-25 00:12:37 +02:00
|
|
|
$service, $customer, $company_settings, $customer_title,
|
2016-07-16 17:56:02 +03:00
|
|
|
$customer_message, $customer_link, new Email($customer['email']));
|
2015-11-25 00:12:37 +02:00
|
|
|
}
|
2013-07-09 08:43:59 +03:00
|
|
|
|
2013-09-24 16:09:04 +03:00
|
|
|
if ($send_provider == TRUE) {
|
2016-07-16 17:56:02 +03:00
|
|
|
$email->sendAppointmentDetails($appointment, $provider,
|
2015-11-05 22:38:13 +03:00
|
|
|
$service, $customer, $company_settings, $provider_title,
|
2016-07-16 17:56:02 +03:00
|
|
|
$provider_message, $provider_link, new Email($provider['email']));
|
2013-09-24 16:09:04 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
} catch(Exception $exc) {
|
2013-09-03 21:58:56 +03:00
|
|
|
$warnings[] = exceptionToJavaScript($exc);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
if (!isset($warnings)) {
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2013-07-09 08:43:59 +03:00
|
|
|
} else {
|
|
|
|
echo json_encode(array(
|
|
|
|
'warnings' => $warnings
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-09 08:43:59 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete appointment from the database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
|
|
|
* This method deletes an existing appointment from the database. Once this
|
2013-07-09 08:43:59 +03:00
|
|
|
* action is finished it cannot be undone. Notification emails are send to both
|
2015-11-05 22:38:13 +03:00
|
|
|
* provider and customer and the delete action is executed to the Google Calendar
|
2013-07-09 08:43:59 +03:00
|
|
|
* account of the provider, if the "google_sync" setting is enabled.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-09 08:43:59 +03:00
|
|
|
* @param int $_POST['appointment_id'] The appointment id to be deleted.
|
|
|
|
*/
|
2015-11-05 22:38:13 +03:00
|
|
|
public function ajax_delete_appointment() {
|
2013-07-09 08:43:59 +03:00
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_APPOINTMENTS]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
if (!isset($_POST['appointment_id'])) {
|
|
|
|
throw new Exception('No appointment id provided.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: STORE APPOINTMENT DATA FOR LATER USE IN THIS METHOD
|
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('settings_model');
|
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
$appointment = $this->appointments_model->get_row($_POST['appointment_id']);
|
|
|
|
$provider = $this->providers_model->get_row($appointment['id_users_provider']);
|
|
|
|
$customer = $this->customers_model->get_row($appointment['id_users_customer']);
|
|
|
|
$service = $this->services_model->get_row($appointment['id_services']);
|
2013-07-09 08:43:59 +03:00
|
|
|
|
|
|
|
$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')
|
|
|
|
);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: DELETE APPOINTMENT RECORD FROM DATABASE
|
|
|
|
$this->appointments_model->delete($_POST['appointment_id']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SYNC DELETE WITH GOOGLE CALENDAR
|
2013-07-10 16:57:24 +03:00
|
|
|
if ($appointment['id_google_calendar'] != NULL) {
|
2013-07-09 08:43:59 +03:00
|
|
|
try {
|
2013-07-10 16:57:24 +03:00
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync', $provider['id']);
|
2013-07-09 08:43:59 +03:00
|
|
|
|
|
|
|
if ($google_sync == TRUE) {
|
|
|
|
$google_token = json_decode($this->providers_model
|
2013-07-10 16:57:24 +03:00
|
|
|
->get_setting('google_token', $provider['id']));
|
2013-07-09 08:43:59 +03:00
|
|
|
$this->load->library('Google_Sync');
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
2013-12-26 15:32:25 +02:00
|
|
|
$this->google_sync->delete_appointment($provider, $appointment['id_google_calendar']);
|
2015-11-05 22:38:13 +03:00
|
|
|
}
|
2013-07-09 08:43:59 +03:00
|
|
|
} catch(Exception $exc) {
|
2013-09-03 21:58:56 +03:00
|
|
|
$warnings[] = exceptionToJavaScript($exc);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SEND NOTIFICATION EMAILS TO PROVIDER AND CUSTOMER
|
|
|
|
try {
|
2016-07-16 17:56:02 +03:00
|
|
|
$this->config->load('email');
|
|
|
|
$email = new \EA\Engine\Notifications\Email($this, $this->config->config);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-24 16:09:04 +03:00
|
|
|
$send_provider = $this->providers_model
|
|
|
|
->get_setting('notifications', $provider['id']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2016-07-16 17:56:02 +03:00
|
|
|
if ((bool)$send_provider === TRUE) {
|
|
|
|
$email->sendDeleteAppointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, new Email($provider['email']),
|
|
|
|
new String($_POST['delete_reason']));
|
2013-09-24 16:09:04 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2015-11-25 00:12:37 +02:00
|
|
|
$send_customer = $this->settings_model->get_setting('customer_notifications');
|
|
|
|
|
|
|
|
if ((bool)$send_customer === TRUE) {
|
2016-07-16 17:56:02 +03:00
|
|
|
$email->sendDeleteAppointment($appointment, $provider,
|
|
|
|
$service, $customer, $company_settings, new Email($customer['email']),
|
|
|
|
new String($_POST['delete_reason']));
|
2015-11-25 00:12:37 +02:00
|
|
|
}
|
2013-07-09 08:43:59 +03:00
|
|
|
} catch(Exception $exc) {
|
2013-09-03 21:58:56 +03:00
|
|
|
$warnings[] = exceptionToJavaScript($exc);
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
// :: SEND RESPONSE TO CLIENT BROWSER
|
|
|
|
if (!isset($warnings)) {
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS); // Everything executed successfully.
|
2013-07-09 08:43:59 +03:00
|
|
|
} else {
|
|
|
|
echo json_encode(array(
|
|
|
|
'warnings' => $warnings // There were warnings during the operation.
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-09 08:43:59 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Disable a providers sync setting.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
|
|
|
* This method deletes the "google_sync" and "google_token" settings from the
|
|
|
|
* database. After that the provider's appointments will be no longer synced
|
2013-07-09 08:43:59 +03:00
|
|
|
* with google calendar.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-09 08:43:59 +03:00
|
|
|
* @param string $_POST['provider_id'] The selected provider record id.
|
|
|
|
*/
|
|
|
|
public function ajax_disable_provider_sync() {
|
2015-11-05 22:38:13 +03:00
|
|
|
try {
|
|
|
|
if (!isset($_POST['provider_id']))
|
2013-07-09 08:43:59 +03:00
|
|
|
throw new Exception('Provider id not specified.');
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-11-07 13:25:59 +02:00
|
|
|
if ($this->privileges[PRIV_USERS]['edit'] == FALSE
|
|
|
|
&& $this->session->userdata('user_id') != $_POST['provider_id']) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
$this->load->model('providers_model');
|
2013-11-21 23:58:51 +02:00
|
|
|
$this->load->model('appointments_model');
|
2013-07-09 08:43:59 +03:00
|
|
|
$this->providers_model->set_setting('google_sync', FALSE, $_POST['provider_id']);
|
|
|
|
$this->providers_model->set_setting('google_token', NULL, $_POST['provider_id']);
|
2013-11-21 23:58:51 +02:00
|
|
|
$this->appointments_model->clear_google_sync_ids($_POST['provider_id']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-09 08:43:59 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Filter the customer records with the given key string.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2015-12-30 13:02:14 +02:00
|
|
|
* @param string $_POST['key'] The filter key string.
|
|
|
|
*
|
2013-07-09 08:43:59 +03:00
|
|
|
* @return array Returns the search results.
|
|
|
|
*/
|
|
|
|
public function ajax_filter_customers() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_CUSTOMERS]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('providers_model');
|
2013-07-09 08:43:59 +03:00
|
|
|
$this->load->model('customers_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$key = $this->db->escape_str($_POST['key']);
|
2016-03-10 17:55:00 +02:00
|
|
|
$key = strtoupper($key);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$where_clause =
|
2016-03-10 17:55:00 +02:00
|
|
|
'(first_name LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'last_name LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'email LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'phone_number LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'address LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'city LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'zip_code LIKE upper("%' . $key . '%") OR ' .
|
|
|
|
'notes LIKE upper("%' . $key . '%"))';
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
$customers = $this->customers_model->get_batch($where_clause);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
foreach($customers as &$customer) {
|
|
|
|
$appointments = $this->appointments_model
|
|
|
|
->get_batch(array('id_users_customer' => $customer['id']));
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
foreach($appointments as &$appointment) {
|
|
|
|
$appointment['service'] = $this->services_model
|
|
|
|
->get_row($appointment['id_services']);
|
|
|
|
$appointment['provider'] = $this->providers_model
|
|
|
|
->get_row($appointment['id_users_provider']);
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
$customer['appointments'] = $appointments;
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
echo json_encode($customers);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 08:43:59 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-15 17:27:19 +03:00
|
|
|
));
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Insert of update unavailable time period to database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2016-04-27 09:21:40 +03:00
|
|
|
* @param array $_POST['unavailable'] JSON encoded array that contains the unavailable period data.
|
2013-07-09 17:46:48 +03:00
|
|
|
*/
|
|
|
|
public function ajax_save_unavailable() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
// Check privileges
|
|
|
|
$unavailable = json_decode($_POST['unavailable'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($unavailable['id']))
|
|
|
|
? $this->privileges[PRIV_APPOINTMENTS]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_APPOINTMENTS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-26 15:32:25 +02:00
|
|
|
$provider = $this->providers_model->get_row($unavailable['id_users_provider']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
// Add appointment
|
2015-11-05 22:38:13 +03:00
|
|
|
$unavailable['id'] = $this->appointments_model->add_unavailable($unavailable);
|
2013-09-26 19:06:57 +03:00
|
|
|
$unavailable = $this->appointments_model->get_row($unavailable['id']); // fetch all inserted data
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
// Google Sync
|
|
|
|
try {
|
2015-11-05 22:38:13 +03:00
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync',
|
2013-07-09 17:46:48 +03:00
|
|
|
$unavailable['id_users_provider']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
if ($google_sync) {
|
|
|
|
$google_token = json_decode($this->providers_model->get_setting('google_token',
|
|
|
|
$unavailable['id_users_provider']));
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$this->load->library('google_sync');
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
if ($unavailable['id_google_calendar'] == NULL) {
|
2013-12-26 15:32:25 +02:00
|
|
|
$google_event = $this->google_sync->add_unavailable($provider, $unavailable);
|
2013-07-10 16:57:24 +03:00
|
|
|
$unavailable['id_google_calendar'] = $google_event->id;
|
|
|
|
$this->appointments_model->add_unavailable($unavailable);
|
|
|
|
} else {
|
2013-12-26 15:32:25 +02:00
|
|
|
$google_event = $this->google_sync->update_unavailable($provider, $unavailable);
|
2013-07-10 16:57:24 +03:00
|
|
|
}
|
2013-07-09 17:46:48 +03:00
|
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
$warnings[] = $exc;
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
if (isset($warnings)) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'warnings' => $warnings
|
|
|
|
));
|
|
|
|
} else {
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2013-07-09 17:46:48 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
} catch(Exception $exc) {
|
2013-07-09 17:46:48 +03:00
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-09 17:46:48 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete an unavailable time period from database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-09 17:46:48 +03:00
|
|
|
* @param numeric $_POST['unavailable_id'] Record id to be deleted.
|
|
|
|
*/
|
|
|
|
public function ajax_delete_unavailable() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_APPOINTMENTS]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
$this->load->model('appointments_model');
|
|
|
|
$this->load->model('providers_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
$unavailable = $this->appointments_model->get_row($_POST['unavailable_id']);
|
|
|
|
$provider = $this->providers_model->get_row($unavailable['id_users_provider']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
// Delete unavailable
|
2013-07-10 16:57:24 +03:00
|
|
|
$this->appointments_model->delete_unavailable($unavailable['id']);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
// Google Sync
|
2013-07-10 16:57:24 +03:00
|
|
|
try {
|
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync', $provider['id']);
|
|
|
|
if ($google_sync == TRUE) {
|
|
|
|
$google_token = json_decode($this->providers_model->get_setting('google_token', $provider['id']));
|
|
|
|
$this->load->library('google_sync');
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
2013-12-26 15:32:25 +02:00
|
|
|
$this->google_sync->delete_unavailable($provider, $unavailable['id_google_calendar']);
|
2013-07-10 16:57:24 +03:00
|
|
|
}
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
$warnings[] = $exc;
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-10 16:57:24 +03:00
|
|
|
if (isset($warnings)) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'warnings' => $warnings
|
|
|
|
));
|
|
|
|
} else {
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2013-07-10 16:57:24 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-15 17:27:19 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
/**
|
2013-07-19 18:29:59 +03:00
|
|
|
* [AJAX] Save (insert or update) a customer record.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-15 17:27:19 +03:00
|
|
|
* @param array $_POST['customer'] JSON encoded array that contains the customer's data.
|
|
|
|
*/
|
|
|
|
public function ajax_save_customer() {
|
|
|
|
try {
|
|
|
|
$this->load->model('customers_model');
|
|
|
|
$customer = json_decode($_POST['customer'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($customer['id']))
|
|
|
|
? $this->privileges[PRIV_CUSTOMERS]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_CUSTOMERS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$customer_id = $this->customers_model->add($customer);
|
|
|
|
echo json_encode(array(
|
|
|
|
'status' => AJAX_SUCCESS,
|
|
|
|
'id' => $customer_id
|
|
|
|
));
|
2013-07-15 17:27:19 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-15 17:27:19 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
/**
|
2013-07-19 18:29:59 +03:00
|
|
|
* [AJAX] Delete customer from database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-15 17:27:19 +03:00
|
|
|
* @param numeric $_POST['customer_id'] Customer record id to be deleted.
|
|
|
|
*/
|
|
|
|
public function ajax_delete_customer() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_CUSTOMERS]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-15 17:27:19 +03:00
|
|
|
$this->load->model('customers_model');
|
|
|
|
$this->customers_model->delete($_POST['customer_id']);
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2013-07-15 17:27:19 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-09 17:46:48 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Save (insert or update) service record.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @param array $_POST['service'] Contains the service data (json encoded).
|
|
|
|
*/
|
2013-07-16 17:21:33 +03:00
|
|
|
public function ajax_save_service() {
|
2013-07-17 19:29:51 +03:00
|
|
|
try {
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$service = json_decode($_POST['service'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($service['id']))
|
|
|
|
? $this->privileges[PRIV_SERVICES]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_SERVICES]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$service_id =$this->services_model->add($service);
|
|
|
|
echo json_encode(array(
|
|
|
|
'status' => AJAX_SUCCESS,
|
|
|
|
'id' => $service_id
|
|
|
|
));
|
2013-07-17 19:29:51 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-17 19:29:51 +03:00
|
|
|
));
|
|
|
|
}
|
2013-07-16 17:21:33 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete service record from database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @param numeric $_POST['service_id'] Record id to be deleted.
|
|
|
|
*/
|
2013-07-16 17:21:33 +03:00
|
|
|
public function ajax_delete_service() {
|
2013-07-17 19:29:51 +03:00
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_SERVICES]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-17 19:29:51 +03:00
|
|
|
$this->load->model('services_model');
|
|
|
|
$result = $this->services_model->delete($_POST['service_id']);
|
|
|
|
echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-17 19:29:51 +03:00
|
|
|
));
|
|
|
|
}
|
2013-07-16 17:21:33 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Filter service records by given key string.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @param string $_POST['key'] Key string used to filter the records.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @return array Returns a json encoded array back to client.
|
|
|
|
*/
|
2013-07-16 17:21:33 +03:00
|
|
|
public function ajax_filter_services() {
|
2013-07-17 19:29:51 +03:00
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_SERVICES]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-17 19:29:51 +03:00
|
|
|
$this->load->model('services_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
$key = $this->db->escape_str($_POST['key']);
|
|
|
|
$where =
|
|
|
|
'(name LIKE "%' . $key . '%" OR duration LIKE "%' . $key . '%" OR ' .
|
2013-07-17 19:29:51 +03:00
|
|
|
'price LIKE "%' . $key . '%" OR currency LIKE "%' . $key . '%" OR ' .
|
|
|
|
'description LIKE "%' . $key . '%")';
|
|
|
|
$services = $this->services_model->get_batch($where);
|
|
|
|
echo json_encode($services);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-17 19:29:51 +03:00
|
|
|
));
|
|
|
|
}
|
2013-07-16 17:21:33 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Save (insert or update) category record.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @param array $_POST['category'] Json encoded array with the category data. If an id
|
|
|
|
* value is provided then the category is going to be udpated instead of inserted.
|
|
|
|
*/
|
2013-07-16 17:21:33 +03:00
|
|
|
public function ajax_save_service_category() {
|
2013-07-17 19:29:51 +03:00
|
|
|
try {
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$category = json_decode($_POST['category'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($category['id']))
|
|
|
|
? $this->privileges[PRIV_SERVICES]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_SERVICES]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$category_id = $this->services_model->add_category($category);
|
|
|
|
echo json_encode(array(
|
|
|
|
'status' => AJAX_SUCCESS,
|
|
|
|
'id' => $category_id
|
|
|
|
));
|
2013-07-17 19:29:51 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-17 19:29:51 +03:00
|
|
|
));
|
|
|
|
}
|
2013-07-16 17:21:33 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete category record from database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @param numeric $_POST['category_id'] Record id to be deleted.
|
|
|
|
*/
|
2013-07-16 17:21:33 +03:00
|
|
|
public function ajax_delete_service_category() {
|
2013-07-17 19:29:51 +03:00
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_SERVICES]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-17 19:29:51 +03:00
|
|
|
$this->load->model('services_model');
|
2013-07-19 18:29:59 +03:00
|
|
|
$result = $this->services_model->delete_category($_POST['category_id']);
|
2013-07-17 19:29:51 +03:00
|
|
|
echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-17 19:29:51 +03:00
|
|
|
));
|
|
|
|
}
|
2013-07-16 17:21:33 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-19 18:29:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Filter services categories with key string.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @param string $_POST['key'] The key string used to filter the records.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-07-19 18:29:59 +03:00
|
|
|
* @return array Returns a json encoded array back to client with the category records.
|
|
|
|
*/
|
2013-07-16 17:21:33 +03:00
|
|
|
public function ajax_filter_service_categories() {
|
2013-07-17 19:29:51 +03:00
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_SERVICES]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-07-17 19:29:51 +03:00
|
|
|
$this->load->model('services_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
$key = $this->db->escape_str($_POST['key']);
|
2013-07-17 19:29:51 +03:00
|
|
|
$where = '(name LIKE "%' . $key . '%" OR description LIKE "%' . $key . '%")';
|
2013-07-19 18:29:59 +03:00
|
|
|
$categories = $this->services_model->get_all_categories($where);
|
2013-07-17 19:29:51 +03:00
|
|
|
echo json_encode($categories);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
2013-09-03 21:58:56 +03:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Filter admin records with string key.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param string $_POST['key'] The key string used to filter the records.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @return array Returns a json encoded array back to client with the admin records.
|
|
|
|
*/
|
|
|
|
public function ajax_filter_admins() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USERS]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->load->model('admins_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
$key = $this->db->escape_str($_POST['key']);
|
|
|
|
$where =
|
|
|
|
'(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' .
|
|
|
|
'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' .
|
2013-09-03 21:58:56 +03:00
|
|
|
'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' .
|
|
|
|
'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' .
|
|
|
|
'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")';
|
|
|
|
$admins = $this->admins_model->get_batch($where);
|
|
|
|
echo json_encode($admins);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Save (insert or update) admin record into database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param array $_POST['admin'] A json encoded array that contains the admin data. If an 'id'
|
|
|
|
* value is provided then the record is going to be updated.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2015-11-05 22:38:13 +03:00
|
|
|
* @return array Returns an array with the operation status and the record id that was
|
2013-09-25 18:43:17 +03:00
|
|
|
* saved into the database.
|
2013-09-03 21:58:56 +03:00
|
|
|
*/
|
|
|
|
public function ajax_save_admin() {
|
|
|
|
try {
|
|
|
|
$this->load->model('admins_model');
|
|
|
|
$admin = json_decode($_POST['admin'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($admin['id']))
|
|
|
|
? $this->privileges[PRIV_USERS]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_USERS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$admin_id = $this->admins_model->add($admin);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$response = array(
|
|
|
|
'status' => AJAX_SUCCESS,
|
2015-11-05 22:38:13 +03:00
|
|
|
'id' => $admin_id
|
2013-09-25 18:43:17 +03:00
|
|
|
);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
echo json_encode($response);
|
2013-09-03 21:58:56 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete an admin record from the database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param numeric $_POST['admin_id'] The id of the record to be deleted.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @return string Returns the operation result constant (AJAX_SUCESS or AJAX_FAILURE).
|
|
|
|
*/
|
|
|
|
public function ajax_delete_admin() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USERS]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->load->model('admins_model');
|
|
|
|
$result = $this->admins_model->delete($_POST['admin_id']);
|
|
|
|
echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Filter provider records with string key.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param string $_POST['key'] The key string used to filter the records.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @return array Returns a json encoded array back to client with the provider records.
|
|
|
|
*/
|
|
|
|
public function ajax_filter_providers() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USERS]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->load->model('providers_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
$key = $this->db->escape_str($_POST['key']);
|
|
|
|
$where =
|
|
|
|
'(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' .
|
|
|
|
'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' .
|
2013-09-03 21:58:56 +03:00
|
|
|
'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' .
|
|
|
|
'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' .
|
|
|
|
'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")';
|
|
|
|
$providers = $this->providers_model->get_batch($where);
|
|
|
|
echo json_encode($providers);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Save (insert or update) a provider record into database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param array $_POST['provider'] A json encoded array that contains the provider data. If an 'id'
|
|
|
|
* value is provided then the record is going to be updated.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2015-11-05 22:38:13 +03:00
|
|
|
* @return string Returns the success contant 'AJAX_SUCCESS' so javascript knows that
|
2013-09-03 21:58:56 +03:00
|
|
|
* everything completed successfully.
|
|
|
|
*/
|
|
|
|
public function ajax_save_provider() {
|
|
|
|
try {
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$provider = json_decode($_POST['provider'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($provider['id']))
|
|
|
|
? $this->privileges[PRIV_USERS]['add']
|
2013-09-26 19:06:57 +03:00
|
|
|
: $this->privileges[PRIV_USERS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-24 16:09:04 +03:00
|
|
|
if (!isset($provider['settings']['working_plan'])) {
|
2013-09-23 18:42:36 +03:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
$provider['settings']['working_plan'] = $this->settings_model
|
|
|
|
->get_setting('company_working_plan');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$provider_id = $this->providers_model->add($provider);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
echo json_encode(array(
|
|
|
|
'status' => AJAX_SUCCESS,
|
|
|
|
'id' => $provider_id
|
|
|
|
));
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete a provider record from the database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param numeric $_POST['provider_id'] The id of the record to be deleted.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @return string Returns the operation result constant (AJAX_SUCESS or AJAX_FAILURE).
|
|
|
|
*/
|
|
|
|
public function ajax_delete_provider() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USERS]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->load->model('providers_model');
|
|
|
|
$result = $this->providers_model->delete($_POST['provider_id']);
|
|
|
|
echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Filter secretary records with string key.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param string $_POST['key'] The key string used to filter the records.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @return array Returns a json encoded array back to client with the secretary records.
|
|
|
|
*/
|
|
|
|
public function ajax_filter_secretaries() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USERS]['view'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->load->model('secretaries_model');
|
2015-11-05 22:38:13 +03:00
|
|
|
$key = $this->db->escape_str($_POST['key']);
|
|
|
|
$where =
|
|
|
|
'(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' .
|
|
|
|
'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' .
|
2013-09-03 21:58:56 +03:00
|
|
|
'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' .
|
|
|
|
'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' .
|
|
|
|
'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")';
|
|
|
|
$secretaries = $this->secretaries_model->get_batch($where);
|
|
|
|
echo json_encode($secretaries);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Save (insert or update) a secretary record into database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
|
|
|
* @param array $_POST['secretary'] A json encoded array that contains the secretary data.
|
2013-09-03 21:58:56 +03:00
|
|
|
* If an 'id' value is provided then the record is going to be updated.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2015-11-05 22:38:13 +03:00
|
|
|
* @return string Returns the success contant 'AJAX_SUCCESS' so javascript knows that
|
2013-09-03 21:58:56 +03:00
|
|
|
* everything completed successfully.
|
|
|
|
*/
|
|
|
|
public function ajax_save_secretary() {
|
|
|
|
try {
|
|
|
|
$this->load->model('secretaries_model');
|
|
|
|
$secretary = json_decode($_POST['secretary'], true);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
|
|
|
$REQUIRED_PRIV = (!isset($secretary['id']))
|
2013-09-26 19:06:57 +03:00
|
|
|
? $this->privileges[PRIV_USERS]['add']
|
|
|
|
: $this->privileges[PRIV_USERS]['edit'];
|
|
|
|
if ($REQUIRED_PRIV == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
$secretary_id = $this->secretaries_model->add($secretary);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
echo json_encode(array(
|
|
|
|
'status' => AJAX_SUCCESS,
|
|
|
|
'id' => $secretary_id
|
|
|
|
));
|
2013-09-03 21:58:56 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Delete a secretary record from the database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @param numeric $_POST['secretary_id'] The id of the record to be deleted.
|
2015-12-30 13:02:14 +02:00
|
|
|
*
|
2013-09-03 21:58:56 +03:00
|
|
|
* @return string Returns the operation result constant (AJAX_SUCESS or AJAX_FAILURE).
|
|
|
|
*/
|
|
|
|
public function ajax_delete_secretary() {
|
|
|
|
try {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USERS]['delete'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-03 21:58:56 +03:00
|
|
|
$this->load->model('secretaries_model');
|
|
|
|
$result = $this->secretaries_model->delete($_POST['secretary_id']);
|
|
|
|
echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2013-07-17 19:29:51 +03:00
|
|
|
));
|
|
|
|
}
|
2013-07-16 17:21:33 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
/**
|
|
|
|
* [AJAX] Save a setting or multiple settings in the database.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
|
|
|
* This method is used to store settings in the database. It can be either system
|
2013-09-14 19:10:59 +03:00
|
|
|
* or user settings, one or many. Use the $_POST variables accordingly.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-09-14 19:10:59 +03:00
|
|
|
* @param array $_POST['settings'] Contains an array with settings.
|
|
|
|
* @param bool $_POST['type'] Determines the settings type, can be either SETTINGS_SYSTEM
|
|
|
|
* or SETTINGS_USER.
|
|
|
|
*/
|
|
|
|
public function ajax_save_settings() {
|
|
|
|
try {
|
|
|
|
if ($_POST['type'] == SETTINGS_SYSTEM) {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_SYSTEM_SETTINGS]['edit'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2013-09-18 19:36:29 +03:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
$settings = json_decode($_POST['settings'], true);
|
|
|
|
$this->settings_model->save_settings($settings);
|
2013-09-14 19:10:59 +03:00
|
|
|
} else if ($_POST['type'] == SETTINGS_USER) {
|
2013-09-26 19:06:57 +03:00
|
|
|
if ($this->privileges[PRIV_USER_SETTINGS]['edit'] == FALSE) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2013-09-14 19:10:59 +03:00
|
|
|
$this->load->model('user_model');
|
2013-09-20 16:58:11 +03:00
|
|
|
$this->user_model->save_settings(json_decode($_POST['settings'], true));
|
2013-09-14 19:10:59 +03:00
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-18 19:36:29 +03:00
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2013-09-14 19:10:59 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
/**
|
2015-11-05 22:38:13 +03:00
|
|
|
* [AJAX] This method checks whether the username already exists in the database.
|
|
|
|
*
|
2013-09-23 18:42:36 +03:00
|
|
|
* @param string $_POST['username'] Record's username to validate.
|
|
|
|
* @param bool $_POST['record_exists'] Whether the record already exists in database.
|
|
|
|
*/
|
|
|
|
public function ajax_validate_username() {
|
2015-11-05 22:38:13 +03:00
|
|
|
try {
|
|
|
|
// We will only use the function in the admins_model because it is sufficient
|
2013-09-24 19:05:40 +03:00
|
|
|
// for the rest user types for now (providers, secretaries).
|
2013-09-23 18:42:36 +03:00
|
|
|
$this->load->model('admins_model');
|
2013-11-23 21:10:31 +02:00
|
|
|
$is_valid = $this->admins_model->validate_username($_POST['username'], $_POST['user_id']);
|
2013-09-23 18:42:36 +03:00
|
|
|
echo json_encode($is_valid);
|
|
|
|
} catch(Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-23 18:55:42 +02:00
|
|
|
/**
|
|
|
|
* [AJAX] Change system language for current user.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-12-23 18:55:42 +02:00
|
|
|
* The language setting is stored in session data and retrieved every time the user
|
|
|
|
* visits any of the system pages.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-12-23 18:55:42 +02:00
|
|
|
* @param string $_POST['language'] Selected language name.
|
|
|
|
*/
|
|
|
|
public function ajax_change_language() {
|
|
|
|
try {
|
2013-12-26 02:57:59 +02:00
|
|
|
// Check if language exists in the available languages.
|
|
|
|
$found = false;
|
|
|
|
foreach($this->config->item('available_languages') as $lang) {
|
|
|
|
if ($lang == $_POST['language']) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
2015-11-05 22:38:13 +03:00
|
|
|
}
|
2013-12-26 02:57:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$found)
|
|
|
|
throw new Exception('Translations for the given language does not exist (' . $_POST['language'] . ').');
|
|
|
|
|
|
|
|
$this->session->set_userdata('language', $_POST['language']);
|
|
|
|
$this->config->set_item('language', $_POST['language']);
|
|
|
|
|
|
|
|
echo json_encode(AJAX_SUCCESS);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-23 18:55:42 +02:00
|
|
|
} catch(Exception $exc) {
|
2013-12-26 02:57:59 +02:00
|
|
|
echo json_encode(array(
|
2013-12-23 18:55:42 +02:00
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
2015-11-05 22:38:13 +03:00
|
|
|
));
|
2013-12-23 18:55:42 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-25 03:22:37 +02:00
|
|
|
/**
|
2015-11-05 22:38:13 +03:00
|
|
|
* This method will return a list of the available google calendars.
|
|
|
|
*
|
|
|
|
* The user will need to select a specific calendar from this list to sync his
|
|
|
|
* appointments with. Google access must be already granted for the specific
|
2013-12-25 03:22:37 +02:00
|
|
|
* provider.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-12-25 03:22:37 +02:00
|
|
|
* @param string $_POST['provider_id'] Provider record id.
|
|
|
|
*/
|
|
|
|
public function ajax_get_google_calendars() {
|
|
|
|
try {
|
2013-12-26 02:57:59 +02:00
|
|
|
$this->load->library('google_sync');
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
|
|
|
|
if (!isset($_POST['provider_id']))
|
|
|
|
throw new Exception('Provider id is required in order to fetch the google calendars.');
|
|
|
|
|
2015-11-05 22:38:13 +03:00
|
|
|
// Check if selected provider has sync enabled.
|
2013-12-26 02:57:59 +02:00
|
|
|
$google_sync = $this->providers_model->get_setting('google_sync', $_POST['provider_id']);
|
|
|
|
if ($google_sync) {
|
|
|
|
$google_token = json_decode($this->providers_model->get_setting('google_token', $_POST['provider_id']));
|
|
|
|
$this->google_sync->refresh_token($google_token->refresh_token);
|
|
|
|
$calendars = $this->google_sync->get_google_calendars();
|
|
|
|
echo json_encode($calendars);
|
|
|
|
} else {
|
|
|
|
echo json_encode(AJAX_FAILURE);
|
|
|
|
}
|
2013-12-25 03:22:37 +02:00
|
|
|
} catch(Exception $exc) {
|
2013-12-26 02:57:59 +02:00
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
2013-12-25 03:22:37 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-26 02:57:59 +02:00
|
|
|
/**
|
2015-11-05 22:38:13 +03:00
|
|
|
* Select a specific google calendar for a provider.
|
|
|
|
*
|
2013-12-26 02:57:59 +02:00
|
|
|
* All the appointments will be synced with this particular calendar.
|
2015-11-05 22:38:13 +03:00
|
|
|
*
|
2013-12-26 02:57:59 +02:00
|
|
|
* @param numeric $_POST['provider_id'] Provider record id.
|
|
|
|
* @param string $_POST['calendar_id'] Google calendar's id.
|
|
|
|
*/
|
|
|
|
public function ajax_select_google_calendar() {
|
|
|
|
try {
|
|
|
|
if ($this->privileges[PRIV_USERS]['edit'] == FALSE
|
|
|
|
&& $this->session->userdata('user_id') != $_POST['provider_id']) {
|
|
|
|
throw new Exception('You do not have the required privileges for this task.');
|
|
|
|
}
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-26 02:57:59 +02:00
|
|
|
$this->load->model('providers_model');
|
|
|
|
$result = $this->providers_model->set_setting('google_calendar', $_POST['calendar_id'], $_POST['provider_id']);
|
|
|
|
echo json_encode(($result) ? AJAX_SUCCESS : AJAX_FAILURE);
|
2015-11-05 22:38:13 +03:00
|
|
|
|
2013-12-26 02:57:59 +02:00
|
|
|
} catch (Exception $exc) {
|
|
|
|
echo json_encode(array(
|
|
|
|
'exceptions' => array(exceptionToJavaScript($exc))
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 08:43:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file backend_api.php */
|
2015-11-05 22:38:13 +03:00
|
|
|
/* Location: ./application/controllers/backend_api.php */
|