|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Models\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
|
6
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @property array $non_translatable |
|
12
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $translations |
|
13
|
|
|
*/ |
|
14
|
|
|
trait Translatable |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function bootTranslatable() |
|
20
|
|
|
{ |
|
21
|
|
|
static::deleted(function ($model) { |
|
22
|
|
|
Translation::query() |
|
23
|
|
|
->where('translatable_id', '=', $model->getKey()) |
|
24
|
|
|
->where('translatable_type', '=', get_class($model)) |
|
25
|
|
|
->delete(); |
|
26
|
|
|
}); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Initialize a translatable model. |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
public function initializeTranslatable() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->nonTranslatable = []; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the list of non translatable fields. |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getNonTranslatable() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->nonTranslatable; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Translations relationship. |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
53
|
|
|
*/ |
|
54
|
|
|
public function translations(): MorphMany |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->morphMany(Translation::class, 'translatable')->withPivot(['locale_id', 'translation_id']); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create and return a translation entry for given locale ID. |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $localeId |
|
63
|
|
|
* @return \BBS\Nova\Translation\Models\Translation |
|
64
|
|
|
*/ |
|
65
|
|
|
public function createTranslationEntry(int $localeId, int $translationId = 0) |
|
66
|
|
|
{ |
|
67
|
|
|
Translation::query()->create([ |
|
68
|
|
|
'locale_id' => $localeId, |
|
69
|
|
|
'translation_id' => ! empty($translationId) ? $translationId : static::freshTranslationId(), |
|
70
|
|
|
'translatable_id' => $this->getKey(), |
|
|
|
|
|
|
71
|
|
|
'translatable_type' => get_class($this), |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return next fresh translation ID. |
|
77
|
|
|
* |
|
78
|
|
|
* @return int |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function freshTranslationId() |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Translation $lastTranslation */ |
|
83
|
|
|
$lastTranslation = Translation::query() |
|
|
|
|
|
|
84
|
|
|
->select('translation_id') |
|
85
|
|
|
->orderBy('translation_id', 'desc') |
|
86
|
|
|
->first(); |
|
87
|
|
|
|
|
88
|
|
|
return ! empty($lastTranslation) ? ($lastTranslation->translation_id + 1) : 1; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Scope a query to only retrieve items from given locale. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
95
|
|
|
* @param string $iso |
|
96
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
97
|
|
|
* @throws \Exception |
|
98
|
|
|
*/ |
|
99
|
|
|
public function scopeInLocale(Builder $builder, string $iso = '') |
|
100
|
|
|
{ |
|
101
|
|
|
$iso = ! empty($iso) ? $iso : app()->getLocale(); |
|
102
|
|
|
|
|
103
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Locale $locale */ |
|
104
|
|
|
$locale = Locale::query()->select('id')->where('iso', '=', $iso)->first(); |
|
|
|
|
|
|
105
|
|
|
if (empty($locale)) { |
|
106
|
|
|
throw new Exception('Invalid locale provided in inLocale() scope "'.$iso.'"'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $builder->join('translations', function ($join) use ($locale) { |
|
110
|
|
|
$model = new static; |
|
111
|
|
|
|
|
112
|
|
|
$join |
|
113
|
|
|
->on($model->getTable().'.'.$model->getKeyName(), '=', 'translations.translatable_id') |
|
|
|
|
|
|
114
|
|
|
->where('translations.translatable_type', '=', get_class($model)) |
|
115
|
|
|
->where('translations.locale_id', '=', $locale->id); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.