1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable; |
4
|
|
|
|
5
|
|
|
use Spatie\Translatable\HasTranslations as OriginalHasTranslations; |
6
|
|
|
|
7
|
|
|
trait HasTranslations |
8
|
|
|
{ |
9
|
|
|
use OriginalHasTranslations; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var bool |
13
|
|
|
*/ |
14
|
|
|
public $locale = false; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get the attributes that were casted in the model. |
19
|
|
|
* Used for translations because Spatie/Laravel-Translatable |
20
|
|
|
* overwrites the getCasts() method. |
21
|
|
|
* |
22
|
|
|
* @return [type] [description] |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
public function getCastedAttributes() : array |
25
|
|
|
{ |
26
|
|
|
return parent::getCasts(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/* |
30
|
|
|
|-------------------------------------------------------------------------- |
31
|
|
|
| SPATIE/LARAVEL-TRANSLATABLE OVERWRITES |
32
|
|
|
|-------------------------------------------------------------------------- |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Use the forced locale if present. |
37
|
|
|
* |
38
|
|
|
* @param string $key |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function getAttributeValue($key) |
42
|
|
|
{ |
43
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
44
|
|
|
return parent::getAttributeValue($key); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$translation = $this->getTranslation($key, $this->locale ?: config('app.locale')); |
48
|
|
|
|
49
|
|
|
return is_array($translation) ? array_first($translation) : $translation; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/* |
53
|
|
|
|-------------------------------------------------------------------------- |
54
|
|
|
| ELOQUENT OVERWRITES |
55
|
|
|
|-------------------------------------------------------------------------- |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create translated items as json. |
60
|
|
|
* |
61
|
|
|
* @param array $attributes |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
|
|
public static function create(array $attributes = []) |
65
|
|
|
{ |
66
|
|
|
$locale = $attributes['locale'] ?? \App::getLocale(); |
67
|
|
|
$attributes = array_except($attributes, ['locale']); |
68
|
|
|
|
69
|
|
|
$model = new static(); |
70
|
|
|
|
71
|
|
|
// do the actual saving |
72
|
|
|
foreach ($attributes as $attribute => $value) { |
73
|
|
|
if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
74
|
|
|
$model->setTranslation($attribute, $locale, $value); |
75
|
|
|
} else { // the attribute is NOT translatable |
76
|
|
|
$model->{$attribute} = $value; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$model->save(); |
80
|
|
|
|
81
|
|
|
return $model; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Update translated items as json. |
86
|
|
|
* |
87
|
|
|
* @param array $attributes |
88
|
|
|
* @param array $options |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function update(array $attributes = [], array $options = []) |
92
|
|
|
{ |
93
|
|
|
if (! $this->exists) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$locale = $attributes['locale'] ?? \App::getLocale(); |
98
|
|
|
$attributes = array_except($attributes, ['locale']); |
99
|
|
|
|
100
|
|
|
// do the actual saving |
101
|
|
|
foreach ($attributes as $attribute => $value) { |
102
|
|
|
if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
103
|
|
|
$this->setTranslation($attribute, $locale, $value); |
104
|
|
|
} else { // the attribute is NOT translatable |
105
|
|
|
$this->{$attribute} = $value; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->save($options); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/* |
113
|
|
|
|-------------------------------------------------------------------------- |
114
|
|
|
| CUSTOM METHODS |
115
|
|
|
|-------------------------------------------------------------------------- |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Check if a model is translatable, by the adapter's standards. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function translationEnabledForModel() |
124
|
|
|
{ |
125
|
|
|
return property_exists($this, 'translatable'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get all locales the admin is allowed to use. |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function getAvailableLocales() |
134
|
|
|
{ |
135
|
|
|
return config('backpack.crud.locales'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set the locale property. Used in normalizeLocale() to force the translation |
140
|
|
|
* to a different language that the one set in app()->getLocale();. |
141
|
|
|
* |
142
|
|
|
* @param string |
143
|
|
|
*/ |
144
|
|
|
public function setLocale($locale) |
145
|
|
|
{ |
146
|
|
|
$this->locale = $locale; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the locale property. Used in SpatieTranslatableSluggableService |
151
|
|
|
* to save the slug for the appropriate language. |
152
|
|
|
* |
153
|
|
|
* @param string |
154
|
|
|
*/ |
155
|
|
|
public function getLocale() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
if ($this->locale) { |
158
|
|
|
return $this->locale; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return \Request::input('locale', \App::getLocale()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Magic method to get the db entries already translated in the wanted locale. |
166
|
|
|
* |
167
|
|
|
* @param string $method |
168
|
|
|
* @param array $parameters |
169
|
|
|
* @return |
170
|
|
|
*/ |
171
|
|
|
public function __call($method, $parameters) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
switch ($method) { |
174
|
|
|
// translate all find methods |
175
|
|
|
case 'find': |
|
|
|
|
176
|
|
|
case 'findOrFail': |
|
|
|
|
177
|
|
|
case 'findMany': |
|
|
|
|
178
|
|
|
case 'findBySlug': |
|
|
|
|
179
|
|
|
case 'findBySlugOrFail': |
|
|
|
|
180
|
|
|
|
181
|
|
|
$translation_locale = \Request::input('locale', \App::getLocale()); |
182
|
|
|
|
183
|
|
|
if ($translation_locale) { |
184
|
|
|
$item = parent::__call($method, $parameters); |
185
|
|
|
|
186
|
|
|
if ($item) { |
187
|
|
|
$item->setLocale($translation_locale); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $item; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return parent::__call($method, $parameters); |
194
|
|
|
break; |
|
|
|
|
195
|
|
|
|
196
|
|
|
// do not translate any other methods |
197
|
|
|
default: |
198
|
|
|
return parent::__call($method, $parameters); |
199
|
|
|
break; |
|
|
|
|
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.