Blame view

backend/controllers/SiteController.php 3.15 KB
1755c393   Yarik   Basic template in...
1
  <?php
543b9b1c   Yarik   Composer ready
2
      namespace backend\controllers;
3cae1b75   Alexey Boroda   -Analytics almost...
3
4
5
  
      use backend\models\Analytics;
      use common\models\Settings;
543b9b1c   Yarik   Composer ready
6
7
8
9
10
      use Yii;
      use yii\web\Controller;
      use yii\filters\VerbFilter;
      use yii\filters\AccessControl;
      use common\models\LoginForm;
3cae1b75   Alexey Boroda   -Analytics almost...
11
  
1755c393   Yarik   Basic template in...
12
      /**
543b9b1c   Yarik   Composer ready
13
       * Site controller
1755c393   Yarik   Basic template in...
14
       */
543b9b1c   Yarik   Composer ready
15
      class SiteController extends Controller
1755c393   Yarik   Basic template in...
16
      {
543b9b1c   Yarik   Composer ready
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
          /**
           * @inheritdoc
           */
          public function behaviors()
          {
              return [
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              'actions' => [
                                  'login',
                                  'error',
                              ],
                              'allow'   => true,
                          ],
                          [
                              'actions' => [
                                  'logout',
                                  'index',
                                  'analytic',
                              ],
                              'allow'   => true,
                              'roles'   => [ '@' ],
0bdfedea   Alexey Boroda   -Analytics started
41
                          ],
1755c393   Yarik   Basic template in...
42
43
                      ],
                  ],
543b9b1c   Yarik   Composer ready
44
45
46
47
48
                  'verbs'  => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'logout' => [ 'post' ],
                      ],
1755c393   Yarik   Basic template in...
49
                  ],
543b9b1c   Yarik   Composer ready
50
51
              ];
          }
3cae1b75   Alexey Boroda   -Analytics almost...
52
      
543b9b1c   Yarik   Composer ready
53
54
55
56
57
58
59
60
61
62
63
          /**
           * @inheritdoc
           */
          public function actions()
          {
              return [
                  'error' => [
                      'class' => 'yii\web\ErrorAction',
                  ],
              ];
          }
3cae1b75   Alexey Boroda   -Analytics almost...
64
      
543b9b1c   Yarik   Composer ready
65
66
67
68
69
70
71
          /**
           * Displays homepage.
           *
           * @return string
           */
          public function actionIndex()
          {
3cae1b75   Alexey Boroda   -Analytics almost...
72
73
74
75
76
77
78
              $settings = Settings::getInstance();
          
              $analytics = new Analytics(
                  [
                      'viewId' => $settings->analytics_key,
                  ]
              );
543b9b1c   Yarik   Composer ready
79
          
3cae1b75   Alexey Boroda   -Analytics almost...
80
81
82
83
84
85
86
87
              return $this->render(
                  'analytics',
                  [
                      'data' => $analytics->generateData(),
                  ]
              );
          }
      
543b9b1c   Yarik   Composer ready
88
89
90
91
92
93
94
95
96
97
          /**
           * Login action.
           *
           * @return string
           */
          public function actionLogin()
          {
              if (!Yii::$app->user->isGuest) {
                  return $this->goHome();
              }
3cae1b75   Alexey Boroda   -Analytics almost...
98
          
543b9b1c   Yarik   Composer ready
99
100
101
102
103
104
105
106
107
108
109
110
              $model = new LoginForm();
              if ($model->load(Yii::$app->request->post()) && $model->login()) {
                  return $this->goBack();
              } else {
                  return $this->renderPartial(
                      'login',
                      [
                          'model' => $model,
                      ]
                  );
              }
          }
3cae1b75   Alexey Boroda   -Analytics almost...
111
      
543b9b1c   Yarik   Composer ready
112
113
114
115
116
117
118
119
          /**
           * Logout action.
           *
           * @return string
           */
          public function actionLogout()
          {
              Yii::$app->user->logout();
3cae1b75   Alexey Boroda   -Analytics almost...
120
          
1755c393   Yarik   Basic template in...
121
122
              return $this->goHome();
          }
3cae1b75   Alexey Boroda   -Analytics almost...
123
      
543b9b1c   Yarik   Composer ready
124
125
          public function actionAnalytic()
          {
05a089dd   Alexey Boroda   -Analytics almost...
126
              return $this->render('analytic');
1755c393   Yarik   Basic template in...
127
128
          }
      }