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']['password'])) {
206: if (strlen($secretary['settings']['password']) < MIN_PASSWORD_LENGTH) {
207: throw new Exception('The user password must be at least '
208: . MIN_PASSWORD_LENGTH . ' characters long.');
209: }
210: }
211:
212: return TRUE;
213: }
214:
215: 216: 217: 218: 219: 220: 221:
222: public function delete($secretary_id) {
223: if (!is_numeric($secretary_id)) {
224: throw new Exception('Invalid argument type $secretary_id : ' . $secretary_id);
225: }
226:
227: $num_rows = $this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows();
228: if ($num_rows == 0) {
229: return FALSE;
230: }
231:
232: return $this->db->delete('ea_users', array('id' => $secretary_id));
233: }
234:
235: 236: 237: 238: 239: 240: 241: 242:
243: public function get_row($secretary_id) {
244: if (!is_numeric($secretary_id)) {
245: throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
246: }
247:
248:
249: if ($this->db->get_where('ea_users', array('id' => $secretary_id))->num_rows() == 0) {
250: throw new Exception('The given secretary id does not match a record in the database.');
251: }
252:
253: $secretary = $this->db->get_where('ea_users', array('id' => $secretary_id))->row_array();
254:
255: $secretary_providers = $this->db->get_where('ea_secretaries_providers',
256: array('id_users_secretary' => $secretary['id']))->result_array();
257: $secretary['providers'] = array();
258: foreach($secretary_providers as $secretary_provider) {
259: $secretary['providers'][] = $secretary_provider['id_users_provider'];
260: }
261:
262: $secretary['settings'] = $this->db->get_where('ea_user_settings',
263: array('id_users' => $secretary['id']))->row_array();
264: unset($secretary['settings']['id_users']);
265:
266: return $secretary;
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279:
280: public function get_value($field_name, $secretary_id) {
281: if (!is_string($field_name)) {
282: throw new Exception('$field_name argument is not a string : ' . $field_name);
283: }
284:
285: if (!is_numeric($secretary_id)) {
286: throw new Exception('$secretary_id argument is not a valid numeric value: ' . $secretary_id);
287: }
288:
289:
290: $result = $this->db->get_where('ea_users', array('id' => $secretary_id));
291: if ($result->num_rows() == 0) {
292: throw new Exception('The record with the given id does not exist in the '
293: . 'database : ' . $secretary_id);
294: }
295:
296:
297: $provider = $result->row_array();
298: if (!isset($provider[$field_name])) {
299: throw new Exception('The given $field_name argument does not exist in the '
300: . 'database: ' . $field_name);
301: }
302:
303: return $provider[$field_name];
304: }
305:
306: 307: 308: 309: 310: 311: 312:
313: public function get_batch($where_clause = '') {
314: $role_id = $this->get_secretary_role_id();
315:
316: if ($where_clause != '') {
317: $this->db->where($where_clause);
318: }
319:
320: $this->db->where('id_roles', $role_id);
321: $batch = $this->db->get('ea_users')->result_array();
322:
323:
324: foreach ($batch as &$secretary) {
325: $secretary_providers = $this->db->get_where('ea_secretaries_providers',
326: array('id_users_secretary' => $secretary['id']))->result_array();
327:
328: $secretary['providers'] = array();
329: foreach($secretary_providers as $secretary_provider) {
330: $secretary['providers'][] = $secretary_provider['id_users_provider'];
331: }
332:
333: $secretary['settings'] = $this->db->get_where('ea_user_settings',
334: array('id_users' => $secretary['id']))->row_array();
335: unset($secretary['settings']['id_users']);
336: }
337:
338: return $batch;
339: }
340:
341: 342: 343: 344: 345:
346: public function get_secretary_role_id() {
347: return intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_SECRETARY))->row()->id);
348: }
349:
350: 351: 352: 353: 354:
355: private function save_providers($providers, $secretary_id) {
356: if (!is_array($providers)) {
357: throw new Exception('Invalid argument given $providers: ' . print_r($providers, TRUE));
358: }
359:
360:
361: $this->db->delete('ea_secretaries_providers', array('id_users_secretary' => $secretary_id));
362:
363: if (count($providers) > 0) {
364: foreach ($providers as $provider_id) {
365: $this->db->insert('ea_secretaries_providers', array(
366: 'id_users_secretary' => $secretary_id,
367: 'id_users_provider' => $provider_id
368: ));
369: }
370: }
371: }
372:
373: 374: 375: 376: 377: 378:
379: private function save_settings($settings, $secretary_id) {
380: if (!is_numeric($secretary_id)) {
381: throw new Exception('Invalid $provider_id argument given :' . $secretary_id);
382: }
383:
384: if (count($settings) == 0 || !is_array($settings)) {
385: throw new Exception('Invalid $settings argument given:' . print_r($settings, TRUE));
386: }
387:
388:
389: $num_rows = $this->db->get_where('ea_user_settings',
390: array('id_users' => $secretary_id))->num_rows();
391: if ($num_rows == 0) {
392: $this->db->insert('ea_user_settings', array('id_users' => $secretary_id));
393: }
394:
395: foreach($settings as $name => $value) {
396: $this->set_setting($name, $value, $secretary_id);
397: }
398: }
399:
400: 401: 402: 403: 404: 405: 406:
407: public function get_setting($setting_name, $secretary_id) {
408: $provider_settings = $this->db->get_where('ea_user_settings',
409: array('id_users' => $secretary_id))->row_array();
410: return $provider_settings[$setting_name];
411: }
412:
413: 414: 415: 416: 417: 418: 419: 420: 421:
422: public function set_setting($setting_name, $value, $secretary_id) {
423: $this->db->where(array('id_users' => $secretary_id));
424: return $this->db->update('ea_user_settings', array($setting_name => $value));
425: }
426: }
427:
428:
429: