Blame view

frontend/models/SignupForm.php 3.24 KB
b0f143c3   Yarik   first commit
1
2
3
4
  <?php
  namespace frontend\models;
  
  use common\models\User;
76f36646   Yarik   test
5
  use common\models\UserInfo;
b0f143c3   Yarik   first commit
6
7
8
9
10
11
12
13
14
15
16
  use yii\base\Model;
  use Yii;
  
  /**
   * Signup form
   */
  class SignupForm extends Model
  {
      public $username;
      public $email;
      public $password;
f0f915df   Administrator   add Vitaliy's wid...
17
18
19
20
      public $firstname;
      public $lastname;
      public $verifyCode;
      public $location;
998611ce   Administrator   add Vitaliy's wid...
21
      public $type;
30fbd0bc   Administrator   09.02.16
22
23
24
      public $is_customer;
      public $is_freelancer;
      public $city;
649de741   Administrator   24.02.16
25
      public $name;
143979c5   Yarik   test
26
      public $city_custom;
3dc20ff7   Administrator   24.02.16
27
28
29
30
31
  
  
      const SCENARIO_USER = 'user';
      const SCENARIO_COMPANY = 'company';
  
b0f143c3   Yarik   first commit
32
33
34
35
36
37
38
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              ['username', 'filter', 'filter' => 'trim'],
42931736   Yarik   test
39
              [['username', 'city'], 'required'],
b0f143c3   Yarik   first commit
40
41
42
              ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
              ['username', 'string', 'min' => 2, 'max' => 255],
  
143979c5   Yarik   test
43
              [['city', 'city_custom'], 'string', 'min' => 2, 'max' => 255],
f0f915df   Administrator   add Vitaliy's wid...
44
  
b0f143c3   Yarik   first commit
45
46
47
48
49
              ['email', 'filter', 'filter' => 'trim'],
              ['email', 'required'],
              ['email', 'email'],
              ['email', 'string', 'max' => 255],
              ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
7e6d3f52   Administrator   24.02.16
50
51
52
              ['firstname', 'required'],
  
  
f0f915df   Administrator   add Vitaliy's wid...
53
54
              ['verifyCode', 'captcha'],
  
998611ce   Administrator   add Vitaliy's wid...
55
              ['type', 'integer'],
30fbd0bc   Administrator   09.02.16
56
57
              ['is_customer', 'integer'],
              ['is_freelancer', 'integer'],
998611ce   Administrator   add Vitaliy's wid...
58
  
b0f143c3   Yarik   first commit
59
60
              ['password', 'required'],
              ['password', 'string', 'min' => 6],
3dc20ff7   Administrator   24.02.16
61
62
63
64
65
66
              [
                  'name',
                  'required',
                  'on'=>[SignupForm::SCENARIO_COMPANY]
              ],
              [
788e40d8   Yarik   test
67
                  ['name', 'lastname'],
3dc20ff7   Administrator   24.02.16
68
69
70
71
72
73
74
75
                  'safe',
                  'on'=>[SignupForm::SCENARIO_USER]
              ],
              [
                  'name',
                  'required',
                  'on'=>[SignupForm::SCENARIO_DEFAULT]
              ],
b0f143c3   Yarik   first commit
76
77
78
79
80
81
82
83
84
85
          ];
      }
  
      /**
       * Signs user up.
       *
       * @return User|null the saved model or null if saving fails
       */
      public function signup()
      {
42931736   Yarik   test
86
87
88
          if(empty($this->city)) {
              $this->city = $this->city_custom;
          }
b0f143c3   Yarik   first commit
89
90
91
          if ($this->validate()) {
              $user = new User();
              $user->username = $this->username;
83cba62c   Administrator   24.02.16
92
93
              $user->firstname = $this->firstname;
              $user->lastname = $this->lastname;
b0f143c3   Yarik   first commit
94
              $user->email = $this->email;
3dc20ff7   Administrator   24.02.16
95
              $user->type = $this->type;
b0f143c3   Yarik   first commit
96
97
              $user->setPassword($this->password);
              $user->generateAuthKey();
83cba62c   Administrator   24.02.16
98
  
b0f143c3   Yarik   first commit
99
              if ($user->save()) {
b0f143c3   Yarik   first commit
100
101
102
103
104
105
                  return $user;
              }
          }
  
          return null;
      }
30fbd0bc   Administrator   09.02.16
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'username' => 'Логин',
              'password' => 'Пароль',
              'email' => 'E-mail',
              'type' => Yii::t('app', 'Is Default'),
              'firstname' => 'Имя',
              'lastname' => 'Фамилия',
              'city' => 'Город',
              'alt_location' => 'Город не в списке',
              'is_customer' => '',
              'is_freelancer' => '',
649de741   Administrator   24.02.16
122
              'name' => 'Название компании'
30fbd0bc   Administrator   09.02.16
123
124
          ];
      }
b0f143c3   Yarik   first commit
125
126
  
  }