Blame view

frontend/widgets/Seo.php 9.59 KB
4253cbec   root   first commit
1
2
3
4
  <?php
  namespace frontend\widgets;
  
  use common\models\SeoDynamic;
e337d04a   Administrator   liniya first commit
5
  
4253cbec   root   first commit
6
  use common\modules\product\models\Brand;
4253cbec   root   first commit
7
8
  use common\modules\rubrication\models\TaxOption;
  use yii\base\Widget;
4253cbec   root   first commit
9
  use yii\helpers\Html;
4253cbec   root   first commit
10
11
12
13
14
15
16
17
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
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
  
  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;
      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;
          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', []);
  
  
  
                  if (isset($filter['brands']) && count($filter['brands']) == 1) {
  
                      $model = Brand::find()->where(['alias' => $filter['brands'][0]])->one();
4253cbec   root   first commit
102
  
e337d04a   Administrator   liniya first commit
103
                      if($this->selectSeoData(self::H1) == $this->category_name) {
4253cbec   root   first commit
104
  
e337d04a   Administrator   liniya first commit
105
106
                          return $this->selectSeoData(self::H1) . ' ' . $model->name;
                      }else {
4253cbec   root   first commit
107
  
e337d04a   Administrator   liniya first commit
108
                          return $this->selectSeoData(self::H1);
4253cbec   root   first commit
109
  
4253cbec   root   first commit
110
111
                      }
  
4253cbec   root   first commit
112
113
114
                  } else  if (isset($filter["naznacenie"]) && count($filter["naznacenie"]) == 1) {
  
                  $model = TaxOption::find()->where(['alias' => $filter["naznacenie"]])->one();
e337d04a   Administrator   liniya first commit
115
                      if($this->selectSeoData(self::H1) == $this->category_name) {
4253cbec   root   first commit
116
  
e337d04a   Administrator   liniya first commit
117
118
                          return $this->selectSeoData(self::H1) . ' ' . $model->value->value;
                      }else {
4253cbec   root   first commit
119
  
e337d04a   Administrator   liniya first commit
120
                          return $this->selectSeoData(self::H1);
4253cbec   root   first commit
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  
                      }
  
  
  
                  } else {
                      return $this->selectSeoData(self::H1);
                  }
                      break;
              case self::TITLE:
  
                  $filter = \Yii::$app->request->get('filters', []);
  
  
                  $title = $this->selectSeoData(self::TITLE);
  
  
                  if(!empty($filter) && isset($this->fields['meta-title']) && $title == $this->fields['meta-title']) {
  
                      $array = [
                          'category' => $this->category_name
                      ];
  
  
                      if (isset($filter['brands']) && count($filter['brands']) == 1) {
                          $model = Brand::find()->where(['alias' => $filter['brands'][0]])->one();
4253cbec   root   first commit
147
  
e337d04a   Administrator   liniya first commit
148
                          $array['brand'] = $model->name;
4253cbec   root   first commit
149
150
                      }
  
e337d04a   Administrator   liniya first commit
151
                      if (isset($filter["pol"]) && count($filter["pol"]) == 1) {
4253cbec   root   first commit
152
  
e337d04a   Administrator   liniya first commit
153
154
                          $model = TaxOption::find()->where(['alias' => $filter["pol"]])->one();
                          $array['sex'] = $model->value->value;
4253cbec   root   first commit
155
  
4253cbec   root   first commit
156
  
e337d04a   Administrator   liniya first commit
157
                      }
4253cbec   root   first commit
158
  
e337d04a   Administrator   liniya first commit
159
                      if (isset($filter["naznacenie"]) && count($filter["naznacenie"]) == 1) {
4253cbec   root   first commit
160
  
e337d04a   Administrator   liniya first commit
161
162
                          $model = TaxOption::find()->where(['alias' => $filter["naznacenie"]])->one();
                          $array['naz'] = $model->value->value;
4253cbec   root   first commit
163
164
165
  
                      }
  
e337d04a   Administrator   liniya first commit
166
167
168
169
                      if (isset($filter["god"]) && count($filter["god"]) == 1) {
  
                          $model = TaxOption::find()->where(['alias' => $filter["god"]])->one();
                          $array['year'] = $model->value->value;
4253cbec   root   first commit
170
  
4253cbec   root   first commit
171
172
                      }
  
e337d04a   Administrator   liniya first commit
173
                      return $this->getTitleString($array);
4253cbec   root   first commit
174
  
e337d04a   Administrator   liniya first commit
175
                  } else if (!empty($title)) {
4253cbec   root   first commit
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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
                      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
                      ]);
  
                  }
  
                  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
                      || isset($filter["pol"]) && count($filter["pol"]) > 1
                      || isset($filter["naznacenie"]) && count($filter["naznacenie"]) > 1
                      || isset($filter["god"]) && count($filter["god"]) > 1
  
                  ) {
                      $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) ||  !empty($paginate) || isset($filter['prices'])
                  ) {
                      $this->getView()->registerMetaTag([
                          'name' => 'robots',
                          'content' => 'noindex,follow'
                      ]);
                  } 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();
      }
  
  
4253cbec   root   first commit
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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 [];
          }
      }
  
      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;
          } else {
  
              $widgetData = $this->findSeoByDynamic();
  
              if ($widgetData instanceof SeoDynamic) {
  
                  $result = $widgetData->$param;
  
              }
  
          }
  
          return $this->replaceData($result);
  
      }
  
      public function getTitleString($array){
e337d04a   Administrator   liniya first commit
334
335
336
          $template = "{category} {naz} {brand}  {sex} {year} купить в Украине, Киев, Харькове - цены, фото, отзывы | Rukzachok.com.ua";
          foreach ($array as $field_name => $field_value) {
              $template = str_replace('{' . $field_name . '}', mb_strtolower($field_value), $template);
4253cbec   root   first commit
337
          }
e337d04a   Administrator   liniya first commit
338
339
          $template =  preg_replace('/\{.[^\}]*\}\s/','',$template);
          return $template;
4253cbec   root   first commit
340
341
342
343
344
  
      }
  
  
  }