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