1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The SEO support behavior for ActiveRecords in Yii 2 framework |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/inblank/yii2-seobility |
6
|
|
|
* @copyright Copyright (c) 2016 Pavel Aleksandrov <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace inblank\seobility; |
11
|
|
|
|
12
|
|
|
use inblank\seobility\models\Seo; |
13
|
|
|
use yii\base\Behavior; |
14
|
|
|
use yii\db\ActiveRecord; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The SEO support behavior for ActiveRecords in Yii 2 framework |
19
|
|
|
* |
20
|
|
|
* @property ActiveRecord $owner |
21
|
|
|
* |
22
|
|
|
* @author Pavel Aleksandrov <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class SeobilityBehavior extends Behavior |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Seo[] seo data cache for owner |
28
|
|
|
*/ |
29
|
|
|
public $_seo = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
1 |
|
public function events() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
1 |
|
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave', |
38
|
1 |
|
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', |
39
|
1 |
|
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
40
|
1 |
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get all SEO data for model |
45
|
|
|
* @param bool $force if `true`, force get with overwrite current SEO data |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
1 |
|
public function getAllSeobility($force = false) |
49
|
|
|
{ |
50
|
1 |
|
$this->_seo = (array)($force ? [] : $this->_seo) + Seo::findAll($this->owner); |
51
|
1 |
|
return ArrayHelper::toArray($this->_seo, ['inblank\seobility\models\Seo' => ['title', 'keywords', 'description']]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set SEO data |
56
|
|
|
* @param string[] $values setting values |
57
|
|
|
* @param int $condition SEO data condition for select. Condition =0 for default SEO data |
58
|
|
|
*/ |
59
|
1 |
|
public function setSeobility($values, $condition = 0) |
60
|
|
|
{ |
61
|
1 |
|
$condition = (int)$condition; |
62
|
1 |
|
if (!array_key_exists($condition, $this->_seo)) { |
63
|
1 |
|
$this->_seo[$condition] = Seo::find($this->owner, $condition); |
64
|
1 |
|
} |
65
|
1 |
|
$this->_seo[$condition]->setAttributes($values); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* After save event |
70
|
|
|
*/ |
71
|
1 |
|
public function afterSave() |
72
|
|
|
{ |
73
|
1 |
|
foreach ($this->_seo as $i => $seo) { |
74
|
1 |
|
$seo->save(); |
75
|
1 |
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* After delete event |
80
|
|
|
*/ |
81
|
1 |
|
public function afterDelete() |
82
|
|
|
{ |
83
|
1 |
|
Seo::deleteAll($this->owner); |
84
|
1 |
|
$this->_seo = []; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Delete SEO data |
89
|
|
|
* @param int $condition delete SEO data condition |
90
|
|
|
*/ |
91
|
1 |
|
public function deleteSeobility($condition = 0) |
92
|
|
|
{ |
93
|
1 |
|
$condition = (int)$condition; |
94
|
1 |
|
$this->getSeobility($condition, false); |
95
|
1 |
|
$this->_seo[$condition]->delete(); |
96
|
1 |
|
unset($this->_seo[$condition]); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get SEO data |
101
|
|
|
* @param int $condition SEO data condition for get. Default SEO data have condition=0 |
102
|
|
|
* @param bool $defaultIfNotFound flag that get default SEO data if not found by condition |
103
|
|
|
* @param int $defaultCondition default SEO data condition for get. |
104
|
|
|
* If can't get SEO data with this condition will be return empty SEO data |
105
|
|
|
* @return \string[] |
106
|
|
|
*/ |
107
|
1 |
|
public function getSeobility($condition = 0, $defaultIfNotFound = true, $defaultCondition = 0) |
108
|
|
|
{ |
109
|
1 |
|
$condition = (int)$condition; |
110
|
1 |
|
if (!array_key_exists($condition, $this->_seo)) { |
111
|
1 |
|
$this->_seo[$condition] = Seo::find($this->owner, $condition); |
112
|
1 |
|
if ($this->_seo[$condition]->getIsNewRecord() && $defaultIfNotFound) { |
113
|
1 |
|
$this->getSeobility((int)$defaultCondition, false); |
114
|
1 |
|
} |
115
|
1 |
|
} |
116
|
1 |
|
return $this->_seo[$condition]->getAttributes(['title', 'keywords', 'description']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Delete all SEO data |
121
|
|
|
*/ |
122
|
1 |
|
public function deleteAllSeobility() |
123
|
|
|
{ |
124
|
1 |
|
Seo::deleteAll($this->owner); |
125
|
1 |
|
} |
126
|
|
|
} |
127
|
|
|
|