Laravel-Backpack /
CRUD
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Backpack\CRUD\app\Models\Traits\SpatieTranslatable; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Arr; |
||||||
| 6 | use Illuminate\Support\Facades\App; |
||||||
| 7 | use Illuminate\Support\Facades\Request; |
||||||
| 8 | use Spatie\Translatable\HasTranslations as OriginalHasTranslations; |
||||||
| 9 | |||||||
| 10 | trait HasTranslations |
||||||
| 11 | { |
||||||
| 12 | use OriginalHasTranslations; |
||||||
| 13 | |||||||
| 14 | /** |
||||||
| 15 | * @var bool |
||||||
| 16 | */ |
||||||
| 17 | public $locale = false; |
||||||
| 18 | |||||||
| 19 | /* |
||||||
| 20 | |-------------------------------------------------------------------------- |
||||||
| 21 | | SPATIE/LARAVEL-TRANSLATABLE OVERWRITES |
||||||
| 22 | |-------------------------------------------------------------------------- |
||||||
| 23 | */ |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * Use the forced locale if present. |
||||||
| 27 | * |
||||||
| 28 | * @param string $key |
||||||
| 29 | * @return mixed |
||||||
| 30 | */ |
||||||
| 31 | public function getAttributeValue($key) |
||||||
| 32 | { |
||||||
| 33 | if (! $this->isTranslatableAttribute($key)) { |
||||||
| 34 | return parent::getAttributeValue($key); |
||||||
| 35 | } |
||||||
| 36 | |||||||
| 37 | $useFallbackLocale = property_exists($this, 'useFallbackLocale') ? $this->useFallbackLocale : true; |
||||||
| 38 | |||||||
| 39 | $translation = $this->getTranslation($key, $this->locale ?: config('app.locale'), $useFallbackLocale); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 40 | |||||||
| 41 | // if it's a fake field, json_encode it |
||||||
| 42 | if (is_array($translation)) { |
||||||
| 43 | return json_encode($translation, JSON_UNESCAPED_UNICODE); |
||||||
| 44 | } |
||||||
| 45 | |||||||
| 46 | return $translation; |
||||||
| 47 | } |
||||||
| 48 | |||||||
| 49 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||||||
| 50 | { |
||||||
| 51 | $locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
||||||
| 52 | |||||||
| 53 | $translations = $this->getTranslations($key); |
||||||
| 54 | |||||||
| 55 | $translation = $translations[$locale] ?? ''; |
||||||
| 56 | |||||||
| 57 | if ($this->hasGetMutator($key)) { |
||||||
|
0 ignored issues
–
show
The method
hasGetMutator() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 58 | return $this->mutateAttribute($key, $translation); |
||||||
|
0 ignored issues
–
show
The method
mutateAttribute() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | return $translation; |
||||||
| 62 | } |
||||||
| 63 | |||||||
| 64 | /* |
||||||
| 65 | |-------------------------------------------------------------------------- |
||||||
| 66 | | ELOQUENT OVERWRITES |
||||||
| 67 | |-------------------------------------------------------------------------- |
||||||
| 68 | */ |
||||||
| 69 | |||||||
| 70 | /** |
||||||
| 71 | * Create translated items as json. |
||||||
| 72 | * |
||||||
| 73 | * @param array $attributes |
||||||
| 74 | * @return static |
||||||
| 75 | */ |
||||||
| 76 | public static function create(array $attributes = []) |
||||||
| 77 | { |
||||||
| 78 | $locale = $attributes['locale'] ?? App::getLocale(); |
||||||
| 79 | $attributes = Arr::except($attributes, ['locale']); |
||||||
| 80 | $non_translatable = []; |
||||||
| 81 | |||||||
| 82 | $model = new static(); |
||||||
| 83 | |||||||
| 84 | // do the actual saving |
||||||
| 85 | foreach ($attributes as $attribute => $value) { |
||||||
| 86 | if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
||||||
| 87 | $model->setTranslation($attribute, $locale, $value); |
||||||
| 88 | } else { // the attribute is NOT translatable |
||||||
| 89 | $non_translatable[$attribute] = $value; |
||||||
| 90 | } |
||||||
| 91 | } |
||||||
| 92 | $model->fill($non_translatable)->save(); |
||||||
|
0 ignored issues
–
show
The method
fill() does not exist on Backpack\CRUD\app\Models...latable\HasTranslations. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 93 | |||||||
| 94 | return $model; |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | /** |
||||||
| 98 | * Update translated items as json. |
||||||
| 99 | * |
||||||
| 100 | * @param array $attributes |
||||||
| 101 | * @param array $options |
||||||
| 102 | * @return bool |
||||||
| 103 | */ |
||||||
| 104 | public function update(array $attributes = [], array $options = []) |
||||||
| 105 | { |
||||||
| 106 | if (! $this->exists) { |
||||||
| 107 | return false; |
||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | $locale = $attributes['_locale'] ?? App::getLocale(); |
||||||
| 111 | $attributes = Arr::except($attributes, ['_locale']); |
||||||
| 112 | $non_translatable = []; |
||||||
| 113 | |||||||
| 114 | // do the actual saving |
||||||
| 115 | foreach ($attributes as $attribute => $value) { |
||||||
| 116 | if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
||||||
| 117 | $this->setTranslation($attribute, $locale, $value); |
||||||
| 118 | } else { // the attribute is NOT translatable |
||||||
| 119 | $non_translatable[$attribute] = $value; |
||||||
| 120 | } |
||||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | return $this->fill($non_translatable)->save($options); |
||||||
| 124 | } |
||||||
| 125 | |||||||
| 126 | /* |
||||||
| 127 | |-------------------------------------------------------------------------- |
||||||
| 128 | | CUSTOM METHODS |
||||||
| 129 | |-------------------------------------------------------------------------- |
||||||
| 130 | */ |
||||||
| 131 | |||||||
| 132 | /** |
||||||
| 133 | * Check if a model is translatable, by the adapter's standards. |
||||||
| 134 | * |
||||||
| 135 | * @return bool |
||||||
| 136 | */ |
||||||
| 137 | public function translationEnabledForModel() |
||||||
| 138 | { |
||||||
| 139 | return property_exists($this, 'translatable'); |
||||||
| 140 | } |
||||||
| 141 | |||||||
| 142 | /** |
||||||
| 143 | * Get all locales the admin is allowed to use. |
||||||
| 144 | * |
||||||
| 145 | * @return array |
||||||
| 146 | */ |
||||||
| 147 | public function getAvailableLocales() |
||||||
| 148 | { |
||||||
| 149 | return config('backpack.crud.locales'); |
||||||
| 150 | } |
||||||
| 151 | |||||||
| 152 | /** |
||||||
| 153 | * Set the locale property. Used in normalizeLocale() to force the translation |
||||||
| 154 | * to a different language that the one set in app()->getLocale();. |
||||||
| 155 | * |
||||||
| 156 | * @param string |
||||||
| 157 | */ |
||||||
| 158 | public function setLocale($locale) |
||||||
| 159 | { |
||||||
| 160 | $this->locale = $locale; |
||||||
| 161 | } |
||||||
| 162 | |||||||
| 163 | /** |
||||||
| 164 | * Get the locale property. Used in SpatieTranslatableSluggableService |
||||||
| 165 | * to save the slug for the appropriate language. |
||||||
| 166 | * |
||||||
| 167 | * @param string |
||||||
| 168 | */ |
||||||
| 169 | public function getLocale() |
||||||
| 170 | { |
||||||
| 171 | if ($this->locale) { |
||||||
| 172 | return $this->locale; |
||||||
| 173 | } |
||||||
| 174 | |||||||
| 175 | return Request::input('_locale', App::getLocale()); |
||||||
| 176 | } |
||||||
| 177 | |||||||
| 178 | /** |
||||||
| 179 | * Magic method to get the db entries already translated in the wanted locale. |
||||||
| 180 | * |
||||||
| 181 | * @param string $method |
||||||
| 182 | * @param array $parameters |
||||||
| 183 | * @return |
||||||
| 184 | */ |
||||||
| 185 | public function __call($method, $parameters) |
||||||
| 186 | { |
||||||
| 187 | switch ($method) { |
||||||
| 188 | // translate all find methods |
||||||
| 189 | case 'find': |
||||||
| 190 | case 'findOrFail': |
||||||
| 191 | case 'findMany': |
||||||
| 192 | case 'findBySlug': |
||||||
| 193 | case 'findBySlugOrFail': |
||||||
| 194 | $translation_locale = Request::input('_locale', App::getLocale()); |
||||||
| 195 | |||||||
| 196 | if ($translation_locale) { |
||||||
| 197 | $item = parent::__call($method, $parameters); |
||||||
| 198 | |||||||
| 199 | if ($item instanceof \Traversable) { |
||||||
| 200 | foreach ($item as $instance) { |
||||||
| 201 | $instance->setLocale($translation_locale); |
||||||
| 202 | } |
||||||
| 203 | } elseif ($item) { |
||||||
| 204 | $item->setLocale($translation_locale); |
||||||
| 205 | } |
||||||
| 206 | |||||||
| 207 | return $item; |
||||||
| 208 | } |
||||||
| 209 | |||||||
| 210 | return parent::__call($method, $parameters); |
||||||
| 211 | break; |
||||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||||
| 212 | |||||||
| 213 | // do not translate any other methods |
||||||
| 214 | default: |
||||||
| 215 | return parent::__call($method, $parameters); |
||||||
| 216 | break; |
||||||
| 217 | } |
||||||
| 218 | } |
||||||
| 219 | } |
||||||
| 220 |