1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dimsav\Translatable; |
4
|
|
|
|
5
|
|
|
use App; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
9
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
10
|
|
|
use Dimsav\Translatable\Exception\LocalesNotDefinedException; |
11
|
|
|
|
12
|
|
|
trait Translatable |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Alias for getTranslation(). |
16
|
|
|
* |
17
|
|
|
* @param string|null $locale |
18
|
|
|
* @param bool $withFallback |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
21
|
|
|
*/ |
22
|
|
|
public function translate($locale = null, $withFallback = false) |
23
|
|
|
{ |
24
|
|
|
return $this->getTranslation($locale, $withFallback); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Alias for getTranslation(). |
29
|
|
|
* |
30
|
|
|
* @param string $locale |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
33
|
|
|
*/ |
34
|
|
|
public function translateOrDefault($locale) |
35
|
|
|
{ |
36
|
|
|
return $this->getTranslation($locale, true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Alias for getTranslationOrNew(). |
41
|
|
|
* |
42
|
|
|
* @param string $locale |
43
|
|
|
* |
44
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
45
|
|
|
*/ |
46
|
|
|
public function translateOrNew($locale) |
47
|
|
|
{ |
48
|
|
|
return $this->getTranslationOrNew($locale); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string|null $locale |
53
|
|
|
* @param bool $withFallback |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
56
|
|
|
*/ |
57
|
|
|
public function getTranslation($locale = null, $withFallback = null) |
58
|
|
|
{ |
59
|
|
|
$configFallbackLocale = $this->getFallbackLocale(); |
60
|
|
|
$locale = $locale ?: $this->locale(); |
61
|
|
|
$withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
62
|
|
|
$fallbackLocale = $this->getFallbackLocale($locale); |
63
|
|
|
|
64
|
|
|
if ($translation = $this->getTranslationByLocaleKey($locale)) { |
65
|
|
|
return $translation; |
66
|
|
|
} |
67
|
|
|
if ($withFallback && $fallbackLocale) { |
68
|
|
|
if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
69
|
|
|
return $translation; |
70
|
|
|
} |
71
|
|
|
if ($translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
72
|
|
|
return $translation; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|null $locale |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function hasTranslation($locale = null) |
85
|
|
|
{ |
86
|
|
|
$locale = $locale ?: $this->locale(); |
87
|
|
|
|
88
|
|
|
foreach ($this->translations as $translation) { |
|
|
|
|
89
|
|
|
if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getTranslationModelName() |
101
|
|
|
{ |
102
|
|
|
return $this->translationModel ?: $this->getTranslationModelNameDefault(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getTranslationModelNameDefault() |
109
|
|
|
{ |
110
|
|
|
$config = app()->make('config'); |
111
|
|
|
|
112
|
|
|
return get_class($this).$config->get('translatable.translation_suffix', 'Translation'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getRelationKey() |
119
|
|
|
{ |
120
|
|
|
if ($this->translationForeignKey) { |
121
|
|
|
$key = $this->translationForeignKey; |
|
|
|
|
122
|
|
|
} elseif ($this->primaryKey !== 'id') { |
|
|
|
|
123
|
|
|
$key = $this->primaryKey; |
124
|
|
|
} else { |
125
|
|
|
$key = $this->getForeignKey(); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $key; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getLocaleKey() |
135
|
|
|
{ |
136
|
|
|
$config = app()->make('config'); |
137
|
|
|
|
138
|
|
|
return $this->localeKey ?: $config->get('translatable.locale_key', 'locale'); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
143
|
|
|
*/ |
144
|
|
|
public function translations() |
145
|
|
|
{ |
146
|
|
|
return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey()); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $key |
151
|
|
|
* |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
public function getAttribute($key) |
155
|
|
|
{ |
156
|
|
|
list($attribute, $locale) = $this->getAttributeAndLocale($key); |
157
|
|
|
|
158
|
|
|
if ($this->isTranslationAttribute($attribute)) { |
159
|
|
|
if ($this->getTranslation($locale) === null) { |
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue |
164
|
|
|
// on it. This way, we can use Eloquent's checking for Mutation, type casting, and |
165
|
|
|
// Date fields. |
166
|
|
|
if ($this->hasGetMutator($attribute)) { |
|
|
|
|
167
|
|
|
$this->attributes[$attribute] = $this->getTranslation($locale)->$attribute; |
|
|
|
|
168
|
|
|
|
169
|
|
|
return $this->getAttributeValue($attribute); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->getTranslation($locale)->$attribute; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return parent::getAttribute($key); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $key |
180
|
|
|
* @param mixed $value |
181
|
|
|
* |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function setAttribute($key, $value) |
185
|
|
|
{ |
186
|
|
|
list($attribute, $locale) = $this->getAttributeAndLocale($key); |
187
|
|
|
|
188
|
|
|
if ($this->isTranslationAttribute($attribute)) { |
189
|
|
|
$this->getTranslationOrNew($locale)->$attribute = $value; |
190
|
|
|
} else { |
191
|
|
|
return parent::setAttribute($key, $value); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param array $options |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function save(array $options = []) |
203
|
|
|
{ |
204
|
|
|
if ($this->exists) { |
|
|
|
|
205
|
|
|
if (count($this->getDirty()) > 0) { |
|
|
|
|
206
|
|
|
// If $this->exists and dirty, parent::save() has to return true. If not, |
|
|
|
|
207
|
|
|
// an error has occurred. Therefore we shouldn't save the translations. |
208
|
|
|
if (parent::save($options)) { |
209
|
|
|
return $this->saveTranslations(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return false; |
213
|
|
|
} else { |
214
|
|
|
// If $this->exists and not dirty, parent::save() skips saving and returns |
|
|
|
|
215
|
|
|
// false. So we have to save the translations |
216
|
|
|
if ($saved = $this->saveTranslations()) { |
217
|
|
|
$this->fireModelEvent('saved', false); |
|
|
|
|
218
|
|
|
$this->fireModelEvent('updated', false); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $saved; |
222
|
|
|
} |
223
|
|
|
} elseif (parent::save($options)) { |
224
|
|
|
// We save the translations only if the instance is saved in the database. |
225
|
|
|
return $this->saveTranslations(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param string $locale |
233
|
|
|
* |
234
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
235
|
|
|
*/ |
236
|
|
|
protected function getTranslationOrNew($locale) |
237
|
|
|
{ |
238
|
|
|
if (($translation = $this->getTranslation($locale, false)) === null) { |
239
|
|
|
$translation = $this->getNewTranslation($locale); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $translation; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param array $attributes |
247
|
|
|
* |
248
|
|
|
* @throws \Illuminate\Database\Eloquent\MassAssignmentException |
249
|
|
|
* @return $this |
250
|
|
|
*/ |
251
|
|
|
public function fill(array $attributes) |
252
|
|
|
{ |
253
|
|
|
foreach ($attributes as $key => $values) { |
254
|
|
|
if ($this->isKeyALocale($key)) { |
255
|
|
|
$this->getTranslationOrNew($key)->fill($values); |
256
|
|
|
unset($attributes[$key]); |
257
|
|
|
} else { |
258
|
|
|
list($attribute, $locale) = $this->getAttributeAndLocale($key); |
259
|
|
|
if ($this->isTranslationAttribute($attribute)) { |
260
|
|
|
$this->getTranslationOrNew($locale)->fill([$attribute => $values]); |
261
|
|
|
unset($attributes[$key]); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return parent::fill($attributes); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $key |
271
|
|
|
*/ |
272
|
|
|
private function getTranslationByLocaleKey($key) |
273
|
|
|
{ |
274
|
|
|
foreach ($this->translations as $translation) { |
275
|
|
|
if ($translation->getAttribute($this->getLocaleKey()) == $key) { |
276
|
|
|
return $translation; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return null; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param null $locale |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
private function getFallbackLocale($locale = null) |
289
|
|
|
{ |
290
|
|
|
if ($locale && $this->isLocaleCountryBased($locale)) { |
291
|
|
|
if ($fallback = $this->getLanguageFromCountryBasedLocale($locale)) { |
292
|
|
|
return $fallback; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return app()->make('config')->get('translatable.fallback_locale'); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param $locale |
301
|
|
|
* |
302
|
|
|
* @return bool |
303
|
|
|
*/ |
304
|
|
|
private function isLocaleCountryBased($locale) |
305
|
|
|
{ |
306
|
|
|
return strpos($locale, $this->getLocaleSeparator()) !== false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param $locale |
311
|
|
|
* |
312
|
|
|
* @return string |
313
|
|
|
*/ |
314
|
|
|
private function getLanguageFromCountryBasedLocale($locale) |
315
|
|
|
{ |
316
|
|
|
$parts = explode($this->getLocaleSeparator(), $locale); |
317
|
|
|
|
318
|
|
|
return array_get($parts, 0); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @return bool|null |
323
|
|
|
*/ |
324
|
|
|
private function useFallback() |
325
|
|
|
{ |
326
|
|
|
if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) { |
327
|
|
|
return $this->useTranslationFallback; |
|
|
|
|
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return app()->make('config')->get('translatable.use_fallback'); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param string $key |
335
|
|
|
* |
336
|
|
|
* @return bool |
337
|
|
|
*/ |
338
|
|
|
public function isTranslationAttribute($key) |
339
|
|
|
{ |
340
|
|
|
return in_array($key, $this->translatedAttributes); |
|
|
|
|
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param string $key |
345
|
|
|
* |
346
|
|
|
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
347
|
|
|
* @return bool |
348
|
|
|
*/ |
349
|
|
|
protected function isKeyALocale($key) |
350
|
|
|
{ |
351
|
|
|
$locales = $this->getLocales(); |
352
|
|
|
|
353
|
|
|
return in_array($key, $locales); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
358
|
|
|
* @return array |
359
|
|
|
*/ |
360
|
|
|
protected function getLocales() |
361
|
|
|
{ |
362
|
|
|
$localesConfig = (array) app()->make('config')->get('translatable.locales'); |
363
|
|
|
|
364
|
|
|
if (empty($localesConfig)) { |
365
|
|
|
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '. |
366
|
|
|
' and that the locales configuration is defined.'); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$locales = []; |
370
|
|
|
foreach ($localesConfig as $key => $locale) { |
371
|
|
|
if (is_array($locale)) { |
372
|
|
|
$locales[] = $key; |
373
|
|
|
foreach ($locale as $countryLocale) { |
374
|
|
|
$locales[] = $key.$this->getLocaleSeparator().$countryLocale; |
375
|
|
|
} |
376
|
|
|
} else { |
377
|
|
|
$locales[] = $locale; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $locales; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return string |
386
|
|
|
*/ |
387
|
|
|
protected function getLocaleSeparator() |
388
|
|
|
{ |
389
|
|
|
return app()->make('config')->get('translatable.locale_separator', '-'); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
|
|
protected function saveTranslations() |
396
|
|
|
{ |
397
|
|
|
$saved = true; |
398
|
|
|
foreach ($this->translations as $translation) { |
399
|
|
|
if ($saved && $this->isTranslationDirty($translation)) { |
400
|
|
|
$translation->setAttribute($this->getRelationKey(), $this->getKey()); |
|
|
|
|
401
|
|
|
$saved = $translation->save(); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
return $saved; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @param \Illuminate\Database\Eloquent\Model $translation |
410
|
|
|
* |
411
|
|
|
* @return bool |
412
|
|
|
*/ |
413
|
|
|
protected function isTranslationDirty(Model $translation) |
414
|
|
|
{ |
415
|
|
|
$dirtyAttributes = $translation->getDirty(); |
416
|
|
|
unset($dirtyAttributes[$this->getLocaleKey()]); |
417
|
|
|
|
418
|
|
|
return count($dirtyAttributes) > 0; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* @param string $locale |
423
|
|
|
* |
424
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
425
|
|
|
*/ |
426
|
|
|
public function getNewTranslation($locale) |
427
|
|
|
{ |
428
|
|
|
$modelName = $this->getTranslationModelName(); |
429
|
|
|
$translation = new $modelName(); |
430
|
|
|
$translation->setAttribute($this->getLocaleKey(), $locale); |
431
|
|
|
$this->translations->add($translation); |
432
|
|
|
|
433
|
|
|
return $translation; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @param $key |
438
|
|
|
* |
439
|
|
|
* @return bool |
440
|
|
|
*/ |
441
|
|
|
public function __isset($key) |
442
|
|
|
{ |
443
|
|
|
return $this->isTranslationAttribute($key) || parent::__isset($key); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
448
|
|
|
* @param string $locale |
449
|
|
|
* |
450
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
451
|
|
|
*/ |
452
|
|
View Code Duplication |
public function scopeTranslatedIn(Builder $query, $locale = null) |
|
|
|
|
453
|
|
|
{ |
454
|
|
|
$locale = $locale ?: $this->locale(); |
455
|
|
|
|
456
|
|
|
return $query->whereHas('translations', function (Builder $q) use ($locale) { |
457
|
|
|
$q->where($this->getLocaleKey(), '=', $locale); |
458
|
|
|
}); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
463
|
|
|
* @param string $locale |
464
|
|
|
* |
465
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
466
|
|
|
*/ |
467
|
|
View Code Duplication |
public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
|
|
|
468
|
|
|
{ |
469
|
|
|
$locale = $locale ?: $this->locale(); |
470
|
|
|
|
471
|
|
|
return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) { |
472
|
|
|
$q->where($this->getLocaleKey(), '=', $locale); |
473
|
|
|
}); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
478
|
|
|
* |
479
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
480
|
|
|
*/ |
481
|
|
|
public function scopeTranslated(Builder $query) |
482
|
|
|
{ |
483
|
|
|
return $query->has('translations'); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Adds scope to get a list of translated attributes, using the current locale. |
488
|
|
|
* Example usage: Country::listsTranslations('name')->get()->toArray() |
489
|
|
|
* Will return an array with items: |
490
|
|
|
* [ |
491
|
|
|
* 'id' => '1', // The id of country |
492
|
|
|
* 'name' => 'Griechenland' // The translated name |
493
|
|
|
* ]. |
494
|
|
|
* |
495
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
496
|
|
|
* @param string $translationField |
497
|
|
|
*/ |
498
|
|
|
public function scopeListsTranslations(Builder $query, $translationField) |
499
|
|
|
{ |
500
|
|
|
$withFallback = $this->useFallback(); |
501
|
|
|
$translationTable = $this->getTranslationsTable(); |
502
|
|
|
$localeKey = $this->getLocaleKey(); |
503
|
|
|
|
504
|
|
|
$query |
|
|
|
|
505
|
|
|
->select($this->getTable().'.'.$this->getKeyName(), $translationTable.'.'.$translationField) |
|
|
|
|
506
|
|
|
->leftJoin($translationTable, $translationTable.'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName()) |
|
|
|
|
507
|
|
|
->where($translationTable.'.'.$localeKey, $this->locale()); |
508
|
|
|
if ($withFallback) { |
509
|
|
|
$query->orWhere(function (Builder $q) use ($translationTable, $localeKey) { |
510
|
|
|
$q->where($translationTable.'.'.$localeKey, $this->getFallbackLocale()) |
511
|
|
|
->whereNotIn($translationTable.'.'.$this->getRelationKey(), function (QueryBuilder $q) use ( |
512
|
|
|
$translationTable, |
513
|
|
|
$localeKey |
514
|
|
|
) { |
515
|
|
|
$q->select($translationTable.'.'.$this->getRelationKey()) |
516
|
|
|
->from($translationTable) |
517
|
|
|
->where($translationTable.'.'.$localeKey, $this->locale()); |
518
|
|
|
}); |
519
|
|
|
}); |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* This scope eager loads the translations for the default and the fallback locale only. |
525
|
|
|
* We can use this as a shortcut to improve performance in our application. |
526
|
|
|
* |
527
|
|
|
* @param Builder $query |
528
|
|
|
*/ |
529
|
|
|
public function scopeWithTranslation(Builder $query) |
530
|
|
|
{ |
531
|
|
|
$query->with([ |
532
|
|
|
'translations' => function (Relation $query) { |
533
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); |
534
|
|
|
|
535
|
|
|
if ($this->useFallback()) { |
536
|
|
|
return $query->orWhere($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->getFallbackLocale()); |
537
|
|
|
} |
538
|
|
|
}, |
539
|
|
|
]); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* This scope filters results by checking the translation fields. |
544
|
|
|
* |
545
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
546
|
|
|
* @param string $key |
547
|
|
|
* @param string $value |
548
|
|
|
* @param string $locale |
549
|
|
|
* |
550
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
551
|
|
|
*/ |
552
|
|
View Code Duplication |
public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
|
|
|
553
|
|
|
{ |
554
|
|
|
return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
555
|
|
|
$query->where($this->getTranslationsTable().'.'.$key, $value); |
556
|
|
|
if ($locale) { |
|
|
|
|
557
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale); |
558
|
|
|
} |
559
|
|
|
}); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* This scope filters results by checking the translation fields. |
564
|
|
|
* |
565
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
566
|
|
|
* @param string $key |
567
|
|
|
* @param string $value |
568
|
|
|
* @param string $locale |
569
|
|
|
* |
570
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
571
|
|
|
*/ |
572
|
|
View Code Duplication |
public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
|
|
|
573
|
|
|
{ |
574
|
|
|
return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
575
|
|
|
$query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value); |
576
|
|
|
if ($locale) { |
|
|
|
|
577
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale); |
578
|
|
|
} |
579
|
|
|
}); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* @return array |
584
|
|
|
*/ |
585
|
|
|
public function toArray() |
586
|
|
|
{ |
587
|
|
|
$attributes = parent::toArray(); |
588
|
|
|
|
589
|
|
|
$hiddenAttributes = $this->getHidden(); |
|
|
|
|
590
|
|
|
|
591
|
|
|
foreach ($this->translatedAttributes as $field) { |
|
|
|
|
592
|
|
|
if (in_array($field, $hiddenAttributes)) { |
593
|
|
|
continue; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
if ($translations = $this->getTranslation()) { |
|
|
|
|
597
|
|
|
$attributes[$field] = $translations->$field; |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
return $attributes; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* @return string |
606
|
|
|
*/ |
607
|
|
|
private function getTranslationsTable() |
608
|
|
|
{ |
609
|
|
|
return app()->make($this->getTranslationModelName())->getTable(); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* @return string |
614
|
|
|
*/ |
615
|
|
|
protected function locale() |
616
|
|
|
{ |
617
|
|
|
return app()->make('config')->get('translatable.locale') |
618
|
|
|
?: app()->make('translator')->getLocale(); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Deletes all translations for this model. |
623
|
|
|
* |
624
|
|
|
* @param string|array|null $locales The locales to be deleted (array or single string) |
625
|
|
|
* (e.g., ["en", "de"] would remove these translations). |
626
|
|
|
*/ |
627
|
|
|
public function deleteTranslations($locales = null) |
628
|
|
|
{ |
629
|
|
|
if ($locales === null) { |
630
|
|
|
$this->translations()->delete(); |
631
|
|
|
} else { |
632
|
|
|
$locales = (array) $locales; |
633
|
|
|
$this->translations()->whereIn($this->getLocaleKey(), $locales)->delete(); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
// we need to manually "reload" the collection built from the relationship |
637
|
|
|
// otherwise $this->translations()->get() would NOT be the same as $this->translations |
|
|
|
|
638
|
|
|
$this->load('translations'); |
|
|
|
|
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* @param $key |
643
|
|
|
* |
644
|
|
|
* @return array |
645
|
|
|
*/ |
646
|
|
|
private function getAttributeAndLocale($key) |
647
|
|
|
{ |
648
|
|
|
if (str_contains($key, ':')) { |
649
|
|
|
return explode(':', $key); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
return [$key, $this->locale()]; |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: