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: class Admins_Model extends CI_Model {
23: 24: 25:
26: public function __construct() {
27: parent::__construct();
28: }
29:
30: 31: 32: 33: 34: 35: 36:
37: public function add($admin) {
38: if (!$this->validate($admin)) {
39: throw new Exception('Admin data are invalid: ' . print_r($admin, TRUE));
40: }
41:
42: if ($this->exists($admin) && !isset($admin['id'])) {
43: $admin['id'] = $this->find_record_id($admin);
44: }
45:
46: if (!isset($admin['id'])) {
47: $admin['id'] = $this->insert($admin);
48: } else {
49: $admin['id'] = $this->update($admin);
50: }
51:
52: return intval($admin['id']);
53: }
54:
55: 56: 57: 58: 59: 60: 61: 62:
63: public function exists($admin) {
64: if (!isset($admin['email'])) {
65: throw new Exception('Admin email is not provided: ' . print_r($admin, TRUE));
66: }
67:
68:
69: $num_rows = $this->db
70: ->select('*')
71: ->from('ea_users')
72: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
73: ->where('ea_users.email', $admin['email'])
74: ->where('ea_roles.slug', DB_SLUG_ADMIN)
75: ->get()->num_rows();
76:
77: return ($num_rows > 0) ? TRUE : FALSE;
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: public function insert($admin) {
88: $admin['id_roles'] = $this->get_admin_role_id();
89: $settings = $admin['settings'];
90: unset($admin['settings']);
91:
92: $this->db->trans_begin();
93:
94: if (!$this->db->insert('ea_users', $admin)) {
95: throw new Exception('Could not insert admin into the database.');
96: }
97:
98: $admin['id'] = intval($this->db->insert_id());
99: $settings['id_users'] = $admin['id'];
100:
101:
102: if (!$this->db->insert('ea_user_settings', $settings)) {
103: $this->db->trans_rollback();
104: throw new Exception('Could not insert admin settings into the database.');
105: }
106:
107: $this->db->trans_complete();
108:
109: return $admin['id'];
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: public function update($admin) {
120: $settings = $admin['settings'];
121: unset($admin['settings']);
122: $settings['id_users'] = $admin['id'];
123:
124: $this->db->where('id', $admin['id']);
125: if (!$this->db->update('ea_users', $admin)) {
126: throw new Exception('Could not update admin record.');
127: }
128:
129: $this->db->where('id_users', $settings['id_users']);
130: if (!$this->db->update('ea_user_settings', $settings)) {
131: throw new Exception('Could not update admin settings.');
132: }
133:
134: return intval($admin['id']);
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144:
145: public function find_record_id($admin) {
146: if (!isset($admin['email'])) {
147: throw new Exception('Admin email was not provided: ' . print_r($admin, TRUE));
148: }
149:
150: $result = $this->db
151: ->select('ea_users.id')
152: ->from('ea_users')
153: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
154: ->where('ea_users.email', $admin['email'])
155: ->where('ea_roles.slug', DB_SLUG_ADMIN)
156: ->get();
157:
158: if ($result->num_rows() == 0) {
159: throw new Exception('Could not find admin record id.');
160: }
161:
162: return intval($result->row()->id);
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function validate($admin) {
172: $this->load->helper('data_validation');
173:
174: try {
175:
176: if (isset($admin['id'])) {
177: $num_rows = $this->db->get_where('ea_users', array('id' => $admin['id']))
178: ->num_rows();
179: if ($num_rows == 0) {
180: throw new Exception('Given admin id does not exist in database: ' . $admin['id']);
181: }
182: }
183:
184:
185: if (!isset($admin['last_name'])
186: || !isset($admin['email'])
187: || !isset($admin['phone_number'])) {
188: throw new Exception('Not all required fields are provided : ' . print_r($admin, TRUE));
189: }
190:
191:
192: if (!filter_var($admin['email'], FILTER_VALIDATE_EMAIL)) {
193: throw new Exception('Invalid email address provided : ' . $admin['email']);
194: }
195:
196: return TRUE;
197: } catch (Exception $exc) {
198: return FALSE;
199: }
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209: 210:
211: public function delete($admin_id) {
212: if (!is_numeric($admin_id)) {
213: throw new Exception('Invalid argument type $admin_id : ' . $admin_id);
214: }
215:
216:
217:
218: $admin_count = $this->db->get_where('ea_users',
219: array('id_roles' => $this->get_admin_role_id()))->num_rows();
220: if ($admin_count == 1) {
221: throw new Exception('Record could not be deleted. The system requires at least '
222: . 'one admin user.');
223: }
224:
225: $num_rows = $this->db->get_where('ea_users', array('id' => $admin_id))->num_rows();
226: if ($num_rows == 0) {
227: return FALSE;
228: }
229:
230: return $this->db->delete('ea_users', array('id' => $admin_id));
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: public function get_row($admin_id) {
241: if (!is_numeric($admin_id)) {
242: throw new Exception('$admin_id argument is not a valid numeric value: ' . $admin_id);
243: }
244:
245:
246: if ($this->db->get_where('ea_users', array('id' => $admin_id))->num_rows() == 0) {
247: throw new Exception('The given admin id does not match a record in the database.');
248: }
249:
250: $admin = $this->db->get_where('ea_users', array('id' => $admin_id))->row_array();
251:
252: $admin['settings'] = $this->db->get_where('ea_user_settings',
253: array('id_users' => $admin_id))->row_array();
254: unset($admin['settings']['id_users']);
255:
256:
257: return $admin;
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
271: public function get_value($field_name, $admin_id) {
272: if (!is_string($field_name)) {
273: throw new Exception('$field_name argument is not a string : ' . $field_name);
274: }
275:
276: if (!is_numeric($admin_id)) {
277: throw new Exception('$admin_id argument is not a valid numeric value: ' . $admin_id);
278: }
279:
280:
281: $result = $this->db->get_where('ea_users', array('id' => $admin_id));
282: if ($result->num_rows() == 0) {
283: throw new Exception('The record with the given id does not exist in the '
284: . 'database : ' . $admin_id);
285: }
286:
287:
288: $provider = $result->row_array();
289: if (!isset($provider[$field_name])) {
290: throw new Exception('The given $field_name argument does not exist in the '
291: . 'database: ' . $field_name);
292: }
293:
294: return $provider[$field_name];
295: }
296:
297: 298: 299: 300: 301: 302: 303:
304: public function get_batch($where_clause = '') {
305: $role_id = $this->get_admin_role_id();
306:
307: if ($where_clause != '') {
308: $this->db->where($where_clause);
309: }
310:
311: $batch = $this->db->get_where('ea_users', array('id_roles' => $role_id))->result_array();
312:
313:
314: foreach ($batch as &$admin) {
315: $admin['settings'] = $this->db->get_where('ea_user_settings',
316: array('id_users' => $admin['id']))->row_array();
317: unset($admin['settings']['id_users']);
318: }
319:
320: return $batch;
321: }
322:
323: 324: 325: 326: 327:
328: public function get_admin_role_id() {
329: return intval($this->db->get_where('ea_roles', array('slug' => DB_SLUG_ADMIN))->row()->id);
330: }
331: }
332:
333:
334: