1: <?php
2: class Services_Model extends CI_Model {
3: 4: 5:
6: function __construct() {
7: parent::__construct();
8: }
9:
10: 11: 12: 13: 14: 15: 16:
17: public function add($service) {
18: if (!$this->validate($service)) {
19: throw new Exception('Service data are invalid.');
20: }
21:
22: if (!isset($service['id'])) {
23: $service['id'] = $this->insert($service);
24: } else {
25: $this->update($service);
26: }
27:
28: return intval($service['id']);
29: }
30:
31: 32: 33: 34: 35: 36:
37: public function insert($service) {
38: if (!$this->db->insert('ea_services', $service)) {
39: throw new Exception('Could not insert service record.');
40: }
41: return intval($this->db->insert_id());
42: }
43:
44: 45: 46: 47: 48: 49:
50: public function update($service) {
51: $this->db->where('id', $service['id']);
52: if (!$this->db->update('ea_services', $service)) {
53: throw new Exception('Could not update service record');
54: }
55: }
56:
57: 58: 59: 60: 61: 62:
63: public function exists($service) {
64: if (!isset($service['name'])
65: || !isset($service['duration'])
66: || !isset($service['price'])) {
67: throw new Exception('Not all service fields are provided in order to check whether '
68: . 'a service record already exists: ' . print_r($service, TRUE));
69: }
70:
71: $num_rows = $this->db->get_where('ea_services', array(
72: 'name' => $service['name'],
73: 'duration' => $service['duration'],
74: 'price' => $service['price']
75: ))->num_rows();
76:
77: return ($num_rows > 0) ? TRUE : FALSE;
78: }
79:
80: 81: 82: 83: 84: 85:
86: public function validate($service) {
87: $this->load->helper('data_validation');
88:
89: try {
90:
91:
92: if (isset($service['id'])) {
93: $num_rows = $this->db->get_where('ea_services', array('id' => $service['id']))
94: ->num_rows();
95: if ($num_rows == 0) {
96: throw new Exception('Provided service id does not exist in the database.');
97: }
98: }
99:
100:
101: if ($service['id_service_categories'] != NULL) {
102: $num_rows = $this->db->get_where('ea_service_categories',
103: array('id' => $service['id_service_categories']))->num_rows();
104: if ($num_rows == 0) {
105: throw new Exception('Provided service category id does not exist in database.');
106: }
107: }
108:
109:
110: if ($service['name'] == '') {
111: throw new Exception('Not all required service fields where provided: '
112: . print_r($service, TRUE));
113: }
114:
115:
116: if ($service['duration'] !== NULL) {
117: if (!is_numeric($service['duration'])) {
118: throw new Exception('Service duration is not numeric.');
119: }
120: }
121:
122: if ($service['price'] !== NULL) {
123: if (!is_numeric($service['price'])) {
124: throw new Exception('Service price is not numeric.');
125: }
126: }
127:
128: return TRUE;
129: } catch(Exception $exc) {
130: return FALSE;
131: }
132: }
133:
134: 135: 136: 137: 138: 139: 140: 141:
142: public function find_record_id($service) {
143: if (!isset($service['name'])
144: || !isset($service['duration'])
145: || !isset($service['price'])) {
146: throw new Exception('Not all required fields where provided in order to find the '
147: . 'service record id.');
148: }
149:
150: $result = $this->db->get_where('ea_services', array(
151: 'name' => $service['name'],
152: 'duration' => $service['duration'],
153: 'price' => $service['price']
154: ));
155:
156: if ($result->num_rows() == 0) {
157: throw new Exception('Cound not find service record id');
158: }
159:
160: return $result->row()->id;
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function delete($service_id) {
170: if (!is_numeric($service_id)) {
171: throw new Exception('Invalid argument type $service_id (value:"' . $service_id . '"');
172: }
173:
174: $num_rows = $this->db->get_where('ea_services', array('id' => $service_id))->num_rows();
175: if ($num_rows == 0) {
176: return FALSE;
177: }
178:
179: return $this->db->delete('ea_services', array('id' => $service_id));
180: }
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: public function get_row($service_id) {
191: if (!is_numeric($service_id)) {
192: throw new Exception('$service_id argument is not an numeric (value: "' . $service_id . '")');
193: }
194: return $this->db->get_where('ea_services', array('id' => $service_id))->row_array();
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204:
205: public function get_value($field_name, $service_id) {
206: if (!is_numeric($service_id)) {
207: throw new Exception('Invalid argument provided as $service_id : ' . $service_id);
208: }
209:
210: if (!is_string($field_name)) {
211: throw new Exception('$field_name argument is not a string : ' . $field_name);
212: }
213:
214: if ($this->db->get_where('ea_services', array('id' => $service_id))->num_rows() == 0) {
215: throw new Exception('The record with the $service_id argument does not exist in the database : ' . $service_id);
216: }
217:
218: $row_data = $this->db->get_where('ea_services', array('id' => $service_id))->row_array();
219: if (!isset($row_data[$field_name])) {
220: throw new Exception('The given $field_name argument does not exist in the database : ' . $field_name);
221: }
222:
223: $setting = $this->db->get_where('ea_services', array('id' => $service_id))->row_array();
224: return $setting[$field_name];
225: }
226:
227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function get_batch($where_clause = NULL) {
237: if ($where_clause != NULL) {
238: $this->db->where($where_clause);
239: }
240:
241: return $this->db->get('ea_services')->result_array();
242: }
243:
244: 245: 246: 247: 248: 249:
250: public function get_available_services() {
251: $this->db->distinct();
252: return $this->db
253: ->select('ea_services.*, ea_service_categories.name AS category_name, '
254: . 'ea_service_categories.id AS category_id')
255: ->from('ea_services')
256: ->join('ea_services_providers',
257: 'ea_services_providers.id_services = ea_services.id', 'inner')
258: ->join('ea_service_categories',
259: 'ea_service_categories.id = ea_services.id_service_categories', 'left')
260: ->get()->result_array();
261: }
262:
263: 264: 265: 266: 267: 268:
269: public function add_category($category) {
270: if (!$this->validate_category($category)) {
271: throw new Exception('Service category data are invalid.');
272: }
273:
274: if (!isset($category['id'])) {
275: $this->db->insert('ea_service_categories', $category);
276: $category['id'] = $this->db->insert_id();
277: } else {
278: $this->db->where('id', $category['id']);
279: $this->db->update('ea_service_categories', $category);
280: }
281:
282: return intval($category['id']);
283: }
284:
285: 286: 287: 288: 289: 290:
291: public function delete_category($category_id) {
292: if (!is_numeric($category_id)) {
293: throw new Exception('Invalid argument given for $category_id: ' . $category_id);
294: }
295:
296: $num_rows = $this->db->get_where('ea_service_categories', array('id' => $category_id))
297: ->num_rows();
298: if ($num_rows == 0) {
299: throw new Exception('Service category record not found in database.');
300: }
301:
302: $this->db->where('id', $category_id);
303: return $this->db->delete('ea_service_categories');
304: }
305:
306: 307: 308: 309: 310: 311:
312: public function get_category($category_id) {
313: if (!is_numeric($category_id)) {
314: throw new Exception('Invalid argument type given $category_id: ' . $category_id);
315: }
316:
317: $result = $this->db->get_where('ea_service_categories', array('id' => $category_id));
318:
319: if ($result->num_rows() == 0) {
320: throw new Exception('Service category record does not exist.');
321: }
322:
323: return $result->row_array();
324: }
325:
326: 327: 328: 329: 330:
331: public function get_all_categories($where = '') {
332: if ($where !== '') $this->db->where($where);
333: return $this->db->get('ea_service_categories')->result_array();
334: }
335:
336: 337: 338: 339: 340: 341: 342:
343: public function validate_category($category) {
344: try {
345:
346: if (!isset($category['name'])) {
347: throw new Exception('Not all required fields where provided ');
348: }
349:
350: if ($category['name'] == '' || $category['name'] == NULL) {
351: throw new Exception('Required fields cannot be empty or null ($category: '
352: . print_r($category, TRUE) . ')');
353: }
354:
355: return TRUE;
356: } catch(Exception $exc) {
357: return FALSE;
358: }
359:
360: }
361: }
362:
363:
364: