Blame view

frontend/models/SignupForm.php 3.18 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;
b0f143c3   Yarik   first commit
26
  
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
39
40
41
42
      /**
       * @inheritdoc
       */
      public function rules()
      {
          return [
              ['username', 'filter', 'filter' => 'trim'],
              ['username', 'required'],
              ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
              ['username', 'string', 'min' => 2, 'max' => 255],
  
30fbd0bc   Administrator   09.02.16
43
              ['city', '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()
      {
83cba62c   Administrator   24.02.16
86
  
b0f143c3   Yarik   first commit
87
88
89
          if ($this->validate()) {
              $user = new User();
              $user->username = $this->username;
83cba62c   Administrator   24.02.16
90
91
              $user->firstname = $this->firstname;
              $user->lastname = $this->lastname;
b0f143c3   Yarik   first commit
92
              $user->email = $this->email;
3dc20ff7   Administrator   24.02.16
93
              $user->type = $this->type;
b0f143c3   Yarik   first commit
94
95
              $user->setPassword($this->password);
              $user->generateAuthKey();
83cba62c   Administrator   24.02.16
96
  
b0f143c3   Yarik   first commit
97
              if ($user->save()) {
76f36646   Yarik   test
98
  //                $user->link('userInfo', new UserInfo(['city' => $this->city]));
b0f143c3   Yarik   first commit
99
100
101
102
103
104
                  return $user;
              }
          }
  
          return null;
      }
30fbd0bc   Administrator   09.02.16
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
      /**
       * @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
121
              'name' => 'Название компании'
30fbd0bc   Administrator   09.02.16
122
123
          ];
      }
b0f143c3   Yarik   first commit
124
125
  
  }