Skip to content

Commit a8a4fc9

Browse files
committed
improve TranslationBehavior
1 parent a38a596 commit a8a4fc9

File tree

1 file changed

+80
-16
lines changed

1 file changed

+80
-16
lines changed

src/behaviors/TranslationBehavior.php

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ class TranslationBehavior extends Behavior
8282
*/
8383
public $languages = [];
8484

85+
/**
86+
* Delete related records when delete owner
87+
* @var bool
88+
*/
89+
public $deleteTranslations = true;
90+
8591
protected $_newModels = [];
8692

8793
public function init()
8894
{
8995
parent::init();
90-
if (!in_array($this->defaultLanguage, $this->languages)) {
96+
if (!in_array($this->defaultLanguage, $this->languages, true)) {
9197
throw new InvalidConfigException('Default language must be exist');
9298
}
9399
}
@@ -100,19 +106,26 @@ public function events()
100106
return [
101107
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
102108
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
109+
ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete',
103110
];
104111
}
105112

106-
public function afterSave()
113+
/**
114+
* Delete related records when delete owner
115+
*/
116+
public function afterDelete()
107117
{
108-
/** @var ActiveRecord $owner */
109-
$owner = $this->owner;
110-
111-
foreach ($this->getModels() as $model) {
112-
$owner->link($this->relation, $model);
118+
if ($this->deleteTranslations) {
119+
/** @var ActiveRecord $owner */
120+
foreach ($this->getModels() as $model) {
121+
$model->delete();
122+
}
113123
}
114124
}
115125

126+
/**
127+
* @return ActiveRecord[]
128+
*/
116129
protected function getModels()
117130
{
118131
$selfModels = ArrayHelper::index($this->owner->{$this->relation}, function ($value) {
@@ -121,16 +134,37 @@ protected function getModels()
121134
return array_merge($this->getNewModels(), $selfModels);
122135
}
123136

137+
/**
138+
* @return ActiveRecord[]
139+
*/
124140
protected function getNewModels()
125141
{
126-
if (empty($this->_newModels)) {
142+
if (count($this->_newModels) === 0) {
127143
foreach ($this->languages as $key => $language) {
128144
$this->_newModels[$language] = new $this->langClassName([$this->languageField => $key]);
129145
}
130146
}
131147
return $this->_newModels;
132148
}
133149

150+
/**
151+
* Save related translations
152+
*/
153+
public function afterSave()
154+
{
155+
/** @var ActiveRecord $owner */
156+
$owner = $this->owner;
157+
158+
foreach ($this->getModels() as $model) {
159+
$owner->link($this->relation, $model);
160+
}
161+
}
162+
163+
/**
164+
* Modify owner model validators for translation attributes
165+
* e.g. `['title', 'required']` convert to `[['title_en', 'title_fr'], 'required']`
166+
* @param \yii\base\Component $owner
167+
*/
134168
public function attach($owner)
135169
{
136170
parent::attach($owner);
@@ -154,10 +188,15 @@ public function attach($owner)
154188
}
155189

156190
$params = array_slice($rule, 2);
157-
$validators[] = Validator::createValidator($rule[1], $owner, $rule_attributes, $params);
191+
$validators->append(Validator::createValidator($rule[1], $owner, $rule_attributes, $params));
158192
}
159193
}
160194

195+
/**
196+
* @param $attribute
197+
* @param $language
198+
* @return mixed|string
199+
*/
161200
private function getAttributeName($attribute, $language)
162201
{
163202
if ($this->attributeNamePattern instanceof \Closure) {
@@ -169,6 +208,12 @@ private function getAttributeName($attribute, $language)
169208
]);
170209
}
171210

211+
/**
212+
* Check translation attributes
213+
* @param string $name
214+
* @param bool $checkVars
215+
* @return bool
216+
*/
172217
public function canGetProperty($name, $checkVars = true)
173218
{
174219
if ($this->hasOwnProperty($name)) {
@@ -177,20 +222,31 @@ public function canGetProperty($name, $checkVars = true)
177222
return parent::canGetProperty($name, $checkVars);
178223
}
179224

225+
/**
226+
* Check translation attributes
227+
* @param $name
228+
* @return bool
229+
*/
180230
public function hasOwnProperty($name)
181231
{
182232
foreach ($this->translationAttributes as $attribute) {
183-
if ($name == $attribute) {
233+
if ($name === $attribute) {
184234
return true;
185235
}
186236
foreach ($this->languages as $language) {
187-
if ($this->getAttributeName($attribute, $language) == $name) {
237+
if ($this->getAttributeName($attribute, $language) === $name) {
188238
return true;
189239
}
190240
}
191241
}
192242
}
193243

244+
/**
245+
* Check translation attributes
246+
* @param string $name
247+
* @param bool $checkVars
248+
* @return bool
249+
*/
194250
public function canSetProperty($name, $checkVars = true)
195251
{
196252
if ($this->hasOwnProperty($name)) {
@@ -199,36 +255,44 @@ public function canSetProperty($name, $checkVars = true)
199255
return parent::canSetProperty($name, $checkVars);
200256
}
201257

258+
/**
259+
* @param string $name
260+
* @return mixed
261+
*/
202262
public function __get($name)
203263
{
204264
$list = $this->getModels();
205265

206-
207266
foreach ($this->translationAttributes as $attribute) {
208-
if ($name == $attribute) {
267+
if ($name === $attribute) {
209268
return $list[$this->defaultLanguage]->{$attribute};
210269
}
211270
foreach ($list as $language) {
212271
$langKey = $this->languages[$language->{$this->languageField}];
213-
if ($this->getAttributeName($attribute, $langKey) == $name) {
272+
if ($this->getAttributeName($attribute, $langKey) === $name) {
214273
return $list[$langKey]->{$attribute};
215274
}
216275
}
217276
}
218277
return parent::__get($name);
219278
}
220279

280+
/**
281+
* @param string $name
282+
* @param mixed $value
283+
* @return mixed
284+
*/
221285
public function __set($name, $value)
222286
{
223287
$list = $this->getModels();
224288

225289
foreach ($this->translationAttributes as $attribute) {
226-
if ($name == $attribute) {
290+
if ($name === $attribute) {
227291
return $list[$this->defaultLanguage]->{$attribute} = $value;
228292
}
229293
foreach ($list as $language) {
230294
$langKey = $this->languages[$language->{$this->languageField}];
231-
if ($this->getAttributeName($attribute, $langKey) == $name) {
295+
if ($this->getAttributeName($attribute, $langKey) === $name) {
232296
return $list[$langKey]->{$attribute} = $value;
233297
}
234298
}

0 commit comments

Comments
 (0)