· 9 years ago · May 29, 2017, 09:24 AM
1<?php
2
3namespace frontend\models;
4
5use yii\db\ActiveRecord;
6use yii\behaviors\TimestampBehavior;
7use yii\db\Expression;
8use Yii;
9
10/**
11 * This is the model class for table "tb_user".
12 *
13 * @property integer $id
14 * @property string $username
15 * @property string $password
16 * @property integer $status
17 * @property string $activation_token
18 * @property string $ts_created_at
19 * @property string $ts_modified_at
20 * @property integer $fk_user_person
21 *
22 * @property TbActivityLog[] $tbActivityLogs
23 * @property TbAdArea[] $tbAdAreas
24 * @property TbCategory[] $tbCategories
25 * @property TbClaim[] $tbClaims
26 * @property TbLink[] $tbLinks
27 * @property TbListing[] $tbListings
28 * @property TbListing[] $tbListings0
29 * @property TbMessage[] $tbMessages
30 * @property TbMessage[] $tbMessages0
31 * @property TbNewsletterSubscriber[] $tbNewsletterSubscribers
32 * @property TbPackage[] $tbPackages
33 * @property TbReview[] $tbReviews
34 * @property TbPerson $fkUserPerson
35 * @property TbUserFavourite[] $tbUserFavourites
36 */
37
38
39class TbUsers extends \yii\db\ActiveRecord
40{
41 public $password_repeat;
42 public $oldpass;
43 public $redirect = null;// if redirect required, url should be here. Case when clicked on add listing, but nee dto sign up first
44
45 public $updated = false;
46 // public $new_pass;
47 /**
48 * @inheritdoc
49 */
50 public static function tableName()
51 {
52 return 'tb_user';
53 }
54
55 /**
56 * @inheritdoc
57 */
58 public function rules()
59 {
60 return [
61 [['password','password_repeat'],'required','on'=>['create','update','password_change']],
62 ['username','required','message'=>'Email cannot be blank ','on' => ['create']],
63 [['password','password_repeat','oldpass'],'required','on'=>['change-password']],
64 [['password','password_repeat' ],'required','on'=>['password-reset']],
65 [['password'],'string','length' => [6, 24]],
66 [['oldpass'],'checkOldPassword','on'=>['password_change','change-password']],
67 [['username','password'],'required','on' => ['default']],
68 //[['username'],'unique',"on"=>'create'],
69 [['username'],'new_and_unique','on'=>['email_update','create']],
70 [['id'],'required','on'=>['email_update','password_change','change-password']],
71 // [['username'],'required','on'=>['create','email_update']],
72 [['username'],'email','message'=>'invalid email','on'=>['create','email_update']],
73 [['password_repeat'], 'compare', 'compareAttribute'=>'password','message'=>'Passwords doesnot match','on'=>['create','password_change','change-password','password-reset']],
74 //[['username'],'validateUsername','on'=>'createInside',''],
75 [['status', 'fk_user_person'], 'integer'],
76 [['ts_created_at', 'ts_modified_at','activation_token','redirect'], 'safe'],
77 [['username', 'password'], 'string', 'max' => 255],
78 [['fk_user_person'], 'exist', 'skipOnError' => true, 'targetClass' => TbPerson::className(), 'targetAttribute' => ['fk_user_person' => 'id']],
79 ];
80 }
81 public function new_and_unique($attribute,$params)
82 {
83 $query = $this->find()->where(['username'=>$this->username]);
84 if($this->id) {
85 $query->andWhere(['!=','id',$this->id]);
86
87 }
88 //var_dump($query->prepare(Yii::$app->db->queryBuilder)->createCommand()->rawSql);
89
90 $err = $query->one()!=null?true:false;
91 if($err)
92 $this->addError($attribute,'The email is already taken ');
93 }
94 public function scenarios() {
95 return [
96 'email_update' => ['username','id'],
97 'login' => ['username','password'],
98 'password_change' => ['password','password_repeat','id'],
99 'password-reset' => ['password','password_repeat','id'],
100 'change-password' => ['password','password_repeat','oldpass','id'],
101 'create' => ['password','password_repeat','fk_user_person','id','username','activation_token'],
102 'update' => ['password','password_repeat','fk_user_person','id','username','status'],
103 ];
104 }
105 public function validateUsername() {
106
107
108 }
109
110
111 /**
112 * @inheritdoc
113 */
114 /* public function validateUsername()
115 { if(TbUsers::model()->exists('username=:username',array('username'=>$this->username))
116 $this->addError('username','Username already exists.');
117 } */
118 public function attributeLabels()
119 {
120 return [
121 'id' => Yii::t('app', 'ID'),
122 'username' => Yii::t('app', 'Username'),
123 'password' => Yii::t('app', 'Password'),
124 'status' => Yii::t('app', 'Status'),
125 'ts_created_at' => Yii::t('app', 'Ts Created At'),
126 'ts_modified_at' => Yii::t('app', 'Ts Modified At'),
127 'fk_user_person' => Yii::t('app', 'Fk User Person'),
128 ];
129 }
130
131
132 public function checkOldPassword($attribute, $params){
133 //$user = User::find()->where(['username'=>Yii::$app->user->identity->username])->one();
134 //$session = Yii::$app->session;
135
136 $user = $this->find()->where(['id'=>$this->id])->one();
137 $passwordHash = $user->password;
138 $isMatching = Yii::$app->security->validatePassword($this->oldpass,$passwordHash);
139
140
141 if(!$isMatching){
142 $this->addError($attribute,'Current password is incorrect');
143 }
144 return $isMatching;
145 }
146
147
148
149 /**
150 * @return \yii\db\ActiveQuery
151 */
152 public function getTbActivityLogs()
153 {
154 return $this->hasMany(TbActivityLog::className(), ['fk_activity_log_user' => 'id']);
155 }
156
157 /**
158 * @return \yii\db\ActiveQuery
159 */
160 public function getTbAdAreas()
161 {
162 return $this->hasMany(TbAdArea::className(), ['fk_ad_area_user' => 'id']);
163 }
164
165 /**
166 * @return \yii\db\ActiveQuery
167 */
168 public function getTbCategories()
169 {
170 return $this->hasMany(TbCategory::className(), ['fk_category_user' => 'id']);
171 }
172
173 /**
174 * @return \yii\db\ActiveQuery
175 */
176 public function getTbClaims()
177 {
178 return $this->hasMany(TbClaim::className(), ['fk_claim_user' => 'id']);
179 }
180
181 /**
182 * @return \yii\db\ActiveQuery
183 */
184 public function getTbLinks()
185 {
186 return $this->hasMany(TbLink::className(), ['fk_link_user' => 'id']);
187 }
188
189 /**
190 * @return \yii\db\ActiveQuery
191 */
192 public function getTbListings()
193 {
194 return $this->hasMany(TbListing::className(), ['fk_listing_user' => 'id']);
195 }
196
197 /**
198 * @return \yii\db\ActiveQuery
199 */
200 public function getTbListings0()
201 {
202 return $this->hasMany(TbListing::className(), ['fk_listing_user' => 'id']);
203 }
204
205 /**
206 * @return \yii\db\ActiveQuery
207 */
208 public function getTbMessages()
209 {
210 return $this->hasMany(TbMessage::className(), ['fk_message_user_from' => 'id']);
211 }
212
213 /**
214 * @return \yii\db\ActiveQuery
215 */
216 public function getTbMessages0()
217 {
218 return $this->hasMany(TbMessage::className(), ['fk_message_user_to' => 'id']);
219 }
220
221 /**
222 * @return \yii\db\ActiveQuery
223 */
224 public function getTbNewsletterSubscribers()
225 {
226 return $this->hasMany(TbNewsletterSubscriber::className(), ['fk_newsletter_subscriber_subscriber' => 'id']);
227 }
228
229 /**
230 * @return \yii\db\ActiveQuery
231 */
232 public function getTbPackages()
233 {
234 return $this->hasMany(TbPackage::className(), ['fk_package_user' => 'id']);
235 }
236
237 /**
238 * @return \yii\db\ActiveQuery
239 */
240 public function getTbReviews()
241 {
242 return $this->hasMany(TbReview::className(), ['fk_review_user' => 'id']);
243 }
244
245 /**
246 * @return \yii\db\ActiveQuery
247 */
248 public function getFkUserPerson()
249 {
250 return $this->hasOne(TbPerson::className(), ['id' => 'fk_user_person']);
251 }
252
253 /**
254 * @return \yii\db\ActiveQuery
255 */
256 public function getTbUserFavourites()
257 {
258 return $this->hasMany(TbUserFavourite::className(), ['fk_user_favourite_user' => 'id']);
259 }
260 public function toggle_status($id) {
261 $connection = Yii::$app->db;
262 $colVal = $connection->createCommand('SELECT status from tb_user WHERE id =:id')->bindParam(':id',$id)->queryOne();
263 if(!$colVal){
264 return -1;
265 }
266
267 $status = $colVal['status'];
268 $status = $status ==1 ?0:1;
269
270 $connection->createCommand()->update('tb_user', ['status' => $status], 'id=:id')->bindParam(':id',$id)->execute();
271
272 return $status;
273 }
274 public function getListinguser()
275 {
276 $query = new Query;
277 $model = Yii::$app->db->createCommand('SELECT first_name from tb_person join tb_user on tb_person.id= tb_user.fk_user_person join tb_listing on tb_listing.fk_listing_user = tb_user.id')
278 ->queryOne();
279 $data = $model['first_name'];
280 print_r($data);
281 return $data;
282 }
283 public function update_user($id,$data) {
284 $connection = Yii::$app->db;
285 $connection->createCommand()->update('tb_person', $data, 'id=:id')->bindParam(':id',$id)->execute();
286
287 return true;
288 }
289
290 public function changepassword($id,$new_pass) {
291 $connection = Yii::$app->db;
292 $new_pass = ['password' => $new_pass];
293 $connection->createCommand()->update('tb_user', $new_pass, 'fk_user_person=:id')->bindParam(':id',$id)->execute();
294
295 return true;
296 }
297 public function verifyAccount($id)
298 {
299
300 $connection = Yii::$app->db;
301 $colVal = $connection->createCommand('SELECT account_verfied from tb_user WHERE fk_user_person =:id')->bindParam(':id',$id)->queryOne();
302
303 if(!$colVal){
304 return -1;
305 }
306
307 $account_verfied = $colVal['account_verfied'];
308 $account_verfied = $account_verfied ==1 ?0:1;
309
310 $connection->createCommand()->update('tb_user', ['account_verfied' => $account_verfied], 'fk_user_person=:id')->bindParam(':id',$id)->execute();
311
312 return $account_verfied;
313 }
314 public function verifyPhone($id)
315 {
316
317 $connection = Yii::$app->db;
318 $colVal = $connection->createCommand('SELECT phone_verified from tb_user WHERE fk_user_person =:id')->bindParam(':id',$id)->queryOne();
319
320 if(!$colVal){
321 return -1;
322 }
323
324 $phone_verified = $colVal['phone_verified'];
325 $phone_verified = $phone_verified ==1 ?0:1;
326
327 $connection->createCommand()->update('tb_user', ['phone_verified' => $phone_verified], 'fk_user_person=:id')->bindParam(':id',$id)->execute();
328
329 return $phone_verified;
330 }
331 public function activate_user($id)
332 {
333
334 $connection = Yii::$app->db;
335 $colVal = $connection->createCommand('SELECT status from tb_user WHERE id =:id')->bindParam(':id',$id)->queryOne();
336
337 if(!$colVal){
338 return -1;
339 }
340
341 $status = $colVal['status'];
342 $status = $status ==1 ?0:1;
343
344 $connection->createCommand()->update('tb_user', ['status' => $status], 'id=:id')->bindParam(':id',$id)->execute();
345
346 return $status;
347 }
348
349 public function behaviors()
350 {
351 return [
352 [
353
354 'class' => TimestampBehavior::className(),
355 'createdAtAttribute' => 'ts_created_at',
356 'updatedAtAttribute' => 'ts_modified_at',
357 'value' => new Expression('NOW()'),
358 // if you're using datetime instead of UNIX timestamp:
359 // 'value' => new Expression('NOW()'),
360 ],
361 ];
362 }
363
364 public function insertNew() {
365 $model = Yii::$app->utilities->cloneModel(TbUsers::className(),$this);
366 $model->save(false);
367 $userId = $model->id;
368 $ret = $userId?$model:null;
369 return $ret;
370 }
371
372 public function insertFromPersonIfNotExisting($modelPerson) { // checks whether a person with same email exists and inserts if not
373
374 $user = $this->find()->where(['username'=>$modelPerson->email])->one();//current record
375 //print_r($user);exit;
376 while(1){
377 if(!$user) { //user exists already
378 $modelPerson->setScenario('createdFromListing');
379 $modelPerson->status = 1;
380 $modelPerson->dob = date('Y-m-d H:i:s');
381 $modelPerson = $modelPerson->insertNew();
382 if(!$modelPerson) break;
383
384 $this->username = $modelPerson->email;
385 $this->password = sha1(time());
386 $this->fk_user_person = $modelPerson->id;
387 $this->account_verfied = 0;
388 $this->email_verified = 0;
389 //$this->activate_person=0;
390 $this->phone_verified = 0;
391 $this->status = 1;
392 $user = $this->insertNew();
393 }
394 // else{
395 // echo $modelPerson->first_name;
396 // $modelPerson->first_name = $modelPerson->first_name;
397 // print_r($modelPerson);exit;
398 // $modelPerson->update(false);
399 // }
400 break;
401 }
402 return $user;
403 }
404 public function activateUser() {
405 $this->status = 1;
406 $this->account_verfied = 1;
407 $connection = Yii::$app->db;
408 $this->update(false);
409
410 return true;
411 }
412 public function activatePhone($id) {
413 $connection = Yii::$app->db;
414 $connection->createCommand()->update('tb_user', ['phone_verified' => 1], 'id=:id')->bindParam(':id',$id)->execute();
415
416 return true;
417 }
418 public function hasPassword() {
419 return $this->password != null;
420 }
421
422}