Blame view

frontend/controllers/BlogController.php 8.79 KB
298bc0f4   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
  <?php
      
      namespace frontend\controllers;
      
      use common\models\blog\Article;
      use common\models\blog\Category;
      use common\models\blog\Tag;
      use yii\data\ActiveDataProvider;
45d40b65   alex   123
9
      use yii\db\ActiveQuery;
298bc0f4   Alexey Boroda   first commit
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
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      
      /**
       * Class BlogController
       *
       * @package frontend\controllers
       */
      class BlogController extends Controller
      {
          public function actionIndex($q = '')
          {
              $tags = Tag::find()
                         ->with(
                             [
                                 'language',
                             ]
                         )
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
  
              $categories = Category::find()
                                    ->with(
                      [
                          'language',
                      ]
                  )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
  
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => Article::find()
6fc26f8f   Anastasia   - slider
44
                                           ->orderBy(
298bc0f4   Alexey Boroda   first commit
45
46
47
48
                                                 [
                                                     'created_at' => SORT_DESC,
                                                 ]
                                             )
6fc26f8f   Anastasia   - slider
49
                                           ->with(
298bc0f4   Alexey Boroda   first commit
50
                                                 [
45d40b65   alex   123
51
52
53
                                                     'categories' => function (ActiveQuery $query){
                                                          $query->with('language')->where(['status' => true]);
                                                     },
298bc0f4   Alexey Boroda   first commit
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                                                 ]
  
                                             )
                          ->joinWith('language')
                          ->where([ 'blog_article.status' => true ])
                          ->andFilterWhere(
                                                 [
                                                     'ilike',
                                                     'blog_article_lang.title',
                                                     $q,
                                                 ]
                          )
                          ->distinct(),
                      'pagination' => [
                          'pageSize' => 3,
                      ],
                  ]
              );
cfc1b89a   alex   123
72
          
298bc0f4   Alexey Boroda   first commit
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
              return $this->render(
                  'index',
                  [
                      'tags'         => $tags,
                      'categories' => $categories,
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
      
          public function actionView($id)
          {
              $model = $this->findModel($id);
      
              $tags = Tag::find()
                         ->with([ 'language' ])
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
      
              return $this->render(
                  'view',
                  [
                      'tags'  => $tags,
                      'model' => $model,
                  ]
              );
          }
      
          public function actionCategory($id)
          {
              $tags = Tag::find()
                         ->with(
                             [
                                 'language',
                             ]
                         )
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
      
              /**
               * @var Category $model
               */
              $model = Category::find()
                               ->where(
                                   [
                                       'id' => $id,
                                   ]
                               )
                               ->with(
                                   [
                                       'articles',
                                   ]
                               )
                               ->where(['status' => true])
                               ->orderBy([ 'sort' => SORT_ASC ])
                               ->one();
45d40b65   alex   123
129
130
131
              if (empty($model)){
                  throw new NotFoundHttpException();
              }
298bc0f4   Alexey Boroda   first commit
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => $model->getArticles()
                                            ->with(
                                                [
                                                    'language',
                                                    'categories.language',
                                                ]
                                            )
                          ->where(['blog_article.status' => true])
                                            ->orderBy(
                                                [
                                                    'created_at' => SORT_DESC,
                                                ]
                                            ),
                      'pagination' => [
                          'pageSize' => 3,
                      ],
                  ]
              );
      
              return $this->render(
                  'category',
                  [
                      'tags'         => $tags,
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
                  ]
              );
          }
      
          public function actionTag($id)
          {
              $tags = Tag::find()
                         ->with(
                             [
                                 'language',
                             ]
                         )
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
      
              /**
               * @var Category $model
               */
              $model = Tag::find()
                          ->where(
                              [
                                  'id' => $id,
                              ]
                          )
                          ->with(
                              [
                                  'articles',
                              ]
                          )
                          ->one();
      
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => $model->getArticles()
                                            ->with(
                                                [
                                                    'language',
298bc0f4   Alexey Boroda   first commit
196
                                                ]
45d40b65   alex   123
197
198
199
200
201
202
203
204
                                            )->with(
                              [
                                  'categories' => function (ActiveQuery $query){
                                      $query->with('language')->where(['status' => true]);
                                  },
                              ]
      
                          )
298bc0f4   Alexey Boroda   first commit
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
                          ->where(['blog_article.status' => true])
                                            ->orderBy(
                                                [
                                                    'created_at' => SORT_DESC,
                                                ]
                                            ),
                      'pagination' => [
                          'pageSize' => 3,
                      ],
                  ]
              );
      
              return $this->render(
                  'tag',
                  [
                      'tags'         => $tags,
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
                  ]
              );
          }
      
          /**
           * @param $id
           *
           * @return Article
           * @throws \yii\web\NotFoundHttpException
           */
          protected function findModel($id)
          {
              /**
               * @var Article | null $model
               */
              $model = Article::find()
6fc26f8f   Anastasia   - slider
239
240
                            ->where([ 'id' => $id ])
                            ->with(
298bc0f4   Alexey Boroda   first commit
241
242
                                  [
                                      'language',
298bc0f4   Alexey Boroda   first commit
243
244
                                      'tags.language',
                                  ]
45d40b65   alex   123
245
246
247
248
249
250
251
252
                              )->with(
                      [
                          'categories' => function (ActiveQuery $query){
                              $query->with('language')->where(['status' => true]);
                          },
                      ]
      
                  )
6fc26f8f   Anastasia   - slider
253
254
                            ->andWhere([ 'status' => true ])
                            ->one();
298bc0f4   Alexey Boroda   first commit
255
256
257
258
259
260
261
262
      
              if (empty($model)) {
                  throw new NotFoundHttpException(\Yii::t('app', 'Article not found'));
              } else {
                  return $model;
              }
          }
      }