From 421f0dd77264625eb76ac1ab96a88a634804f500 Mon Sep 17 00:00:00 2001 From: alexvenga Date: Wed, 22 Nov 2023 21:42:05 +0300 Subject: [PATCH] New 2.0.0 version --- composer.json | 3 +- src/Fields/Translatable.php | 164 ++++++++++++------------------------ 2 files changed, 55 insertions(+), 112 deletions(-) diff --git a/composer.json b/composer.json index 7f14408..8764008 100644 --- a/composer.json +++ b/composer.json @@ -31,14 +31,13 @@ "require": { "php": "^8.0|^8.1|^8.2" }, - "require-dev": { - }, "autoload": { "psr-4": { "VI\\MoonShineSpatieTranslatable\\": "src" } }, "conflict": { + "moonshine/moonshine": "<2.0" }, "scripts": { }, diff --git a/src/Fields/Translatable.php b/src/Fields/Translatable.php index 0db7b79..2a0445b 100644 --- a/src/Fields/Translatable.php +++ b/src/Fields/Translatable.php @@ -2,20 +2,28 @@ namespace VI\MoonShineSpatieTranslatable\Fields; -use Illuminate\Database\Eloquent\Model; +use Closure; +use Illuminate\Contracts\View\View; use Illuminate\Support\Str; -use Illuminate\Validation\ValidationException; +use MoonShine\Fields\Field; use MoonShine\Fields\Fields; use MoonShine\Fields\Json; use MoonShine\Fields\Select; use MoonShine\Fields\Text; use MoonShine\Fields\Textarea; use MoonShine\Fields\TinyMce; +use MoonShine\Support\FieldEmptyValue; -class Translatable extends Json +final class Translatable extends Json { - protected $inputField = Text::class; + protected bool $keyValue = true; + protected bool $onlyValue = false; + + /** + * @var class-string + */ + protected string $inputField = Text::class; protected array $languagesCodes = [ "af", "sq", "am", "ar", "an", "hy", "ast", "az", "eu", "be", "bn", "bs", "br", "bg", "ca", "ckb", "zh", "zh-hk", @@ -33,30 +41,36 @@ class Translatable extends Json protected array $priorityLanguagesCodes = []; - protected bool $keyValue = true; - - public function textarea(): static + protected function prepareFill(array $raw = [], mixed $casted = null): mixed { - - $this->inputField = Textarea::class; - - return $this; + return $casted->getTranslations($this->column()); } - public function tinyMce(): static + /** + * @throws \Throwable + */ + public function onlyValue( + string $value = 'Value' + ): static { - - $this->inputField = TinyMce::class; - - return $this; + throw new \Exception('Can`t set onlyValue for this field!'); } - public function json(): static + public function getFields(mixed $data = null): Fields { - $this->inputField = Json::class; + $inputField = $this->inputField::make(__('Value'), 'value'); - return $this; + if (empty($this->fields)) { + $this->fields([ + Select::make(__('Code'), 'key') + ->options(array_combine($this->getLanguagesCodes(), + array_map(static fn($code) => Str::upper($code), $this->getLanguagesCodes()))), + $inputField, + ]); + } + + return parent::getFields(); } public function languages(array $languages): static @@ -93,114 +107,44 @@ protected function getLanguagesCodes(): array ->toArray(); } - public function keyValue(string $key = 'Language', string $value = 'Value'): static + public function textarea(): static { - $this->fields([ - Select::make($key, 'key') - ->options(array_combine($this->getLanguagesCodes(), - array_map(static fn($code) => Str::upper($code), $this->getLanguagesCodes()))) - ->nullable(), - $this->inputField::make($value, 'value'), - ]); + + $this->inputField = Textarea::class; return $this; } - public function getFields(): Fields + public function tinyMce(): static { + $this->inputField = TinyMce::class; - $inputField = $this->inputField::make(__('Value'), 'value'); - - if ($this->inputField === Json::class) { - $inputField = $inputField - ->fullpage() - ->fields([ - Text::make(__('Key'), 'title'), - Text::make(__('Value'), 'value') - ]); - } - - if (empty($this->fields)) { - $this->fields([ - Select::make(__('Code'), 'key') - ->options(array_combine($this->getLanguagesCodes(), - array_map(static fn($code) => Str::upper($code), $this->getLanguagesCodes()))) - ->nullable(), - $inputField, - ]); - } - - return parent::getFields(); - } - - public function hasFields(): bool - { - return true; + return $this; } - public function indexViewValue(Model $item, bool $container = false): string + public function json(): static { - return (string)$item->{$this->field()}; - } + $this->inputField = Json::class; - public function exportViewValue(Model $item): string - { - return (string)$item->{$this->field()}; + return $this; } - public function formViewValue(Model $item): mixed + public function keyValue(string $key = 'Language', string $value = 'Value'): static { - $translations = collect($item->getTranslations($this->field())); - - if ($translations->isEmpty() && $this->requiredLanguagesCodes) { - $translations = []; - foreach ($this->requiredLanguagesCodes as $code) { - $translations[$code] = ''; - } - $translations = collect($translations); - } + $this->fields([ + Select::make($key, 'key') + ->options(array_combine($this->getLanguagesCodes(), + array_map(static fn($code) => Str::upper($code), $this->getLanguagesCodes()))) + ->nullable(), + $this->inputField::make($value, 'value'), + ]); - return $translations->mapWithKeys(fn(string|array $value, string $key) => [ - $key => ['key' => $key, 'value' => $value] - ]) - ->values() - ->toArray(); + return $this; } - /** - * @throws ValidationException - */ - public function save(Model $item): Model + public function hasFields(): bool { - if ($this->isCanSave() && $this->requestValue() !== false) { - - $array = collect($this->requestValue()) - ->filter(fn($data) => !empty($data['key']) && !empty($data['value'])) - ->mapWithKeys(fn($data) => [$data['key'] => $data['value']]) - ->toArray(); - - $notSetLanguages = array_diff($this->requiredLanguagesCodes, array_keys($array)); - - if (!empty($notSetLanguages)) { - throw ValidationException::withMessages( - [ - $this->field() => - sprintf('The field %s does not have translation values set for the following languages: %s', - $this->label(), implode(', ', $notSetLanguages)), - ] - ); - } - - if ($this->isRemovable()) { - $item->replaceTranslations($this->field(), $array); - return $item; - } - - $item->setTranslations($this->field(), $array); - return $item; - - } - - return $item; + return true; } + }