Blame view

frontend/controllers/BlogController.php 9.67 KB
c237629a   Anastasia   first commit
1
2
3
4
5
6
7
  <?php
      
      namespace frontend\controllers;
      
      use common\models\blog\Article;
      use common\models\blog\Category;
      use common\models\blog\Tag;
642cabb9   alex   микроразметка тест 6
8
      use frontend\helpers\Url;
c237629a   Anastasia   first commit
9
      use yii\data\ActiveDataProvider;
68e72651   Anastasia   - blog
10
11
      use yii\db\ActiveQuery;
      use yii\helpers\ArrayHelper;
c237629a   Anastasia   first commit
12
13
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
2d393ae5   alex   добавил микроразм...
14
15
      use frontend\microdata\MicrodataFabric;
      use Yii;
8fe794e8   alex   правка микроразме...
16
      use common\models\User;
69bdaf70   alex   почистил мусор в ...
17
  
c237629a   Anastasia   first commit
18
19
20
21
22
23
24
      /**
       * Class BlogController
       *
       * @package frontend\controllers
       */
      class BlogController extends Controller
      {
68e72651   Anastasia   - blog
25
          public function actionIndex()
c237629a   Anastasia   first commit
26
          {
c237629a   Anastasia   first commit
27
28
29
30
31
32
33
34
35
36
  
              $categories = Category::find()
                                    ->with(
                      [
                          'language',
                      ]
                  )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
df88ecc9   Anastasia   - index package
37
              $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
c237629a   Anastasia   first commit
38
39
40
41
42
43
44
45
46
47
48
49
50
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => Article::find()
                                             ->orderBy(
                                                 [
                                                     'created_at' => SORT_DESC,
                                                 ]
                                             )
                                             ->with(
                                                 [
                                                     'categories.language',
                                                 ]
  
68e72651   Anastasia   - blog
51
52
53
                                             )->with(['comments' => function (ActiveQuery $query){
                                                 $query->andWhere(['status' => true]);
                          }])
c237629a   Anastasia   first commit
54
55
                          ->joinWith('language')
                          ->where([ 'blog_article.status' => true ])
c237629a   Anastasia   first commit
56
57
                          ->distinct(),
                      'pagination' => [
68e72651   Anastasia   - blog
58
                          'pageSize' => 6,
69bdaf70   alex   почистил мусор в ...
59
  
c237629a   Anastasia   first commit
60
61
62
                      ],
                  ]
              );
69bdaf70   alex   почистил мусор в ...
63
64
  
  	        return $this->render(
c237629a   Anastasia   first commit
65
66
                  'index',
                  [
68e72651   Anastasia   - blog
67
                      'categories' => $data,
c237629a   Anastasia   first commit
68
                      'dataProvider' => $dataProvider,
69bdaf70   alex   почистил мусор в ...
69
  
c237629a   Anastasia   first commit
70
71
72
73
                  ]
              );
          }
      
68e72651   Anastasia   - blog
74
          public function actionView($id)
c237629a   Anastasia   first commit
75
          {
2d393ae5   alex   добавил микроразм...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  
  	        /**
  	         *
  
  
  
  
  
  
  	         */
  
  
  
  
  
  
68e72651   Anastasia   - blog
92
              $model = $this->findModel($id);
2d393ae5   alex   добавил микроразм...
93
94
95
96
97
98
99
100
101
  
  	        $jsonLdMicrodata = [
  		        'context' => 'http://schema.org',
  		        "type" => "Article",
  		        "mainEntityOfPage" => [
  			        "@type" => "WebPage",
  			        "@id" => "https://google.com/article"
  		        ],
  		        "headline" => "Article headline",
139caeae   alex   поправил микрораз...
102
103
  		        "datePublished" => date('d-m-Y', $model->created_at),
  		        "dateModified" => date('d-m-Y', $model->created_at),
642cabb9   alex   микроразметка тест 6
104
105
106
107
  		        'image' => [
  			        #	'https://google.com/logo.jpg'
  			        ($model->image) ? Yii::$app->request->hostInfo . $model->image->getPath() : 'https://google.com/logo.jpg'
  		        ],
8fe794e8   alex   правка микроразме...
108
109
110
111
112
  
  		        "author" => [
  			        "@type" => "Person",
  			        "name" => 'Admin'
  		        ],
2d393ae5   alex   добавил микроразм...
113
114
115
116
117
  
  		        "publisher" => [
  			        "@type" => "Organization",
  			        "name" => Yii::t('app', 'ABC short'),
  			        "description" => ($model->language->body_preview) ? $model->language->body_preview : $model->language->title,
8fe794e8   alex   правка микроразме...
118
119
120
121
122
123
  			        "logo" => [
  				        "@type" => "ImageObject",
  				        "url" => "https://google.com/logo.jpg"
  			        ]
  		        ]
  	        ];
2d393ae5   alex   добавил микроразм...
124
  
2d393ae5   alex   добавил микроразм...
125
126
127
  
  	        $resultMicrodata = new MicrodataFabric();
  	        $resultMicrodata = $resultMicrodata::createJsonFromArticle($jsonLdMicrodata)->toJson();
2d393ae5   alex   добавил микроразм...
128
129
  
  
68e72651   Anastasia   - blog
130
131
132
133
134
135
136
137
              $model->views +=1;
              $model->save();
              
              $tags = Tag::find()
                         ->with([ 'language' ])
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
              
c237629a   Anastasia   first commit
138
              return $this->render(
68e72651   Anastasia   - blog
139
140
141
142
                  'view',
                  [
                      'tags'  => $tags,
                      'model' => $model,
2d393ae5   alex   добавил микроразм...
143
  	                'jsMicrodata' => $resultMicrodata
68e72651   Anastasia   - blog
144
                  ]
c237629a   Anastasia   first commit
145
146
147
148
149
              );
          }
      
          public function actionCategory($id)
          {
df88ecc9   Anastasia   - index package
150
151
152
153
154
155
156
157
158
159
              $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
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
201
202
      
              /**
               * @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
203
204
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
205
                      'categories'   => $data
c237629a   Anastasia   first commit
206
207
208
209
210
211
                  ]
              );
          }
      
          public function actionTag($id)
          {
df88ecc9   Anastasia   - index package
212
213
214
215
216
217
218
219
220
221
              $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
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
261
262
      
              /**
               * @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
263
264
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
265
                      'categories' => $data
c237629a   Anastasia   first commit
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
298
299
                  ]
              );
          }
      
          /**
           * @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;
              }
          }
      }