Seo.php
3.8 KB
1
2
3
4
5
6
7
8
9
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?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;
public $seo_text;
public $h1;
public $key;
public $project_name;
const SEO_TEXT = 'seo_text';
const DESCRIPTION = 'description';
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:
$filter = \Yii::$app->request->get('filter', []);
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;
}
$title = $this->selectSeoData(self::TITLE);
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
]);
}
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 Html::encode($str);
}
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;
} else {
$widgetData = $this->findSeoByDynamic();
if($widgetData instanceof SeoDynamic){
$result = $widgetData->$param;
}
}
return $this->replaceData($result);
}
}