Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
User.php
1 <?php
2 
3 class User extends CActiveRecord
4 {
5  const STATUS_NOACTIVE = 0;
6  const STATUS_ACTIVE = 1;
7  const STATUS_BANED = -1;
8 
9  /*
10  * Field shows user should to be imported
11  */
12  protected $isExists = null;
13 
14  public $justSaved = false;
15  /**
16  * @var string for backend search
17  */
18  public $firstname;
19 
20  /**
21  * @var string for backend search
22  */
23  public $lastname;
24 
25  /**
26  * The followings are the available columns in table 'users':
27  * @var integer $id
28  * @var string $username
29  * @var string $password
30  * @var string $email
31  * @var string $activkey
32  * @var integer $createtime
33  * @var integer $lastvisit
34  * @var integer $superuser
35  * @var integer $status
36  */
37 
38  /**
39  * Returns the static model of the specified AR class.
40  * @return CActiveRecord the static model class
41  */
42  public static function model($className = __CLASS__)
43  {
44  return parent::model($className);
45  }
46 
47  /**
48  * @return string the associated database table name
49  */
50  public function tableName()
51  {
52  return Yii::app()->getModule('user')->tableUsers;
53  }
54 
55 // public function behaviors()
56 // {
57 //// return array('datetimeI18NBehavior' => array('class' => 'DateTimeI18NBehavior'));
58 // }
59 
60  /**
61  * @return array validation rules for model attributes.
62  */
63  public function rules()
64  {
65  // NOTE: you should only define rules for those attributes that
66  // will receive user inputs.
67 
68  if(Yii::app()->getModule('user')->isAdmin()){
69  return array(
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),
80  //array('createtime', 'default', 'value' => date('Y-m-d H:i:s')), move to db
81  //array('lastvisit', 'default', 'value' => NULL),
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'),
84  );
85  }else{
86 
87  if(Yii::app()->user->id == $this->id){
88  return array(
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),
93  //array('username', 'unique', 'message' => UserModule::t("This user's name already exists.")),
94  //array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => UserModule::t("Incorrect symbols (A-z0-9).")),
95  array('username,email, createtime, lastvisit, status', 'safe', 'on' => 'search'),
96  );
97  }else{
98  return array();
99  }
100  }
101  }
102 
103  /**
104  * @return array relational rules.
105  */
106  public function relations()
107  {
108  $relations = array(
109  'profile' => array(self::HAS_ONE, 'Profile', 'user_id'),
110  );
111  if (isset(Yii::app()->getModule('user')->relations)) $relations = array_merge($relations, Yii::app()->getModule('user')->relations);
112  return $relations;
113  }
114 
115 // public function beforeSave()
116 // {
117 // if ($this->isNewRecord) {
118 // if (empty($this->createtime)) {
119 // $this->createtime = date('Y-m-d H:i:s');
120 // }
121 // $this->lastvisit = NULL;
122 // }
123 //
124 // return parent::beforeSave();
125 // }
126 
127  /*
128  * Method returns true if user with same email is already exists
129  */
130  public function isExists(){
131  //if(!isset($this->isExists))
132  $this->isExists = User::model()->count('email=:email',array(':email'=>$this->email))?true:false;
133  return $this->isExists;
134  }
135 
136  public static function create($account, $service)
137  {
138  $user = User::model()->findByAttributes(array('email' => $service->getEmail()));
139  if (!$user) {
140  $user = new User();
141  $user->username = $service->getName();
142  $user->attributes = array(
143  //'username' => $service->getName(), //for unknown reason this doesn't work
144  'email' => $service->getEmail(),
145  'password' => 'empty',
146  'activkey' => 'empty',
147  'status' => 1
148  );
149 
150  if (!$user->save()) {
151  throw new Exception(print_r($user->getErrors(), true));
152  }
153 
154  $profile = new Profile;
155  $profile->user_id = $user->id;
156 
157  if (isset($service->attributes['firstname'])) {
158  $profile->firstname = $service->attributes['firstname'];
159  }
160  if (isset($service->attributes['lastname'])) {
161  $profile->lastname = $service->attributes['lastname'];
162  }
163  if (isset($service->attributes['gender'])) {
164  $profile->gender = $service->attributes['gender'];
165  }
166 
167  if (!$profile->save(false)) {
168  throw new Exception(print_r($profile->getErrors(), true));
169  }
170  }
171  $account->user_id = $user->id;
172  $account->save();
173 
174  return $user;
175  }
176 
177  /**
178  * @return array customized attribute labels (name=>label)
179  */
180  public function attributeLabels()
181  {
182  return array(
183  'username' => UserModule::t("Username"),
184  'password' => UserModule::t("Password"),
185  'verifyPassword' => UserModule::t("Retype Password"),
186  'email' => UserModule::t("E-mail"),
187  'verifyCode' => UserModule::t("Verification Code"),
188  'id' => UserModule::t("Id"),
189  'activkey' => UserModule::t("activation key"),
190  'createtime' => UserModule::t("Registration date"),
191  'lastvisit' => UserModule::t("Last visit"),
192  'status' => UserModule::t("Status"),
193  );
194  }
195 
196  public function scopes()
197  {
198  return array(
199  'active' => array(
200  'condition' => 'status=' . self::STATUS_ACTIVE,
201  ),
202  'notactvie' => array(
203  'condition' => 'status=' . self::STATUS_NOACTIVE,
204  ),
205  'banned' => array(
206  'condition' => 'status=' . self::STATUS_BANED,
207  ),
208  'superuser' => array(
209  ),
210  'notsafe' => array(
211  'select' => 'id, username, password, email, activkey, createtime, lastvisit, status',
212  ),
213  );
214  }
215 
216  public function defaultScope()
217  {
218  return array(
219  'select' => 'id, username, email, createtime, lastvisit, status',
220  );
221  }
222 
223  public static function itemAlias($type, $code = NULL)
224  {
225  $_items = array(
226  'UserStatus' => array(
227  self::STATUS_NOACTIVE => UserModule::t('Not active'),
228  self::STATUS_ACTIVE => UserModule::t('Active'),
229  self::STATUS_BANED => UserModule::t('Banned'),
230  ),
231  'AdminStatus' => array(
232  '0' => UserModule::t('No'),
233  '1' => UserModule::t('Yes'),
234  ),
235  );
236  if (isset($code))
237  return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
238  else
239  return isset($_items[$type]) ? $_items[$type] : false;
240  }
241 
242  /**
243  * Retrieves a list of models based on the current search/filter conditions.
244  * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
245  */
246  public function search()
247  {
248  // Warning: Please modify the following code to remove attributes that
249  // should not be searched.
250 
251  $criteria = new CDbCriteria;
252 
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);
262 
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);
267 
268  return new CActiveDataProvider($this, array(
269  'criteria' => $criteria,
270  'pagination' => array(
271  'pageSize' => Yii::app()->controller->module->user_page_size,
272  ),
273  ));
274  }
275 }