PluginProbe
ShiftController Employee Shift Scheduling / 2.2.4
ShiftController Employee Shift Scheduling v2.2.4
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / application / models / user_model.php

user_model.php in ShiftController Employee Shift Scheduling 2.2.4, at application/models/user_model.php

249 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 include_once( NTS_SYSTEM_APPPATH . '/core/MY_Model.php' );
3 class User_model extends MY_model
4 {
5 const LEVEL_STAFF = 1;
6 const LEVEL_MANAGER = 2;
7 const LEVEL_ADMIN = 3;
8
9 const STATUS_ACTIVE = 1;
10 const STATUS_ARCHIVE = 0;
11
12 var $salt_length = 10;
13 var $table = 'users';
14 var $default_order_by = array('last_name' => 'ASC', 'first_name' => 'ASC');
15 var $build_title = array(
16 '_first_name_', ' ', '_last_name_'
17 );
18
19 var $has_many = array(
20 'timeoff' => array(
21 'class' => 'timeoff_model',
22 'other_field' => 'user',
23 ),
24 'shift' => array(
25 'class' => 'shift_model',
26 'other_field' => 'user',
27 ),
28 );
29
30 var $validation = array(
31 'first_name' => array(
32 'label' => 'lang:user_first_name',
33 'rules' => array('required', 'trim', 'max_length' => 50)
34 ),
35 'last_name' => array(
36 'label' => 'lang:user_last_name',
37 // 'rules' => array('required', 'trim', 'max_length' => 50)
38 'rules' => array('trim', 'max_length' => 50)
39 ),
40 'email' => array(
41 'label' => 'lang:common_email',
42 'rules' => array('required', 'trim', 'valid_email', 'unique'),
43 ),
44 'password' => array(
45 'label' => 'lang:common_password',
46 'rules' => array('required', 'trim', 'hash_password'),
47 ),
48 'confirm_password' => array(
49 'label' => 'lang:common_password_confirm',
50 'rules' => array('trim', 'hash_password', 'matches' => 'password'),
51 ),
52 'level' => array(
53 'label' => 'lang:user_level',
54 'rules' => array(
55 'enum' => array(
56 self::LEVEL_STAFF,
57 self::LEVEL_MANAGER,
58 self::LEVEL_ADMIN
59 )
60 ),
61 ),
62 'active' => array(
63 'label' => 'lang:user_status',
64 'rules' => array(
65 'enum' => array(
66 self::STATUS_ACTIVE,
67 self::STATUS_ARCHIVE
68 )
69 ),
70 ),
71 );
72
73 var $prop_text = array(
74 'level' => array(
75 self::LEVEL_STAFF => 'lang:user_level_staff',
76 self::LEVEL_MANAGER => 'lang:user_level_manager',
77 self::LEVEL_ADMIN => 'lang:user_level_admin',
78 ),
79 'active' => array(
80 self::STATUS_ACTIVE => array( 'lang:user_status_active', 'success' ),
81 self::STATUS_ARCHIVE => array( 'lang:user_status_archived', 'default' ),
82 ),
83 );
84
85 var $my_fields = array(
86 array(
87 'name' => 'first_name',
88 'label' => 'lang:user_first_name',
89 'size' => 16,
90 'required' => TRUE,
91 ),
92 array(
93 'name' => 'last_name',
94 'label' => 'lang:user_last_name',
95 'size' => 16,
96 'required' => TRUE,
97 ),
98 array(
99 'name' => 'email',
100 'size' => 24,
101 'required' => TRUE,
102 'label' => 'lang:common_email',
103 ),
104 array(
105 'name' => 'password',
106 'type' => 'password',
107 'label' => 'lang:common_password',
108 'size' => 16,
109 'required' => TRUE,
110 ),
111 array(
112 'name' => 'confirm_password',
113 'type' => 'password',
114 'label' => 'lang:common_password_confirm',
115 'size' => 16,
116 'required' => TRUE,
117 ),
118 array(
119 'name' => 'level',
120 'label' => 'lang:user_level',
121 'type' => 'dropdown',
122 ),
123 );
124
125 public function id_label()
126 {
127 return $this->prop_text('active', TRUE);
128 }
129
130 public function count_staff()
131 {
132 $CI =& ci_get_instance();
133 $working_levels = $CI->app_conf->get('working_levels');
134
135 $this->clear();
136 /* get those users who can be assigned to shifts */
137 $this->where('active', self::STATUS_ACTIVE);
138 if( $working_levels )
139 {
140 if( ! is_array($working_levels) )
141 $working_levels = array( $working_levels );
142 $this->where_in('level', $working_levels);
143 }
144 $return = $this->count();
145 return $return;
146 }
147
148 public function get_staff()
149 {
150 $CI =& ci_get_instance();
151 $working_levels = $CI->app_conf->get('working_levels');
152
153 $this->clear();
154 /* get those users who can be assigned to shifts */
155 $this->where('active', self::STATUS_ACTIVE);
156 if( $working_levels )
157 {
158 if( ! is_array($working_levels) )
159 $working_levels = array( $working_levels );
160 $this->where_in('level', $working_levels);
161 }
162 $return = $this->get()->all;
163 return $return;
164 }
165
166 /* redefine the drop down in forms */
167 protected function _load_titles()
168 {
169 $return = array();
170 $staff = $this->get_staff();
171 foreach( $staff as $sta )
172 {
173 $return[ $sta->id ] = $sta->title();
174 }
175 return $return;
176 }
177
178 public function delete($object = '', $related_field = '')
179 {
180 // if something is given, then just pass it over, the caller must be knowing what he's doing
181 if( $object )
182 {
183 return parent::delete( $object, $related_field );
184 }
185 // if empty then delete all has_many and has_one
186 else
187 {
188 $has = array_merge( array_keys($this->has_one), array_keys($this->has_many) );
189 foreach ( $has as $rfield )
190 {
191 $this->{$rfield}->get()->delete_all();
192 }
193 return parent::delete();
194 }
195 }
196
197 public function title( $html = FALSE )
198 {
199 $return = '';
200 if( $html )
201 {
202 $return .= '<i class="fa fa-user"></i> ';
203 }
204 $return .= $this->first_name . ' ' . $this->last_name;
205 // $return .= ' [' . $this->email . ']';
206 return $return;
207 }
208
209 public function full_name()
210 {
211 return $this->first_name . ' ' . $this->last_name;
212 }
213
214 /* check password */
215 function check_password( $pass )
216 {
217 $this->get_by_email( $this->email );
218 if ( ! $this->exists() )
219 {
220 return FALSE;
221 }
222
223 $this->salt = substr($this->password, 0, $this->salt_length);
224 $this->password = $pass;
225
226 $this->validate();
227 $this->get();
228 if ( ! $this->exists() )
229 {
230 return FALSE;
231 }
232 return TRUE;
233 }
234
235 /* validation methods */
236 function _hash_password( $field )
237 {
238 if (!empty($this->{$field}))
239 {
240 // Generate a random salt if empty
241 if (empty($this->salt))
242 {
243 $this->salt = substr( md5(uniqid(rand(), true)), 0, $this->salt_length );
244 }
245 $this->{$field} = $this->salt . substr( sha1($this->salt . $this->{$field}), 0, -$this->salt_length );
246 }
247 }
248 }
249