1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BBSLab\NovaTranslation\Models\Traits; |
4
|
|
|
|
5
|
|
|
use BBSLab\NovaTranslation\Models\Locale; |
6
|
|
|
use BBSLab\NovaTranslation\Models\Observers\TranslatableObserver; |
7
|
|
|
use BBSLab\NovaTranslation\Models\Translation; |
8
|
|
|
use BBSLab\NovaTranslation\Models\TranslationRelation; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne; |
11
|
|
|
use Illuminate\Database\Query\JoinClause; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @property \BBSLab\NovaTranslation\Models\Translation $translation |
15
|
|
|
* @property \Illuminate\Database\Eloquent\Collection|\BBSLab\NovaTranslation\Models\Translation[] $translations |
16
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder locale(?string $iso = null) |
17
|
|
|
*/ |
18
|
|
|
trait Translatable |
19
|
|
|
{ |
20
|
|
|
protected $_deleting_translation = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public static function bootTranslatable() |
26
|
|
|
{ |
27
|
|
|
static::observe(TranslatableObserver::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the list of non translatable fields. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getNonTranslatable(): array |
36
|
|
|
{ |
37
|
|
|
return isset($this->nonTranslatable) ? $this->nonTranslatable : []; |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the list of fields to duplicate on create. |
42
|
|
|
* (Other fields MUST BE nullable in database). |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getOnCreateTranslatable(): array |
47
|
|
|
{ |
48
|
|
|
return isset($this->onCreateTranslatable) ? $this->onCreateTranslatable : $this->getNonTranslatable(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Translation relationship. |
53
|
|
|
* |
54
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphOne |
55
|
|
|
*/ |
56
|
|
|
public function translation(): MorphOne |
57
|
|
|
{ |
58
|
|
|
return $this->morphOne(Translation::class, 'translatable'); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return current item translations. |
63
|
|
|
* |
64
|
|
|
* @return \BBSLab\NovaTranslation\Models\TranslationRelation |
65
|
|
|
*/ |
66
|
|
|
public function translations(): TranslationRelation |
67
|
|
|
{ |
68
|
|
|
return new TranslationRelation($this); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create and return a translation entry for given locale ID. |
73
|
|
|
* |
74
|
|
|
* @param int $localeId |
75
|
|
|
* @param int $translationId |
76
|
|
|
* @return \BBSLab\NovaTranslation\Models\Translation |
77
|
|
|
*/ |
78
|
|
|
public function upsertTranslationEntry(int $localeId, int $translationId = 0): Translation |
79
|
|
|
{ |
80
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Translation $translation */ |
81
|
|
|
$translation = Translation::query() |
82
|
|
|
->firstOrCreate([ |
83
|
|
|
'locale_id' => $localeId, |
84
|
|
|
'translation_id' => ! empty($translationId) ? $translationId : $this->freshTranslationId(), |
85
|
|
|
'translatable_id' => $this->getKey(), |
|
|
|
|
86
|
|
|
'translatable_type' => static::class, |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
return $translation; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return next fresh translation ID. |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function freshTranslationId(): int |
98
|
|
|
{ |
99
|
|
|
$lastTranslation = Translation::query() |
100
|
|
|
->where('translatable_type', '=', static::class) |
101
|
|
|
->max('translation_id') ?? 0; |
102
|
|
|
|
103
|
|
|
return $lastTranslation + 1; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Scope a query to only retrieve items from given locale. |
108
|
|
|
* |
109
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
110
|
|
|
* @param string $iso |
111
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
112
|
|
|
*/ |
113
|
|
|
public function scopeLocale(EloquentBuilder $builder, string $iso = null) |
114
|
|
|
{ |
115
|
|
|
return $builder->join('translations', function (JoinClause $join) { |
116
|
|
|
$join->on($this->getQualifiedKeyName(), '=', 'translations.translatable_id') |
|
|
|
|
117
|
|
|
->where('translations.translatable_type', '=', static::class); |
118
|
|
|
}) |
119
|
|
|
->join('locales', function (JoinClause $join) use ($iso) { |
120
|
|
|
$join->on('locales.id', '=', 'translations.locale_id') |
121
|
|
|
->where('locales.iso', '=', $iso ?? app()->getLocale()); |
122
|
|
|
}) |
123
|
|
|
->select($this->qualifyColumn('*')); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Translate model to given locale and return translated model. |
128
|
|
|
* |
129
|
|
|
* @param \BBSLab\NovaTranslation\Models\Locale $locale |
130
|
|
|
* @return \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable |
131
|
|
|
*/ |
132
|
|
|
public function translate(Locale $locale) |
133
|
|
|
{ |
134
|
|
|
/** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $translated */ |
135
|
|
|
$translated = optional( |
136
|
|
|
$this->translations() |
|
|
|
|
137
|
|
|
->where('locale_id', '=', $locale->getKey()) |
138
|
|
|
->with('translatable') |
139
|
|
|
->first() |
140
|
|
|
)->translatable; |
141
|
|
|
|
142
|
|
|
return $translated ?? static::withoutEvents(function () use ($locale) { |
143
|
|
|
/** @var self $translated */ |
144
|
|
|
$translated = $this->newQuery()->create( |
|
|
|
|
145
|
|
|
$this->only( |
|
|
|
|
146
|
|
|
$this->getOnCreateTranslatable() |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
$translated->upsertTranslationEntry($locale->getKey(), $this->translation->translation_id); |
150
|
|
|
|
151
|
|
|
return $translated; |
152
|
|
|
}); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set deleting translation state. |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
public function deletingTranslation(): void |
161
|
|
|
{ |
162
|
|
|
$this->_deleting_translation = true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Determine is the model currently in a delete translation process. |
167
|
|
|
* |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function isDeletingTranslation(): bool |
171
|
|
|
{ |
172
|
|
|
return $this->_deleting_translation; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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: