3 class User extends CActiveRecord
5 const STATUS_NOACTIVE = 0;
6 const STATUS_ACTIVE = 1;
7 const STATUS_BANED = -1;
12 protected $isExists = null;
14 public $justSaved =
false;
42 public static function model($className = __CLASS__)
52 return Yii::app()->getModule(
'user')->tableUsers;
68 if(Yii::app()->getModule(
'user')->isAdmin()){
70 #array(
'username, password, email',
'required'),
71 array(
'username',
'length',
'max' => 20,
'min' => 3,
'message' =>
UserModule::t(
"Incorrect username (length between 3 and 20 characters).")),
72 array(
'password',
'length',
'max' => 128,
'min' => 4,
'message' =>
UserModule::t(
"Incorrect password (minimal length 4 symbols).")),
73 array(
'email',
'email',
'message' =>
UserModule::t(
"Email invalid.") ),
74 array(
'username',
'unique',
'message' =>
UserModule::t(
"This user's name already exists."),
'except'=>
'nameIsNotUniq'),
75 array(
'email',
'unique',
'message' =>
UserModule::t(
"This user's email address already exists.")),
76 array(
'username',
'match',
'pattern' =>
'/^[A-Za-z0-9_]+$/u',
'message' =>
UserModule::t(
"Incorrect symbols (A-z0-9).")),
77 array(
'status',
'in',
'range' => array(self::STATUS_NOACTIVE, self::STATUS_ACTIVE, self::STATUS_BANED),
'message' =>
UserModule::t(
"Incorrect status.")),
78 array(
'username, email, status',
'required'),
79 array(
'status',
'numerical',
'integerOnly' =>
true),
82 array(
'createtime, lastvisit',
'type',
'type' =>
'datetime',
'datetimeFormat' =>
'yyyy-MM-dd hh:mm:ss',
'message' =>
UserModule::t(
"Date invalid.") ),
83 array(
'id, username, password, email, activkey, createtime, lastvisit, status, is_bulk, firstname, lastname',
'safe',
'on' =>
'search'),
87 if(Yii::app()->user->id == $this->id){
89 array(
'email',
'required'),
90 array(
'email',
'email'),
91 array(
'email',
'unique',
'message' =>
UserModule::t(
"This user's email address already exists.")),
92 array(
'status',
'numerical',
'integerOnly' =>
true),
95 array(
'username,email, createtime, lastvisit, status',
'safe',
'on' =>
'search'),
109 'profile' => array(self::HAS_ONE,
'Profile',
'user_id'),
111 if (isset(Yii::app()->getModule(
'user')->
relations)) $relations = array_merge($relations, Yii::app()->getModule(
'user')->
relations);
130 public function isExists(){
132 $this->isExists =
User::model()->count(
'email=:email',array(
':email'=>$this->email))?
true:
false;
133 return $this->isExists;
136 public static function create($account, $service)
138 $user =
User::model()->findByAttributes(array(
'email' => $service->getEmail()));
141 $user->username = $service->getName();
142 $user->attributes = array(
144 'email' => $service->getEmail(),
145 'password' =>
'empty',
146 'activkey' =>
'empty',
150 if (!$user->save()) {
151 throw new Exception(print_r($user->getErrors(),
true));
155 $profile->user_id = $user->id;
157 if (isset($service->attributes[
'firstname'])) {
158 $profile->firstname = $service->attributes[
'firstname'];
160 if (isset($service->attributes[
'lastname'])) {
161 $profile->lastname = $service->attributes[
'lastname'];
163 if (isset($service->attributes[
'gender'])) {
164 $profile->gender = $service->attributes[
'gender'];
167 if (!$profile->save(
false)) {
168 throw new Exception(print_r($profile->getErrors(),
true));
171 $account->user_id = $user->id;
196 public function scopes()
200 'condition' =>
'status=' . self::STATUS_ACTIVE,
202 'notactvie' => array(
203 'condition' =>
'status=' . self::STATUS_NOACTIVE,
206 'condition' =>
'status=' . self::STATUS_BANED,
208 'superuser' => array(
211 'select' =>
'id, username, password, email, activkey, createtime, lastvisit, status',
216 public function defaultScope()
219 'select' =>
'id, username, email, createtime, lastvisit, status',
223 public static function itemAlias($type, $code = NULL)
226 'UserStatus' => array(
231 'AdminStatus' => array(
237 return isset($_items[$type][$code]) ? $_items[$type][$code] :
false;
239 return isset($_items[$type]) ? $_items[$type] :
false;
251 $criteria =
new CDbCriteria;
253 $criteria->compare(
'id', $this->
id);
254 $criteria->compare(
'username', $this->username,
true);
255 $criteria->compare(
'password', $this->password,
true);
256 $criteria->compare(
'email', $this->email,
true);
257 $criteria->compare(
'activkey', $this->activkey,
true);
258 $criteria->compare(
'createtime', $this->createtime,
true);
259 $criteria->compare(
'lastvisit', $this->lastvisit,
true);
260 $criteria->compare(
'status', $this->status);
261 $criteria->compare(
'is_bulk', $this->is_bulk);
263 $criteria->together =
true;
264 $criteria->with = array(
'profile');
265 $criteria->compare(
'profile.firstname', $this->firstname,
true);
266 $criteria->compare(
'profile.lastname', $this->lastname,
true);
268 return new CActiveDataProvider($this, array(
269 'criteria' => $criteria,
270 'pagination' => array(
271 'pageSize' => Yii::app()->controller->module->user_page_size,