move into sonardesk
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Localization\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
use Modules\Localization\Models\Locale;
|
||||
|
||||
final class TranslationController extends Controller
|
||||
{
|
||||
public function edit(string $subject, int $id): View
|
||||
{
|
||||
abort_unless(auth()->user()->can('localization.translate'), 403);
|
||||
[$record, $config] = $this->resolve($subject, $id);
|
||||
$record->load('translations.locale');
|
||||
|
||||
return view('localization::admin.translate', ['record' => $record, 'subject' => $subject, 'config' => $config, 'locales' => Locale::query()->active()->get()]);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $subject, int $id): RedirectResponse
|
||||
{
|
||||
abort_unless($request->user()->can('localization.translate'), 403);
|
||||
[$record, $config] = $this->resolve($subject, $id);
|
||||
$data = $request->validate(['translations' => ['array'], 'translations.*' => ['array']]);
|
||||
foreach ($data['translations'] ?? [] as $localeCode => $fields) {
|
||||
foreach ($config['fields'] as $field) {
|
||||
$record->setTranslation($field, $localeCode, $fields[$field] ?? null, $request->user()->id);
|
||||
}
|
||||
}
|
||||
|
||||
return back()->with('success', 'Translations saved.');
|
||||
}
|
||||
|
||||
private function resolve(string $subject, int $id): array
|
||||
{
|
||||
$config = config("localization.supported_subjects.$subject");
|
||||
abort_unless(is_array($config), 404);
|
||||
$record = $config['model']::query()->findOrFail($id);
|
||||
|
||||
return [$record, $config];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user