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