1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32: class Providers_Model extends CI_Model {
33: 34: 35:
36: public function __construct() {
37: parent::__construct();
38: }
39:
40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
50: public function add($provider) {
51: if (!$this->validate($provider)) {
52: throw new Exception('Provider data are not valid :' . print_r($provider, TRUE));
53: }
54:
55: if ($this->exists($provider) && !isset($provider['id'])) {
56: $provider['id'] = $this->find_record_id($provider);
57: }
58:
59: if (!isset($provider['id'])) {
60: $provider['id'] = $this->insert($provider);
61: } else {
62: $provider['id'] = $this->update($provider);
63: }
64:
65: return intval($provider['id']);
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75:
76: public function exists($provider) {
77: if (!isset($provider['email'])) {
78: throw new Exception('Provider email is not provided :' . print_r($provider, TRUE));
79: }
80:
81:
82: $num_rows = $this->db
83: ->select('*')
84: ->from('ea_users')
85: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
86: ->where('ea_users.email', $provider['email'])
87: ->where('ea_roles.slug', DB_SLUG_PROVIDER)
88: ->get()->num_rows();
89:
90: return ($num_rows > 0) ? TRUE : FALSE;
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: public function insert($provider) {
101:
102: $provider['id_roles'] = $this->get_providers_role_id();
103:
104:
105: $services = $provider['services'];
106: unset($provider['services']);
107: $settings = $provider['settings'];
108: unset($provider['settings']);
109:
110:
111: if (!$this->db->insert('ea_users', $provider)) {
112: throw new Exception('Could not insert provider into the database');
113: }
114:
115: $provider['id'] = $this->db->insert_id();
116: $this->save_settings($settings, $provider['id']);
117: $this->save_services($services, $provider['id']);
118:
119:
120: return intval($provider['id']);
121: }
122:
123: 124: 125: 126: 127: 128: 129:
130: public function update($provider) {
131:
132: $services = $provider['services'];
133: unset($provider['services']);
134: $settings = $provider['settings'];
135: unset($provider['settings']);
136:
137:
138: $this->db->where('id', $provider['id']);
139: if (!$this->db->update('ea_users', $provider)) {
140: throw new Exception('Could not update provider record.');
141: }
142:
143: $this->save_services($services, $provider['id']);
144: $this->save_settings($settings, $provider['id']);
145:
146:
147: return intval($provider['id']);
148: }
149:
150: 151: 152: 153: 154: 155: 156: 157:
158: public function find_record_id($provider) {
159: if (!isset($provider['email'])) {
160: throw new Exception('Provider email was not provided :' . print_r($provider, TRUE));
161: }
162:
163: $result = $this->db
164: ->select('ea_users.id')
165: ->from('ea_users')
166: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
167: ->where('ea_users.email', $provider['email'])
168: ->where('ea_roles.slug', DB_SLUG_PROVIDER)
169: ->get();
170:
171: if ($result->num_rows() == 0) {
172: throw new Exception('Could not find provider record id.');
173: }
174:
175: return intval($result->row()->id);
176: }
177:
178: 179: 180: 181: 182: 183:
184: public function validate($provider) {
185: $this->load->helper('data_validation');
186:
187: try {
188:
189: if (isset($provider['id'])) {
190: $num_rows = $this->db->get_where('ea_users',
191: array('id' => $provider['id']))->num_rows();
192: if ($num_rows == 0) {
193: throw new Exception('Provided record id does not exist in the database.');
194: }
195: }
196:
197:
198: if (!isset($provider['last_name'])
199: || !isset($provider['email'])
200: || !isset($provider['phone_number'])) {
201: throw new Exception('Not all required fields are provided : ' . print_r($provider, TRUE));
202: }
203:
204:
205: if (!filter_var($provider['email'], FILTER_VALIDATE_EMAIL)) {
206: throw new Exception('Invalid email address provided : ' . $provider['email']);
207: }
208:
209:
210: if (!isset($provider['services']) || !is_array($provider['services'])) {
211: throw new Exception('Invalid provider services given: ' . print_r($provider, TRUE));
212: } else {
213: foreach($provider['services'] as $service_id) {
214: if (!is_numeric($service_id)) {
215: throw new Exception('A provider service with invalid id was found: '
216: . print_r($provider, TRUE));
217: }
218: }
219: }
220:
221:
222: if (!isset($provider['settings']) || count($provider['settings']) == 0
223: || !is_array($provider['settings'])) {
224: throw new Exception('Invalid provider settings given: ' . print_r($provider, TRUE));
225: }
226:
227: return TRUE;
228: } catch (Exception $exc) {
229: return FALSE;
230: }
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public function delete($provider_id) {
241: if (!is_numeric($provider_id)) {
242: throw new Exception('Invalid argument type $provider_id : ' . $provider_id);
243: }
244:
245: $num_rows = $this->db->get_where('ea_users', array('id' => $provider_id))->num_rows();
246: if ($num_rows == 0) {
247: return FALSE;
248: }
249:
250: return $this->db->delete('ea_users', array('id' => $provider_id));
251: }
252:
253: 254: 255: 256: 257: 258: 259: 260:
261: public function get_row($provider_id) {
262: if (!is_numeric($provider_id)) {
263: throw new Exception('$provider_id argument is not a valid numeric value: ' . $provider_id);
264: }
265:
266:
267: if ($this->db->get_where('ea_users', array('id' => $provider_id))->num_rows() == 0) {
268: throw new Exception('Selected record does not exist in the database.');
269: }
270:
271:
272: $provider = $this->db->get_where('ea_users', array('id' => $provider_id))->row_array();
273:
274:
275:
276: $services = $this->db->get_where('ea_services_providers',
277: array('id_users' => $provider_id))->result_array();
278: $provider['services'] = array();
279: foreach($services as $service) {
280: $provider['services'][] = $service['id_services'];
281: }
282:
283:
284: $provider['settings'] = $this->db->get_where('ea_user_settings',
285: array('id_users' => $provider_id))->row_array();
286: unset($provider['settings']['id_users']);
287:
288:
289: return $provider;
290: }
291:
292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302:
303: public function get_value($field_name, $provider_id) {
304: if (!is_numeric($provider_id)) {
305: throw new Exception('Invalid argument provided as $provider_id : ' . $provider_id);
306: }
307:
308: if (!is_string($field_name)) {
309: throw new Exception('$field_name argument is not a string : ' . $field_name);
310: }
311:
312:
313: $result = $this->db->get_where('ea_users', array('id' => $provider_id));
314: if ($result->num_rows() == 0) {
315: throw new Exception('The record with the $provider_id argument does not exist in '
316: . 'the database : ' . $provider_id);
317: }
318:
319: $provider = $result->row_array();
320: if (!isset($provider[$field_name])) {
321: throw new Exception('The given $field_name argument does not exist in the '
322: . 'database : ' . $field_name);
323: }
324:
325: return $provider[$field_name];
326: }
327:
328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338:
339: public function get_batch($where_clause = '') {
340:
341:
342: $role_id = $this->get_providers_role_id();
343:
344: if ($where_clause != '') {
345: $this->db->where($where_clause);
346: }
347:
348: $batch = $this->db->get_where('ea_users',
349: array('id_roles' => $role_id))->result_array();
350:
351:
352: foreach($batch as &$provider) {
353:
354: $services = $this->db->get_where('ea_services_providers',
355: array('id_users' => $provider['id']))->result_array();
356: $provider['services'] = array();
357: foreach($services as $service) {
358: $provider['services'][] = $service['id_services'];
359: }
360:
361:
362: $provider['settings'] = $this->db->get_where('ea_user_settings',
363: array('id_users' => $provider['id']))->row_array();
364: unset($provider['settings']['id_users']);
365: }
366:
367:
368: return $batch;
369: }
370:
371: 372: 373: 374: 375: 376: 377: 378: 379: 380:
381: public function get_available_providers() {
382:
383: $this->db
384: ->select('ea_users.*')
385: ->from('ea_users')
386: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
387: ->where('ea_roles.slug', DB_SLUG_PROVIDER);
388:
389: $providers = $this->db->get()->result_array();
390:
391:
392: foreach($providers as &$provider) {
393:
394: $services = $this->db->get_where('ea_services_providers',
395: array('id_users' => $provider['id']))->result_array();
396: $provider['services'] = array();
397: foreach($services as $service) {
398: $provider['services'][] = $service['id_services'];
399: }
400:
401:
402: $provider['settings'] = $this->db->get_where('ea_user_settings',
403: array('id_users' => $provider['id']))->row_array();
404: unset($provider['settings']['id_users']);
405: }
406:
407:
408: return $providers;
409: }
410:
411: 412: 413: 414: 415:
416: public function get_providers_role_id() {
417: return $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id;
418: }
419:
420: 421: 422: 423: 424: 425: 426: 427:
428: public function get_setting($setting_name, $provider_id) {
429: $provider_settings = $this->db->get_where('ea_user_settings',
430: array('id_users' => $provider_id))->row_array();
431: return $provider_settings[$setting_name];
432: }
433:
434: 435: 436: 437: 438: 439: 440: 441: 442:
443: public function set_setting($setting_name, $value, $provider_id) {
444: $this->db->where(array('id_users' => $provider_id));
445: return $this->db->update('ea_user_settings', array($setting_name => $value));
446: }
447:
448: 449: 450: 451: 452: 453:
454: private function save_settings($settings, $provider_id) {
455: if (!is_numeric($provider_id)) {
456: throw new Exception('Invalid $provider_id argument given :' . $provider_id);
457: }
458:
459: if (count($settings) == 0 || !is_array($settings)) {
460: throw new Exception('Invalid $settings argument given:' . print_r($settings, TRUE));
461: }
462:
463:
464: if ($this->db->get_where('ea_user_settings', array('id_users' => $provider_id))
465: ->num_rows() == 0) {
466: $this->db->insert('ea_user_settings', array('id_users' => $provider_id));
467: }
468:
469: foreach($settings as $name=>$value) {
470: $this->set_setting($name, $value, $provider_id);
471: }
472: }
473:
474: 475: 476: 477: 478: 479: 480: 481:
482: private function save_services($services, $provider_id) {
483:
484: if (!is_array($services)) {
485: throw new Exception('Invalid argument type $services: ' . $services);
486: }
487:
488: if (!is_numeric($provider_id)) {
489: throw new Exception('Invalid argument type $provider_id: ' . $provider_id);
490: }
491:
492:
493: $this->db->delete('ea_services_providers', array('id_users' => $provider_id));
494: foreach($services as $service_id) {
495: $service_provider = array(
496: 'id_users' => $provider_id,
497: 'id_services' => $service_id
498: );
499: $this->db->insert('ea_services_providers', $service_provider);
500: }
501: }
502: }
503:
504:
505: