| 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 delete($object = '', $related_field = '') |
| 131 |
{ |
| 132 |
// if something is given, then just pass it over, the caller must be knowing what he's doing |
| 133 |
if( $object ) |
| 134 |
{ |
| 135 |
return parent::delete( $object, $related_field ); |
| 136 |
} |
| 137 |
// if empty then delete all has_many and has_one |
| 138 |
else |
| 139 |
{ |
| 140 |
$has = array_merge( array_keys($this->has_one), array_keys($this->has_many) ); |
| 141 |
foreach ( $has as $rfield ) |
| 142 |
{ |
| 143 |
$this->{$rfield}->get()->delete_all(); |
| 144 |
} |
| 145 |
return parent::delete(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public function title( $html = FALSE ) |
| 150 |
{ |
| 151 |
$return = ''; |
| 152 |
if( $html ) |
| 153 |
{ |
| 154 |
$return .= '<i class="icon-user"></i> '; |
| 155 |
} |
| 156 |
$return .= $this->first_name . ' ' . $this->last_name; |
| 157 |
// $return .= ' [' . $this->email . ']'; |
| 158 |
return $return; |
| 159 |
} |
| 160 |
|
| 161 |
public function full_name() |
| 162 |
{ |
| 163 |
return $this->first_name . ' ' . $this->last_name; |
| 164 |
} |
| 165 |
|
| 166 |
/* check password */ |
| 167 |
function check_password( $pass ) |
| 168 |
{ |
| 169 |
$this->get_by_email( $this->email ); |
| 170 |
if ( ! $this->exists() ) |
| 171 |
{ |
| 172 |
return FALSE; |
| 173 |
} |
| 174 |
|
| 175 |
$this->salt = substr($this->password, 0, $this->salt_length); |
| 176 |
$this->password = $pass; |
| 177 |
|
| 178 |
$this->validate(); |
| 179 |
$this->get(); |
| 180 |
if ( ! $this->exists() ) |
| 181 |
{ |
| 182 |
return FALSE; |
| 183 |
} |
| 184 |
return TRUE; |
| 185 |
} |
| 186 |
|
| 187 |
/* validation methods */ |
| 188 |
function _hash_password( $field ) |
| 189 |
{ |
| 190 |
if (!empty($this->{$field})) |
| 191 |
{ |
| 192 |
// Generate a random salt if empty |
| 193 |
if (empty($this->salt)) |
| 194 |
{ |
| 195 |
$this->salt = substr( md5(uniqid(rand(), true)), 0, $this->salt_length ); |
| 196 |
} |
| 197 |
$this->{$field} = $this->salt . substr( sha1($this->salt . $this->{$field}), 0, -$this->salt_length ); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|