Blame view

backend/controllers/SiteController.php 3.76 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
              $settings = Settings::getInstance();
c8500c2f   Alexey Boroda   -Analytics in pro...
73
74
75
76
77
78
79
80
81
      
              if (empty($settings->analytics_key)) {
                  return $this->render('index');
              } else {
                  $analytics = new Analytics(
                      [
                          'viewId' => $settings->analytics_key,
                      ]
                  );
5e021d7a   Alexey Boroda   -Tabs for analyti...
82
                  $data = $analytics->generateData();
4acbd19d   Alexey Boroda   -Bar tabs
83
      
5e021d7a   Alexey Boroda   -Tabs for analyti...
84
85
                  $browsers = $data[ 'table' ][ 0 ];
                  arsort($browsers);
4acbd19d   Alexey Boroda   -Bar tabs
86
      
5e021d7a   Alexey Boroda   -Tabs for analyti...
87
88
                  $cityes = $data[ 'table' ][ 1 ];
                  arsort($cityes);
4acbd19d   Alexey Boroda   -Bar tabs
89
      
5e021d7a   Alexey Boroda   -Tabs for analyti...
90
91
                  $countries = $data[ 'table' ][ 2 ];
                  arsort($countries);
4acbd19d   Alexey Boroda   -Bar tabs
92
      
c8500c2f   Alexey Boroda   -Analytics in pro...
93
94
95
                  return $this->render(
                      'analytics',
                      [
5e021d7a   Alexey Boroda   -Tabs for analyti...
96
97
98
99
                          'data'      => $data,
                          'browsers'  => $browsers,
                          'cityes'    => $cityes,
                          'countries' => $countries,
c8500c2f   Alexey Boroda   -Analytics in pro...
100
101
102
                      ]
                  );
              }
3cae1b75   Alexey Boroda   -Analytics almost...
103
104
          }
      
543b9b1c   Yarik   Composer ready
105
106
107
108
109
110
111
112
113
114
          /**
           * Login action.
           *
           * @return string
           */
          public function actionLogin()
          {
              if (!Yii::$app->user->isGuest) {
                  return $this->goHome();
              }
5e021d7a   Alexey Boroda   -Tabs for analyti...
115
      
543b9b1c   Yarik   Composer ready
116
117
118
119
120
121
122
123
124
125
126
127
              $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...
128
      
543b9b1c   Yarik   Composer ready
129
130
131
132
133
134
135
136
          /**
           * Logout action.
           *
           * @return string
           */
          public function actionLogout()
          {
              Yii::$app->user->logout();
5e021d7a   Alexey Boroda   -Tabs for analyti...
137
      
1755c393   Yarik   Basic template in...
138
139
              return $this->goHome();
          }
3cae1b75   Alexey Boroda   -Analytics almost...
140
      
543b9b1c   Yarik   Composer ready
141
142
          public function actionAnalytic()
          {
05a089dd   Alexey Boroda   -Analytics almost...
143
              return $this->render('analytic');
1755c393   Yarik   Basic template in...
144
145
          }
      }