2013-05-04 00:26:04 +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
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) 2013 - 2015, Alex Tselegidis
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2015-07-08 01:36:16 +03:00
|
|
|
/**
|
|
|
|
* Customers Model
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2013-04-20 20:20:16 +03:00
|
|
|
class Customers_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 a customer record to the database.
|
|
|
|
*
|
2013-04-20 20:20:16 +03:00
|
|
|
* This method adds a customer to the database. If the customer
|
2013-05-04 00:26:04 +03:00
|
|
|
* 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 $customer Associative array with the customer's
|
2013-05-04 00:26:04 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
* @return int Returns the customer id.
|
2013-04-20 20:20:16 +03:00
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function add($customer) {
|
2013-06-08 12:54:45 +03:00
|
|
|
// Validate the customer data before doing anything.
|
2014-01-11 02:07:25 +02:00
|
|
|
$this->validate($customer);
|
2013-06-20 00:12:06 +03:00
|
|
|
|
|
|
|
// :: CHECK IF CUSTOMER ALREADY EXIST (FROM EMAIL).
|
2013-07-06 03:00:04 +03:00
|
|
|
if ($this->exists($customer) && !isset($customer['id'])) {
|
2013-06-20 00:12:06 +03:00
|
|
|
// Find the customer id from the database.
|
2013-07-26 09:16:24 +03:00
|
|
|
$customer['id'] = $this->find_record_id($customer);
|
2013-06-20 00:12:06 +03:00
|
|
|
}
|
2013-05-15 18:03:47 +03:00
|
|
|
|
2013-06-20 00:12:06 +03:00
|
|
|
// :: INSERT OR UPDATE CUSTOMER RECORD
|
2013-07-06 03:00:04 +03:00
|
|
|
if (!isset($customer['id'])) {
|
|
|
|
$customer['id'] = $this->insert($customer);
|
2013-05-15 18:03:47 +03:00
|
|
|
} else {
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->update($customer);
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-05-15 18:03:47 +03:00
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
return $customer['id'];
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-04 00:26:04 +03:00
|
|
|
* Check if a particular customer record already exists.
|
2013-04-20 20:20:16 +03:00
|
|
|
*
|
2013-05-04 00:26:04 +03:00
|
|
|
* This method checks wether the given customer already exists in
|
2013-05-15 18:03:47 +03:00
|
|
|
* the database. It doesn't search with the id, but with the following
|
|
|
|
* fields: "email"
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $customer Associative array with the customer's
|
2013-05-15 18:03:47 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
* @return bool Returns wether the record exists or not.
|
2013-04-20 20:20:16 +03:00
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function exists($customer) {
|
|
|
|
if (!isset($customer['email'])) {
|
|
|
|
throw new Exception('Customer\'s email is not provided.');
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method shouldn't depend on another method of this class.
|
|
|
|
$num_rows = $this->db
|
2013-07-15 17:27:19 +03:00
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.email', $customer['email'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
->get()->num_rows();
|
2013-05-15 18:03:47 +03:00
|
|
|
|
|
|
|
return ($num_rows > 0) ? TRUE : FALSE;
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-04 00:26:04 +03:00
|
|
|
* Insert a new customer record to the database.
|
2013-04-20 20:20:16 +03:00
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $customer Associative array with the customer'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-04-20 20:20:16 +03:00
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
private function insert($customer) {
|
2013-05-04 00:26:04 +03:00
|
|
|
// 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.
|
2013-05-15 18:03:47 +03:00
|
|
|
$customer_role_id = $this->db
|
2013-07-15 17:27:19 +03:00
|
|
|
->select('id')
|
|
|
|
->from('ea_roles')
|
|
|
|
->where('slug', DB_SLUG_CUSTOMER)
|
|
|
|
->get()->row()->id;
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
$customer['id_roles'] = $customer_role_id;
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
if (!$this->db->insert('ea_users', $customer)) {
|
|
|
|
throw new Exception('Could not insert customer to the database.');
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
2013-05-15 18:03:47 +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 customer record in the database.
|
|
|
|
*
|
|
|
|
* The customer data argument should already include the record
|
|
|
|
* id in order to process the update operation.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $customer Associative array with the customer's
|
2013-05-04 00:26:04 +03:00
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
* @return int Returns the updated record id.
|
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
private function update($customer) {
|
2014-01-11 02:07:25 +02:00
|
|
|
// Do not update empty string values.
|
|
|
|
foreach ($customer as $key => $value) {
|
|
|
|
if ($value === '')
|
|
|
|
unset($customer[$key]);
|
|
|
|
}
|
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
$this->db->where('id', $customer['id']);
|
|
|
|
if (!$this->db->update('ea_users', $customer)) {
|
|
|
|
throw new Exception('Could not update customer to the database.');
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 03:00:04 +03:00
|
|
|
return intval($customer['id']);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the database id of a customer record.
|
|
|
|
*
|
|
|
|
* The customer data should include the following fields in order to
|
|
|
|
* get the unique id from the database: "email"
|
|
|
|
*
|
|
|
|
* <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 $customer Array with the customer data. The
|
2013-05-15 18:03:47 +03:00
|
|
|
* keys of the array should have the same names as the db fields.
|
|
|
|
* @return int Returns the id.
|
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function find_record_id($customer) {
|
|
|
|
if (!isset($customer['email'])) {
|
2013-07-10 16:57:24 +03:00
|
|
|
throw new Exception('Customer\'s email was not provided : '
|
2013-07-06 03:00:04 +03:00
|
|
|
. print_r($customer, TRUE));
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get customer's role id
|
|
|
|
$result = $this->db
|
2013-07-15 17:27:19 +03:00
|
|
|
->select('ea_users.id')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_users.email', $customer['email'])
|
|
|
|
->where('ea_roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
->get();
|
2013-05-15 18:03:47 +03:00
|
|
|
|
|
|
|
if ($result->num_rows() == 0) {
|
2013-07-27 00:30:44 +03:00
|
|
|
throw new Exception('Could not find customer record id.');
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result->row()->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-01-11 02:07:25 +02:00
|
|
|
* Validate customer data before the insert or update operation is executed.
|
2013-05-15 18:03:47 +03:00
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param array $customer Contains the customer data.
|
2013-05-15 18:03:47 +03:00
|
|
|
* @return bool Returns the validation result.
|
|
|
|
*/
|
2013-07-06 03:00:04 +03:00
|
|
|
public function validate($customer) {
|
2013-05-15 18:03:47 +03:00
|
|
|
$this->load->helper('data_validation');
|
|
|
|
|
2013-09-26 19:06:57 +03:00
|
|
|
// If a customer id is provided, check whether the record
|
|
|
|
// exist in the database.
|
|
|
|
if (isset($customer['id'])) {
|
|
|
|
$num_rows = $this->db->get_where('ea_users',
|
|
|
|
array('id' => $customer['id']))->num_rows();
|
|
|
|
if ($num_rows == 0) {
|
|
|
|
throw new Exception('Provided customer id does not '
|
|
|
|
. 'exist in the database.');
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
// Validate required fields
|
|
|
|
if (!isset($customer['last_name'])
|
|
|
|
|| !isset($customer['email'])
|
|
|
|
|| !isset($customer['phone_number'])) {
|
|
|
|
throw new Exception('Not all required fields are provided : '
|
|
|
|
. print_r($customer, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate email address
|
|
|
|
if (!filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) {
|
|
|
|
throw new Exception('Invalid email address provided : '
|
|
|
|
. $customer['email']);
|
|
|
|
}
|
2013-11-23 21:10:31 +02:00
|
|
|
|
|
|
|
// When inserting a record the email address must be unique.
|
2013-11-24 19:01:46 +02:00
|
|
|
$customer_id = (isset($customer['id'])) ? $customer['id'] : '';
|
|
|
|
|
|
|
|
$num_rows = $this->db
|
|
|
|
->select('*')
|
|
|
|
->from('ea_users')
|
|
|
|
->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
|
|
|
|
->where('ea_roles.slug', DB_SLUG_CUSTOMER)
|
|
|
|
->where('ea_users.email', $customer['email'])
|
|
|
|
->where('ea_users.id <>', $customer_id)
|
|
|
|
->get()
|
|
|
|
->num_rows();
|
|
|
|
|
|
|
|
if ($num_rows > 0) {
|
|
|
|
throw new Exception('Given email address belongs to another customer record. '
|
|
|
|
. 'Please use a different email.');
|
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
|
|
|
|
return TRUE;
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing customer record from the database.
|
|
|
|
*
|
2013-07-27 00:30:44 +03:00
|
|
|
* @param numeric $customer_id The record id to be deleted.
|
2013-05-04 00:26:04 +03:00
|
|
|
* @return bool Returns the delete operation result.
|
|
|
|
*/
|
2013-07-27 00:30:44 +03:00
|
|
|
public function delete($customer_id) {
|
|
|
|
if (!is_numeric($customer_id)) {
|
|
|
|
throw new Exception('Invalid argument type $customer_id : ' . $customer_id);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
2013-07-27 00:30:44 +03:00
|
|
|
$num_rows = $this->db->get_where('ea_users', array('id' => $customer_id))->num_rows();
|
2013-05-15 18:03:47 +03:00
|
|
|
if ($num_rows == 0) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-07-27 00:30:44 +03:00
|
|
|
return $this->db->delete('ea_users', array('id' => $customer_id));
|
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-04-20 20:20:16 +03:00
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @param numeric $customer_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.
|
2013-04-20 20:20:16 +03:00
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
public function get_row($customer_id) {
|
2013-05-17 16:09:10 +03:00
|
|
|
if (!is_numeric($customer_id)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Invalid argument provided as $customer_id : ' . $customer_id);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
|
|
|
* @param string $field_name The field name of the value to be
|
|
|
|
* returned.
|
|
|
|
* @param int $customer_id The selected record's id.
|
|
|
|
* @return string Returns the records value from the database.
|
|
|
|
*/
|
|
|
|
public function get_value($field_name, $customer_id) {
|
2013-05-17 16:09:10 +03:00
|
|
|
if (!is_numeric($customer_id)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Invalid argument provided as $customer_id : '
|
2013-06-08 12:54:45 +03:00
|
|
|
. $customer_id);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_string($field_name)) {
|
2013-07-10 16:57:24 +03:00
|
|
|
throw new Exception('$field_name argument is not a string : '
|
2013-06-08 12:54:45 +03:00
|
|
|
. $field_name);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->db->get_where('ea_users', array('id' => $customer_id))->num_rows() == 0) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('The record with the $customer_id argument '
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'does not exist in the database : ' . $customer_id);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
2013-06-08 12:54:45 +03:00
|
|
|
$row_data = $this->db->get_where('ea_users', array('id' => $customer_id)
|
|
|
|
)->row_array();
|
2013-05-15 18:03:47 +03:00
|
|
|
if (!isset($row_data[$field_name])) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('The given $field_name argument does not'
|
2013-06-08 12:54:45 +03:00
|
|
|
. 'exist in the database : ' . $field_name);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
2013-07-26 09:16:24 +03:00
|
|
|
$customer = $this->db->get_where('ea_users', array('id' => $customer_id))->row_array();
|
|
|
|
|
|
|
|
return $customer[$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 $whereClause (OPTIONAL) The WHERE clause of
|
|
|
|
* the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD.
|
|
|
|
* @return array Returns the rows from the database.
|
|
|
|
*/
|
2013-05-15 18:03:47 +03:00
|
|
|
public function get_batch($where_clause = '') {
|
2013-05-04 00:26:04 +03:00
|
|
|
$customers_role_id = $this->get_customers_role_id();
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-05-15 18:03:47 +03:00
|
|
|
if ($where_clause != '') {
|
2013-05-04 00:26:04 +03:00
|
|
|
$this->db->where($where_clause);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('id_roles', $customers_role_id);
|
|
|
|
|
2013-05-08 17:31:17 +03:00
|
|
|
return $this->db->get('ea_users')->result_array();
|
2013-05-04 00:26:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the customers role id from the database.
|
|
|
|
*
|
|
|
|
* @return int Returns the role id for the customer records.
|
|
|
|
*/
|
|
|
|
public function get_customers_role_id() {
|
|
|
|
return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_CUSTOMER))->row()->id;
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
/* End of file customers_model.php */
|
|
|
|
/* Location: ./application/models/customers_model.php */
|