Refactor PHP controllers and models (use of protected members and corrections in docblock comments).

This commit is contained in:
Alex Tselegidis 2016-04-27 08:21:40 +02:00
parent e3eede1745
commit 8461c0dca4
13 changed files with 305 additions and 246 deletions

View file

@ -277,6 +277,7 @@ class Appointments extends CI_Controller {
* @param numeric $_POST['service_duration'] The selected service duration in minutes.
* @param string $_POST['manage_mode'] Contains either 'true' or 'false' and determines the if current user
* is managing an already booked appointment or not.
*
* @return Returns a json object with the available hours.
*/
public function ajax_get_available_hours() {
@ -300,17 +301,17 @@ class Appointments extends CI_Controller {
// If the user has selected the "any-provider" option then we will need to search
// for an available provider that will provide the requested service.
if ($_POST['provider_id'] === ANY_PROVIDER) {
$_POST['provider_id'] = $this->search_any_provider($_POST['service_id'], $_POST['selected_date']);
$_POST['provider_id'] = $this->_search_any_provider($_POST['service_id'], $_POST['selected_date']);
if ($_POST['provider_id'] === NULL) {
echo json_encode(array());
return;
}
}
$empty_periods = $this->get_provider_available_time_periods($_POST['provider_id'],
$empty_periods = $this->_get_provider_available_time_periods($_POST['provider_id'],
$_POST['selected_date'], $exclude_appointments);
$available_hours = $this->calculate_available_hours($empty_periods, $_POST['selected_date'],
$available_hours = $this->_calculate_available_hours($empty_periods, $_POST['selected_date'],
$_POST['service_duration'], filter_var($_POST['manage_mode'], FILTER_VALIDATE_BOOLEAN));
echo json_encode($available_hours);
@ -349,7 +350,7 @@ class Appointments extends CI_Controller {
}
// Check appointment availability.
if (!$this->check_datetime_availability()) {
if (!$this->_check_datetime_availability()) {
throw new Exception($this->lang->line('requested_hour_is_unavailable'));
}
@ -462,6 +463,67 @@ class Appointments extends CI_Controller {
}
}
/**
* [AJAX] Get Unavailable Dates
*
* Get an array with the available dates of a specific provider, service and month
* of the year. Provide the "provider_id", "service_id" and "selected_date" as GET
* parameters to the request. The "selected_date" parameter must have the Y-m-d format.
*
* @return string Returns a JSON array with the dates that are unavailable.
*/
public function ajax_get_unavailable_dates() {
try {
$provider_id = $this->input->get('provider_id');
$service_id = $this->input->get('service_id');
$selected_date = new DateTime($this->input->get('selected_date'));
$number_of_days = (int)$selected_date->format('t');
$unavailable_dates = array();
// Handle the "Any Provider" case.
if ($provider_id === ANY_PROVIDER) {
$provider_id = $this->_search_any_provider($service_id, $this->input->get('selected_date'));
if ($provider_id === NULL) { // No provider is available in the selected date.
for ($i=1; $i<=$number_of_days; $i++) {
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
$unavailable_dates[] = $current_date->format('Y-m-d');
}
echo json_encode($unavailable_dates);
return;
}
}
// Get the available time periods for every day of this month.
$this->load->model('services_model');
$service_duration = (int)$this->services_model->get_value('duration', $service_id);
for ($i=1; $i<=$number_of_days; $i++) {
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
if ($current_date < new DateTime()) { // Past dates become immediatelly unavailable.
$unavailable_dates[] = $current_date->format('Y-m-d');
continue;
}
$empty_periods = $this->_get_provider_available_time_periods($provider_id,
$current_date->format('Y-m-d'));
$available_hours = $this->_calculate_available_hours($empty_periods, $current_date->format('Y-m-d'),
$service_duration);
if (empty($available_hours)) {
$unavailable_dates[] = $current_date->format('Y-m-d');
}
}
echo json_encode($unavailable_dates);
} catch(Exception $exc) {
echo json_encode(array(
'exceptions' => array(exceptionToJavaScript($exc))
));
}
}
/**
* Check whether the provider is still available in the selected appointment date.
*
@ -473,7 +535,7 @@ class Appointments extends CI_Controller {
*
* @return bool Returns whether the selected datetime is still available.
*/
private function check_datetime_availability() {
protected function _check_datetime_availability() {
$this->load->model('services_model');
$appointment = $_POST['post_data']['appointment'];
@ -483,13 +545,13 @@ class Appointments extends CI_Controller {
$exclude_appointments = (isset($appointment['id'])) ? array($appointment['id']) : array();
if ($appointment['id_users_provider'] === ANY_PROVIDER) {
$appointment['id_users_provider'] = $this->search_any_provider($appointment['id_services'],
$appointment['id_users_provider'] = $this->_search_any_provider($appointment['id_services'],
date('Y-m-d', strtotime($appointment['start_datetime'])));
$_POST['post_data']['appointment']['id_users_provider'] = $appointment['id_users_provider'];
return TRUE; // The selected provider is always available.
}
$available_periods = $this->get_provider_available_time_periods(
$available_periods = $this->_get_provider_available_time_periods(
$appointment['id_users_provider'], date('Y-m-d', strtotime($appointment['start_datetime'])),
$exclude_appointments);
@ -530,7 +592,7 @@ class Appointments extends CI_Controller {
*
* @return array Returns an array with the available time periods of the provider.
*/
private function get_provider_available_time_periods($provider_id, $selected_date,
protected function _get_provider_available_time_periods($provider_id, $selected_date,
$exclude_appointments = array()) {
$this->load->model('appointments_model');
$this->load->model('providers_model');
@ -674,7 +736,7 @@ class Appointments extends CI_Controller {
*
* @return int Returns the ID of the provider that can provide the service at the selected date.
*/
private function search_any_provider($service_id, $selected_date) {
protected function _search_any_provider($service_id, $selected_date) {
$this->load->model('providers_model');
$this->load->model('services_model');
$available_providers = $this->providers_model->get_available_providers();
@ -685,8 +747,8 @@ class Appointments extends CI_Controller {
foreach($available_providers as $provider) {
foreach($provider['services'] as $provider_service_id) {
if ($provider_service_id == $service_id) { // Check if the provider is available for the requested date.
$empty_periods = $this->get_provider_available_time_periods($provider['id'], $selected_date);
$available_hours = $this->calculate_available_hours($empty_periods, $selected_date, $service['duration']);
$empty_periods = $this->_get_provider_available_time_periods($provider['id'], $selected_date);
$available_hours = $this->_calculate_available_hours($empty_periods, $selected_date, $service['duration']);
if (count($available_hours) > $max_hours_count) {
$provider_id = $provider['id'];
$max_hours_count = count($available_hours);
@ -706,14 +768,14 @@ class Appointments extends CI_Controller {
* available hour is added to the "$available_hours" array.
*
* @param array $empty_periods Contains the empty periods as generated by the
* "get_provider_available_time_periods" method.
* "_get_provider_available_time_periods" method.
* @param string $selected_date The selected date to be search (format )
* @param numeric $service_duration The service duration is required for the hour calculation.
* @param bool $manage_mode (optional) Whether we are currently on manage mode (editing an existing appointment).
*
* @return array Returns an array with the available hours for the appointment.
*/
private function calculate_available_hours(array $empty_periods, $selected_date, $service_duration,
protected function _calculate_available_hours(array $empty_periods, $selected_date, $service_duration,
$manage_mode = FALSE) {
$this->load->model('settings_model');
@ -770,67 +832,6 @@ class Appointments extends CI_Controller {
return $available_hours;
}
/**
* [AJAX] Get Unavailable Dates
*
* Get an array with the available dates of a specific provider, service and month
* of the year. Provide the "provider_id", "service_id" and "selected_date" as GET
* parameters to the request. The "selected_date" parameter must have the Y-m-d format.
*
* @return string Returns a JSON array with the dates that are unavailable.
*/
public function ajax_get_unavailable_dates() {
try {
$provider_id = $this->input->get('provider_id');
$service_id = $this->input->get('service_id');
$selected_date = new DateTime($this->input->get('selected_date'));
$number_of_days = (int)$selected_date->format('t');
$unavailable_dates = array();
// Handle the "Any Provider" case.
if ($provider_id === ANY_PROVIDER) {
$provider_id = $this->search_any_provider($service_id, $this->input->get('selected_date'));
if ($provider_id === NULL) { // No provider is available in the selected date.
for ($i=1; $i<=$number_of_days; $i++) {
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
$unavailable_dates[] = $current_date->format('Y-m-d');
}
echo json_encode($unavailable_dates);
return;
}
}
// Get the available time periods for every day of this month.
$this->load->model('services_model');
$service_duration = (int)$this->services_model->get_value('duration', $service_id);
for ($i=1; $i<=$number_of_days; $i++) {
$current_date = new DateTime($selected_date->format('Y-m') . '-' . $i);
if ($current_date < new DateTime()) { // Past dates become immediatelly unavailable.
$unavailable_dates[] = $current_date->format('Y-m-d');
continue;
}
$empty_periods = $this->get_provider_available_time_periods($provider_id,
$current_date->format('Y-m-d'));
$available_hours = $this->calculate_available_hours($empty_periods, $current_date->format('Y-m-d'),
$service_duration);
if (empty($available_hours)) {
$unavailable_dates[] = $current_date->format('Y-m-d');
}
}
echo json_encode($unavailable_dates);
} catch(Exception $exc) {
echo json_encode(array(
'exceptions' => array(exceptionToJavaScript($exc))
));
}
}
}
/* End of file appointments.php */

View file

@ -17,6 +17,9 @@
* @package Controllers
*/
class Backend extends CI_Controller {
/**
* Class Constructor
*/
public function __construct() {
parent::__construct();
$this->load->library('session');
@ -43,7 +46,7 @@ class Backend extends CI_Controller {
*/
public function index($appointment_hash = '') {
$this->session->set_userdata('dest_url', site_url('backend'));
if (!$this->has_privileges(PRIV_APPOINTMENTS)) return;
if (!$this->_has_privileges(PRIV_APPOINTMENTS)) return;
$this->load->model('appointments_model');
$this->load->model('providers_model');
@ -93,7 +96,7 @@ class Backend extends CI_Controller {
*/
public function customers() {
$this->session->set_userdata('dest_url', site_url('backend/customers'));
if (!$this->has_privileges(PRIV_CUSTOMERS)) return;
if (!$this->_has_privileges(PRIV_CUSTOMERS)) return;
$this->load->model('providers_model');
$this->load->model('customers_model');
@ -127,7 +130,7 @@ class Backend extends CI_Controller {
*/
public function services() {
$this->session->set_userdata('dest_url', site_url('backend/services'));
if (!$this->has_privileges(PRIV_SERVICES)) return;
if (!$this->_has_privileges(PRIV_SERVICES)) return;
$this->load->model('customers_model');
$this->load->model('services_model');
@ -157,7 +160,7 @@ class Backend extends CI_Controller {
*/
public function users() {
$this->session->set_userdata('dest_url', site_url('backend/users'));
if (!$this->has_privileges(PRIV_USERS)) return;
if (!$this->_has_privileges(PRIV_USERS)) return;
$this->load->model('providers_model');
$this->load->model('secretaries_model');
@ -192,8 +195,8 @@ class Backend extends CI_Controller {
*/
public function settings() {
$this->session->set_userdata('dest_url', site_url('backend/settings'));
if (!$this->has_privileges(PRIV_SYSTEM_SETTINGS, FALSE)
&& !$this->has_privileges(PRIV_USER_SETTINGS)) return;
if (!$this->_has_privileges(PRIV_SYSTEM_SETTINGS, FALSE)
&& !$this->_has_privileges(PRIV_USER_SETTINGS)) return;
$this->load->model('settings_model');
$this->load->model('user_model');
@ -236,7 +239,7 @@ class Backend extends CI_Controller {
* not. If the user is not logged in then he will be prompted to log in. If he hasn't the
* required privileges then an info message will be displayed.
*/
private function has_privileges($page, $redirect = TRUE) {
protected function _has_privileges($page, $redirect = TRUE) {
// Check if user is logged in.
$user_id = $this->session->userdata('user_id');
if ($user_id == FALSE) { // User not logged in, display the login view.
@ -270,7 +273,7 @@ class Backend extends CI_Controller {
*/
public function update() {
try {
if (!$this->has_privileges(PRIV_SYSTEM_SETTINGS, TRUE))
if (!$this->_has_privileges(PRIV_SYSTEM_SETTINGS, TRUE))
throw new Exception('You do not have the required privileges for this task!');
$this->load->library('migration');
@ -292,7 +295,7 @@ class Backend extends CI_Controller {
*
* @param array $view Contains the view data.
*/
private function set_user_data(&$view) {
protected function set_user_data(&$view) {
$this->load->model('roles_model');
// Get privileges

View file

@ -19,8 +19,14 @@
* @package Controllers
*/
class Backend_api extends CI_Controller {
private $privileges;
/**
* @var array
*/
protected $privileges;
/**
* Class Constructor
*/
public function __construct() {
parent::__construct();
@ -442,8 +448,7 @@ class Backend_api extends CI_Controller {
/**
* [AJAX] Insert of update unavailable time period to database.
*
* @param array $_POST['unavailable'] JSON encoded array that contains the unavailable
* period data.
* @param array $_POST['unavailable'] JSON encoded array that contains the unavailable period data.
*/
public function ajax_save_unavailable() {
try {

View file

@ -77,7 +77,7 @@ class User extends CI_Controller {
}
/**
* Display the forgot password page.
* Display the "forgot password" page.
*/
public function forgot_password() {
$this->load->model('settings_model');
@ -86,6 +86,9 @@ class User extends CI_Controller {
$this->load->view('user/forgot_password', $view);
}
/**
* Display the "not authorized" page.
*/
public function no_privileges() {
$this->load->model('settings_model');
$view['base_url'] = $this->config->item('base_url');

View file

@ -25,9 +25,26 @@ require_once __DIR__ . '/external/google-api-php-client/contrib/Google_CalendarS
* @package Libraries
*/
class Google_Sync {
private $CI;
private $client;
private $service;
/**
* CodeIgniter Instance
*
* @var CodeIgniter
*/
protected $CI;
/**
* Google API Client
*
* @var Google_Client
*/
protected $client;
/**
* Google Calendar Service
*
* @var Google_CalendarService
*/
protected $service;
/**
* Class Constructor
@ -114,6 +131,7 @@ class Google_Sync {
* @parma array $company_settings Contains some company settings that are used
* by this method. By the time the following values must be in the array:
* 'company_name'.
*
* @return Google_Event Returns the Google_Event class object.
*/
public function add_appointment($appointment, $provider, $service, $customer, $company_settings) {
@ -166,6 +184,7 @@ class Google_Sync {
* @parma array $company_settings Contains some company settings that are used
* by this method. By the time the following values must be in the array:
* 'company_name'.
*
* @return Google_Event Returns the Google_Event class object.
*/
public function update_appointment($appointment, $provider, $service, $customer, $company_settings) {
@ -222,6 +241,7 @@ class Google_Sync {
*
* @param array $provider Contains the provider record data.
* @param array $unavailable Contains unavailable period's data.
*
* @return Google_Event Returns the google event's object.
*/
public function add_unavailable($provider, $unavailable) {
@ -251,6 +271,7 @@ class Google_Sync {
*
* @param array $provider Contains the provider record data.
* @param array $unavailable Contains the unavailable period data.
*
* @return Google_Event Returns the Google_Event object.
*/
public function update_unavailable($provider, $unavailable) {
@ -287,7 +308,8 @@ class Google_Sync {
* Get an event object from gcal
*
* @param array $provider Contains the provider record data.
* @param string $google_event_id Id of the google calendar event
* @param string $google_event_id Id of the google calendar event.
*
* @return Google_Event Returns the google event object.
*/
public function get_event($provider, $google_event_id) {
@ -300,6 +322,7 @@ class Google_Sync {
* @param string $google_calendar The name of the google calendar to be used.
* @param date $start The start date of sync period.
* @param date $end The end date of sync period.
*
* @return object Returns an array with Google_Event objects that belong on the given
* sync period (start, end).
*/
@ -321,6 +344,7 @@ class Google_Sync {
* Google Calendar account.
*
* @param string $google_token The user's token will be used to grant access to google calendar.
*
* @return array Returns an array with the available calendars.
*/
public function get_google_calendars() {

View file

@ -21,7 +21,12 @@
* @package Libraries
*/
class Notifications {
private $ci;
/**
* CodeIgniter Instance
*
* @var CodeIgniter
*/
protected $ci;
/**
* Class Constructor
@ -40,10 +45,11 @@ class Notifications {
* @param array $replace_array Array that contains the variables
* to be replaced.
* @param string $email_html The email template hmtl.
*
* @return string Returns the new email html that contain the
* variables of the $replace_array.
*/
private function replace_template_variables($replace_array, $email_html) {
protected function _replace_template_variables($replace_array, $email_html) {
foreach($replace_array as $var=>$value) {
$email_html = str_replace($var, $value, $email_html);
}
@ -69,6 +75,7 @@ class Notifications {
* @param string $appointment_link This link is going to enable the receiver to make changes
* to the appointment record.
* @param string $receiver_address The receiver email address.
*
* @return bool Returns the operation result.
*/
public function send_appointment_details($appointment_data, $provider_data, $service_data,
@ -110,7 +117,7 @@ class Notifications {
$email_html = file_get_contents(dirname(dirname(__FILE__))
. '/views/emails/appointment_details.php');
$email_html = $this->replace_template_variables($replace_array, $email_html);
$email_html = $this->_replace_template_variables($replace_array, $email_html);
// :: INSTANTIATE EMAIL OBJECT AND SEND EMAIL
$mail = new PHPMailer();
@ -183,7 +190,7 @@ class Notifications {
$email_html = file_get_contents(dirname(dirname(__FILE__))
. '/views/emails/delete_appointment.php');
$email_html = $this->replace_template_variables($replace_array, $email_html);
$email_html = $this->_replace_template_variables($replace_array, $email_html);
// :: SETUP EMAIL OBJECT AND SEND NOTIFICATION
$mail = new PHPMailer();
@ -221,7 +228,7 @@ class Notifications {
$email_html = file_get_contents(dirname(dirname(__FILE__))
. '/views/emails/new_password.php');
$email_html = $this->replace_template_variables($replace_array, $email_html);
$email_html = $this->_replace_template_variables($replace_array, $email_html);
// :: SETUP EMAIL OBJECT AND SEND NOTIFICATION
$mail = new PHPMailer();

View file

@ -27,7 +27,7 @@ class Appointments_Model extends CI_Model {
/**
* Add an appointment record to the database.
*
* This method adds a new appointment to the database. If the
* This method adds a new appointment to the database. If the
* appointment doesn't exists it is going to be inserted, otherwise
* the record is going to be updated.
*
@ -41,9 +41,9 @@ class Appointments_Model extends CI_Model {
// Perform insert() or update() operation.
if (!isset($appointment['id'])) {
$appointment['id'] = $this->insert($appointment);
$appointment['id'] = $this->_insert($appointment);
} else {
$this->update($appointment);
$this->_update($appointment);
}
return $appointment['id'];
@ -89,7 +89,7 @@ class Appointments_Model extends CI_Model {
* data. Each key has the same name with the database fields.
* @return int Returns the id of the new record.
*/
private function insert($appointment) {
protected function _insert($appointment) {
$appointment['book_datetime'] = date('Y-m-d H:i:s');
$appointment['hash'] = $this->generate_hash();
@ -112,7 +112,7 @@ class Appointments_Model extends CI_Model {
* @param array $appointment Associative array with the appointment's
* data. Each key has the same name with the database fields.
*/
private function update($appointment) {
protected function _update($appointment) {
$this->db->where('id', $appointment['id']);
if (!$this->db->update('ea_appointments', $appointment)) {
throw new Exception('Could not update appointment record.');

View file

@ -1,4 +1,4 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
@ -47,9 +47,9 @@ class Customers_Model extends CI_Model {
// :: INSERT OR UPDATE CUSTOMER RECORD
if (!isset($customer['id'])) {
$customer['id'] = $this->insert($customer);
$customer['id'] = $this->_insert($customer);
} else {
$this->update($customer);
$this->_update($customer);
}
return $customer['id'];
@ -90,7 +90,7 @@ class Customers_Model extends CI_Model {
* data. Each key has the same name with the database fields.
* @return int Returns the id of the new record.
*/
private function insert($customer) {
protected function _insert($customer) {
// Before inserting the customer we need to get the customer's role id
// from the database and assign it to the new record as a foreign key.
$customer_role_id = $this->db
@ -118,7 +118,7 @@ class Customers_Model extends CI_Model {
* data. Each key has the same name with the database fields.
* @return int Returns the updated record id.
*/
private function update($customer) {
protected function _update($customer) {
// Do not update empty string values.
foreach ($customer as $key => $value) {
if ($value === '')

View file

@ -69,9 +69,9 @@ class Providers_Model extends CI_Model {
}
if (!isset($provider['id'])) {
$provider['id'] = $this->insert($provider);
$provider['id'] = $this->_insert($provider);
} else {
$provider['id'] = $this->update($provider);
$provider['id'] = $this->_update($provider);
}
return intval($provider['id']);
@ -109,7 +109,7 @@ class Providers_Model extends CI_Model {
* @return int Returns the new record id.
* @throws Exception When the insert operation fails.
*/
public function insert($provider) {
protected function _insert($provider) {
$this->load->helper('general');
// Get provider role id.
@ -144,7 +144,7 @@ class Providers_Model extends CI_Model {
* @return int Returns the record id.
* @throws Exception When the update operation fails.
*/
public function update($provider) {
protected function _update($provider) {
$this->load->helper('general');
// Store service and settings (must not be present on the $provider array).
@ -507,7 +507,7 @@ class Providers_Model extends CI_Model {
* @param array $settings Contains the setting values.
* @param numeric $provider_id Record id of the provider.
*/
private function save_settings($settings, $provider_id) {
protected function save_settings($settings, $provider_id) {
if (!is_numeric($provider_id)) {
throw new Exception('Invalid $provider_id argument given :' . $provider_id);
}
@ -535,7 +535,7 @@ class Providers_Model extends CI_Model {
* @throws Exception When the $services argument type is not array.
* @throws Exception When the $provider_id argumetn type is not numeric.
*/
private function save_services($services, $provider_id) {
protected function save_services($services, $provider_id) {
// Validate method arguments.
if (!is_array($services)) {
throw new Exception('Invalid argument type $services: ' . $services);

View file

@ -1,21 +1,21 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
/**
* Secretaries Model
*
*
* Handles the db actions that have to do with secretaries.
*
*
* Data Structure
* 'first_name'
* 'last_name'
@ -30,7 +30,7 @@
* 'id_roles'
* 'providers' >> array with provider ids that the secretary handles
* 'settings' >> array with the secretary settings
*
*
* @package Models
*/
class Secretaries_Model extends CI_Model {
@ -40,10 +40,10 @@ class Secretaries_Model extends CI_Model {
public function __construct() {
parent::__construct();
}
/**
* Add (insert or update) a secretary user record into database.
*
*
* @param array $secretary Contains the secretary user data.
* @return int Returns the record id.
* @throws Exception When the secretary data are invalid (see validate() method).
@ -54,20 +54,20 @@ class Secretaries_Model extends CI_Model {
if ($this->exists($secretary) && !isset($secretary['id'])) {
$secretary['id'] = $this->find_record_id($secretary);
}
if (!isset($secretary['id'])) {
$secretary['id'] = $this->insert($secretary);
$secretary['id'] = $this->_insert($secretary);
} else {
$secretary['id'] = $this->update($secretary);
$secretary['id'] = $this->_update($secretary);
}
return intval($secretary['id']);
}
/**
* Check whether a particular secretary record exists in the database.
*
* @param array $secretary Contains the secretary data. The 'email' value is required to
*
* @param array $secretary Contains the secretary data. The 'email' value is required to
* be present at the moment.
* @return bool Returns whether the record exists or not.
* @throws Exception When the 'email' value is not present on the $secretary argument.
@ -76,7 +76,7 @@ class Secretaries_Model extends CI_Model {
if (!isset($secretary['email'])) {
throw new Exception('Secretary email is not provided: ' . print_r($secretary, TRUE));
}
// This method shouldn't depend on another method of this class.
$num_rows = $this->db
->select('*')
@ -85,76 +85,76 @@ class Secretaries_Model extends CI_Model {
->where('ea_users.email', $secretary['email'])
->where('ea_roles.slug', DB_SLUG_SECRETARY)
->get()->num_rows();
return ($num_rows > 0) ? TRUE : FALSE;
}
/**
* Insert a new sercretary record into the database.
*
*
* @param array $secretary Contains the secretary data.
* @return int Returns the new record id.
* @throws Exception When the insert operation fails.
*/
public function insert($secretary) {
protected function _insert($secretary) {
$this->load->helper('general');
$providers = $secretary['providers'];
unset($secretary['providers']);
$settings = $secretary['settings'];
unset($secretary['settings']);
unset($secretary['settings']);
$secretary['id_roles'] = $this->get_secretary_role_id();
if (!$this->db->insert('ea_users', $secretary)) {
throw new Exception('Could not insert secretary into the database.');
}
$secretary['id'] = intval($this->db->insert_id());
$settings['salt'] = generate_salt();
$settings['password'] = hash_password($settings['salt'], $settings['password']);
$this->save_providers($providers, $secretary['id']);
$this->save_settings($settings, $secretary['id']);
return $secretary['id'];
}
}
/**
* Update an existing secretary record in the database.
*
*
* @param array $secretary Contains the secretary record data.
* @return int Retuns the record id.
* @throws Exception When the update operation fails.
*/
public function update($secretary) {
protected function _update($secretary) {
$this->load->helper('general');
$providers = $secretary['providers'];
unset($secretary['providers']);
$settings = $secretary['settings'];
unset($secretary['settings']);
unset($secretary['settings']);
if (isset($settings['password'])) {
$salt = $this->db->get_where('ea_user_settings', array('id_users' => $secretary['id']))->row()->salt;
$settings['password'] = hash_password($salt, $settings['password']);
}
$this->db->where('id', $secretary['id']);
if (!$this->db->update('ea_users', $secretary)){
throw new Exception('Could not update secretary record.');
}
$this->save_providers($providers, $secretary['id']);
$this->save_settings($settings, $secretary['id']);
return intval($secretary['id']);
}
/**
* Find the database record id of a secretary.
*
* @param array $secretary Contains the secretary data. The 'email' value is required
*
* @param array $secretary Contains the secretary data. The 'email' value is required
* in order to find the record id.
* @return int Returns the record id
* @throws Exception When the 'email' value is not present on the $secretary array.
@ -163,7 +163,7 @@ class Secretaries_Model extends CI_Model {
if (!isset($secretary['email'])) {
throw new Exception('Secretary email was not provided: ' . print_r($secretary, TRUE));
}
$result = $this->db
->select('ea_users.id')
->from('ea_users')
@ -171,23 +171,23 @@ class Secretaries_Model extends CI_Model {
->where('ea_users.email', $secretary['email'])
->where('ea_roles.slug', DB_SLUG_SECRETARY)
->get();
if ($result->num_rows() == 0) {
throw new Exception('Could not find secretary record id.');
}
return intval($result->row()->id);
}
/**
* Validate secretary user data before add() operation is executed.
*
*
* @param array $secretary Contains the secretary user data.
* @return bool Returns the validation result.
*/
public function validate($secretary) {
$this->load->helper('data_validation');
// If a record id is provided then check whether the record exists in the database.
if (isset($secretary['id'])) {
$num_rows = $this->db->get_where('ea_users', array('id' => $secretary['id']))
@ -205,7 +205,7 @@ class Secretaries_Model extends CI_Model {
// Validate required fields integrity.
if (!isset($secretary['last_name'])
|| !isset($secretary['email'])
|| !isset($secretary['phone_number'])) {
|| !isset($secretary['phone_number'])) {
throw new Exception('Not all required fields are provided : ' . print_r($secretary, TRUE));
}
@ -213,12 +213,12 @@ class Secretaries_Model extends CI_Model {
if (!filter_var($secretary['email'], FILTER_VALIDATE_EMAIL)) {
throw new Exception('Invalid email address provided : ' . $secretary['email']);
}
// Check if username exists.
if (isset($secretary['settings']['username'])) {
$user_id = (isset($secretary['id'])) ? $secretary['id'] : '';
if (!$this->validate_username($secretary['settings']['username'], $user_id)) {
throw new Exception ('Username already exists. Please select a different '
throw new Exception ('Username already exists. Please select a different '
. 'username for this record.');
}
}
@ -226,14 +226,14 @@ class Secretaries_Model extends CI_Model {
// Validate secretary password.
if (isset($secretary['settings']['password'])) {
if (strlen($secretary['settings']['password']) < MIN_PASSWORD_LENGTH) {
throw new Exception('The user password must be at least '
throw new Exception('The user password must be at least '
. MIN_PASSWORD_LENGTH . ' characters long.');
}
}
// When inserting a record the email address must be unique.
$secretary_id = (isset($secretary['id'])) ? $secretary['id'] : '';
$num_rows = $this->db
->select('*')
->from('ea_users')
@ -243,18 +243,18 @@ class Secretaries_Model extends CI_Model {
->where('ea_users.id <>', $secretary_id)
->get()
->num_rows();
if ($num_rows > 0) {
throw new Exception('Given email address belongs to another secretary record. '
throw new Exception('Given email address belongs to another secretary record. '
. 'Please use a different email.');
}
return TRUE;
}
/**
* Delete an existing secretary record from the database.
*
*
* @param numeric $secretary_id The secretary record id to be deleted.
* @return bool Returns the delete operation result.
* @throws Exception When the $secretary_id is not a valid numeric value.
@ -263,18 +263,18 @@ class Secretaries_Model extends CI_Model {
if (!is_numeric($secretary_id)) {
throw new Exception('Invalid argument type $secretary_id : ' . $secretary_id);
}
$num_rows = $this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows();
if ($num_rows == 0) {
return FALSE; // Record does not exist in database.
}
return $this->db->delete('ea_users', array('id' => $secretary_id));
}
/**
* Get a specific secretary record from the database.
*
*
* @param numeric $secretary_id The id of the record to be returned.
* @return array Returns an array with the secretary user data.
* @throws Exception When the $secretary_id is not a valid numeric value.
@ -284,31 +284,31 @@ class Secretaries_Model extends CI_Model {
if (!is_numeric($secretary_id)) {
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
}
// Check if record exists
if ($this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows() == 0) {
throw new Exception('The given secretary id does not match a record in the database.');
}
$secretary = $this->db->get_where('ea_users', array('id' => $secretary_id))->row_array();
$secretary_providers = $this->db->get_where('ea_secretaries_providers',
$secretary_providers = $this->db->get_where('ea_secretaries_providers',
array('id_users_secretary' => $secretary['id']))->result_array();
$secretary['providers'] = array();
foreach($secretary_providers as $secretary_provider) {
$secretary['providers'][] = $secretary_provider['id_users_provider'];
}
$secretary['settings'] = $this->db->get_where('ea_user_settings',
$secretary['settings'] = $this->db->get_where('ea_user_settings',
array('id_users' => $secretary['id']))->row_array();
unset($secretary['settings']['id_users'], $secretary['settings']['salt']);
return $secretary;
}
/**
* Get a specific field value from the database.
*
*
* @param string $field_name The field name of the value to be returned.
* @param numeric $secretary_id Record id of the value to be returned.
* @return string Returns the selected record value from the database.
@ -321,85 +321,85 @@ class Secretaries_Model extends CI_Model {
if (!is_string($field_name)) {
throw new Exception('$field_name argument is not a string : ' . $field_name);
}
if (!is_numeric($secretary_id)) {
throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
}
// Check whether the secretary record exists.
// Check whether the secretary record exists.
$result = $this->db->get_where('ea_users', array('id' => $secretary_id));
if ($result->num_rows() == 0) {
throw new Exception('The record with the given id does not exist in the '
. 'database : ' . $secretary_id);
}
// Check if the required field name exist in database.
$provider = $result->row_array();
if (!isset($provider[$field_name])) {
throw new Exception('The given $field_name argument does not exist in the '
throw new Exception('The given $field_name argument does not exist in the '
. 'database: ' . $field_name);
}
return $provider[$field_name];
}
/**
* Get all, or specific secretary records from database.
*
* @param string|array $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
*
* @param string|array $where_clause (OPTIONAL) The WHERE clause of the query to be executed.
* Use this to get specific secretary records.
* @return array Returns an array with secretary records.
*/
public function get_batch($where_clause = '') {
$role_id = $this->get_secretary_role_id();
if ($where_clause != '') {
$this->db->where($where_clause);
}
$this->db->where('id_roles', $role_id);
$batch = $this->db->get('ea_users')->result_array();
// Include every secretary providers.
foreach ($batch as &$secretary) {
$secretary_providers = $this->db->get_where('ea_secretaries_providers',
$secretary_providers = $this->db->get_where('ea_secretaries_providers',
array('id_users_secretary' => $secretary['id']))->result_array();
$secretary['providers'] = array();
foreach($secretary_providers as $secretary_provider) {
$secretary['providers'][] = $secretary_provider['id_users_provider'];
}
$secretary['settings'] = $this->db->get_where('ea_user_settings',
$secretary['settings'] = $this->db->get_where('ea_user_settings',
array('id_users' => $secretary['id']))->row_array();
unset($secretary['settings']['id_users']);
}
}
return $batch;
}
/**
* Get the secretary users role id.
*
* @return int Returns the role record id.
* Get the secretary users role id.
*
* @return int Returns the role record id.
*/
public function get_secretary_role_id() {
return intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_SECRETARY))->row()->id);
}
/**
* Save a secretary hasndling users.
* @param array $providers Contains the provider ids that are handled by the secretary.
* @param numeric $secretary_id The selected secretary record.
*/
private function save_providers($providers, $secretary_id) {
protected function save_providers($providers, $secretary_id) {
if (!is_array($providers)) {
throw new Exception('Invalid argument given $providers: ' . print_r($providers, TRUE));
}
// Delete old connections
$this->db->delete('ea_secretaries_providers', array('id_users_secretary' => $secretary_id));
if (count($providers) > 0) {
foreach ($providers as $provider_id) {
$this->db->insert('ea_secretaries_providers', array(
@ -409,52 +409,52 @@ class Secretaries_Model extends CI_Model {
}
}
}
/**
* Save the secretary settings (used from insert or update operation).
*
*
* @param array $settings Contains the setting values.
* @param numeric $secretary_id Record id of the secretary.
*/
private function save_settings($settings, $secretary_id) {
protected function save_settings($settings, $secretary_id) {
if (!is_numeric($secretary_id)) {
throw new Exception('Invalid $provider_id argument given :' . $secretary_id);
}
if (count($settings) == 0 || !is_array($settings)) {
throw new Exception('Invalid $settings argument given:' . print_r($settings, TRUE));
}
// Check if the setting record exists in db.
$num_rows = $this->db->get_where('ea_user_settings',
$num_rows = $this->db->get_where('ea_user_settings',
array('id_users' => $secretary_id))->num_rows();
if ($num_rows == 0) {
$this->db->insert('ea_user_settings', array('id_users' => $secretary_id));
}
foreach($settings as $name => $value) {
$this->set_setting($name, $value, $secretary_id);
}
}
/**
* Get a providers setting from the database.
*
*
* @param string $setting_name The setting name that is going to be returned.
* @param int $secretary_id The selected provider id.
* @return string Returs the value of the selected user setting.
*/
public function get_setting($setting_name, $secretary_id) {
$provider_settings = $this->db->get_where('ea_user_settings',
$provider_settings = $this->db->get_where('ea_user_settings',
array('id_users' => $secretary_id))->row_array();
return $provider_settings[$setting_name];
}
/**
* Set a provider's setting value in the database.
*
* Set a provider's setting value in the database.
*
* The provider and settings record must already exist.
*
*
* @param string $setting_name The setting's name.
* @param string $value The setting's value.
* @param numeric $secretary_id The selected provider id.
@ -463,20 +463,20 @@ class Secretaries_Model extends CI_Model {
$this->db->where(array('id_users' => $secretary_id));
return $this->db->update('ea_user_settings', array($setting_name => $value));
}
/**
* Validate Records Username
*
* Validate Records Username
*
* @param string $username The provider records username.
* @param numeric $user_id The user record id.
* @return bool Returns the validation result.
*/
public function validate_username($username, $user_id) {
$num_rows = $this->db->get_where('ea_user_settings',
$num_rows = $this->db->get_where('ea_user_settings',
array('username' => $username, 'id_users <> ' => $user_id))->num_rows();
return ($num_rows > 0) ? FALSE : TRUE;
}
}
/* End of file secretaries_model.php */
/* Location: ./application/models/secretaries_model.php */
/* Location: ./application/models/secretaries_model.php */

View file

@ -1,4 +1,4 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
@ -29,15 +29,16 @@ class Services_Model extends CI_Model {
*
* @param array $service Contains the service data. If an 'id' value is provided then
* the record will be updated.
*
* @return numeric Returns the record id.
*/
public function add($service) {
$this->validate($service);
if (!isset($service['id'])) {
$service['id'] = $this->insert($service);
$service['id'] = $this->_insert($service);
} else {
$this->update($service);
$this->_update($service);
}
return intval($service['id']);
@ -47,9 +48,10 @@ class Services_Model extends CI_Model {
* Insert service record into database.
*
* @param array $service Contains the service record data.
*
* @return int Returns the new service record id.
*/
public function insert($service) {
protected function _insert($service) {
if (!$this->db->insert('ea_services', $service)) {
throw new Exception('Could not insert service record.');
}
@ -62,7 +64,7 @@ class Services_Model extends CI_Model {
* @param array $service Contains the service data. The record id needs to be included in
* the array.
*/
public function update($service) {
protected function _update($service) {
$this->db->where('id', $service['id']);
if (!$this->db->update('ea_services', $service)) {
throw new Exception('Could not update service record');
@ -96,6 +98,7 @@ class Services_Model extends CI_Model {
* Validate a service record data.
*
* @param array $service Contains the service data.
*
* @return bool Returns the validation result.
*/
public function validate($service) {
@ -175,6 +178,7 @@ class Services_Model extends CI_Model {
* Delete a service record from database.
*
* @param numeric $service_id Record id to be deleted.
*
* @return bool Returns the delete operation result.
*/
public function delete($service_id) {
@ -194,9 +198,9 @@ class Services_Model extends CI_Model {
* Get a specific row from the services db table.
*
* @param numeric $service_id The record's id to be returned.
* @return array Returns an associative array with the selected
* record's data. Each key has the same name as the database
* field names.
*
* @return array Returns an associative array with the selected record's data. Each key
* has the same name as the database field names.
*/
public function get_row($service_id) {
if (!is_numeric($service_id)) {
@ -211,6 +215,7 @@ class Services_Model extends CI_Model {
* @param string $field_name The field name of the value to be
* returned.
* @param int $service_id The selected record's id.
*
* @return string Returns the records value from the database.
*/
public function get_value($field_name, $service_id) {
@ -242,6 +247,7 @@ class Services_Model extends CI_Model {
*
* @param string $whereClause (OPTIONAL) The WHERE clause of
* the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD.
*
* @return array Returns the rows from the database.
*/
public function get_batch($where_clause = NULL) {
@ -255,8 +261,7 @@ class Services_Model extends CI_Model {
/**
* This method returns all the services from the database.
*
* @return array Returns an object array with all the
* database services.
* @return array Returns an object array with all the database services.
*/
public function get_available_services() {
$this->db->distinct();
@ -275,6 +280,7 @@ class Services_Model extends CI_Model {
* Add (insert or update) a service category record into database.
*
* @param array $category Containst the service category data.
*
* @return int Returns the record id.s
*/
public function add_category($category) {
@ -297,6 +303,7 @@ class Services_Model extends CI_Model {
* Delete a service category record from the database.
*
* @param numeric $category_id Record id to be deleted.
*
* @return bool Returns the delete operation result.
*/
public function delete_category($category_id) {
@ -318,6 +325,7 @@ class Services_Model extends CI_Model {
* Get a service category record data.
*
* @param numeric $category_id Record id to be retrieved.
*
* @return array Returns the record data from the database.
*/
public function get_category($category_id) {
@ -349,6 +357,7 @@ class Services_Model extends CI_Model {
* a service category record into database in order to secure the record integrity.
*
* @param array $category Contains the service category data.
*
* @return bool Returns the validation result.
*/
public function validate_category($category) {

View file

@ -1,4 +1,4 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
@ -27,16 +27,15 @@ class Settings_Model extends CI_Model {
/**
* Get setting value from database.
*
* This method returns a system setting from the
* database.
* This method returns a system setting from the database.
*
* @expectedException Exception
*
* @param string $name The database setting name.
* @return string Returns the database value for
* the selected setting.
*
* @return string Returns the database value for the selected setting.
*/
function get_setting($name) {
public function get_setting($name) {
if (!is_string($name)) { // Check argument type.
throw new Exception('$name argument is not a string : ' . $name);
}
@ -59,9 +58,10 @@ class Settings_Model extends CI_Model {
*
* @param string $name The setting name.
* @param type $value The setting value.
*
* @return int Returns the setting database id.
*/
function set_setting($name, $value) {
public function set_setting($name, $value) {
if (!is_string($name)) {
throw new Exception('$name argument is not a string : ' . $name);
}
@ -94,9 +94,10 @@ class Settings_Model extends CI_Model {
* @expectedException Exception
*
* @param string $name The setting name to be removed.
*
* @return bool Returns the delete operation result.
*/
function remove_setting($name) {
public function remove_setting($name) {
if (!is_string($name)) {
throw new Exception('$name is not a string : ' . $name);
}
@ -115,6 +116,7 @@ class Settings_Model extends CI_Model {
* saving them one by one.
*
* @param array $settings Contains all the system settings.
*
* @return bool Returns the save operation result.
*
* @throws Exception When the update operation won't work for a specific setting.

View file

@ -30,6 +30,7 @@ class User_Model extends CI_Model {
* Returns the user settings from the database.
*
* @param numeric $user_id User record id of which the settings will be returned.
*
* @return array Returns an array with user settings.
*/
public function get_settings($user_id) {
@ -43,6 +44,7 @@ class User_Model extends CI_Model {
* This method saves the user settings into the database.
*
* @param array $user Contains the current users settings.
*
* @return bool Returns the operation result.
*/
public function save_settings($user) {
@ -72,6 +74,7 @@ class User_Model extends CI_Model {
* Retrieve user's salt from database.
*
* @param string $username This will be used to find the user record.
*
* @return string Returns the salt db value.
*/
public function get_salt($username) {
@ -84,8 +87,8 @@ class User_Model extends CI_Model {
*
* @param string $username Given user's name.
* @param type $password Given user's password (not hashed yet).
* @return array|null Returns the session data of the logged in user or null on
* failure.
*
* @return array|null Returns the session data of the logged in user or null on failure.
*/
public function check_login($username, $password) {
$this->load->helper('general');
@ -109,6 +112,7 @@ class User_Model extends CI_Model {
* Get the given user's display name (first + last name).
*
* @param numeric $user_id The given user record id.
*
* @return string Returns the user display name.
*/
public function get_user_display_name($user_id) {
@ -124,6 +128,7 @@ class User_Model extends CI_Model {
*
* @param string $username
* @param string $email
*
* @return string|bool Returns the new password on success or FALSE on failure.
*/
public function regenerate_password($username, $email) {