1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Model for store and access SEO data for ActiveRecord in Yii2 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\models; |
11
|
|
|
|
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\db\ActiveRecord; |
14
|
|
|
use yii\db\Query; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Model for store and access SEO data for ActiveRecord in Yii2 framework |
18
|
|
|
* |
19
|
|
|
* @package inblank\seobility |
20
|
|
|
*/ |
21
|
|
|
class Seo extends Model |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string suffix for build table name for the tables stored SEO data for ActiveRecord |
25
|
|
|
*/ |
26
|
|
|
static public $tableSuffix = 'seo'; |
27
|
|
|
/** |
28
|
|
|
* @var int SEO data owner model id |
29
|
|
|
*/ |
30
|
|
|
public $model_id; |
31
|
|
|
/** |
32
|
|
|
* @var int SEO data condition |
33
|
|
|
*/ |
34
|
|
|
public $condition = 0; |
35
|
|
|
/** |
36
|
|
|
* @var string SEO title |
37
|
|
|
*/ |
38
|
|
|
public $title = ''; |
39
|
|
|
/** |
40
|
|
|
* @var string SEO keywords |
41
|
|
|
*/ |
42
|
|
|
public $keywords = ''; |
43
|
|
|
/** |
44
|
|
|
* @var string SEO description |
45
|
|
|
*/ |
46
|
|
|
public $description = ''; |
47
|
|
|
/** |
48
|
|
|
* @var bool new record flag |
49
|
|
|
*/ |
50
|
|
|
protected $_isNewRecord; |
51
|
|
|
/** |
52
|
|
|
* @var ActiveRecord SEO data owner |
53
|
|
|
*/ |
54
|
|
|
protected $_owner; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Search SEO data. If not found, will be returned new SEO model with empty data |
58
|
|
|
* @param ActiveRecord $owner the ActiveRecord model for which to find data |
59
|
|
|
* @param int $condition optional condition for searched SEO data |
60
|
|
|
* @return self |
61
|
|
|
*/ |
62
|
1 |
|
public static function find(ActiveRecord $owner, $condition = 0) |
63
|
|
|
{ |
64
|
1 |
|
$seo = new self(); |
65
|
1 |
|
$seo->_owner = $owner; |
66
|
1 |
|
$condition = (int)$condition; |
67
|
1 |
|
$data = $owner->getIsNewRecord() ? false : (new Query()) |
68
|
1 |
|
->from(Seo::tableName($owner)) |
69
|
1 |
|
->limit(1) |
70
|
1 |
|
->andWhere([ |
71
|
1 |
|
'condition' => $condition, |
72
|
1 |
|
'model_id' => $owner->getPrimaryKey(), |
73
|
1 |
|
])->one($owner->getDb()); |
74
|
1 |
|
$seo->_isNewRecord = empty($data); |
75
|
1 |
|
if ($data) { |
76
|
1 |
|
$seo->setAttributes($data); |
77
|
1 |
|
} |
78
|
1 |
|
$seo->condition = $condition; |
79
|
1 |
|
return $seo; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get table name of SEO data table for ActiveRecord |
84
|
|
|
* @param ActiveRecord $activeRecord |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public static function tableName(ActiveRecord $activeRecord) |
88
|
|
|
{ |
89
|
1 |
|
$tableName = $activeRecord->tableName(); |
90
|
1 |
|
if (substr($tableName, -2) == '}}') { |
91
|
1 |
|
return preg_replace('/{{(.+)}}/', '{{$1_' . self::$tableSuffix . '}}', $tableName); |
92
|
|
|
} else { |
93
|
1 |
|
return $tableName . '_' . self::$tableSuffix; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Find all SEO data. |
99
|
|
|
* Result will be in format ['condition1'=>Seo(), 'condition2'=>Seo(), ...] |
100
|
|
|
* @param ActiveRecord $owner the model for which to find data |
101
|
|
|
* @return self[] |
102
|
|
|
*/ |
103
|
1 |
|
public static function findAll(ActiveRecord $owner) |
104
|
|
|
{ |
105
|
|
|
/** @var self[] $seoList */ |
106
|
1 |
|
$seoList = []; |
107
|
1 |
|
if ($owner->getIsNewRecord()) { |
108
|
1 |
|
return $seoList; |
109
|
|
|
} |
110
|
1 |
|
$query = (new Query())->from(Seo::tableName($owner))->andWhere(['model_id' => $owner->getPrimaryKey()]); |
111
|
1 |
|
foreach ($query->all($owner->getDb()) as $seo) { |
112
|
1 |
|
$seo = new Seo($seo); |
113
|
1 |
|
$seo->_isNewRecord = false; |
114
|
1 |
|
$seo->_owner = $owner; |
115
|
1 |
|
$seoList[$seo['condition']] = $seo; |
116
|
1 |
|
} |
117
|
1 |
|
return $seoList; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Delete all SEO data for ActiveRecord |
122
|
|
|
* @param ActiveRecord $owner |
123
|
|
|
*/ |
124
|
1 |
|
public static function deleteAll(ActiveRecord $owner) |
125
|
|
|
{ |
126
|
1 |
|
$owner->getDb()->createCommand()->delete(self::tableName($owner), ['model_id' => $owner->getPrimaryKey()])->execute(); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
1 |
|
public function rules() |
133
|
|
|
{ |
134
|
|
|
return [ |
135
|
1 |
|
[['title', 'keywords', 'description'], 'safe'], |
136
|
1 |
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Save SEO data |
141
|
|
|
*/ |
142
|
1 |
|
public function save() |
143
|
|
|
{ |
144
|
1 |
|
if (empty($this->_owner)) { |
145
|
1 |
|
$this->addError('owner', "You can't use Seo model without owner"); |
146
|
1 |
|
return false; |
147
|
|
|
} |
148
|
1 |
|
$this->model_id = $this->_owner->getPrimaryKey(); |
149
|
1 |
|
$command = $this->_owner->getDb()->createCommand(); |
150
|
1 |
|
$attributes = ['title', 'keywords', 'description']; |
151
|
1 |
|
$tableName = self::tableName($this->_owner); |
152
|
1 |
|
if ($this->_isNewRecord) { |
153
|
1 |
|
if (empty(array_filter($this->getAttributes($attributes)))) { |
154
|
|
|
// don't save new SEO data with empty values |
155
|
1 |
|
return true; |
156
|
|
|
} |
157
|
1 |
|
$command->insert($tableName, $this->toArray()); |
158
|
1 |
|
} else { |
159
|
1 |
|
$command->update( |
160
|
1 |
|
$tableName, $this->toArray($attributes), |
161
|
1 |
|
['model_id' => $this->model_id, 'condition' => $this->condition] |
162
|
1 |
|
); |
163
|
|
|
} |
164
|
1 |
|
$command->execute(); |
165
|
1 |
|
$this->_isNewRecord = false; |
166
|
1 |
|
return true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Delete SEO data |
171
|
|
|
* @return $this|bool |
172
|
|
|
*/ |
173
|
1 |
|
public function delete() |
174
|
|
|
{ |
175
|
1 |
|
if (!$this->_isNewRecord) { |
176
|
1 |
|
$this->_owner->getDb() |
177
|
1 |
|
->createCommand() |
178
|
1 |
|
->delete( |
179
|
1 |
|
self::tableName($this->_owner), |
180
|
1 |
|
['model_id' => $this->_owner->getPrimaryKey(), 'condition' => $this->condition] |
181
|
1 |
|
)->execute(); |
182
|
1 |
|
} |
183
|
1 |
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get isNewRecord flag |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
1 |
|
public function getIsNewRecord() |
190
|
|
|
{ |
191
|
1 |
|
return $this->_isNewRecord; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|