2013-05-04 00:26:04 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
2015-07-08 01:36:16 +03:00
|
|
|
/**
|
|
|
|
* Appointments Model
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2013-04-20 20:20:16 +03:00
|
|
|
class Appointments_Model extends CI_Model {
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2013-04-20 20:20:16 +03:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Add an appointment record to the database.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $appointment Associative array with the appointment
|
2013-05-04 00:26:04 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
* @return int Returns the appointments id.
|
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function add($appointment) {
|
2013-05-11 13:19:18 +03:00
|
|
|
// Validate the appointment data before doing anything.
|
2013-09-26 19:06:57 +03:00
|
|
|
$this->validate($appointment);
|
2013-06-08 12:54:45 +03:00
|
|
|
|
|
|
|
// Perform insert() or update() operation.
|
2013-07-06 03:00:04 +03:00
|
|
|
if (!isset($appointment['id'])) {
|
|
|
|
$appointment['id'] = $this->insert($appointment);
|
2013-05-11 13:19:18 +03:00
|
|
|
} else {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->update($appointment);
|
2013-05-11 13:19:18 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
return $appointment['id'];
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Check if a particular appointment record already exists.
|
|
|
|
*
|
2013-05-15 18:03:47 +03:00
|
|
|
* This method checks wether the given appointment already exists
|
|
|
|
* in the database. It doesn't search with the id, but by using the
|
|
|
|
* following fields: "start_datetime", "end_datetime", "id_users_provider",
|
|
|
|
* "id_users_customer", "id_services".
|
2013-05-04 00:26:04 +03:00
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $appointment Associative array with the appointment's
|
2013-05-04 00:26:04 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
2013-05-11 13:19:18 +03:00
|
|
|
* @return bool Returns wether the record exists or not.
|
2013-05-04 00:26:04 +03:00
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function exists($appointment) {
|
|
|
|
if (!isset($appointment['start_datetime'])
|
|
|
|
|| !isset($appointment['end_datetime'])
|
|
|
|
|| !isset($appointment['id_users_provider'])
|
|
|
|
|| !isset($appointment['id_users_customer'])
|
|
|
|
|| !isset($appointment['id_services'])) {
|
|
|
|
throw new Exception('Not all appointment field values '
|
|
|
|
. 'are provided : ' . print_r($appointment, TRUE));
|
2013-05-11 13:19:18 +03:00
|
|
|
}
|
2013-05-15 18:03:47 +03:00
|
|
|
|
|
|
|
$num_rows = $this->db->get_where('ea_appointments', array(
|
2013-07-06 03:00:04 +03:00
|
|
|
'start_datetime' => $appointment['start_datetime'],
|
|
|
|
'end_datetime' => $appointment['end_datetime'],
|
|
|
|
'id_users_provider' => $appointment['id_users_provider'],
|
|
|
|
'id_users_customer' => $appointment['id_users_customer'],
|
|
|
|
'id_services' => $appointment['id_services'],))
|
2013-06-08 12:54:45 +03:00
|
|
|
->num_rows();
|
2013-05-15 18:03:47 +03:00
|
|
|
|
|
|
|
return ($num_rows > 0) ? TRUE : FALSE;
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new appointment record to the database.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $appointment Associative array with the appointment's
|
2013-05-04 00:26:04 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
* @return int Returns the id of the new record.
|
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
private function insert($appointment) {
|
|
|
|
$appointment['book_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$appointment['hash'] = $this->generate_hash();
|
2013-06-08 12:54:45 +03:00
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
if (!$this->db->insert('ea_appointments', $appointment)) {
|
|
|
|
throw new Exception('Could not insert appointment record.');
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
2013-06-08 12:54:45 +03:00
|
|
|
|
2013-05-11 13:19:18 +03:00
|
|
|
return intval($this->db->insert_id());
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Update an existing appointment record in the database.
|
|
|
|
*
|
|
|
|
* The appointment data argument should already include the record
|
|
|
|
* id in order to process the update operation.
|
|
|
|
*
|
2013-06-08 12:54:45 +03:00
|
|
|
* @expectedException DatabaseException Raises when the update operation
|
|
|
|
* failes to complete successfully.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $appointment Associative array with the appointment's
|
2013-05-04 00:26:04 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
private function update($appointment) {
|
|
|
|
$this->db->where('id', $appointment['id']);
|
|
|
|
if (!$this->db->update('ea_appointments', $appointment)) {
|
|
|
|
throw new Exception('Could not update appointment record.');
|
2013-05-11 13:19:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the database id of an appointment record.
|
|
|
|
*
|
2013-06-08 12:54:45 +03:00
|
|
|
* The appointment data should include the following fields in order
|
|
|
|
* to get the unique id from the database: "start_datetime", "end_datetime",
|
|
|
|
* "id_users_provider", "id_users_customer", "id_services".
|
2013-05-11 13:19:18 +03:00
|
|
|
*
|
2013-05-14 22:56:16 +03:00
|
|
|
* <strong>IMPORTANT!</strong> The record must already exists in the
|
|
|
|
* database, otherwise an exception is raised.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $appointment Array with the appointment data. The
|
2013-05-11 13:19:18 +03:00
|
|
|
* keys of the array should have the same names as the db fields.
|
2013-06-08 12:54:45 +03:00
|
|
|
* @return int Returns the db id of the record that matches the apppointment
|
|
|
|
* data.
|
2013-05-11 13:19:18 +03:00
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function find_record_id($appointment) {
|
2013-05-11 13:19:18 +03:00
|
|
|
$this->db->where(array(
|
2013-07-06 03:00:04 +03:00
|
|
|
'start_datetime' => $appointment['start_datetime'],
|
|
|
|
'end_datetime' => $appointment['end_datetime'],
|
|
|
|
'id_users_provider' => $appointment['id_users_provider'],
|
|
|
|
'id_users_customer' => $appointment['id_users_customer'],
|
|
|
|
'id_services' => $appointment['id_services']
|
2013-05-11 13:19:18 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$result = $this->db->get('ea_appointments');
|
|
|
|
|
|
|
|
if ($result->num_rows() == 0) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Could not find appointment record id.');
|
2013-05-11 13:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result->row()->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-08 12:54:45 +03:00
|
|
|
* Validate appointment data before the insert or update operations
|
|
|
|
* are executed.
|
2013-05-11 13:19:18 +03:00
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $appointment Contains the appointment data.
|
2013-05-14 22:56:16 +03:00
|
|
|
* @return bool Returns the validation result.
|
2013-05-11 13:19:18 +03:00
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function validate($appointment) {
|
2013-05-11 13:19:18 +03:00
|
|
|
$this->load->helper('data_validation');
|
|
|
|
|
2013-09-26 19:06:57 +03:00
|
|
|
// If a appointment id is given, check wether the record exists
|
|
|
|
// in the database.
|
|
|
|
if (isset($appointment['id'])) {
|
|
|
|
$num_rows = $this->db->get_where('ea_appointments',
|
|
|
|
array('id' => $appointment['id']))->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
throw new Exception('Provided appointment id does not '
|
|
|
|
. 'exist in the database.');
|
2013-05-11 13:19:18 +03:00
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if appointment dates are valid.
|
|
|
|
if (!validate_mysql_datetime($appointment['start_datetime'])) {
|
|
|
|
throw new Exception('Appointment start datetime is invalid.');
|
|
|
|
}
|
2013-05-11 13:19:18 +03:00
|
|
|
|
2013-09-26 19:06:57 +03:00
|
|
|
if (!validate_mysql_datetime($appointment['end_datetime'])) {
|
|
|
|
throw new Exception('Appointment end datetime is invalid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the provider's id is valid.
|
|
|
|
$num_rows = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.id', $appointment['id_users_provider'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_PROVIDER)
|
|
|
|
->get()->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
throw new Exception('Appointment provider id is invalid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($appointment['is_unavailable'] == FALSE) {
|
|
|
|
// Check if the customer's id is valid.
|
2013-05-14 22:56:16 +03:00
|
|
|
$num_rows = $this->db
|
2013-06-08 12:54:45 +03:00
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
2013-09-26 19:06:57 +03:00
|
|
|
->where('ea_users.id', $appointment['id_users_customer'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_CUSTOMER)
|
2013-06-08 12:54:45 +03:00
|
|
|
->get()->num_rows();
|
2013-05-14 22:56:16 +03:00
|
|
|
if ($num_rows == 0) {
|
2013-09-26 19:06:57 +03:00
|
|
|
throw new Exception('Appointment customer id is invalid.');
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
|
|
|
|
// Check if the service id is valid.
|
|
|
|
$num_rows = $this->db->get_where('ea_services',
|
|
|
|
array('id' => $appointment['id_services']))->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
throw new Exception('Appointment customer id is invalid.');
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Delete an existing appointment record from the database.
|
|
|
|
*
|
2013-05-15 18:03:47 +03:00
|
|
|
* @expectedException InvalidArgumentException Raises when the $appointment_id
|
|
|
|
* is not an integer.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param numeric $appointment_id The record id to be deleted.
|
2013-05-04 00:26:04 +03:00
|
|
|
* @return bool Returns the delete operation result.
|
|
|
|
*/
|
|
|
|
public function delete($appointment_id) {
|
2013-05-17 16:09:10 +03:00
|
|
|
if (!is_numeric($appointment_id)) {
|
2013-07-09 17:46:48 +03:00
|
|
|
throw new Exception('Invalid argument type $appointment_id (value:"' . $appointment_id . '")');
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
|
|
|
|
2013-07-09 17:46:48 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows();
|
2013-05-14 22:56:16 +03:00
|
|
|
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
return FALSE; // Record does not exist.
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
$this->db->where('id', $appointment_id);
|
|
|
|
return $this->db->delete('ea_appointments');
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Get a specific row from the appointments table.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param numeric $appointment_id The record's id to be returned.
|
2013-05-04 00:26:04 +03:00
|
|
|
* @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($appointment_id) {
|
2013-05-17 16:09:10 +03:00
|
|
|
if (!is_numeric($appointment_id)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Invalid argument given. Expected '
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'integer for the $appointment_id : ' . $appointment_id);
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
2013-06-08 12:54:45 +03:00
|
|
|
return $this->db->get_where('ea_appointments',
|
|
|
|
array('id' => $appointment_id))->row_array();
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
2013-06-08 12:54:45 +03:00
|
|
|
* @param string $field_name The field name of the value to be returned.
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param numeric $appointment_id The selected record's id.
|
2013-05-04 00:26:04 +03:00
|
|
|
* @return string Returns the records value from the database.
|
|
|
|
*/
|
|
|
|
public function get_value($field_name, $appointment_id) {
|
2013-05-17 16:09:10 +03:00
|
|
|
if (!is_numeric($appointment_id)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Invalid argument given, expected '
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'integer for the $appointment_id : ' . $appointment_id);
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_string($field_name)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Invalid argument given, expected '
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'string for the $field_name : ' . $field_name);
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
|
|
|
|
2013-06-08 12:54:45 +03:00
|
|
|
if ($this->db->get_where('ea_appointments',
|
|
|
|
array('id' => $appointment_id))->num_rows() == 0) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('The record with the provided id '
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'does not exist in the database : ' . $appointment_id);
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
|
|
|
|
2013-06-08 12:54:45 +03:00
|
|
|
$row_data = $this->db->get_where('ea_appointments',
|
|
|
|
array('id' => $appointment_id))->row_array();
|
2013-05-14 22:56:16 +03:00
|
|
|
|
|
|
|
if (!isset($row_data[$field_name])) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('The given field name does not '
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'exist in the database : ' . $field_name);
|
2013-05-14 22:56:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $row_data[$field_name];
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all, or specific records from appointment's table.
|
|
|
|
*
|
|
|
|
* @example $this->Model->getBatch('id = ' . $recordId);
|
|
|
|
*
|
|
|
|
* @param string $where_clause (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 = '') {
|
|
|
|
if ($where_clause != '') {
|
|
|
|
$this->db->where($where_clause);
|
|
|
|
}
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-05-08 17:31:17 +03:00
|
|
|
return $this->db->get('ea_appointments')->result_array();
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-06-03 17:42:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a unique hash for the given appointment data.
|
|
|
|
*
|
|
|
|
* This method uses the current date-time to generate a unique
|
|
|
|
* hash string that is later used to identify this appointment.
|
|
|
|
* Hash is needed when the email is send to the user with an
|
|
|
|
* edit link.
|
|
|
|
*
|
|
|
|
* @return string Returns the unique appointment hash.
|
|
|
|
*/
|
|
|
|
public function generate_hash() {
|
|
|
|
$current_date = new DateTime();
|
|
|
|
return md5($current_date->getTimestamp());
|
|
|
|
}
|
2013-07-09 17:46:48 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts or updates an unavailable period record in the database.
|
|
|
|
*
|
|
|
|
* @param array $unavailable Contains the unavaible data.
|
|
|
|
* @return int Returns the record id.
|
|
|
|
*/
|
|
|
|
public function add_unavailable($unavailable) {
|
|
|
|
// Validate period
|
|
|
|
$start = strtotime($unavailable['start_datetime']);
|
|
|
|
$end = strtotime($unavailable['end_datetime']);
|
|
|
|
if ($start > $end) {
|
|
|
|
throw new Exception('Unavailable period start must be prior to end.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate provider record
|
|
|
|
$where_clause = array(
|
|
|
|
'id' => $unavailable['id_users_provider'],
|
|
|
|
'id_roles' => $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->db->get_where('ea_users', $where_clause)->num_rows() == 0) {
|
|
|
|
throw new Exception('Provider id was not found in database.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add record to database (insert or update).
|
|
|
|
if (!isset($unavailable['id'])) {
|
|
|
|
$unavailable['book_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$unavailable['is_unavailable'] = true;
|
|
|
|
|
|
|
|
$this->db->insert('ea_appointments', $unavailable);
|
|
|
|
$unavailable['id'] = $this->db->insert_id();
|
|
|
|
} else {
|
|
|
|
$this->db->where(array('id' => $unavailable['id']));
|
|
|
|
$this->db->update('ea_appointments', $unavailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $unavailable['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an unavailable period.
|
|
|
|
*
|
|
|
|
* @param numeric $unavailable_id Record id to be deleted.
|
|
|
|
*/
|
|
|
|
public function delete_unavailable($unavailable_id) {
|
|
|
|
if (!is_numeric($unavailable_id)) {
|
2013-07-10 16:57:24 +03:00
|
|
|
throw new Exception('Invalid argument type $unavailable_id (value:"' .
|
2013-07-09 17:46:48 +03:00
|
|
|
$unavailable_id . '")');
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_rows = $this->db->get_where('ea_appointments', array('id' => $unavailable_id))
|
|
|
|
->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
return FALSE; // Record does not exist.
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('id', $unavailable_id);
|
|
|
|
return $this->db->delete('ea_appointments');
|
|
|
|
}
|
2013-11-21 23:58:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear google sync IDs from appointment record.
|
|
|
|
*
|
|
|
|
* @param numeric $provider_id The appointment provider record id.
|
|
|
|
*/
|
|
|
|
public function clear_google_sync_ids($provider_id) {
|
|
|
|
if (!is_numeric($provider_id)) {
|
|
|
|
throw new Exception('Invalid argument type $provider_id (value: "'
|
|
|
|
. $provider_id . '")');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->update('ea_appointments', array('id_google_calendar' => NULL),
|
|
|
|
array('id_users_provider' => $provider_id));
|
|
|
|
}
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
/* End of file appointments_model.php */
|
|
|
|
/* Location: ./application/models/appointments_model.php */
|