]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/TechniqueController.php
tech meta image
[alttp.git] / app / Http / Controllers / TechniqueController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Models\Technique;
6 use App\Models\TechniqueMap;
7 use App\Models\TechniqueTranslation;
8 use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Database\Eloquent\ModelNotFoundException;
10 use Illuminate\Http\Request;
11
12 class TechniqueController extends Controller
13 {
14
15         public function byType(Request $request, $type) {
16                 $techs = Technique::where('index', true)->where('type', '=', $type);
17                 $this->applyFilter($request, $techs);
18                 return $techs->get()->toJson();
19         }
20
21         public function byTypeAndName(Request $request, $type, $name) {
22                 $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->firstOrFail();
23                 $this->authorize('view', $tech);
24                 $tech->load(['chapters', 'relations']);
25                 return $tech->toJson();
26         }
27
28         public function forMap($map) {
29                 $techs = TechniqueMap::with(['technique', 'technique.relations'])->where('map', '=', $map);
30
31                 return $techs->get()->toJson();
32         }
33
34         public function search(Request $request) {
35                 $techs = Technique::where('index', '=', 1);
36                 $this->applyFilter($request, $techs);
37                 return $techs->get()->toJson();
38         }
39
40         public function single(Request $request, Technique $tech) {
41                 $this->authorize('view', $tech);
42                 $tech->load(['chapters', 'relations']);
43                 return $tech->toJson();
44         }
45
46         public function update(Request $request, Technique $content) {
47                 $this->authorize('update', $content);
48
49                 $validatedData = $request->validate([
50                         'attribution' => 'string',
51                         'description' => 'string',
52                         'language' => 'string|in:de,en',
53                         'parent_id' => 'integer|exists:App\\Models\\Technique,id',
54                         'short' => 'string',
55                         'title' => 'string',
56                 ]);
57
58                 if ($validatedData['language'] == 'en') {
59                         $this->applyLocalizedValues($validatedData, $content);
60                         $content->save();
61                 } else {
62                         $translation = $this->getTranslation($content, $validatedData['language']);
63                         $this->applyLocalizedValues($validatedData, $translation);
64                         $translation->save();
65                 }
66
67                 $result = isset($validatedData['parent_id']) ? Technique::findOrFail($validatedData['parent_id']) : $content->fresh();
68                 $result->load(['chapters', 'relations']);
69                 return $result->toJson();
70         }
71
72         public function web(Request $request, $type, $name) {
73                 $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->first();
74                 if ($tech) {
75                         $view = view('app')
76                                 ->with('title', $tech->getTranslatedProperty('title'))
77                                 ->with('description', $tech->getTranslatedProperty('short'));
78                         if ($tech->image) {
79                                 $view = $view->with('image', url($tech->image));
80                         } else if ($tech->gif) {
81                                 $view = $view->with('image', url($tech->gif));
82                         }
83                         return $view;
84                 }
85                 $url_map = [
86                         'dungeon' => 'dungeons',
87                         'location' => 'locations',
88                         'mode' => 'modes',
89                         'ruleset' => 'rulesets',
90                         'tech' => 'tech',
91                 ];
92                 $tech = Technique::where('name', '=', $name)->whereIn('type', array_keys($url_map))->first();
93                 if ($tech && isset($url_map[$tech->type])) {
94                         return redirect('/'.$url_map[$tech->type].'/'.$tech->name);
95                 }
96                 throw new ModelNotFoundException();
97         }
98
99         private function applyLocalizedValues($validatedData, $content) {
100                 foreach (['attribution', 'description', 'short', 'title'] as $name) {
101                         if (isset($validatedData[$name])) {
102                                 $content->{$name} = $validatedData[$name];
103                         }
104                 }
105         }
106
107         private function getTranslation(Technique $content, $language) {
108                 foreach ($content->translations as $translation) {
109                         if ($translation->locale == $language) {
110                                 return $translation;
111                         }
112                 }
113                 $translation = new TechniqueTranslation();
114                 $translation->technique_id = $content->id;
115                 $translation->locale = $language;
116                 return $translation;
117         }
118
119         protected function applyFilter(Request $request, Builder $query) {
120                 $validatedData = $request->validate([
121                         'phrase' => 'string|nullable',
122                         'ruleset' => 'array|nullable',
123                         'ruleset.competitive' => 'boolean',
124                         'ruleset.mg' => 'boolean',
125                         'ruleset.nl' => 'boolean',
126                         'ruleset.owg' => 'boolean',
127                         'type' => 'string|nullable',
128                 ]);
129
130                 if (!empty($validatedData['type'])) {
131                         $query->where('type', '=', $validatedData['type']);
132                 }
133
134                 if (!empty($validatedData['phrase'])) {
135                         $search = $validatedData['phrase'];
136                         $query->where(function (Builder $query) use ($search) {
137                                 $query->where('title', 'LIKE', '%'.$search.'%')
138                                 ->orWhere('short', 'LIKE', '%'.$search.'%');
139                         });
140                 }
141
142                 if (isset($validatedData['ruleset'])) {
143                         $com = isset($validatedData['ruleset']['competitive']) && $validatedData['ruleset']['competitive'];
144                         $owg = isset($validatedData['ruleset']['owg']) && $validatedData['ruleset']['owg'];
145                         $mg = isset($validatedData['ruleset']['mg']) && $validatedData['ruleset']['mg'];
146                         $nl = isset($validatedData['ruleset']['nl']) && $validatedData['ruleset']['nl'];
147                         $any = $com || $owg || $mg || $nl;
148                         $all = $com && $owg && $mg && $nl;
149                         if ($any && !$all) {
150                                 $query->where(function(Builder $query) use ($com, $owg, $mg, $nl) {
151                                         $query->whereNull('rulesets');
152                                         if ($com) {
153                                                 $query->orWhere('rulesets->competitive', '=', true);
154                                         }
155                                         if ($owg) {
156                                                 $query->orWhere('rulesets->owg', '=', true);
157                                         }
158                                         if ($mg) {
159                                                 $query->orWhere('rulesets->mg', '=', true);
160                                         }
161                                         if ($nl) {
162                                                 $query->orWhere('rulesets->nl', '=', true);
163                                         }
164                                 });
165                         }
166                 }
167         }
168
169 }