X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FTechniqueController.php;h=1d51aff846f6f48910be8ae82c0e5e0d5483838a;hb=e4e7cbc7e4944b08d74f5752de337ba7700367f4;hp=b27465e5da1afe0819198cc88338e7f23ce80282;hpb=f17b9f3b6f7f9e678c681c719eea6fb5c41a387f;p=alttp.git diff --git a/app/Http/Controllers/TechniqueController.php b/app/Http/Controllers/TechniqueController.php index b27465e..1d51aff 100644 --- a/app/Http/Controllers/TechniqueController.php +++ b/app/Http/Controllers/TechniqueController.php @@ -4,12 +4,27 @@ 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 { + public function byType(Request $request, $type) { + $techs = Technique::where('index', true)->where('type', '=', $type); + $this->applyFilter($request, $techs); + return $techs->get()->toJson(); + } + + public function byTypeAndName(Request $request, $type, $name) { + $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->firstOrFail(); + $this->authorize('view', $tech); + $tech->load(['chapters', 'relations']); + return $tech->toJson(); + } + public function forMap($map) { $techs = TechniqueMap::with(['technique', 'technique.relations'])->where('map', '=', $map); @@ -17,6 +32,85 @@ class TechniqueController extends Controller } public function search(Request $request) { + $techs = Technique::where('index', '=', 1); + $this->applyFilter($request, $techs); + return $techs->get()->toJson(); + } + + public function single(Request $request, Technique $tech) { + $this->authorize('view', $tech); + $tech->load(['chapters', 'relations']); + 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', 'ruleset' => 'array|nullable', @@ -27,15 +121,13 @@ class TechniqueController extends Controller 'type' => 'string|nullable', ]); - $techs = Technique::where('index', '=', 1); - if (!empty($validatedData['type'])) { - $techs = $techs->where('type', '=', $validatedData['type']); + $query->where('type', '=', $validatedData['type']); } if (!empty($validatedData['phrase'])) { $search = $validatedData['phrase']; - $techs = $techs->where(function (Builder $query) use ($search) { + $query->where(function (Builder $query) use ($search) { $query->where('title', 'LIKE', '%'.$search.'%') ->orWhere('short', 'LIKE', '%'.$search.'%'); }); @@ -49,7 +141,7 @@ class TechniqueController extends Controller $any = $com || $owg || $mg || $nl; $all = $com && $owg && $mg && $nl; if ($any && !$all) { - $techs->where(function(Builder $query) use ($com, $owg, $mg, $nl) { + $query->where(function(Builder $query) use ($com, $owg, $mg, $nl) { $query->whereNull('rulesets'); if ($com) { $query->orWhere('rulesets->competitive', '=', true); @@ -66,14 +158,6 @@ class TechniqueController extends Controller }); } } - - return $techs->get()->toJson(); - } - - public function single(Request $request, Technique $tech) { - $this->authorize('view', $tech); - $tech->load(['chapters', 'relations']); - return $tech->toJson(); } }