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