1 | <?php |
||
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) |
|
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() |
|
86 | |||
87 | /** |
||
88 | * Delete SEO data |
||
89 | * @param int $condition delete SEO data condition |
||
90 | */ |
||
91 | 1 | public function deleteSeobility($condition = 0) |
|
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() |
|
126 | } |
||
127 |