Blame view

frontend/widgets/Seo.php 12.6 KB
4253cbec   root   first commit
1
2
3
4
  <?php
  namespace frontend\widgets;
  
  use common\models\SeoDynamic;
4d5dbae3   Administrator   big commti
5
  use Yii;
4253cbec   root   first commit
6
  use common\modules\product\models\Brand;
4d5dbae3   Administrator   big commti
7
  use common\modules\rubrication\models\TaxGroup;
4253cbec   root   first commit
8
9
  use common\modules\rubrication\models\TaxOption;
  use yii\base\Widget;
4d5dbae3   Administrator   big commti
10
  use yii\helpers\ArrayHelper;
4253cbec   root   first commit
11
  use yii\helpers\Html;
4d5dbae3   Administrator   big commti
12
13
  use yii\helpers\Url;
  use yii\web\HttpException;
4253cbec   root   first commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  
  class Seo extends Widget
  {
      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;
9612cf03   Administrator   big commti
30
      public static $optionsList;
4253cbec   root   first commit
31
32
33
34
35
36
37
38
39
40
41
42
43
44
      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;
9612cf03   Administrator   big commti
45
46
47
48
          if(empty(self::$optionsList)){
              self::$optionsList = ArrayHelper::getColumn(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(),'alias');
          }
  
4253cbec   root   first commit
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
          parent::init();
  
      }
  
  
      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);
  
                  } else {
  
                      $widgetData = static::findSeoByUrl($this->url);
  
                      $result = '';
  
                      if ($widgetData instanceof \common\models\Seo) {
  
                          $result = $widgetData->{self::SEO_TEXT};
  
                      } else {
  
                          $widgetData = $this->findSeoByDynamic();
  
                          if ($widgetData instanceof SeoDynamic) {
  
                              $result = $widgetData->{self::SEO_TEXT};
  
                          }
  
                      }
  
                      return $this->replaceData($result);
                  }
  
  
                  break;
              case self::H1:
  
                  $filter = \Yii::$app->request->get('filters', []);
  
d16d91c3   Administrator   big commti
106
                  $default = $this->selectSeoData(self::H1);
4253cbec   root   first commit
107
  
d16d91c3   Administrator   big commti
108
                  if ($default != $this->{self::H1}) {
7b81b40b   Administrator   big commti
109
  
d16d91c3   Administrator   big commti
110
                      return $default;
4253cbec   root   first commit
111
  
4253cbec   root   first commit
112
  
0bfe5701   Administrator   big commti
113
114
                  } else if(!empty($filter) && !$this->checkFilter($filter)){
  
21c91e43   Administrator   big commti
115
116
                      $array = $this->arrayBuilder($filter);
                      return $this->getNameString($array);
d16d91c3   Administrator   big commti
117
118
                  }
                  else {
7b81b40b   Administrator   big commti
119
  
d16d91c3   Administrator   big commti
120
                      return $default;
4253cbec   root   first commit
121
122
123
124
125
126
127
                  }
                      break;
              case self::TITLE:
  
                  $filter = \Yii::$app->request->get('filters', []);
  
  
4253cbec   root   first commit
128
  
e337d04a   Administrator   liniya first commit
129
  
b905419d   Administrator   big commti
130
                  $title = $this->selectSeoData(self::TITLE);
4d5dbae3   Administrator   big commti
131
132
  
  
ddf2d8f9   Administrator   big commti
133
                  if(!empty($filter) &&  isset($this->fields['meta-title']) && $title == $this->fields['meta-title'] || !empty($filter) && empty($title)) {
b905419d   Administrator   big commti
134
                      $array = $this->arrayBuilder($filter);
4d5dbae3   Administrator   big commti
135
136
137
138
139
  
                      $title_string = $this->getTitleString($array);
  
                      if($title_string){
                          return $title_string;
4253cbec   root   first commit
140
141
                      }
  
4d5dbae3   Administrator   big commti
142
                  }
