Created
October 17, 2018 09:38
-
-
Save andku83/9e4843838ecd8e698bb1f513bfdf147a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace frontend\models; | |
use borales\extensions\phoneInput\PhoneInputValidator; | |
use common\components\apex\Apex; | |
use common\models\Client; | |
use common\models\query\ClientQuery; | |
use common\models\Sms; | |
use Yii; | |
use yii\base\Model; | |
use yii\web\NotFoundHttpException; | |
/** | |
* Class ClientForm | |
* @package frontend\models | |
*/ | |
class ClientForm extends Model | |
{ | |
/** | |
* @var string | |
*/ | |
public $email; | |
/** | |
* @var string | |
*/ | |
public $password; | |
/** | |
* @var string | |
*/ | |
// public $name; | |
/** | |
* @var | |
*/ | |
public $password_confirm; | |
public $first_name; | |
public $last_name; | |
public $birthday; | |
public $country; | |
public $phone; | |
public $code; | |
/** | |
* @var | |
*/ | |
public $agreement; | |
/** | |
* @var bool | |
*/ | |
public $result = false; | |
/** | |
* @var int | |
*/ | |
public $resultStatus = 400; | |
/** | |
* @var string | |
*/ | |
protected $token = false; | |
/** | |
* @var Client|null | |
*/ | |
protected $_client; | |
const SCENARIO_STEP1 = 'step1'; | |
const SCENARIO_STEP2 = 'step2'; | |
const SCENARIO_STEP3 = 'step3'; | |
const SCENARIO_STEP4 = 'step4'; | |
const SCENARIO_ACCOUNT_STATUS = 'account_status'; | |
const CODE_LENGTH = 6; | |
/** | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['email', 'password', 'first_name', 'last_name', 'country'], 'filter', 'filter' => 'trim'], | |
['email', 'email'], | |
['email', 'string', 'max' => 255], | |
[['password'], 'string', 'min' => 8, | |
'message' => Yii::t('frontend', 'Password must contain at least 8 characters, one number, and at least one capital letter'), | |
'tooShort' => Yii::t('frontend', 'Password must contain at least 8 characters, one number, and at least one capital letter'), | |
'tooLong' => Yii::t('frontend', 'Password must contain at least 8 characters, one number, and at least one capital letter')], | |
[['password'], 'match', 'pattern' => '/^.*(?=.*\d)(?=.*[A-Z]).*$/',/* 'pattern' => '/^.*(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/',*/ | |
'message' => Yii::t('frontend', 'Password must contain at least 8 characters, one number, and at least one capital letter')], | |
// [['name'], 'required'], | |
// [['name'], 'filter', 'filter' => 'trim'], | |
// ['name', 'string', 'min' => 2, 'max' => 255], | |
// ['password_confirm', 'compare', 'compareAttribute' => 'password'], | |
// ['agreement', 'compare', 'compareValue' => 1, 'message' => Yii::t('frontend', 'You must accept the user agreement.')], | |
[['first_name', 'last_name', 'country'], 'string', 'min' => 2, 'max' => 255], | |
[['birthday'], 'date', 'format' => 'php:Y-m-d'], | |
[['phone'], PhoneInputValidator::class], | |
[['phone'], 'unique', | |
'targetClass' => Client::class, | |
'filter' => function (ClientQuery $query) { | |
$query->andWhere(['not', ['id' => $this->getClient()->id]]) | |
->andWhere(['phone_active' =>Client::PHONE_STATUS_ACTIVE]); | |
} | |
], | |
[['email'], 'required'], | |
[['email', 'password'], 'required', 'on' => self::SCENARIO_STEP1], | |
[['first_name', 'last_name', 'birthday', 'country'], 'required', 'on' => self::SCENARIO_STEP2], | |
[['phone'], 'required', 'on' => self::SCENARIO_STEP3], | |
[['code'], 'required', 'on' => self::SCENARIO_STEP4], | |
]; | |
} | |
/** | |
* @return array | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'email' => Yii::t('frontend', 'Email'), | |
// 'name' => Yii::t('frontend', 'Name'), | |
'password' => Yii::t('frontend', 'Password'), | |
'password_confirm' => Yii::t('frontend', 'Confirm Password'), | |
]; | |
} | |
public function scenarios() | |
{ | |
$scenarios = parent::scenarios(); | |
$scenarios[self::SCENARIO_STEP1] = ['email', 'password', 'password_confirm']; | |
$scenarios[self::SCENARIO_STEP2] = ['email', 'first_name', 'last_name', 'birthday', 'country']; | |
$scenarios[self::SCENARIO_STEP3] = ['email', 'phone']; | |
$scenarios[self::SCENARIO_STEP4] = ['email', 'code']; | |
$scenarios[self::SCENARIO_ACCOUNT_STATUS] = ['email']; | |
return $scenarios; | |
} | |
/** | |
* @return bool | |
*/ | |
public function add() | |
{ | |
if ($this->validate()) { | |
$client = new Client(); | |
$client->name = $this->email; | |
$client->email = $this->email; | |
$client->password = $this->password; | |
$client->status = Client::STATUS_NOT_ACTIVE; | |
$transaction = $client::getDb()->beginTransaction(); | |
if ($client->save()) { | |
Apex::registerUser($client); | |
} else { | |
$this->addErrors($client->errors); | |
} | |
if ($this->result) | |
$transaction->commit(); | |
} | |
return $this->result; | |
} | |
/** | |
* @return string | |
*/ | |
public function formName() | |
{ | |
return ''; | |
} | |
/** | |
* @return bool | |
*/ | |
public function step1() | |
{ | |
if ($this->validate()) { | |
$client = $this->getClient(false); | |
if (!$client) { | |
$client = new Client(); | |
} | |
$client->name = $this->email; | |
$client->email = $this->email; | |
$client->password = $this->password; | |
$client->status = Client::STATUS_NOT_ACTIVE; | |
if ($client->save()) { | |
$this->result = true; | |
} else { | |
$this->addErrors($client->errors); | |
} | |
} | |
return $this->result; | |
} | |
/** | |
* @return bool | |
*/ | |
public function step2() | |
{ | |
if ($this->validate()) { | |
$client = $this->getClient(); | |
$client->first_name = $this->first_name; | |
$client->last_name = $this->last_name; | |
$client->birthday = $this->birthday; | |
$client->country = $this->country; | |
if ($client->save()) { | |
$this->result = true; | |
} else { | |
$this->addErrors($client->errors); | |
} | |
} | |
return $this->result; | |
} | |
/** | |
* @return bool | |
*/ | |
public function step3() | |
{ | |
if ($this->validate()) { | |
$client = $this->getClient(); | |
if ($client->phone != $this->phone) { | |
$client->phone = $this->phone; | |
$client->phone_active = Client::PHONE_STATUS_INACTIVE; | |
} | |
if (!$client->code) { | |
$client->code = '121212'; | |
//$client->code = $this->generateCode(self::CODE_LENGTH); | |
} | |
if ($client->save()) { | |
$this->sendSms(); | |
$this->result = true; | |
} else { | |
$this->addErrors($client->errors); | |
} | |
} | |
return $this->result; | |
} | |
/** | |
* @return bool | |
*/ | |
public function step4() | |
{ | |
if ($this->validate()) { | |
$client = $this->getClient(); | |
if ($client->code == $this->code) { | |
$client->phone_active = Client::PHONE_STATUS_ACTIVE; | |
if ($client->save()) { | |
$this->result = true; | |
Apex::registerUser($client); | |
} else { | |
$this->addErrors($client->errors); | |
} | |
} else { | |
$this->addError('code', Yii::t('frontend', 'Incorrect code')); | |
} | |
} | |
return $this->result; | |
} | |
/** | |
* @return bool | |
*/ | |
public function accountStatus() | |
{ | |
if ($this->validate()) { | |
if ($client = Client::findOne(['email' => $this->email])) { | |
$this->result = true; | |
$this->resultStatus = 200; | |
return $client->isActive() && Apex::emailVerified($client); | |
} | |
$this->resultStatus = 404; | |
} | |
return false; | |
} | |
/** | |
* @param bool $check | |
* @return Client|null | |
* @throws NotFoundHttpException | |
*/ | |
public function getClient($check = true) | |
{ | |
if (!$this->_client) { | |
$this->_client = Client::findOne(['email' => $this->email, 'status' => Client::STATUS_NOT_ACTIVE]); | |
} | |
if ($check && !$this->_client) { | |
throw new NotFoundHttpException('The requested page does not exist.'); | |
} | |
return $this->_client; | |
} | |
/** | |
* @param int $length | |
* @return string | |
*/ | |
protected function generateCode($length = 6) | |
{ | |
$number = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$number .= mt_rand(0, 9); | |
} | |
return $number; | |
} | |
/** | |
* @return bool | |
*/ | |
public function sendSms() | |
{ | |
$client = $this->getClient(); | |
if ($client->code) { | |
return Sms::send($client->phone, $client->code); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment