1 <?php
2
3 class User extends CActiveRecord
4 {
5 const STATUS_NOACTIVE=0;
6 const STATUS_ACTIVE=1;
7 const STATUS_BANNED=-1;
8
9
10 const STATUS_BANED=-1;
11
12 13 14 15 16 17 18 19 20 21 22 23 24 25
26
27 28 29 30
31 public static function model($className=__CLASS__)
32 {
33 return parent::model($className);
34 }
35
36 37 38
39 public function tableName()
40 {
41 return Yii::app()->getModule('user')->tableUsers;
42 }
43
44 45 46
47 public function rules()
48 {
49
50
51 return ((get_class(Yii::app())=='CConsoleApplication' || (get_class(Yii::app())!='CConsoleApplication' && Yii::app()->getModule('user')->isAdmin()))?array(
52 array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
53 array('password', 'length', 'max'=>128, 'min' => 4,'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
54 array('email', 'email'),
55 array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
56 array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
57 array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
58 array('status', 'in', 'range'=>array(self::STATUS_NOACTIVE,self::STATUS_ACTIVE,self::STATUS_BANNED)),
59 array('superuser', 'in', 'range'=>array(0,1)),
60 array('create_at', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),
61 array('lastvisit_at', 'default', 'value' => '0000-00-00 00:00:00', 'setOnEmpty' => true, 'on' => 'insert'),
62 array('username, email, superuser, status', 'required'),
63 array('superuser, status', 'numerical', 'integerOnly'=>true),
64 array('id, username, password, email, activkey, create_at, lastvisit_at, superuser, status', 'safe', 'on'=>'search'),
65 ):((Yii::app()->user->id==$this->id)?array(
66 array('username, email', 'required'),
67 array('username', 'length', 'max'=>20, 'min' => 3,'message' => UserModule::t("Incorrect username (length between 3 and 20 characters).")),
68 array('email', 'email'),
69 array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
70 array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
71 array('email', 'unique', 'message' => UserModule::t("This user's email address already exists.")),
72 ):array()));
73 }
74
75 76 77
78 public function relations()
79 {
80 $relations = Yii::app()->getModule('user')->relations;
81 if (!isset($relations['profile']))
82 $relations['profile'] = array(self::HAS_ONE, 'Profile', 'user_id');
83 return $relations;
84 }
85
86 87 88
89 public function attributeLabels()
90 {
91 return array(
92 'id' => UserModule::t("Id"),
93 'username'=>UserModule::t("username"),
94 'password'=>UserModule::t("password"),
95 'verifyPassword'=>UserModule::t("Retype Password"),
96 'email'=>UserModule::t("E-mail"),
97 'verifyCode'=>UserModule::t("Verification Code"),
98 'activkey' => UserModule::t("activation key"),
99 'createtime' => UserModule::t("Registration date"),
100 'create_at' => UserModule::t("Registration date"),
101
102 'lastvisit_at' => UserModule::t("Last visit"),
103 'superuser' => UserModule::t("Superuser"),
104 'status' => UserModule::t("Status"),
105 );
106 }
107
108 public function scopes()
109 {
110 return array(
111 'active'=>array(
112 'condition'=>'status='.self::STATUS_ACTIVE,
113 ),
114 'notactive'=>array(
115 'condition'=>'status='.self::STATUS_NOACTIVE,
116 ),
117 'banned'=>array(
118 'condition'=>'status='.self::STATUS_BANNED,
119 ),
120 'superuser'=>array(
121 'condition'=>'superuser=1',
122 ),
123 'notsafe'=>array(
124 'select' => 'id, username, password, email, activkey, create_at, lastvisit_at, superuser, status',
125 ),
126 );
127 }
128
129 public function defaultScope()
130 {
131 return CMap::mergeArray(Yii::app()->getModule('user')->defaultScope,array(
132 'alias'=>'user',
133 'select' => 'user.id, user.username, user.email, user.create_at, user.lastvisit_at, user.superuser, user.status',
134 ));
135 }
136
137 public static function itemAlias($type,$code=NULL) {
138 $_items = array(
139 'UserStatus' => array(
140 self::STATUS_NOACTIVE => UserModule::t('Not active'),
141 self::STATUS_ACTIVE => UserModule::t('Active'),
142 self::STATUS_BANNED => UserModule::t('Banned'),
143 ),
144 'AdminStatus' => array(
145 '0' => UserModule::t('No'),
146 '1' => UserModule::t('Yes'),
147 ),
148 );
149 if (isset($code))
150 return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
151 else
152 return isset($_items[$type]) ? $_items[$type] : false;
153 }
154
155 156 157 158
159 public function search()
160 {
161
162
163
164 $criteria=new CDbCriteria;
165
166 $criteria->compare('id',$this->id);
167 $criteria->compare('username',$this->username,true);
168 $criteria->compare('password',$this->password);
169 $criteria->compare('email',$this->email,true);
170 $criteria->compare('activkey',$this->activkey);
171 $criteria->compare('create_at',$this->create_at);
172 $criteria->compare('lastvisit_at',$this->lastvisit_at);
173 $criteria->compare('superuser',$this->superuser);
174 $criteria->compare('status',$this->status);
175
176 return new CActiveDataProvider(get_class($this), array(
177 'criteria'=>$criteria,
178 'pagination'=>array(
179 'pageSize'=>Yii::app()->getModule('user')->user_page_size,
180 ),
181 ));
182 }
183
184 public function getCreatetime() {
185 return strtotime($this->create_at);
186 }
187
188 public function setCreatetime($value) {
189 $this->create_at=date('Y-m-d H:i:s',$value);
190 }
191
192 public function getLastvisit() {
193 return strtotime($this->lastvisit_at);
194 }
195
196 public function setLastvisit($value) {
197 $this->lastvisit_at=date('Y-m-d H:i:s',$value);
198 }
199
200 public function afterSave() {
201 if (get_class(Yii::app())=='CWebApplication'&&Profile::$regMode==false) {
202 Yii::app()->user->updateSession();
203 }
204 return parent::afterSave();
205 }
206 }