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