Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andku83
Created August 10, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andku83/e462b58c17b492feca0b6b650f5c18ea to your computer and use it in GitHub Desktop.
Save andku83/e462b58c17b492feca0b6b650f5c18ea to your computer and use it in GitHub Desktop.
<?php
namespace backend\models;
use common\models\User;
use common\models\UserProfile;
use Yii;
use yii\base\Exception;
use yii\base\Model;
/**
* Create user form
*/
class UserForm extends Model
{
public $password;
protected $user;
protected $userProfile;
/**
* @var string
*/
public $db = 'db';
/**
* @var array
*/
protected $models = [];
public function rules()
{
return [
['password', 'required', 'on' => 'create'],
['password', 'string', 'min' => 6],
];
}
public function attributeLabels()
{
return [
'password' => Yii::t('common', 'Password'),
];
}
/**
* @param User $model
*/
public function setUser(User $model)
{
$this->user = $model;
}
/**
* @return User
*/
public function getUser()
{
if ($this->user === null) {
$this->user = new User();
}
return $this->user;
}
/**
* @param UserProfile $model
*/
public function setUserProfile(UserProfile $model)
{
$this->userProfile = $model;
}
/**
* @return UserProfile
*/
public function getUserProfile()
{
if ($this->userProfile === null) {
$this->userProfile = $this->getUser()->userProfile;
}
if ($this->userProfile === null) {
$this->userProfile = new UserProfile();
}
return $this->userProfile;
}
/**
* @return array
*/
public function getModels()
{
if (empty($this->models)) {
$this->models = [
'user' => $this->getUser(),
'userProfile' => $this->getUserProfile()
];
}
return $this->models;
}
/**
* @param array $data
* @param null|string $formName
* @return bool
*/
public function load($data, $formName = null)
{
$result = parent::load($data);
foreach ($this->getModels() as $k => &$model) {
$success = $model->load($data);
if (!$success) {
$result = false;
}
}
return $result;
}
/**
* @param bool $runValidation
* @return bool
* @throws Exception
*/
public function save($runValidation = true)
{
$this->getUserProfile()->client_id_p = $this->getUserProfile()->client_id_p ?: $this->getUser()->login_p;
if ($runValidation && !$this->validate()) {
return false;
}
$transaction = $this->getDb()->beginTransaction();
$user = $this->getUser();
$isNewUser = $user->getIsNewRecord();
if ($this->password) {
$user->setPassword($this->password);
}
try {
if (!$user->save()) {
throw new Exception('Model not saved');
}
if ($isNewUser) {
$user->afterSignup($this->getUserProfile()->attributes);
} else {
$this->getUserProfile()->save();
}
$transaction->commit();
} catch (\yii\db\Exception $e) {
Yii::warning($e->getMessage());
$transaction->rollBack();
return false;
}
return true;
}
/**
* @param null $attributeNames
* @param bool $clearErrors
* @return bool
*/
public function validate($attributeNames = null, $clearErrors = true)
{
$this->trigger(Model::EVENT_BEFORE_VALIDATE);
$success = true;
foreach ($this->getModels() as $key => $model) {
/* @var $model Model */
if (!$model->validate()) {
$success = false;
$this->addErrors([$key => $model->getErrors()]);
}
}
$this->trigger(Model::EVENT_AFTER_VALIDATE);
return $success;
}
/**
* @return \yii\db\Connection
*/
public function getDb()
{
return Yii::$app->get($this->db);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment