@@ -82,12 +82,18 @@ class TranslationBehavior extends Behavior
82
82
*/
83
83
public $ languages = [];
84
84
85
+ /**
86
+ * Delete related records when delete owner
87
+ * @var bool
88
+ */
89
+ public $ deleteTranslations = true ;
90
+
85
91
protected $ _newModels = [];
86
92
87
93
public function init ()
88
94
{
89
95
parent ::init ();
90
- if (!in_array ($ this ->defaultLanguage , $ this ->languages )) {
96
+ if (!in_array ($ this ->defaultLanguage , $ this ->languages , true )) {
91
97
throw new InvalidConfigException ('Default language must be exist ' );
92
98
}
93
99
}
@@ -100,19 +106,26 @@ public function events()
100
106
return [
101
107
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave ' ,
102
108
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave ' ,
109
+ ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete ' ,
103
110
];
104
111
}
105
112
106
- public function afterSave ()
113
+ /**
114
+ * Delete related records when delete owner
115
+ */
116
+ public function afterDelete ()
107
117
{
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
+ }
113
123
}
114
124
}
115
125
126
+ /**
127
+ * @return ActiveRecord[]
128
+ */
116
129
protected function getModels ()
117
130
{
118
131
$ selfModels = ArrayHelper::index ($ this ->owner ->{$ this ->relation }, function ($ value ) {
@@ -121,16 +134,37 @@ protected function getModels()
121
134
return array_merge ($ this ->getNewModels (), $ selfModels );
122
135
}
123
136
137
+ /**
138
+ * @return ActiveRecord[]
139
+ */
124
140
protected function getNewModels ()
125
141
{
126
- if (empty ($ this ->_newModels )) {
142
+ if (count ($ this ->_newModels ) === 0 ) {
127
143
foreach ($ this ->languages as $ key => $ language ) {
128
144
$ this ->_newModels [$ language ] = new $ this ->langClassName ([$ this ->languageField => $ key ]);
129
145
}
130
146
}
131
147
return $ this ->_newModels ;
132
148
}
133
149
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
+ */
134
168
public function attach ($ owner )
135
169
{
136
170
parent ::attach ($ owner );
@@ -154,10 +188,15 @@ public function attach($owner)
154
188
}
155
189
156
190
$ 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) );
158
192
}
159
193
}
160
194
195
+ /**
196
+ * @param $attribute
197
+ * @param $language
198
+ * @return mixed|string
199
+ */
161
200
private function getAttributeName ($ attribute , $ language )
162
201
{
163
202
if ($ this ->attributeNamePattern instanceof \Closure) {
@@ -169,6 +208,12 @@ private function getAttributeName($attribute, $language)
169
208
]);
170
209
}
171
210
211
+ /**
212
+ * Check translation attributes
213
+ * @param string $name
214
+ * @param bool $checkVars
215
+ * @return bool
216
+ */
172
217
public function canGetProperty ($ name , $ checkVars = true )
173
218
{
174
219
if ($ this ->hasOwnProperty ($ name )) {
@@ -177,20 +222,31 @@ public function canGetProperty($name, $checkVars = true)
177
222
return parent ::canGetProperty ($ name , $ checkVars );
178
223
}
179
224
225
+ /**
226
+ * Check translation attributes
227
+ * @param $name
228
+ * @return bool
229
+ */
180
230
public function hasOwnProperty ($ name )
181
231
{
182
232
foreach ($ this ->translationAttributes as $ attribute ) {
183
- if ($ name == $ attribute ) {
233
+ if ($ name === $ attribute ) {
184
234
return true ;
185
235
}
186
236
foreach ($ this ->languages as $ language ) {
187
- if ($ this ->getAttributeName ($ attribute , $ language ) == $ name ) {
237
+ if ($ this ->getAttributeName ($ attribute , $ language ) === $ name ) {
188
238
return true ;
189
239
}
190
240
}
191
241
}
192
242
}
193
243
244
+ /**
245
+ * Check translation attributes
246
+ * @param string $name
247
+ * @param bool $checkVars
248
+ * @return bool
249
+ */
194
250
public function canSetProperty ($ name , $ checkVars = true )
195
251
{
196
252
if ($ this ->hasOwnProperty ($ name )) {
@@ -199,36 +255,44 @@ public function canSetProperty($name, $checkVars = true)
199
255
return parent ::canSetProperty ($ name , $ checkVars );
200
256
}
201
257
258
+ /**
259
+ * @param string $name
260
+ * @return mixed
261
+ */
202
262
public function __get ($ name )
203
263
{
204
264
$ list = $ this ->getModels ();
205
265
206
-
207
266
foreach ($ this ->translationAttributes as $ attribute ) {
208
- if ($ name == $ attribute ) {
267
+ if ($ name === $ attribute ) {
209
268
return $ list [$ this ->defaultLanguage ]->{$ attribute };
210
269
}
211
270
foreach ($ list as $ language ) {
212
271
$ langKey = $ this ->languages [$ language ->{$ this ->languageField }];
213
- if ($ this ->getAttributeName ($ attribute , $ langKey ) == $ name ) {
272
+ if ($ this ->getAttributeName ($ attribute , $ langKey ) === $ name ) {
214
273
return $ list [$ langKey ]->{$ attribute };
215
274
}
216
275
}
217
276
}
218
277
return parent ::__get ($ name );
219
278
}
220
279
280
+ /**
281
+ * @param string $name
282
+ * @param mixed $value
283
+ * @return mixed
284
+ */
221
285
public function __set ($ name , $ value )
222
286
{
223
287
$ list = $ this ->getModels ();
224
288
225
289
foreach ($ this ->translationAttributes as $ attribute ) {
226
- if ($ name == $ attribute ) {
290
+ if ($ name === $ attribute ) {
227
291
return $ list [$ this ->defaultLanguage ]->{$ attribute } = $ value ;
228
292
}
229
293
foreach ($ list as $ language ) {
230
294
$ langKey = $ this ->languages [$ language ->{$ this ->languageField }];
231
- if ($ this ->getAttributeName ($ attribute , $ langKey ) == $ name ) {
295
+ if ($ this ->getAttributeName ($ attribute , $ langKey ) === $ name ) {
232
296
return $ list [$ langKey ]->{$ attribute } = $ value ;
233
297
}
234
298
}
0 commit comments