Blame view

frontend/controllers/BlogController.php 8.25 KB
c237629a   Anastasia   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;
68e72651   Anastasia   - blog
9
10
      use yii\db\ActiveQuery;
      use yii\helpers\ArrayHelper;
c237629a   Anastasia   first commit
11
12
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
69bdaf70   alex   почистил мусор в ...
13
14
  
  
c237629a   Anastasia   first commit
15
16
17
18
19
20
21
      /**
       * Class BlogController
       *
       * @package frontend\controllers
       */
      class BlogController extends Controller
      {
68e72651   Anastasia   - blog
22
          public function actionIndex()
c237629a   Anastasia   first commit
23
          {
c237629a   Anastasia   first commit
24
25
26
27
28
29
30
31
32
33
  
              $categories = Category::find()
                                    ->with(
                      [
                          'language',
                      ]
                  )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
df88ecc9   Anastasia   - index package
34
              $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
c237629a   Anastasia   first commit
35
36
37
38
39
40
41
42
43
44
45
46
47
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => Article::find()
                                             ->orderBy(
                                                 [
                                                     'created_at' => SORT_DESC,
                                                 ]
                                             )
                                             ->with(
                                                 [
                                                     'categories.language',
                                                 ]
  
68e72651   Anastasia   - blog
48
49
50
                                             )->with(['comments' => function (ActiveQuery $query){
                                                 $query->andWhere(['status' => true]);
                          }])
c237629a   Anastasia   first commit
51
52
                          ->joinWith('language')
                          ->where([ 'blog_article.status' => true ])
c237629a   Anastasia   first commit
53
54
                          ->distinct(),
                      'pagination' => [
68e72651   Anastasia   - blog
55
                          'pageSize' => 6,
69bdaf70   alex   почистил мусор в ...
56
  
c237629a   Anastasia   first commit
57
58
59
                      ],
                  ]
              );
69bdaf70   alex   почистил мусор в ...
60
61
  
  	        return $this->render(
c237629a   Anastasia   first commit
62
63
                  'index',
                  [
68e72651   Anastasia   - blog
64
                      'categories' => $data,
c237629a   Anastasia   first commit
65
                      'dataProvider' => $dataProvider,
69bdaf70   alex   почистил мусор в ...
66
  
c237629a   Anastasia   first commit
67
68
69
70
                  ]
              );
          }
      
68e72651   Anastasia   - blog
71
          public function actionView($id)
c237629a   Anastasia   first commit
72
          {
68e72651   Anastasia   - blog
73
74
75
76
77
78
79
80
81
              $model = $this->findModel($id);
              $model->views +=1;
              $model->save();
              
              $tags = Tag::find()
                         ->with([ 'language' ])
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
              
c237629a   Anastasia   first commit
82
              return $this->render(
68e72651   Anastasia   - blog
83
84
85
86
87
                  'view',
                  [
                      'tags'  => $tags,
                      'model' => $model,
                  ]
c237629a   Anastasia   first commit
88
89
90
91
92
              );
          }
      
          public function actionCategory($id)
          {
df88ecc9   Anastasia   - index package
93
94
95
96
97
98
99
100
101
102
              $categories = Category::find()
                                    ->with(
                                        [
                                            'language',
                                        ]
                                    )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
              $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
c237629a   Anastasia   first commit
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      
              /**
               * @var Category $model
               */
              $model = Category::find()
                               ->where(
                                   [
                                       'id' => $id,
                                   ]
                               )
                               ->with(
                                   [
                                       'articles',
                                   ]
                               )
                               ->where(['status' => true])
                               ->orderBy([ 'sort' => SORT_ASC ])
                               ->one();
      
              $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',
                  [
c237629a   Anastasia   first commit
146
147
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
148
                      'categories'   => $data
c237629a   Anastasia   first commit
149
150
151
152
153
154
                  ]
              );
          }
      
          public function actionTag($id)
          {
df88ecc9   Anastasia   - index package
155
156
157
158
159
160
161
162
163
164
              $categories = Category::find()
                                    ->with(
                                        [
                                            'language',
                                        ]
                                    )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
              $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
c237629a   Anastasia   first commit
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
196
197
198
199
200
201
202
203
204
205
      
              /**
               * @var Category $model
               */
              $model = Tag::find()
                          ->where(
                              [
                                  'id' => $id,
                              ]
                          )
                          ->with(
                              [
                                  'articles',
                              ]
                          )
                          ->one();
      
              $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(
                  'tag',
                  [
c237629a   Anastasia   first commit
206
207
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
208
                      'categories' => $data
c237629a   Anastasia   first commit
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
239
240
241
242
                  ]
              );
          }
      
          /**
           * @param $id
           *
           * @return Article
           * @throws \yii\web\NotFoundHttpException
           */
          protected function findModel($id)
          {
              /**
               * @var Article | null $model
               */
              $model = Article::find()
                              ->where([ 'id' => $id ])
                              ->with(
                                  [
                                      'language',
                                      'categories.language',
                                      'tags.language',
                                  ]
                              )
                              ->andWhere([ 'status' => true ])
                              ->one();
      
              if (empty($model)) {
                  throw new NotFoundHttpException(\Yii::t('app', 'Article not found'));
              } else {
                  return $model;
              }
          }
      }