2013-05-04 00:26:04 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
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-04-20 20:20:16 +03:00
|
|
|
*
|
2013-05-04 00:26:04 +03:00
|
|
|
* @param array $customer_data Associative array with the customer's
|
|
|
|
* 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-05-04 00:26:04 +03:00
|
|
|
public function add($customer_data) {
|
2013-04-20 20:20:16 +03:00
|
|
|
try {
|
2013-05-04 00:26:04 +03:00
|
|
|
$customer_id = $this->exists($customer_data['email']);
|
|
|
|
|
|
|
|
if (!$customer_id) {
|
|
|
|
$customer_id = $this->insert($customer_data);
|
2013-04-20 20:20:16 +03:00
|
|
|
} else {
|
2013-05-04 00:26:04 +03:00
|
|
|
$customer_data['id'] = $customer_id;
|
|
|
|
$this->update($customer_data);
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
return $customer_id;
|
|
|
|
|
2013-04-20 20:20:16 +03:00
|
|
|
} catch(Exception $exc) {
|
|
|
|
// Send some info for the exception back to the browser.
|
2013-05-04 00:26:04 +03:00
|
|
|
echo $exc->getTraceAsString();
|
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
|
|
|
|
* the database. This method does not search with the id, but witha
|
|
|
|
* the email value of the customer.
|
|
|
|
*
|
|
|
|
* @param string $customer_email The email field value is used to
|
|
|
|
* distinguish customer records.
|
|
|
|
* @return int|bool Returns the record id or FALSE if it doesn't exist.
|
2013-04-20 20:20:16 +03:00
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
public function exists($customer_email) {
|
|
|
|
$this->db->where(array(
|
|
|
|
'email' => $customer_email,
|
|
|
|
'id_roles' => $this->get_customers_role_id()
|
|
|
|
));
|
|
|
|
$result = $this->db->get('ea_users');
|
|
|
|
return ($result->num_rows() > 0) ? $result->row()->id : 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-05-04 00:26:04 +03:00
|
|
|
* @param array $customer_data Associative array with the customer's
|
|
|
|
* 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-05-04 00:26:04 +03:00
|
|
|
private function insert($customer_data) {
|
|
|
|
// 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-04-20 20:20:16 +03:00
|
|
|
$this->db
|
|
|
|
->select('id')
|
|
|
|
->from('ea_roles')
|
2013-05-04 00:26:04 +03:00
|
|
|
->where('slug', DB_SLUG_CUSTOMER);
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
$customer_role_id = $this->db->get()->row()->id;
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
if ($customer_role_id !== NULL) {
|
|
|
|
$customer_data['id_roles'] = $customer_role_id;
|
2013-04-20 20:20:16 +03:00
|
|
|
}
|
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
if (!$this->db->insert('ea_users', $customer_data)) {
|
2013-04-20 20:20:16 +03:00
|
|
|
throw new Exception('Could not insert customer to the database.');
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
return $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.
|
|
|
|
*
|
|
|
|
* @param array $customer_data Associative array with the customer's
|
|
|
|
* data. Each key has the same name with the database fields.
|
|
|
|
* @return int Returns the updated record id.
|
|
|
|
*/
|
|
|
|
private function update($customer_data) {
|
|
|
|
$this->db->where('id', $customer_data['id']);
|
|
|
|
if (!$this->db->update('ea_users', $customer_data)) {
|
2013-04-20 20:20:16 +03:00
|
|
|
throw new Exception('Could not update customer to the database.');
|
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
return $this->db->get_where('ea_users', array('email' => $customer_data['email']))->row()->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an existing customer record from the database.
|
|
|
|
*
|
|
|
|
* @param int $customer_id The record id to be deleted.
|
|
|
|
* @return bool Returns the delete operation result.
|
|
|
|
*/
|
|
|
|
public function delete($customer_id) {
|
|
|
|
$this->db->where('id', $customer_id);
|
|
|
|
return $this->db->delete('ea_users');
|
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-05-04 00:26:04 +03:00
|
|
|
* @param int $customer_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.
|
2013-04-20 20:20:16 +03:00
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
public function get_row($customer_id) {
|
|
|
|
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) {
|
|
|
|
return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array()[$field_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
public function get_batch($where_clause = NULL) {
|
|
|
|
$customers_role_id = $this->get_customers_role_id();
|
2013-04-20 20:20:16 +03:00
|
|
|
|
2013-05-04 00:26:04 +03:00
|
|
|
if ($where_clause != NULL) {
|
|
|
|
$this->db->where($where_clause);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->where('id_roles', $customers_role_id);
|
|
|
|
|
|
|
|
return $this->db->get('ea_users')->results_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 */
|