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