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: $this->db->where('id', $customer['id']);
107: if (!$this->db->update('ea_users', $customer)) {
108: throw new Exception('Could not update customer to the database.');
109: }
110:
111: return intval($customer['id']);
112: }
113:
114: /**
115: * Find the database id of a customer record.
116: *
117: * The customer data should include the following fields in order to
118: * get the unique id from the database: "email"
119: *
120: * <strong>IMPORTANT!</strong> The record must already exists in the
121: * database, otherwise an exception is raised.
122: *
123: * @param array $customer Array with the customer data. The
124: * keys of the array should have the same names as the db fields.
125: * @return int Returns the id.
126: */
127: public function find_record_id($customer) {
128: if (!isset($customer['email'])) {
129: throw new Exception('Customer\'s email was not provided : '
130: . print_r($customer, TRUE));
131: }
132:
133: // Get customer's role id
134: $result = $this->db
135: ->select('ea_users.id')
136: ->from('ea_users')
137: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
138: ->where('ea_users.email', $customer['email'])
139: ->where('ea_roles.slug', DB_SLUG_CUSTOMER)
140: ->get();
141:
142: if ($result->num_rows() == 0) {
143: throw new Exception('Could not find customer record id.');
144: }
145:
146: return $result->row()->id;
147: }
148:
149: /**
150: * Validate customer data before the insert or
151: * update operation is executed.
152: *
153: * @param array $customer Contains the customer data.
154: * @return bool Returns the validation result.
155: */
156: public function validate($customer) {
157: $this->load->helper('data_validation');
158:
159:
160: // If a customer id is provided, check whether the record
161: // exist in the database.
162: if (isset($customer['id'])) {
163: $num_rows = $this->db->get_where('ea_users',
164: array('id' => $customer['id']))->num_rows();
165: if ($num_rows == 0) {
166: throw new Exception('Provided customer id does not '
167: . 'exist in the database.');
168: }
169: }
170: // Validate required fields
171: if (!isset($customer['last_name'])
172: || !isset($customer['email'])
173: || !isset($customer['phone_number'])) {
174: throw new Exception('Not all required fields are provided : '
175: . print_r($customer, TRUE));
176: }
177:
178: // Validate email address
179: if (!filter_var($customer['email'], FILTER_VALIDATE_EMAIL)) {
180: throw new Exception('Invalid email address provided : '
181: . $customer['email']);
182: }
183:
184: return TRUE;
185: }
186:
187: /**
188: * Delete an existing customer record from the database.
189: *
190: * @param numeric $customer_id The record id to be deleted.
191: * @return bool Returns the delete operation result.
192: */
193: public function delete($customer_id) {
194: if (!is_numeric($customer_id)) {
195: throw new Exception('Invalid argument type $customer_id : ' . $customer_id);
196: }
197:
198: $num_rows = $this->db->get_where('ea_users', array('id' => $customer_id))->num_rows();
199: if ($num_rows == 0) {
200: return FALSE;
201: }
202:
203: return $this->db->delete('ea_users', array('id' => $customer_id));
204: }
205:
206: /**
207: * Get a specific row from the appointments table.
208: *
209: * @param numeric $customer_id The record's id to be returned.
210: * @return array Returns an associative array with the selected
211: * record's data. Each key has the same name as the database
212: * field names.
213: */
214: public function get_row($customer_id) {
215: if (!is_numeric($customer_id)) {
216: throw new Exception('Invalid argument provided as $customer_id : ' . $customer_id);
217: }
218: return $this->db->get_where('ea_users', array('id' => $customer_id))->row_array();
219: }
220:
221: /**
222: * Get a specific field value from the database.
223: *
224: * @param string $field_name The field name of the value to be
225: * returned.
226: * @param int $customer_id The selected record's id.
227: * @return string Returns the records value from the database.
228: */
229: public function get_value($field_name, $customer_id) {
230: if (!is_numeric($customer_id)) {
231: throw new Exception('Invalid argument provided as $customer_id : '
232: . $customer_id);
233: }
234:
235: if (!is_string($field_name)) {
236: throw new Exception('$field_name argument is not a string : '
237: . $field_name);
238: }
239:
240: if ($this->db->get_where('ea_users', array('id' => $customer_id))->num_rows() == 0) {
241: throw new Exception('The record with the $customer_id argument '
242: . 'does not exist in the database : ' . $customer_id);
243: }
244:
245: $row_data = $this->db->get_where('ea_users', array('id' => $customer_id)
246: )->row_array();
247: if (!isset($row_data[$field_name])) {
248: throw new Exception('The given $field_name argument does not'
249: . 'exist in the database : ' . $field_name);
250: }
251:
252: $customer = $this->db->get_where('ea_users', array('id' => $customer_id))->row_array();
253:
254: return $customer[$field_name];
255: }
256:
257: /**
258: * Get all, or specific records from appointment's table.
259: *
260: * @example $this->Model->getBatch('id = ' . $recordId);
261: *
262: * @param string $whereClause (OPTIONAL) The WHERE clause of
263: * the query to be executed. DO NOT INCLUDE 'WHERE' KEYWORD.
264: * @return array Returns the rows from the database.
265: */
266: public function get_batch($where_clause = '') {
267: $customers_role_id = $this->get_customers_role_id();
268:
269: if ($where_clause != '') {
270: $this->db->where($where_clause);
271: }
272:
273: $this->db->where('id_roles', $customers_role_id);
274:
275: return $this->db->get('ea_users')->result_array();
276: }
277:
278: /**
279: * Get the customers role id from the database.
280: *
281: * @return int Returns the role id for the customer records.
282: */
283: public function get_customers_role_id() {
284: return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_CUSTOMER))->row()->id;
285: }
286: }
287:
288: /* End of file customers_model.php */
289: /* Location: ./application/models/customers_model.php */