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