4253cbec   root   first commit
143
  
4d5dbae3   Administrator   big commti
144
                  if (!empty($title)) {
4253cbec   root   first commit
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                      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
                      ]);
  
b905419d   Administrator   big commti
161
162
163
                  } else {
  
                      $filter = \Yii::$app->request->get('filters', []);
ddf2d8f9   Administrator   big commti
164
  
b905419d   Administrator   big commti
165
166
                      if(!empty($filter)){
                          $array = $this->arrayBuilder($filter);
ddf2d8f9   Administrator   big commti
167
168
169
170
                          $this->getView()->registerMetaTag([
                              'name' => 'description',
                              'content' => $this->getDescriptionString($array)
                          ]);
b905419d   Administrator   big commti
171
172
                      }
  
4253cbec   root   first commit
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
                  }
  
                  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)) {
  
                      $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
7b81b40b   Administrator   big commti
202
                      || isset($filter)  && $this->checkFilter($filter)
4253cbec   root   first commit
203
204
  
                  ) {
d3d2e248   Administrator   big commti
205
  
4253cbec   root   first commit
206
207
208
209
210
211
                      $this->getView()->registerMetaTag([
                          'name' => 'robots',
                          'content' => 'noindex,nofollow'
                      ]);
  
                  } else if (
d3d2e248   Administrator   big commti
212
                      isset($filter['brands']) && count($filter['brands']) > 1 && isset($filter) && count($filter, COUNT_RECURSIVE) >= 4
4253cbec   root   first commit
213
214
215
                      || isset($filter) && count($filter, COUNT_RECURSIVE) > 4
                      || !empty($sort) ||  !empty($paginate) || isset($filter['prices'])
                  ) {
d3d2e248   Administrator   big commti
216
  
4253cbec   root   first commit
217
218
                      $this->getView()->registerMetaTag([
                          'name' => 'robots',
b905419d   Administrator   big commti
219
                          'content' => 'noindex,nofollow'
4253cbec   root   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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
                      ]);
                  } else {
  
                      $this->getView()->registerMetaTag([
                          'name' => 'robots',
                          'content' => 'index,follow'
                      ]);
                  }
  
  
  
  
                  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);
              }
          }
          $str = str_replace('{project_name}', $this->project_name, $str);
          return $str;
      }
  
      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;
          }
          return self::$check_url;
      }
  
      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();
      }
  
  
4d5dbae3   Administrator   big commti
275
276
277
278
279
      protected function findSeoByDynamicForFilters(){
          return SeoDynamic::find()->joinWith('seoCategory')->where(['param' =>'filters'])->one();
      }
  
  
