Seo.php
1.5 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
<?php
namespace frontend\widgets;
use common\models\SeoCategory;
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 function init(){
$this->url = $_SERVER['REQUEST_URI'];
parent::init();
}
public function run()
{
$row = '';
$widgetData = $this->findSeoByUrl();
if($widgetData instanceof \common\models\Seo){
$row = $widgetData->{$this->row};
} else{
$widgetData = $this->findSeoByDynamic();
if($widgetData instanceof SeoDynamic){
$row = $widgetData->{$this->row};
}
}
if(!empty($this->own_attr) && empty($row)) {
$row = $this->own_attr;
}
return $this->replaceData($row);
}
protected function replaceData($str)
{
if(!empty($this->fields)){
foreach($this->fields as $field_name => $field_value){
$str = str_replace('{'.$field_name.'}', $field_value, $str);
}
}
return Html::encode($str);
}
protected function findSeoByUrl()
{
return \common\models\Seo::findOne(['url'=>$this->url]);
}
protected function findSeoByDynamic()
{
return SeoDynamic::find()->joinWith('seoCategory')->where(['controller'=> \Yii::$app->controller->id, 'action'=>\Yii::$app->controller->action->id])->one();
}
}