Skip to content

Commit

Permalink
improve translations crud validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm committed Apr 19, 2024
1 parent de71dbf commit 6afca7d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 46 deletions.
4 changes: 4 additions & 0 deletions resources/lang/en/translation_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
'revert_confirmation_message' => 'The translation has been reverted successfully',
'revert_confirmation_not_title' => 'Translation <strong>not</strong> reverted',
'revert_confirmation_not_message' => "There's been an error. Your translation might not have been reverted.",

'validation_missing_languages' => 'Missing translation keys.',
'validation_missing_group' => 'Missing group key.',
'validation_missing_key' => 'Missing key.',
];
118 changes: 74 additions & 44 deletions src/Http/Controllers/TranslationManagerCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class TranslationManagerCrudController extends CrudController
public function setup(): void
{
CRUD::setModel(TranslationLine::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/translation-manager');
CRUD::setRoute(config('backpack.base.route_prefix').'/translation-manager');
CRUD::setEntityNameStrings(__('backpack.translation-manager::translation_manager.translation_line'), __('backpack.translation-manager::translation_manager.translation_lines'));

// access to edit and delete buttons
// access to delete button
CRUD::setAccessCondition(['delete'], fn (TranslationLine $entry) => $entry->database);

// disable create
if (!config('backpack.translation-manager.create', false)) {
if (! config('backpack.translation-manager.create', false)) {
CRUD::denyAccess('create');
}
}
Expand All @@ -46,7 +46,7 @@ protected function setupListOperation(): void
'label' => ucfirst(__('backpack.translation-manager::translation_manager.text')),
'value' => fn (TranslationLine $entry): mixed => $entry->getTranslation(App::getLocale()),
'searchLogic' => function (Builder $query, mixed $column, string $search): void {
$query->orWhere('search', 'like', '%' . Str::slug($search) . '%');
$query->orWhere('search', 'like', '%'.Str::slug($search).'%');
},
]);

