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