4253cbec   root   first commit
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
      protected function getViewData()
      {
          $params = $this->getView()->params;
          if (isset($params['seo'])) {
              return $params['seo'];
          } else {
              return [];
          }
      }
  
      protected function selectSeoData($param)
      {
          $result = '';
  
          $widgetData = static::findSeoByUrl($this->url);
  
          if ($widgetData instanceof \common\models\Seo) {
  
              $result = $widgetData->$param;
  
          } else if (!empty($this->$param)) {
  
              $result = $this->$param;
906f0fd5   Administrator   big commti
303
  
4253cbec   root   first commit
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
          } else {
  
              $widgetData = $this->findSeoByDynamic();
  
              if ($widgetData instanceof SeoDynamic) {
  
                  $result = $widgetData->$param;
  
              }
  
          }
  
          return $this->replaceData($result);
  
      }
  
      public function getTitleString($array){
b71e91c1   Administrator   big commti
321
          // "{Название раздела: Название блока фильтра | Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3} - купить в Киеве, Украине - интернет магазин Лінія Світла";
b905419d   Administrator   big commti
322
323
324
325
          $row = '';
          foreach($array as $name => $field){
  
              if($name == 'category' ){
59f37b60   Administrator   big commti
326
                  $row = $field.' | '.$row;
b905419d   Administrator   big commti
327
              } else {
59f37b60   Administrator   big commti
328
                  $row .= $field['name'] .' '.$field['value'].' | '  ;
b905419d   Administrator   big commti
329
330
331
332
333
              }
  
  
  
          }
d16d91c3   Administrator   big commti
334
          $row = substr($row, 0,-2 );
b905419d   Administrator   big commti
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
          $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;
  
      }
  
  
      public function getDescriptionString($array){
b71e91c1   Administrator   big commti
352
          // "Лучшие цены на {Название раздела | Название блока фильтра: Фильтр 1 | Название блока фильтра: Фильтр 2 | Название блока фильтра: Фильтр 3}. Лінія Світла";
b905419d   Administrator   big commti
353
354
          $row = 'Лучшие цены на ';
          foreach($array as $name => $field){
4d5dbae3   Administrator   big commti
355
  
b905419d   Administrator   big commti
356
              if($name == 'category' ){
59f37b60   Administrator   big commti
357
                  $row = $field.' | '.$row;
b905419d   Administrator   big commti
358
              } else {
59f37b60   Administrator   big commti
359
                  $row .= $field['name'] .' '.$field['value'].' | '  ;
4d5dbae3   Administrator   big commti
360
              }
b905419d   Administrator   big commti
361
362
363
  
  
  
4253cbec   root   first commit
364
          }
d16d91c3   Administrator   big commti
365
          $row = substr($row, 0,-2 );
b905419d   Administrator   big commti
366
367
          $row .= ". Лінія Світла";
          return $row;
b905419d   Administrator   big commti
368
369
370
371
  
      }
  
  
21c91e43   Administrator   big commti
372
373
374
375
376
377
378
379
380
381
382
383
384
385
      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'].' | '  ;
              }
  
  
  
          }
d16d91c3   Administrator   big commti
386
          $row = substr($row, 0,-2 );
21c91e43   Administrator   big commti
387
388
389
390
          return $row;
  
      }
  
b905419d   Administrator   big commti
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
      public function arrayBuilder($filter)
      {
  
              $array = [
                  'category' => $this->category_name
              ];
  
  
              if (isset($filter['brands']) && count($filter['brands']) == 1) {
                  $model = Brand::find()->where(['alias' => $filter['brands'][0]])->one();
                  if (!$model instanceof Brand) {
  
                      \Yii::$app->response->redirect(['/site/error'], 404);
                  } else {
                      $array['brand']['name'] = 'Бренд';
                      $array['brand']['value'] = $model->name;
                  }
  
              }
  
  
              $optionsList = ArrayHelper::map(TaxGroup::find()->where(['is_filter' => 'TRUE'])->all(), 'alias', 'name');
  
  
              foreach ($optionsList as $optionList => $name) {
  
  
                  if (isset($filter[$optionList]) && count($filter[$optionList]) == 1) {
  
                      $model = TaxOption::find()->where(['alias' => $filter[$optionList]])->one();
                      if (!$model instanceof TaxOption) {
  
                          \Yii::$app->response->redirect(['site/error'], 404);
                      } else {
                          $array[$optionList]['value'] = $model->value;
                          $array[$optionList]['name'] = $name;
                      }
  
  
                  }
  
  
              }
4d5dbae3   Administrator   big commti
434
  
b905419d   Administrator   big commti
435
          return $array;
4253cbec   root   first commit
436
437
438
  
      }
  
9612cf03   Administrator   big commti
439
440
441
      protected function checkFilter($filter){
          foreach(self::$optionsList as $optionList){
  
0aaf09fd   Administrator   big commti
442
              if(isset($filter[$optionList]) && count($filter[$optionList])  > 1){
9612cf03   Administrator   big commti
443
444
445
446
447
448
449
                  return true;
              }
  
          }
          return false;
      }
  
4253cbec   root   first commit
450
451
  
  }