Expand All @@ -55,7 +55,7 @@ protected function setupListOperation(): void
'label' => ucfirst(__('backpack.translation-manager::translation_manager.key')),
'type' => 'custom_html',
'value' => function (TranslationLine $entry): string {
return '<span class="badge" title="' . $entry->group_key . '">' . Str::limit($entry->group_key, 50) . '</span>';
return '<span class="badge" title="'.$entry->group_key.'">'.Str::limit($entry->group_key, 50).'</span>';
},
'orderable' => true,
'orderLogic' => function (Builder $query, mixed $column, mixed $columnDirection): Builder {
Expand All @@ -77,7 +77,7 @@ protected function setupListOperation(): void
'value' => function (TranslationLine $entry): string {
$value = $entry->database ? 'database' : 'file';

return '<i class="las la-' . $value . '" title="' . $value . '"></i>';
return '<i class="las la-'.$value.'" title="'.$value.'"></i>';
},
]);
}
Expand Down Expand Up @@ -118,63 +118,43 @@ protected function setupShowOperation(): void
*/
protected function setupCreateOperation(): void
{
$attributes = [];

$groups = config('backpack.translation-manager.groups', []);
$canCreate = config('backpack.translation-manager.create');

if (!$canCreate) {
$attributes = ['disabled' => 'disabled'];
}

$this->crud->setValidation([
$validationRule = $this->getValidationRuleWithLocales([
'group' => 'required',
'key' => 'required',
], [], [
'group' => __('backpack.translation-manager::translation_manager.group'),
'key' => __('backpack.translation-manager::translation_manager.key')
'key' => 'required',
]);

CRUD::addField([
'name' => 'group',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.group')),
'wrapper' => ['class' => 'form-group col-md-4'],
'type' => empty($groups) ? 'text' : 'select_from_array',
'options' => $groups,
'attributes' => $attributes,
]);

CRUD::addField([
'name' => 'key',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.key')),
'type' => 'text',
'wrapper' => ['class' => 'form-group col-md-8'],
'attributes' => $attributes,
$validationMessages = $this->getValidationMessagesWithLocale([
'group.required' => __('backpack.translation-manager::translation_manager.validation_missing_group'),
'key.required' => __('backpack.translation-manager::translation_manager.validation_missing_key'),
]);

CRUD::addField([
'name' => 'text',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.text')),
'type' => 'translation-edit-field',
]);
CRUD::setValidation($validationRule, $validationMessages);

CRUD::removeSaveAction('save_and_edit');
$this->setupFormFields();
}

/**
* Setup Update Operation
*/
protected function setupUpdateOperation(): void
{
$this->setupCreateOperation();
{
CRUD::setValidation($this->getValidationRuleWithLocales(), $this->getValidationMessagesWithLocale());

// since we added group and key as fields, we don't want them
// to be editable by the user on the update operation.
CRUD::setOperationSetting('strippedRequest', function ($request): array {

Check failure on line 145 in src/Http/Controllers/TranslationManagerCrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / Laravel ^10.15

Parameter #2 $value of static method Backpack\CRUD\app\Library\CrudPanel\CrudPanel::setOperationSetting() expects bool, Closure given.

Check failure on line 145 in src/Http/Controllers/TranslationManagerCrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / Laravel ^10.15

Parameter #2 $value of static method Backpack\CRUD\app\Library\CrudPanel\CrudPanel::setOperationSetting() expects bool, Closure given.

Check failure on line 145 in src/Http/Controllers/TranslationManagerCrudController.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / Laravel ^11.0

Parameter #2 $value of static method Backpack\CRUD\app\Library\CrudPanel\CrudPanel::setOperationSetting() expects bool, Closure given.
return $request->only(['text']);
});

$this->setupFormFields(true);
}

/**
* Setup Filters
*/
public function setupFilters(): void
{
if (!backpack_pro()) {
if (! backpack_pro()) {
return;
}

Expand Down Expand Up @@ -204,4 +184,54 @@ public function setupFilters(): void
CRUD::addClause('where', 'database', $option === 'database');
});
}


private function setupFormFields(bool $forceDisabledFields = false): void
{
$attributes = [];

$groups = config('backpack.translation-manager.groups', []);
$canCreate = config('backpack.translation-manager.create');

if (! $canCreate || $forceDisabledFields) {
$attributes = ['disabled' => 'disabled'];
}

CRUD::addField([
'name' => 'group',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.group')),
'wrapper' => ['class' => 'form-group col-md-4'],
'type' => empty($groups) ? 'text' : 'select_from_array',
'options' => $groups,
'attributes' => $attributes,
]);

CRUD::addField([
'name' => 'key',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.key')),
'type' => 'text',
'wrapper' => ['class' => 'form-group col-md-8'],
'attributes' => $attributes,
]);

CRUD::addField([
'name' => 'text',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.text')),
'type' => 'translation-edit-field',
]);

CRUD::removeSaveAction('save_and_edit');
}

private function getValidationRuleWithLocales(array $rulesToMerge = []): array
{
$rules = collect(config('backpack.crud.locales'))->mapWithKeys(fn ($locale, $key) => ['text.'.$key => 'present'])->toArray();

return array_merge($rules, $rulesToMerge);
}

private function getValidationMessagesWithLocale(array $messagesToMerge = []): array
{
return array_merge(['text.*' => __('backpack.translation-manager::translation_manager.validation_missing_languages')], $messagesToMerge);
}
}
4 changes: 2 additions & 2 deletions src/Http/Operations/CanUseEditableColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function saveMinorUpdateEntry()
$text = $entry->text;
$text[$locale] = $request->value;

$entry = TranslationLineOriginal::find($entry->id_database);
$entry = TranslationLineOriginal::findOrFail($entry->id_database);
$entry->text = $text;
$entry->save();
} else {
Expand All @@ -49,7 +49,7 @@ public function saveMinorUpdateEntry()
}

// fetch the entry from sushi
$entry = TranslationLine::find($request->id);
$entry = TranslationLine::findOrFail($request->id);
$entry->database = true;

return $entry;
Expand Down

0 comments on commit 6afca7d

Please sign in to comment.