Blame view

frontend/widgets/Seo.php 18.5 KB
4253cbec   root   first commit
1
  <?php
21f69fb1   Alexey Boroda   -Pagination noind...
2
3
4
5
6
7
8
9
10
11
12
      namespace frontend\widgets;
      
      use common\models\SeoDynamic;
      use Yii;
      use common\modules\product\models\Brand;
      use common\modules\rubrication\models\TaxGroup;
      use common\modules\rubrication\models\TaxOption;
      use yii\base\Widget;
      use yii\helpers\ArrayHelper;
      use yii\helpers\Html;
      use yii\helpers\Url;
c1b2b9b3   Alexey Boroda   -Two or more filt...
13
      use yii\helpers\VarDumper;
21f69fb1   Alexey Boroda   -Pagination noind...
14
15
16
      use yii\web\HttpException;
      
      class Seo extends Widget
4253cbec   root   first commit
17
      {
21f69fb1   Alexey Boroda   -Pagination noind...
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
44
45
46
47
48
49
50
51
52
53
54
55
          private $url;
          public $row;
          public $own_attr;
          public $fields;
          public $category_name;
          public $description;
          public $title;
          public $meta;
          public $seo_text;
          public $h1;
          public $key;
          public $name;
          public $project_name;
          public static $optionsList;
          protected static $check_url;
          protected static $check_url_bool;
          
          const SEO_TEXT = 'seo_text';
          const DESCRIPTION = 'description';
          const META = 'meta';
          const H1 = 'h1';
          const TITLE = 'title';
          
          public function init()
          {
              $this->url = \Yii::$app->request->url;
              $this->project_name = \Yii::$app->name;
              if (empty(self::$optionsList)) {
                  self::$optionsList = ArrayHelper::getColumn(
                      TaxGroup::find()
                              ->where([ 'is_filter' => 'TRUE' ])
                              ->all(),
                      'alias'
                  );
              }
              
              parent::init();
              
4253cbec   root   first commit
56
          }
21f69fb1   Alexey Boroda   -Pagination noind...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
          
          public function run()
          {
              
              $seoData = $this->getViewData();
              foreach ($seoData as $key => $value) {
                  $this->$key = $value;
              }
              
              switch ($this->row) {
                  case self::SEO_TEXT:
                      
                      $filter = \Yii::$app->request->get('filters', []);
                      $sort = \Yii::$app->request->get('sort', []);
                      $paginate = \Yii::$app->request->get('page', []);
                      
                      if (empty($filter) && empty($sort) && empty($paginate)) {
                          
                          return $this->selectSeoData(self::SEO_TEXT);
                          
4253cbec   root   first commit
77
                      } else {
21f69fb1   Alexey Boroda   -Pagination noind...
78
79
80
81
82
83
84
                          
                          $widgetData = static::findSeoByUrl($this->url);
                          
                          $result = '';
                          
                          if ($widgetData instanceof \common\models\Seo) {
                              
4253cbec   root   first commit
85
                              $result = $widgetData->{self::SEO_TEXT};
21f69fb1   Alexey Boroda   -Pagination noind...
86
87
88
89
90
91
92
93
94
95
96
                              
                          } else {
                              
                              $widgetData = $this->findSeoByDynamic();
                              
                              if ($widgetData instanceof SeoDynamic) {
                                  
                                  $result = $widgetData->{self::SEO_TEXT};
                                  
                              }
                              
4253cbec   root   first commit
97
                          }
21f69fb1   Alexey Boroda   -Pagination noind...
98
99
                          
                          return $this->replaceData($result);
4253cbec   root   first commit
100
                      }
21f69fb1   Alexey Boroda   -Pagination noind...
101
                      
4253cbec   root   first commit
102
                      break;
21f69fb1   Alexey Boroda   -Pagination noind...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
                  case self::H1:
                      
                      $filter = \Yii::$app->request->get('filters', []);
                      
                      $default = $this->selectSeoData(self::H1);
                      
                      if ($default != $this->{self::H1}) {
                          
                          return $default;
                          
                      } else if (!empty($filter) && !$this->checkFilter($filter)) {
                          
                          $array = $this->arrayBuilder($filter);
                          return $this->getNameString($array);
                      } else {
                          
                          return $default;
4253cbec   root   first commit
120
                      }
21f69fb1   Alexey Boroda   -Pagination noind...
121
122
123
                      break;
                  case self::TITLE:
                      
b905419d   Administrator   big commti
124
                      $filter = \Yii::$app->request->get('filters', []);
21f69fb1   Alexey Boroda   -Pagination noind...
125
126
127
128
129
                      
                      $title = $this->selectSeoData(self::TITLE);
                      
                      if (!empty($filter) && $title == $this->title || !empty($filter) && empty($title)) {
                          
b905419d   Administrator   big commti
130
                          $array = $this->arrayBuilder($filter);
21f69fb1   Alexey Boroda   -Pagination noind...
131
132
133
134
135
136
137
                          
                          $title_string = $this->getTitleString($array);
                          
                          if ($title_string) {
                              return $title_string;
                          }
                          
b905419d   Administrator   big commti
138
                      }
21f69fb1   Alexey Boroda   -Pagination noind...
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
196
197
198
199
200
201
202
203
204
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
                      
                      if (!empty($title)) {
                          
                          return $title;
                      } else {
                          return $this->project_name;
                      }
                      
                      break;
                  case self::DESCRIPTION:
                      $description = $this->selectSeoData(self::DESCRIPTION);
                      
                      if (!empty($description)) {
                          
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'description',
                                       'content' => $description,
                                   ]
                               );
                          
                      } else {
                          
                          $filter = \Yii::$app->request->get('filters', []);
                          
                          if (!empty($filter)) {
                              $array = $this->arrayBuilder($filter);
                              $this->getView()
                                   ->registerMetaTag(
                                       [
                                           'name'    => 'description',
                                           'content' => $this->getDescriptionString($array),
                                       ]
                                   );
                          }
                          
                      }
                      
                      break;
                  case self::META:
                      
                      $meta = $this->selectSeoData(self::META);
                      
                      $filter = \Yii::$app->request->get('filters', []);
                      $sort = \Yii::$app->request->get('sort', []);
                      $paginate = \Yii::$app->request->get('page', []);
                      
                      if (!empty($meta) && empty($sort) && empty($paginate) && !isset($filter[ 'prices' ])) {
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => $meta,
                                   ]
                               );
                          
                      } else if (!empty($filter[ 'special' ])) {
      
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => 'noindex,follow',
                                   ]
                               );
                          
                      } else if (isset($filter[ 'brands' ]) && count(
                              $filter[ 'brands' ]
                          ) > 1 || isset($filter) && $this->checkFilter($filter)
                      
                      ) {
      
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => 'noindex,nofollow',
                                   ]
                               );
                          
                      } else if (isset($filter[ 'brands' ]) && count($filter[ 'brands' ]) > 1 && isset($filter) && count(
                              $filter,
                              COUNT_RECURSIVE
                          ) >= 4 || isset($filter) && count(
                              $filter,
                              COUNT_RECURSIVE
                          ) > 4 || !empty($sort) || isset($filter[ 'prices' ])
                      ) {
      
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => 'noindex,nofollow',
                                   ]
                               );
