Blame view

frontend/modules/user/widgets/Login.php 1.1 KB
d1f8bd40   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  <?php
  
  namespace frontend\modules\user\widgets;
  
  use Yii;
  use yii\helpers\Url;
  use yii\web\Response;
  use yii\base\Widget;
  use frontend\modules\user\models\form\SignInForm;
  
  
  /**
   * Class LogIn
   *
   * @author Andrii Bondarchuk
   * @copyright (c) 2016, VipDesign
   */
  class Login extends Widget
  {
      /**
       * @var string
       */
      public $view = 'login';
  
      /**
       * @var string
       */
      public $redirectUrl = null;
  
      /**
       * @var
       */
      protected $model = SignInForm::class;
  
      /**
       * @return bool
       */
      public function run()
      {
          $model = new $this->model;
          $model->setScenario('signIn');
  
          if ($model->load(Yii::$app->getRequest()->post()) && $model->login()) {
              return ($this->redirectUrl)
                  ? $this->redirect($this->redirectUrl)
                  : $this->goHome();;
          } elseif (Yii::$app->request->isAjax) {
              Yii::$app->response->format = Response::FORMAT_JSON;
              return ['error' => Yii::t('app', 'Incorrect email or password')];
          }
  
          return $this->render($this->view, ['model' => $model]);
      }
  }