]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/TechniqueController.php
basic content editing
[alttp.git] / app / Http / Controllers / TechniqueController.php
index 8694c046de5e1dd1520158c883e55ef8486a52bd..59559a94a1883edb46c50c7f71e33cd33f87b072 100644 (file)
@@ -3,13 +3,92 @@
 namespace App\Http\Controllers;
 
 use App\Models\Technique;
+use App\Models\TechniqueMap;
+use App\Models\TechniqueTranslation;
 use Illuminate\Database\Eloquent\Builder;
 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);
+
+               return $techs->get()->toJson();
+       }
+
        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();
+       }
+
+       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',
@@ -20,15 +99,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.'%');
                        });
@@ -42,7 +119,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);
@@ -59,14 +136,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();
        }
 
 }