Blame view

frontend/controllers/PageController.php 2.38 KB
c0f2e593   Eugeny Galkovskiy   first commit
1
2
  <?php
      namespace frontend\controllers;
4dbca9cd   Alexey Boroda   -Short codes ready
3
      
c0f2e593   Eugeny Galkovskiy   first commit
4
5
      use artbox\core\components\SeoComponent;
      use artbox\core\models\Page;
c0f2e593   Eugeny Galkovskiy   first commit
6
7
8
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      use Yii;
4dbca9cd   Alexey Boroda   -Short codes ready
9
      
c0f2e593   Eugeny Galkovskiy   first commit
10
11
12
13
14
15
16
17
18
19
      /**
       * Class PageController
       *
       * @package frontend\controllers
       */
      class PageController extends Controller
      {
          public function actionView($id)
          {
              $model = $this->findModel($id);
4dbca9cd   Alexey Boroda   -Short codes ready
20
              
c0f2e593   Eugeny Galkovskiy   first commit
21
22
23
24
25
              /**
               * @var SeoComponent $seo
               */
              $seo = Yii::$app->get('seo');
              $seo->setModel($model->lang);
4dbca9cd   Alexey Boroda   -Short codes ready
26
27
28
29
30
31
32
33
34
35
              
              if (strpos($model->lang->body, '[[gallery]]')) {
                  $splited = explode('[[gallery]]', $model->lang->body);
                  $body = array_shift($splited);
                  $lefts = implode('', $splited);
              } else {
                  $body = $model->lang->body;
                  $lefts = null;
              }
              
c0f2e593   Eugeny Galkovskiy   first commit
36
37
38
39
40
41
42
43
44
45
46
47
48
49
              $pages = Page::find()
                           ->with('lang')
                           ->where(
                               [
                                   'not',
                                   [ 'id' => $id ],
                               ]
                           )
                           ->limit(5)
                           ->all();
              
              return $this->render(
                  'view',
                  [
4dbca9cd   Alexey Boroda   -Short codes ready
50
51
                      'model'  => $model,
                      'pages'  => $pages,
bada6a89   Alexey Boroda   -Artbox landing p...
52
                      'images' => $model->getImages(),
4dbca9cd   Alexey Boroda   -Short codes ready
53
54
                      'body'   => $body,
                      'lefts'  => $lefts,
c0f2e593   Eugeny Galkovskiy   first commit
55
56
57
                  ]
              );
          }
4dbca9cd   Alexey Boroda   -Short codes ready
58
          
c0f2e593   Eugeny Galkovskiy   first commit
59
60
61
62
63
64
65
66
67
68
69
70
71
          protected function findModel($id)
          {
              /**
               * @var Page $model
               */
              $model = Page::find()
                           ->where(
                               [
                                   'id' => $id,
                               ]
                           )
                           ->with('lang')
                           ->one();
4dbca9cd   Alexey Boroda   -Short codes ready
72
73
              
              if (!empty($model)) {
c0f2e593   Eugeny Galkovskiy   first commit
74
75
76
77
78
79
80
81
82
                  if ($model->lang->alias_id !== Yii::$app->seo->aliasId) {
                      throw new NotFoundHttpException('Wrong language');
                  }
                  return $model;
              } else {
                  throw new NotFoundHttpException('Model not found');
              }
          }
      }