Blame view

frontend/controllers/BlogController.php 9.64 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;
69bdaf70   alex   почистил мусор в ...
15
  
c237629a   Anastasia   first commit
16
17
18
19
20
21
22
      /**
       * Class BlogController
       *
       * @package frontend\controllers
       */
      class BlogController extends Controller
      {
68e72651   Anastasia   - blog
23
          public function actionIndex()
c237629a   Anastasia   first commit
24
          {
c237629a   Anastasia   first commit
25
26
27
28
29
30
31
32
33
34
  
              $categories = Category::find()
                                    ->with(
                      [
                          'language',
                      ]
                  )
                                    ->where(['status' => true])
                                    ->orderBy([ 'sort' => SORT_ASC ])
                                    ->all();
df88ecc9   Anastasia   - index package
35
              $data = ArrayHelper::map($categories, 'language.alias.value', 'language.title');
c237629a   Anastasia   first commit
36
37
38
39
40
41
42
43
44
45
46
47
48
              $dataProvider = new ActiveDataProvider(
                  [
                      'query'      => Article::find()
                                             ->orderBy(
                                                 [
                                                     'created_at' => SORT_DESC,
                                                 ]
                                             )
                                             ->with(
                                                 [
                                                     'categories.language',
                                                 ]
  
68e72651   Anastasia   - blog
49
50
51
                                             )->with(['comments' => function (ActiveQuery $query){
                                                 $query->andWhere(['status' => true]);
                          }])
c237629a   Anastasia   first commit
52
53
                          ->joinWith('language')
                          ->where([ 'blog_article.status' => true ])
c237629a   Anastasia   first commit
54
55
                          ->distinct(),
                      'pagination' => [
68e72651   Anastasia   - blog
56
                          'pageSize' => 6,
69bdaf70   alex   почистил мусор в ...
57
  
c237629a   Anastasia   first commit
58
59
60
                      ],
                  ]
              );
69bdaf70   alex   почистил мусор в ...
61
62
  
  	        return $this->render(
c237629a   Anastasia   first commit
63
64
                  'index',
                  [
68e72651   Anastasia   - blog
65
                      'categories' => $data,
c237629a   Anastasia   first commit
66
                      'dataProvider' => $dataProvider,
69bdaf70   alex   почистил мусор в ...
67
  
c237629a   Anastasia   first commit
68
69
70
71
                  ]
              );
          }
      
68e72651   Anastasia   - blog
72
          public function actionView($id)
c237629a   Anastasia   first commit
73
          {
2d393ae5   alex   добавил микроразм...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  
  	        /**
  	         *
  
  
  
  
  
  
  	         */
  
  
  
  
  
  
68e72651   Anastasia   - blog
90
              $model = $this->findModel($id);
2d393ae5   alex   добавил микроразм...
91
92
93
94
95
96
97
98
99
  
  	        $jsonLdMicrodata = [
  		        'context' => 'http://schema.org',
  		        "type" => "Article",
  		        "mainEntityOfPage" => [
  			        "@type" => "WebPage",
  			        "@id" => "https://google.com/article"
  		        ],
  		        "headline" => "Article headline",
139caeae   alex   поправил микрораз...
100
101
  		        "datePublished" => date('d-m-Y', $model->created_at),
  		        "dateModified" => date('d-m-Y', $model->created_at),
2d393ae5   alex   добавил микроразм...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  
  		        # оставьте пока что этот участок
  		        # author_id уже есть, если что, можно будет быстро его подключить
  //	            "author"=> [
  //	            "@type"=> "Person",
  //	            "name"=> "John Doe"
  //	                    ],
  
  		        "publisher" => [
  			        "@type" => "Organization",
  			        "name" => Yii::t('app', 'ABC short'),
  			        "description" => ($model->language->body_preview) ? $model->language->body_preview : $model->language->title,
  
  		        ]];
  
  	        if ($model->image) $resultMicrodata['image'] = $model->image->getPath();
  
  
  	        $resultMicrodata = new MicrodataFabric();
  	        $resultMicrodata = $resultMicrodata::createJsonFromArticle($jsonLdMicrodata)->toJson();
  	        #die(var_dump(htmlspecialchars($resultMicrodata)));
  
  
68e72651   Anastasia   - blog
125
126
127
128
129
130
131
132
              $model->views +=1;
              $model->save();
              
              $tags = Tag::find()
                         ->with([ 'language' ])
                         ->orderBy([ 'sort' => SORT_ASC ])
                         ->all();
              
c237629a   Anastasia   first commit
133
              return $this->render(
68e72651   Anastasia   - blog
134
135
136
137
                  'view',
                  [
                      'tags'  => $tags,
                      'model' => $model,
2d393ae5   alex   добавил микроразм...
138
  	                'jsMicrodata' => $resultMicrodata
68e72651   Anastasia   - blog
139
                  ]
c237629a   Anastasia   first commit
140
141
142
143
144
              );
          }
      
          public function actionCategory($id)
          {
df88ecc9   Anastasia   - index package
145
146
147
148
149
150
151
152
153
154
              $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
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
196
197
      
              /**
               * @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
198
199
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
200
                      'categories'   => $data
c237629a   Anastasia   first commit
201
202
203
204
205
206
                  ]
              );
          }
      
          public function actionTag($id)
          {
df88ecc9   Anastasia   - index package
207
208
209
210
211
212
213
214
215
216
              $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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
      
              /**
               * @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
258
259
                      'dataProvider' => $dataProvider,
                      'model'        => $model,
df88ecc9   Anastasia   - index package
260
                      'categories' => $data
c237629a   Anastasia   first commit
261
262
263
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
                  ]
              );
          }
      
          /**
           * @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;
              }
          }
      }