pmtpenza

Untitled

Sep 5th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. <?php
  2. namespace api\models;
  3. use common\models\User;
  4. use backend\models\AuthAssignment;
  5. use Yii;
  6. use yii\base\Exception;
  7. use yii\base\Model;
  8. use yii\helpers\ArrayHelper;
  9. use yii\behaviors\TimestampBehavior;
  10. /**
  11. * Create user form
  12. */
  13. class UserForm extends Model
  14. {
  15. public function behaviors()
  16. {
  17. return [
  18. TimestampBehavior::className(),
  19. ];
  20. }
  21.  
  22. const STATUS_ACTIVE = 10;
  23. public $username;
  24. public $password;
  25. public $status;
  26. public $phone;
  27. public $created_at;
  28. public $updated_at;
  29. public $fio;
  30. public $gender;
  31. public $birthdate;
  32. public $description;
  33. public $avatar;
  34.  
  35. private $model;
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41.  
  42. return [
  43. [['phone'], 'required'],
  44. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  45. [['username'], 'string', 'max' => 255],
  46. [['phone'], 'string', 'max' => 50],
  47. ['phone', 'unique', 'targetClass' => User::class, 'filter' => function ($query) {
  48. if (!$this->getModel()->isNewRecord) {
  49. $query->andWhere(['not', ['id' => $this->getModel()->id]]);
  50. }
  51. }],
  52. ['password', 'required', 'on' => 'create'],
  53. ['password', 'string', 'min' => 6],
  54. ];
  55. }
  56. /**
  57. * @return User
  58. */
  59. public function getModel()
  60. {
  61. if (!$this->model) {
  62. $this->model = new User();
  63. }
  64. return $this->model;
  65. }
  66. /**
  67. * @param User $model
  68. * @return mixed
  69. */
  70. public function setModel($model)
  71. {
  72. $this->username = $model->username;
  73. $this->status = $model->status;
  74. $this->phone = $model->phone;
  75. $this->phone = $model->fio;
  76. $this->phone = $model->gender;
  77. $this->phone = $model->birthdate;
  78. $this->phone = $model->description;
  79. $this->phone = $model->avatar;
  80. $this->model = $model;
  81.  
  82. return $this->model;
  83. }
  84. /**
  85. * @inheritdoc
  86. */
  87. public function attributeLabels()
  88. {
  89. return [
  90. 'username' => 'Имя',
  91. 'password' => 'Пароль',
  92. 'phone' => 'Телефон',
  93.  
  94. ];
  95. }
  96. /**
  97. * Signs user up.
  98. * @return User|null the saved model or null if saving fails
  99. * @throws Exception
  100. */
  101. public function save()
  102. {
  103. if ($this->validate()) {
  104. $model = $this->getModel();
  105. $isNewRecord = $model->getIsNewRecord();
  106. $model->username = $this->username;
  107. $model->status = $this->status;
  108. $model->phone = $this->phone;
  109. $model->fio = $this->fio;
  110. $model->gender = $this->gender;
  111. $model->birthdate = $this->birthdate;
  112. $model->description = $this->description;
  113. $model->avatar = $this->avatar;
  114.  
  115. $model->status = $this->status;
  116. if ($this->password) {
  117. $model->setPassword($this->password);
  118. }
  119. if (!$model->save()) {
  120. throw new Exception('Model not saved');
  121. }
  122. }
  123. return $model;
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment