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