Skip to content

Commit

Permalink
New 2.0.0 version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Nov 22, 2023
1 parent b44955d commit 421f0dd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 112 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
},
Expand Down
164 changes: 54 additions & 110 deletions src/Fields/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Field>
*/
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",
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 421f0dd

Please sign in to comment.