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