1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2:
3: class Customers_Model extends CI_Model {
4: /**
5: * Class Constructor
6: */
7: public function __construct() {
8: parent::__construct();
9: }
10:
11: /**
12: * Add a customer record to the database.
13: *
14: * This method adds a customer to the database. If the customer
15: * doesn't exists it is going to be inserted, otherwise the
16: * record is going to be updated.
17: *
18: * @param array $customer Associative array with the customer's
19: * data. Each key has the same name with the database fields.
20: * @return int Returns the customer id.
21: */
22: public function add($customer) {
23: // Validate the customer data before doing anything.
24: if (!$this->validate($customer)) {
25: throw new Exception('Customer data are not valid.');
26: }
27:
28: // :: CHECK IF CUSTOMER ALREADY EXIST (FROM EMAIL).
29: if ($this->exists($customer) && !isset($customer['id'])) {
30: // Find the customer id from the database.
31: $customer['id'] = $this->find_record_id($customer);
32: }
33:
34: // :: INSERT OR UPDATE CUSTOMER RECORD
35: if (!isset($customer['id'])) {
36: $customer['id'] = $this->insert($customer);
37: } else {
38: $this->update($customer);
39: }
40:
41: return $customer['id'];
42: }
43:
44: /**
45: * Check if a particular customer record already exists.
46: *
47: * This method checks wether the given customer already exists in
48: * the database. It doesn't search with the id, but with the following
49: * fields: "email"
50: *
51: * @param array $customer Associative array with the customer's
52: * data. Each key has the same name with the database fields.
53: * @return bool Returns wether the record exists or not.
54: */
55: public function exists($customer) {
56: if (!isset($customer['email'])) {
57: throw new Exception('Customer\'s email is not provided.');
58: }
59:
60: // This method shouldn't depend on another method of this class.
61: $num_rows = $this->db
62: ->select('*')
63: ->from('ea_users')
64: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
65: ->where('ea_users.email', $customer['email'])
66: ->where('ea_roles.slug', DB_SLUG_CUSTOMER)
67: ->get()->num_rows();
68:
69: return ($num_rows > 0) ? TRUE : FALSE;
70: }
71:
72: /**
73: * Insert a new customer record to the database.
74: *
75: * @param array $customer Associative array with the customer's
76: * data. Each key has the same name with the database fields.
77: * @return int Returns the id of the new record.
78: */
79: private function insert($customer) {
80: // Before inserting the customer we need to get the customer's role id
81: // from the database and assign it to the new record as a foreign key.
82: $customer_role_id = $this->db
83: ->select('id')
84: ->from('ea_roles')
85: ->where('slug', DB_SLUG_CUSTOMER)
86: ->get()->row()->id;
87:
88: $customer['id_roles'] = $customer_role_id;
89:
90: if (!$this->db->insert('ea_users', $customer)) {
91: throw new Exception('Could not insert customer to the database.');
92: }
93:
94: return intval($this->db->insert_id());
95: }
96:
97: /**
98: * Update an existing customer record in the database.
99: *
100: * The customer data argument should already include the record
101: * id in order to process the update operation.
102: *
103: * @param array $customer Associative array with the customer's
104: * data. Each key has the same name with the database fields.
105: * @return int Returns the updated record id.
106: */
107: private function update($customer) {
108: $this->db->where('id', $customer['id']);
109: if (!$this->db->update('ea_users', $customer)) {
110: throw new Exception('Could not update customer to the database.');
111: }
112:
113: return intval($customer['id']);
114: }
115:
116: /**
117: * Find the database id of a customer record.
118: *
119: * The customer data should include the following fields in order to
120: * get the unique id from the database: "email"
121: *
122: * <strong>IMPORTANT!</strong> The record must already exists in the
123: * database, otherwise an exception is raised.
124: *
125: * @param array $customer Array with the customer data. The
126: * keys of the array should have the same names as the db fields.
127: * @return int Returns the id.
128: */
129: public function find_record_id($customer) {
130: if (!isset($customer['email'])) {
131: throw new Exception('Customer\'s email was not provided : '
132: . print_r($customer, TRUE));
133: }
134:
135: // Get customer's role id
136: $result = $this->db
137: ->select('ea_users.id')
138: ->from('ea_users')
139: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
140: ->where('ea_users.email', $customer['email'])
141: ->where('ea_roles.slug', DB_SLUG_CUSTOMER)
142: ->get();
143:
144: if ($result->num_rows() == 0) {
145: throw new Exception('Could not find customer record id.');
146: }
147:
148: return $result->row()->id;
149: }
150:
151: /**
152: * Validate customer data before the insert or
153: * update operation is executed.
154: *
155: * @param array $customer Contains the customer data.
156: * @return bool Returns the validation result.
157: */
158: public function validate($customer) {
159: $this->load->helper('data_validation');
160:
161: try {
162: // If a customer id is provided, check whether the record
163: // exist in the database.
164: if (isset($customer['id'])) {
165: $num_rows = $this->db->get_where('ea_users',
166: array('id' => $customer['id']))->num_rows();
167: if ($num_rows == 0) {
168: throw new Exception('Provided customer id does not '
169: . 'exist in the database.');
170: }
171: }
172: // Validate required fields
173: if (!isset($customer['last_name'])
174: || !isset($customer['email'])
175: || !isset($customer['phone_number'])) {
176: throw new Exception('Not all required fields are provided : '
177: . print_r($customer, TRUE));
178: }
179:
180: // Validate email address
181: if (!filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) {
182: throw new Exception('Invalid email address provided : '
183: . $customer['email']);
184: }
185:
186: return TRUE;
187: } catch (Exception $exc) {
188: return FALSE;
189: }
190: }
191:
192: /**
193: * Delete an existing customer record from the database.
194: *
195: * @param numeric $customer_id The record id to be deleted.
196: * @return bool Returns the delete operation result.
197: */
198: public function delete($customer_id) {
199: if (!is_numeric($customer_id)) {
200: throw new Exception('Invalid argument type $customer_id : ' . $customer_id);
201: }
202:
203: $num_rows = $this->db->get_where('ea_users', array('id' => $customer_id))->num_rows();
204: if ($num_rows == 0) {
205: return FALSE;
206: }
207:
208: return $this->db->delete('ea_users', array('id' => $customer_id));
209: }
210:
211: /**
212: * Get a specific row from the appointments table.
213: *
214: * @param numeric $customer_id The record's id to be returned.
215: * @return array Returns an associative array with the selected
216: * record's data. Each key has the same name as the database
217: * field names.
218: */
219: public function get_row($customer_id) {
220: if (!is_numeric($customer_id)) {
221: throw new Exception('Invalid argument provided as $customer_id : ' . $customer_id);
222: }
223: return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array();
224: }
225:
226: /**
227: * Get a specific field value from the database.
228: *
229: * @param string $field_name The field name of the value to be
230: * returned.
231: * @param int $customer_id The selected record's id.
232: * @return string Returns the records value from the database.
233: */
234: public function get_value($field_name, $customer_id) {
235: if (!is_numeric($customer_id)) {
236: throw new Exception('Invalid argument provided as $customer_id : '
237: . $customer_id);
238: }
239:
240: if (!is_string($field_name)) {
241: throw new Exception('$field_name argument is not a string : '
242: . $field_name);
243: }
244:
245: if ($this->db->get_where('ea_users', array('id' => $customer_id))->num_rows() == 0) {
246: throw new Exception('The record with the $customer_id argument '
247: . 'does not exist in the database : ' . $customer_id);
248: }
249:
250: $row_data = $this->db->get_where('ea_users', array('id' => $customer_id)
251: )->row_array();
252: if (!isset($row_data[$field_name])) {
253: throw new Exception('The given $field_name argument does not'
254: . 'exist in the database : ' . $field_name);
255: }
256:
257: $customer = $this->db->get_where('ea_users', array('id' => $customer_id))->row_array();
258:
259: return $customer[$field_name];
260: }
261:
262: /**
263: * Get all, or specific records from appointment's table.
264: *
265: * @example $this->Model->getBatch('id = ' . $recordId);
266: *
267: * @param string $whereClause (OPTIONAL) The WHERE clause of
268: * the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD.
269: * @return array Returns the rows from the database.
270: */
271: public function get_batch($where_clause = '') {
272: $customers_role_id = $this->get_customers_role_id();
273:
274: if ($where_clause != '') {
275: $this->db->where($where_clause);
276: }
277:
278: $this->db->where('id_roles', $customers_role_id);
279:
280: return $this->db->get('ea_users')->result_array();
281: }
282:
283: /**
284: * Get the customers role id from the database.
285: *
286: * @return int Returns the role id for the customer records.
287: */
288: public function get_customers_role_id() {
289: return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_CUSTOMER))->row()->id;
290: }
291: }
292:
293: /* End of file customers_model.php */
294: /* Location: ./application/models/customers_model.php */