c1b2b9b3   Alexey Boroda   -Two or more filt...
236
237
238
239
240
241
242
243
244
                      } else if (isset($filter) && count($filter, COUNT_RECURSIVE) >= 4) {
                          
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => 'noindex,nofollow',
                                   ]
                               );
21f69fb1   Alexey Boroda   -Pagination noind...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
                      } else if (!empty($paginate)) {
      
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => 'noindex,follow',
                                   ]
                               );
                      } else {
                          $this->getView()
                               ->registerMetaTag(
                                   [
                                       'name'    => 'robots',
                                       'content' => 'index,follow',
                                   ]
                               );
                      }
21f69fb1   Alexey Boroda   -Pagination noind...
263
264
265
266
267
268
269
270
271
272
273
                      break;
              }
              
          }
          
          protected function replaceData($str)
          {
              
              if (!empty($this->fields)) {
                  foreach ($this->fields as $field_name => $field_value) {
                      $str = str_replace('{' . $field_name . '}', $field_value, $str);
4253cbec   root   first commit
274
                  }
21f69fb1   Alexey Boroda   -Pagination noind...
275
276
277
              }
              $str = str_replace('{project_name}', $this->project_name, $str);
              return $str;
4253cbec   root   first commit
278
          }
21f69fb1   Alexey Boroda   -Pagination noind...
279
280
281
282
283
284
          
          protected static function findSeoByUrl($url)
          {
              if (empty(self::$check_url_bool)) {
                  self::$check_url = \common\models\Seo::findOne([ 'url' => $url ]);
                  self::$check_url_bool = true;
4253cbec   root   first commit
285
              }
21f69fb1   Alexey Boroda   -Pagination noind...
286
              return self::$check_url;
4253cbec   root   first commit
287
          }
21f69fb1   Alexey Boroda   -Pagination noind...
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
          
          protected function findSeoByDynamic()
          {
              
              if (!empty($this->key)) {
                  
                  $query = SeoDynamic::find()
                                     ->joinWith('seoCategory')
                                     ->where(
                                         [
                                             'controller' => \Yii::$app->controller->id,
                                             'action'     => \Yii::$app->controller->action->id,
                                             'key'        => $this->key,
                                         ]
                                     );
              } else {
                  
                  $query = SeoDynamic::find()
                                     ->joinWith('seoCategory')
                                     ->where(
                                         [
                                             'controller' => \Yii::$app->controller->id,
                                             'action'     => \Yii::$app->controller->action->id,
                                         ]
                                     );
              }
              
              return $query->one();
4253cbec   root   first commit
316
          }
21f69fb1   Alexey Boroda   -Pagination noind...
317
318
319
320
321
322
323
          
          protected function findSeoByDynamicForFilters()
          {
              return SeoDynamic::find()
                               ->joinWith('seoCategory')
                               ->where([ 'param' => 'filters' ])
                               ->one();
4253cbec   root   first commit
324
          }
21f69fb1   Alexey Boroda   -Pagination noind...
325
326
327
328
329
330
331
332
333
          
          protected function getViewData()
          {
              $params = $this->getView()->params;
              if (isset($params[ 'seo' ])) {
                  return $params[ 'seo' ];
              } else {
                  return [];
              }
4253cbec   root   first commit
334
          }
24a7e405   Alexey Boroda   -Seo widget fix a...
335
          
21f69fb1   Alexey Boroda   -Pagination noind...
336
337
          protected function selectSeoData($param)
          {
24a7e405   Alexey Boroda   -Seo widget fix a...
338
              
24a7e405   Alexey Boroda   -Seo widget fix a...
339
              $result = '';
21f69fb1   Alexey Boroda   -Pagination noind...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
              
              $widgetData = static::findSeoByUrl($this->url);
              
              $seoDynamicData = $this->findSeoByDynamic();
              
              if ($widgetData instanceof \common\models\Seo) {
                  
                  $result = $widgetData->$param;
                  
              } else if ($seoDynamicData instanceof SeoDynamic) {
                  
                  $result = $seoDynamicData->$param;
                  
              } else if (!empty($this->$param)) {
                  
                  $result = $this->$param;
                  
b905419d   Administrator   big commti
357
              } else {
21f69fb1   Alexey Boroda   -Pagination noind...
358
                  $result = '';
b905419d   Administrator   big commti
359
              }
21f69fb1   Alexey Boroda   -Pagination noind...
360
361
362
              
              return $this->replaceData($result);
              
b905419d   Administrator   big commti
363
          }
21f69fb1   Alexey Boroda   -Pagination noind...
364
365
366
367
368
369
370
371
372
373
374
375
376
          
          public function getTitleString($array)
          {
              // "{Название раздела: Название блока фильтра | Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3} - купить в Киеве, Украине - интернет магазин Лінія Світла";
              $row = '';
              foreach ($array as $name => $field) {
                  
                  if ($name == 'category') {
                      $row = $field . ' | ' . $row;
                  } else {
                      $row .= $field[ 'name' ] . ' ' . $field[ 'value' ] . ' | ';
                  }
                  
4d5dbae3   Administrator   big commti
377
              }
21f69fb1   Alexey Boroda   -Pagination noind...
378
379
380
381
382
383
384
385
386
387
388
389
390
391
              $row = substr($row, 0, -2);
              $row .= " - купить в Киеве, Украине - интернет магазин Лінія Світла";
              return $row;
              //        $template = SeoDynamic::find()->select('title')->where(['param' =>'filters'])->one();
              //        if($template instanceof SeoDynamic){
              //            foreach ($array as $field_name => $field_value) {
              //                $template->title = str_replace('{' . $field_name . '}', mb_strtolower($field_value), $template->title);
              //            }
              //            $template =  preg_replace('/\{.[^\}]*\}\s/','',$template->title);
              //            return $template;
              //        }
              //
              //        return false;
              
4253cbec   root   first commit
392
          }
