Skip to content

Commit

Permalink
Merge pull request #22 from Laravel-Backpack/pr-19-update
Browse files Browse the repository at this point in the history
PR #19 update
  • Loading branch information
pxpm authored Apr 19, 2024
2 parents 151cc29 + b03b278 commit 5784196
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 30 deletions.
4 changes: 4 additions & 0 deletions resources/lang/ca/translation_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
'revert_confirmation_message' => 'La traducció s\'ha revertit correctament',
'revert_confirmation_not_title' => 'Traducció <strong>no</strong> revertida',
'revert_confirmation_not_message' => 'Hi ha hagut un error. És possible que la teva traducció no s\'hagi pogut revertir.',

'validation_missing_languages' => 'Falten claus de traducció.',
'validation_missing_group' => 'Falta la clau del grup.',
'validation_missing_key' => 'Falta la clau de traducció.',
];
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 translation group key.',
'validation_missing_key' => 'Missing translation key.',
];
4 changes: 4 additions & 0 deletions resources/lang/es/translation_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
'revert_confirmation_message' => 'La traducción ha sido revertida con éxito',
'revert_confirmation_not_title' => 'Traducción <strong>no</strong> revertida',
'revert_confirmation_not_message' => 'Ha habido un error. Es posible que tu traducción no haya sido revertida.',

'validation_missing_languages' => 'Faltan claves de traducción.',
'validation_missing_group' => 'Falta la clave del grupo.',
'validation_missing_key' => 'Falta la clave de traducción.',
];
103 changes: 75 additions & 28 deletions src/Http/Controllers/TranslationManagerCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setup(): void
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
Expand Down Expand Up @@ -118,44 +118,35 @@ protected function setupShowOperation(): void
*/
protected function setupCreateOperation(): void
{
$groups = config('backpack.translation-manager.groups', []);

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' => [
'disabled' => 'disabled',
],
$validationRule = $this->getValidationRuleWithLocales([
'group' => 'required',
'key' => 'required',
]);

CRUD::addField([
'name' => 'key',
'label' => ucfirst(__('backpack.translation-manager::translation_manager.key')),
'type' => 'text',
'wrapper' => ['class' => 'form-group col-md-8'],
'attributes' => [
'disabled' => 'disabled',
],
$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 {
return $request->only(['text']);
});

$this->setupFormFields(true);
}

/**
Expand Down Expand Up @@ -193,4 +184,60 @@ 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
{
$locales = config('backpack.crud.locales');
$localesCount = count($locales);

$rules = collect($locales)->mapWithKeys(fn ($locale, $key) => ['text.'.$key => 'bail|present'])->toArray();
$rules['text'] = ['bail', 'min:'.$localesCount, 'max:'.$localesCount];

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 5784196

Please sign in to comment.