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