Blame view

frontend/controllers/BlogController.php 9.53 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;
2d393ae5   alex   добавил микроразм...
13
14
      use frontend\microdata\MicrodataFabric;
      use Yii;
8fe794e8   alex   правка микроразме...
15
      use common\models\User;
69bdaf70   alex   почистил мусор в ...
16
  
c237629a   Anastasia   first commit
17
18
19
20
21
22
23
      /**
       * Class BlogController
       *
       * @package frontend\controllers
       */
      class BlogController extends Controller
      {
68e72651   Anastasia   - blog
24
          public function actionIndex()
c237629a   Anastasia   first commit
25
          {
c237629a   Anastasia   first commit
26
27
28
29
30
31
32
33
34
35
  
              $categories = Category::find()
                                    ->with(
                      [
                          'language',
                      ]
                  )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
df88ecc9   Anastasia   - index package
36
              $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
c237629a   Anastasia   first commit
37
38
39
40
41
42
43
44
45
46
47
48
49
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => Article::find()
                                             ->orderBy(
                                                 [
                                                     'created_at' => SORT_DESC,
                                                 ]
                                             )
                                             ->with(
                                                 [
                                                     'categories.language',
                                                 ]
  
68e72651   Anastasia   - blog
50
51
52
                                             )->with(['comments' => function (ActiveQuery $query){
                                                 $query->andWhere(['status' => true]);
                          }])
c237629a   Anastasia   first commit
53
54
                          ->joinWith('language')
                          ->where([ 'blog_article.status' => true ])
c237629a   Anastasia   first commit
55
56
                          ->distinct(),
                      'pagination' => [
68e72651   Anastasia   - blog
57
                          'pageSize' => 6,
69bdaf70   alex   почистил мусор в ...
58
  
c237629a   Anastasia   first commit
59
60
61
                      ],
                  ]
              );
69bdaf70   alex   почистил мусор в ...
62
63
  
  	        return $this->render(
c237629a   Anastasia   first commit
64
65
                  'index',
                  [
68e72651   Anastasia   - blog
66
                      'categories' => $data,
c237629a   Anastasia   first commit
67
                      'dataProvider' => $dataProvider,
69bdaf70   alex   почистил мусор в ...
68
  
c237629a   Anastasia   first commit
69
70
71
72
                  ]
              );
          }
      
68e72651   Anastasia   - blog
73
          public function actionView($id)
c237629a   Anastasia   first commit
74
          {
2d393ae5   alex   добавил микроразм...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  
  	        /**
  	         *
  
  
  
  
  
  
  	         */
  
  
  
  
  
  
68e72651   Anastasia   - blog
91
              $model = $this->findModel($id);
2d393ae5   alex   добавил микроразм...
92
93
94
95
96
97
98
99
100
  
  	        $jsonLdMicrodata = [
  		        'context' => 'http://schema.org',
  		        "type" => "Article",
  		        "mainEntityOfPage" => [
  			        "@type" => "WebPage",
  			        "@id" => "https://google.com/article"
  		        ],
  		        "headline" => "Article headline",
139caeae   alex   поправил микрораз...
101
102
  		        "datePublished" => date('d-m-Y', $model->created_at),
  		        "dateModified" => date('d-m-Y', $model->created_at),
2d393ae5   alex   добавил микроразм...
103
  
8fe794e8   alex   правка микроразме...
104
105
106
107
108
  
  		        "author" => [
  			        "@type" => "Person",
  			        "name" => 'Admin'
  		        ],
2d393ae5   alex   добавил микроразм...
109
110
111
112
113
  
  		        "publisher" => [
  			        "@type" => "Organization",
  			        "name" => Yii::t('app', 'ABC short'),
  			        "description" => ($model->language->body_preview) ? $model->language->body_preview : $model->language->title,
8fe794e8   alex   правка микроразме...
114
115
116
117
118
119
  			        "logo" => [
  				        "@type" => "ImageObject",
  				        "url" => "https://google.com/logo.jpg"
  			        ]
  		        ]
  	        ];
2d393ae5   alex   добавил микроразм...
120
121
122
123
124
125
  
  	        if ($model->image) $resultMicrodata['image'] = $model->image->getPath();
  
  
  	        $resultMicrodata = new MicrodataFabric();
  	        $resultMicrodata = $resultMicrodata::createJsonFromArticle($jsonLdMicrodata)->toJson();
2d393ae5   alex   добавил микроразм...
126
127
  
  
68e72651   Anastasia   - blog
128
129
130
131
132
133
134
135
              $model->views +=1;
              $model->save();
              
              $tags = Tag::find()
                         ->with([ 'language' ])
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
              
c237629a   Anastasia   first commit
136
              return $this->render(
68e72651   Anastasia   - blog
137
138
139
140
                  'view',
                  [
                      'tags'  => $tags,
                      'model' => $model,
2d393ae5   alex   добавил микроразм...
141
  	                'jsMicrodata' => $resultMicrodata
68e72651   Anastasia   - blog
142
                  ]
c237629a   Anastasia   first commit
143
144
145
146
147
              );
          }
      
          public function actionCategory($id)
          {
df88ecc9   Anastasia   - index package
148
149
150
151
152
153
154
155
156
157
              $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
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
196
197
198
199
200
      
              /**
               * @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
201
202
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
203
                      'categories'   => $data
c237629a   Anastasia   first commit
204
205
206
207
208
209
                  ]
              );
          }
      
          public function actionTag($id)
          {
df88ecc9   Anastasia   - index package
210
211
212
213
214
215
216
217
218
219
              $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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
      
              /**
               * @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
261
262
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
263
                      'categories' => $data
c237629a   Anastasia   first commit
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
                  ]
              );
          }
      
          /**
           * @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;
              }
          }
      }