1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\ModelTraits\Translatable; |
4
|
|
|
|
5
|
|
|
use Spatie\Translatable\HasTranslations; |
6
|
|
|
|
7
|
|
|
trait SpatieTranslatableAdaptor |
8
|
|
|
{ |
9
|
|
|
use HasTranslations; |
10
|
|
|
|
11
|
|
|
public $locale = false; |
12
|
|
|
|
13
|
|
|
/* |
14
|
|
|
|-------------------------------------------------------------------------- |
15
|
|
|
| SPATIE/LARAVEL-TRANSLATABLE OVERWRITES |
16
|
|
|
|-------------------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Use the forced locale if present. |
21
|
|
|
* |
22
|
|
|
* @param [type] $key [description] |
|
|
|
|
23
|
|
|
* @return [type] [description] |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function getAttributeValue($key) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
28
|
|
|
return parent::getAttributeValue($key); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->getTranslation($key, $this->locale ?: config('app.locale')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/* |
35
|
|
|
|-------------------------------------------------------------------------- |
36
|
|
|
| ELOQUENT OVERWRITES |
37
|
|
|
|-------------------------------------------------------------------------- |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create translated items as json. |
42
|
|
|
* |
43
|
|
|
* @param array $attributes [description] |
44
|
|
|
* @return [type] [description] |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public static function create(array $attributes = []) |
47
|
|
|
{ |
48
|
|
|
$locale = $attributes['locale'] ? $attributes['locale'] : App::getLocale(); |
49
|
|
|
$attributes = array_except($attributes, ['locale']); |
50
|
|
|
|
51
|
|
|
$model = new static(); |
52
|
|
|
|
53
|
|
|
// do the actual saving |
54
|
|
|
foreach ($attributes as $attribute => $value) { |
55
|
|
|
if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
56
|
|
|
$model->setTranslation($attribute, $locale, $value); |
57
|
|
|
} else { // the attribute is NOT translatable |
58
|
|
|
$model->{$attribute} = $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$model->save(); |
62
|
|
|
|
63
|
|
|
return $model; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Update translated items as json. |
68
|
|
|
* |
69
|
|
|
* @param array $attributes |
70
|
|
|
* @param array $options |
71
|
|
|
* @return bool |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function update(array $attributes = [], array $options = []) |
74
|
|
|
{ |
75
|
|
|
if (! $this->exists) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$locale = $attributes['locale'] ? $attributes['locale'] : App::getLocale(); |
80
|
|
|
$attributes = array_except($attributes, ['locale']); |
81
|
|
|
|
82
|
|
|
// do the actual saving |
83
|
|
|
foreach ($attributes as $attribute => $value) { |
84
|
|
|
if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
85
|
|
|
$this->setTranslation($attribute, $locale, $value); |
86
|
|
|
} else { // the attribute is NOT translatable |
87
|
|
|
$this->{$attribute} = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
$this->save($options); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the database entry in the wanted locale. |
97
|
|
|
* |
98
|
|
|
* @param [int] The id of the row in the db to fetch. |
99
|
|
|
* |
100
|
|
|
* @return [Eloquent Collection] The row in the db. |
|
|
|
|
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function findOrFail($id) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$translation_locale = \Request::input('locale'); |
105
|
|
|
$default_locale = \App::getLocale(); |
|
|
|
|
106
|
|
|
|
107
|
|
|
if ($translation_locale) { |
108
|
|
|
$item = parent::findOrFail($id); |
109
|
|
|
$item->setLocale($translation_locale); |
110
|
|
|
|
111
|
|
|
return $item; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return parent::findOrFail($id); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the database entry in the wanted locale. |
119
|
|
|
* |
120
|
|
|
* @param [int] The id of the row in the db to fetch. |
121
|
|
|
* |
122
|
|
|
* @return [Eloquent Collection] The row in the db. |
|
|
|
|
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function find($id) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$translation_locale = \Request::input('locale'); |
127
|
|
|
$default_locale = \App::getLocale(); |
|
|
|
|
128
|
|
|
|
129
|
|
|
if ($translation_locale) { |
130
|
|
|
$item = parent::find($id); |
131
|
|
|
$item->setLocale($translation_locale); |
132
|
|
|
|
133
|
|
|
return $item; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return parent::find($id); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/* |
140
|
|
|
|-------------------------------------------------------------------------- |
141
|
|
|
| CUSTOM METHODS |
142
|
|
|
|-------------------------------------------------------------------------- |
143
|
|
|
*/ |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if a model is translatable, by the adapter's standards. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function translationEnabledForModel() |
151
|
|
|
{ |
152
|
|
|
return property_exists($this, 'translatable'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get all locales the admin is allowed to use. |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function getAvailableLocales() |
161
|
|
|
{ |
162
|
|
|
return config('backpack.crud.locales'); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Set the locale property. Used in normalizeLocale() to force the translation |
167
|
|
|
* to a different language that the one set in app()->getLocale();. |
168
|
|
|
* |
169
|
|
|
* @param string |
170
|
|
|
*/ |
171
|
|
|
public function setLocale($locale) |
172
|
|
|
{ |
173
|
|
|
$this->locale = $locale; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.