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
|
|
|
$translation = $this->getTranslation($key, $this->locale ?: config('app.locale')); |
32
|
|
|
|
33
|
|
|
return is_array($translation) ? array_first($translation) : $translation; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
|-------------------------------------------------------------------------- |
38
|
|
|
| ELOQUENT OVERWRITES |
39
|
|
|
|-------------------------------------------------------------------------- |
40
|
|
|
*/ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create translated items as json. |
44
|
|
|
* |
45
|
|
|
* @param array $attributes [description] |
46
|
|
|
*/ |
47
|
|
|
public static function create(array $attributes = []) |
48
|
|
|
{ |
49
|
|
|
$locale = $attributes['locale'] ?? \App::getLocale(); |
50
|
|
|
$attributes = array_except($attributes, ['locale']); |
51
|
|
|
|
52
|
|
|
$model = new static(); |
53
|
|
|
|
54
|
|
|
// do the actual saving |
55
|
|
|
foreach ($attributes as $attribute => $value) { |
56
|
|
|
if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
57
|
|
|
$model->setTranslation($attribute, $locale, $value); |
58
|
|
|
} else { // the attribute is NOT translatable |
59
|
|
|
$model->{$attribute} = $value; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$model->save(); |
63
|
|
|
|
64
|
|
|
return $model; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Update translated items as json. |
69
|
|
|
* |
70
|
|
|
* @param array $attributes |
71
|
|
|
* @param array $options |
72
|
|
|
* @return bool |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
public function update(array $attributes = [], array $options = []) |
75
|
|
|
{ |
76
|
|
|
if (! $this->exists) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$locale = $attributes['locale'] ? $attributes['locale'] : App::getLocale(); |
81
|
|
|
$attributes = array_except($attributes, ['locale']); |
82
|
|
|
|
83
|
|
|
// do the actual saving |
84
|
|
|
foreach ($attributes as $attribute => $value) { |
85
|
|
|
if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
86
|
|
|
$this->setTranslation($attribute, $locale, $value); |
87
|
|
|
} else { // the attribute is NOT translatable |
88
|
|
|
$this->{$attribute} = $value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
$this->save($options); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/* |
97
|
|
|
|-------------------------------------------------------------------------- |
98
|
|
|
| CUSTOM METHODS |
99
|
|
|
|-------------------------------------------------------------------------- |
100
|
|
|
*/ |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Check if a model is translatable, by the adapter's standards. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function translationEnabledForModel() |
108
|
|
|
{ |
109
|
|
|
return property_exists($this, 'translatable'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get all locales the admin is allowed to use. |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getAvailableLocales() |
118
|
|
|
{ |
119
|
|
|
return config('backpack.crud.locales'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the locale property. Used in normalizeLocale() to force the translation |
124
|
|
|
* to a different language that the one set in app()->getLocale();. |
125
|
|
|
* |
126
|
|
|
* @param string |
127
|
|
|
*/ |
128
|
|
|
public function setLocale($locale) |
129
|
|
|
{ |
130
|
|
|
$this->locale = $locale; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Magic method to get the db entries already translated in the wanted locale. |
135
|
|
|
*/ |
136
|
|
|
public function __call($method, $parameters) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
switch ($method) { |
139
|
|
|
// translate all find methods |
140
|
|
|
case 'find': |
|
|
|
|
141
|
|
|
case 'findOrFail': |
|
|
|
|
142
|
|
|
case 'findMany': |
|
|
|
|
143
|
|
|
|
144
|
|
|
$translation_locale = \Request::input('locale'); |
145
|
|
|
$default_locale = \App::getLocale(); |
|
|
|
|
146
|
|
|
|
147
|
|
|
if ($translation_locale) { |
148
|
|
|
$item = parent::__call($method, $parameters); |
149
|
|
|
$item->setLocale($translation_locale); |
150
|
|
|
|
151
|
|
|
return $item; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return parent::__call($method, $parameters); |
155
|
|
|
break; |
|
|
|
|
156
|
|
|
|
157
|
|
|
// do not translate any other methods |
158
|
|
|
default: |
159
|
|
|
return parent::__call($method, $parameters); |
160
|
|
|
break; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.