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