21f69fb1   Alexey Boroda   -Pagination noind...
393
394
395
396
397
398
399
400
401
402
403
404
405
          
          public function getDescriptionString($array)
          {
              // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла";
              $row = 'Лучшие цены на ';
              foreach ($array as $name => $field) {
                  
                  if ($name == 'category') {
                      $row = $field . ' | ' . $row;
                  } else {
                      $row .= $field[ 'name' ] . ' ' . $field[ 'value' ] . ' | ';
                  }
                  
21c91e43   Administrator   big commti
406
              }
21f69fb1   Alexey Boroda   -Pagination noind...
407
408
409
410
              $row = substr($row, 0, -2);
              $row .= ". Лінія Світла";
              return $row;
              
21c91e43   Administrator   big commti
411
          }
21f69fb1   Alexey Boroda   -Pagination noind...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
          
          public function getNameString($array)
          {
              // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла";
              $row = '';
              foreach ($array as $name => $field) {
                  
                  if ($name == 'category') {
                      $row = $field . ' | ' . $row;
                  } else {
                      $row .= $field[ 'name' ] . ' ' . $field[ 'value' ] . ' | ';
                  }
                  
              }
              $row = substr($row, 0, -2);
              return $row;
              
          }
          
          public function arrayBuilder($filter)
          {
              
b905419d   Administrator   big commti
434
              $array = [
21f69fb1   Alexey Boroda   -Pagination noind...
435
                  'category' => $this->category_name,
b905419d   Administrator   big commti
436
              ];
21f69fb1   Alexey Boroda   -Pagination noind...
437
438
439
440
441
              
              if (isset($filter[ 'brands' ]) && count($filter[ 'brands' ]) == 1) {
                  $model = Brand::find()
                                ->where([ 'alias' => $filter[ 'brands' ][ 0 ] ])
                                ->one();
b905419d   Administrator   big commti
442
                  if (!$model instanceof Brand) {
21f69fb1   Alexey Boroda   -Pagination noind...
443
444
                      
                      \Yii::$app->response->redirect([ '/site/error' ], 404);
b905419d   Administrator   big commti
445
                  } else {
21f69fb1   Alexey Boroda   -Pagination noind...
446
447
                      $array[ 'brand' ][ 'name' ] = 'Бренд';
                      $array[ 'brand' ][ 'value' ] = $model->name;
b905419d   Administrator   big commti
448
                  }
21f69fb1   Alexey Boroda   -Pagination noind...
449
                  
b905419d   Administrator   big commti
450
              }
21f69fb1   Alexey Boroda   -Pagination noind...
451
452
453
454
455
456
457
458
459
              
              $optionsList = ArrayHelper::map(
                  TaxGroup::find()
                          ->where([ 'is_filter' => 'TRUE' ])
                          ->all(),
                  'alias',
                  'name'
              );
              
b905419d   Administrator   big commti
460
              foreach ($optionsList as $optionList => $name) {
21f69fb1   Alexey Boroda   -Pagination noind...
461
462
463
464
465
466
                  
                  if (isset($filter[ $optionList ]) && count($filter[ $optionList ]) == 1) {
                      
                      $model = TaxOption::find()
                                        ->where([ 'alias' => $filter[ $optionList ] ])
                                        ->one();
b905419d   Administrator   big commti
467
                      if (!$model instanceof TaxOption) {
21f69fb1   Alexey Boroda   -Pagination noind...
468
469
                          
                          \Yii::$app->response->redirect([ 'site/error' ], 404);
b905419d   Administrator   big commti
470
                      } else {
21f69fb1   Alexey Boroda   -Pagination noind...
471
472
                          $array[ $optionList ][ 'value' ] = $model->value;
                          $array[ $optionList ][ 'name' ] = $name;
b905419d   Administrator   big commti
473
                      }
21f69fb1   Alexey Boroda   -Pagination noind...
474
                      
b905419d   Administrator   big commti
475
                  }
21f69fb1   Alexey Boroda   -Pagination noind...
476
                  
b905419d   Administrator   big commti
477
              }
21f69fb1   Alexey Boroda   -Pagination noind...
478
479
480
481
482
483
484
485
486
487
488
489
490
              
              return $array;
              
          }
          
          protected function checkFilter($filter)
          {
              foreach (self::$optionsList as $optionList) {
                  
                  if (isset($filter[ $optionList ]) && count($filter[ $optionList ]) > 1) {
                      return true;
                  }
                  
9612cf03   Administrator   big commti
491
              }
21f69fb1   Alexey Boroda   -Pagination noind...
492
              return false;
9612cf03   Administrator   big commti
493
          }
21f69fb1   Alexey Boroda   -Pagination noind...
494
495
          
      }