]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/TechniqueController.php
serverside tech init
[alttp.git] / app / Http / Controllers / TechniqueController.php
index 7173f1edce28ad515b364e79d2eeb98a454dead1..1d51aff846f6f48910be8ae82c0e5e0d5483838a 100644 (file)
@@ -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',