X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FTechniqueController.php;h=1d51aff846f6f48910be8ae82c0e5e0d5483838a;hb=e4e7cbc7e4944b08d74f5752de337ba7700367f4;hp=7173f1edce28ad515b364e79d2eeb98a454dead1;hpb=9c8e6581de9599f34d231a141134af015cb28be5;p=alttp.git diff --git a/app/Http/Controllers/TechniqueController.php b/app/Http/Controllers/TechniqueController.php index 7173f1e..1d51aff 100644 --- a/app/Http/Controllers/TechniqueController.php +++ b/app/Http/Controllers/TechniqueController.php @@ -4,7 +4,9 @@ namespace App\Http\Controllers; use App\Models\Technique; use App\Models\TechniqueMap; +use App\Models\TechniqueTranslation; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\Request; class TechniqueController extends Controller @@ -41,6 +43,73 @@ class TechniqueController extends Controller return $tech->toJson(); } + public function update(Request $request, Technique $content) { + $this->authorize('update', $content); + + $validatedData = $request->validate([ + 'attribution' => 'string', + 'description' => 'string', + 'language' => 'string|in:de,en', + 'parent_id' => 'integer|exists:App\\Models\\Technique,id', + 'short' => 'string', + 'title' => 'string', + ]); + + if ($validatedData['language'] == 'en') { + $this->applyLocalizedValues($validatedData, $content); + $content->save(); + } else { + $translation = $this->getTranslation($content, $validatedData['language']); + $this->applyLocalizedValues($validatedData, $translation); + $translation->save(); + } + + $result = isset($validatedData['parent_id']) ? Technique::findOrFail($validatedData['parent_id']) : $content->fresh(); + $result->load(['chapters', 'relations']); + return $result->toJson(); + } + + public function web(Request $request, $type, $name) { + $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->first(); + if ($tech) { + return view('app') + ->with('title', $tech->getTranslatedProperty('title')) + ->with('description', $tech->getTranslatedProperty('short')); + } + $url_map = [ + 'dungeon' => 'dungeons', + 'location' => 'locations', + 'mode' => 'modes', + 'ruleset' => 'rulesets', + 'tech' => 'tech', + ]; + $tech = Technique::where('name', '=', $name)->whereIn('type', array_keys($url_map))->first(); + if ($tech && isset($url_map[$tech->type])) { + return redirect('/'.$url_map[$tech->type].'/'.$tech->name); + } + throw new ModelNotFoundException(); + } + + private function applyLocalizedValues($validatedData, $content) { + foreach (['attribution', 'description', 'short', 'title'] as $name) { + if (isset($validatedData[$name])) { + $content->{$name} = $validatedData[$name]; + } + } + } + + private function getTranslation(Technique $content, $language) { + foreach ($content->translations as $translation) { + if ($translation->locale == $language) { + return $translation; + } + } + $translation = new TechniqueTranslation(); + $translation->technique_id = $content->id; + $translation->locale = $language; + return $translation; + } + protected function applyFilter(Request $request, Builder $query) { $validatedData = $request->validate([ 'phrase' => 'string|nullable',