Blame view

frontend/widgets/Seo.php 5.47 KB
3f2bc3d0   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?php
  namespace frontend\widgets;
  use common\models\SeoDynamic;
  use yii\base\Widget;
  use yii\helpers\Html;
  
  class Seo extends Widget
  {
      private $url;
      public  $row;
      public  $own_attr;
      public  $fields;
      public $description;
      public $title;
0d0c080c   Dmytry Fedorchuk   seo module filter...
15
      public $meta;
3f2bc3d0   Administrator   first commit
16
17
18
19
      public $seo_text;
      public $h1;
      public $key;
      public $project_name;
7d20bafe   Dmytry Fedorchuk   htacess www
20
21
22
23
  
  
  
  
3f2bc3d0   Administrator   first commit
24
25
      const SEO_TEXT = 'seo_text';
      const DESCRIPTION = 'description';
0d0c080c   Dmytry Fedorchuk   seo module filter...
26
      const META = 'meta';
3f2bc3d0   Administrator   first commit
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
      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:
                  return $this->selectSeoData(self::SEO_TEXT);
  
                  break;
              case self::H1:
                  return $this->selectSeoData(self::H1);
                  break;
              case self::TITLE:
  
0d0c080c   Dmytry Fedorchuk   seo module filter...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
                  $filter = \Yii::$app->request->get('filter', []);
                  //        var_dump($filter );die();
  //                if(!empty($filter)){
  //                    $filter_row = '';
  //                    foreach($filter as $sub_filter_name => $sub_filter_array){
  //                        if($sub_filter_name=='options'){
  //                            foreach($sub_filter_array as $f_name=>$f_values){
  //                                $filter_row .= $f_name.':'.implode(',',$f_values).'|';
  //                            }
  //                        }
  //
  //                    }
  //                    $this->fields['name'] = $filter_row;
  //                }
3f2bc3d0   Administrator   first commit
71
72
73
74
  
                  $title = $this->selectSeoData(self::TITLE);
  
  
0d0c080c   Dmytry Fedorchuk   seo module filter...
75
76
77
78
  //                if(!empty($filter_row)){
  //                    return $filter_row;
  //                }
                  if (!empty($title)) {
3f2bc3d0   Administrator   first commit
79
80
81
82
83
84
85
86
87
                      return $title;
                  } else {
                      return $this->project_name;
                  }
  
                  break;
              case self::DESCRIPTION:
                  $description = $this->selectSeoData(self::DESCRIPTION);
  
0d0c080c   Dmytry Fedorchuk   seo module filter...
88
                  if (!empty($description)) {
3f2bc3d0   Administrator   first commit
89
90
91
  
                      $this->getView()->registerMetaTag([
                          'name' => 'description',
0d0c080c   Dmytry Fedorchuk   seo module filter...
92
                          'content' => $description
3f2bc3d0   Administrator   first commit
93
94
95
96
97
                      ]);
  
                  }
  
                  break;
0d0c080c   Dmytry Fedorchuk   seo module filter...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
              case self::META:
                  $meta = $this->selectSeoData(self::META);
  
                  $filter = \Yii::$app->request->get('filter', []);
  
  
  
                   if(!empty($meta)){
  
                      $this->getView()->registerMetaTag([
                          'name' => 'robots',
                          'content' =>  $meta
                      ]);
  
                   } else if (
                      isset($filter['brands']) && count($filter['brands']) > 1
                      || isset($filter['options']["pol"]) && count($filter['options']["pol"]) > 1
                      || isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) > 1
                      || isset($filter['options']["god"]) && count($filter['options']["god"]) > 1
                      || isset($filter['brands']) && count($filter['brands']) <= 1 && isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) >= 4
                      || isset($filter['options']) && count($filter['options'], COUNT_RECURSIVE) > 4
  
                   ){
                       $this->getView()->registerMetaTag([
                           'name' => 'robots',
                           'content' => 'noindex,follow'
                       ]);
  
                   } else if($filter) {
  
                       $this->getView()->registerMetaTag([
                           'name' => 'robots',
                           'content' => 'index,follow'
                       ]);
                   }
  
  
                  break;
3f2bc3d0   Administrator   first commit
136
137
138
139
140
141
142
143
144
145
146
147
148
149
          }
  
  
      }
  
      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);
4495dea0   Administrator   проапдейтил роли
150
          return $str;
3f2bc3d0   Administrator   first commit
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
      }
  
      protected function findSeoByUrl()
      {
          return  \common\models\Seo::findOne(['url'=>$this->url]);
      }
  
      protected function findSeoByDynamic()
      {
          $query = SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id]);
          if(!empty($this->key)){
              $query->andWhere(['key'=>$this->key]);
          }
          return $query->one();
      }
  
  
      protected function getViewData(){
          $params = $this->getView()->params;
          if(isset($params['seo'])){
              return $params['seo'];
          } else {
              return [];
          }
      }
  
      protected function selectSeoData($param){
          $result = '';
  
          $widgetData = $this->findSeoByUrl();
  
          if($widgetData instanceof \common\models\Seo){
  
              $result =  $widgetData->$param;
  
          }else if(!empty($this->$param)){
  
              $result = $this->$param;
3f2bc3d0   Administrator   first commit
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
          } else {
  
              $widgetData = $this->findSeoByDynamic();
  
              if($widgetData instanceof SeoDynamic){
  
                  $result =  $widgetData->$param;
  
              }
  
          }
  
          return $this->replaceData($result);
  
      }
  
  
  }