3 namespace App\Http\Controllers;
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;
12 class TechniqueController extends Controller
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();
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();
28 public function forMap($map) {
29 $techs = TechniqueMap::with(['technique', 'technique.relations'])->where('map', '=', $map);
31 return $techs->get()->toJson();
34 public function search(Request $request) {
35 $techs = Technique::where('index', '=', 1);
36 $this->applyFilter($request, $techs);
37 return $techs->get()->toJson();
40 public function single(Request $request, Technique $tech) {
41 $this->authorize('view', $tech);
42 $tech->load(['chapters', 'relations']);
43 return $tech->toJson();
46 public function update(Request $request, Technique $content) {
47 $this->authorize('update', $content);
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',
58 if ($validatedData['language'] == 'en') {
59 $this->applyLocalizedValues($validatedData, $content);
62 $translation = $this->getTranslation($content, $validatedData['language']);
63 $this->applyLocalizedValues($validatedData, $translation);
67 $result = isset($validatedData['parent_id']) ? Technique::findOrFail($validatedData['parent_id']) : $content->fresh();
68 $result->load(['chapters', 'relations']);
69 return $result->toJson();
72 public function web(Request $request, $type, $name) {
73 $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->first();
76 ->with('title', $tech->getTranslatedProperty('title'))
77 ->with('description', $tech->getTranslatedProperty('short'));
79 $view = $view->with('image', url($tech->image));
80 } else if ($tech->gif) {
81 $view = $view->with('image', url($tech->gif));
86 'dungeon' => 'dungeons',
87 'location' => 'locations',
89 'ruleset' => 'rulesets',
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);
96 throw new ModelNotFoundException();
99 private function applyLocalizedValues($validatedData, $content) {
100 foreach (['attribution', 'description', 'short', 'title'] as $name) {
101 if (isset($validatedData[$name])) {
102 $content->{$name} = $validatedData[$name];
107 private function getTranslation(Technique $content, $language) {
108 foreach ($content->translations as $translation) {
109 if ($translation->locale == $language) {
113 $translation = new TechniqueTranslation();
114 $translation->technique_id = $content->id;
115 $translation->locale = $language;
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',
130 if (!empty($validatedData['type'])) {
131 $query->where('type', '=', $validatedData['type']);
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.'%');
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;
150 $query->where(function(Builder $query) use ($com, $owg, $mg, $nl) {
151 $query->whereNull('rulesets');
153 $query->orWhere('rulesets->competitive', '=', true);
156 $query->orWhere('rulesets->owg', '=', true);
159 $query->orWhere('rulesets->mg', '=', true);
162 $query->orWhere('rulesets->nl', '=', true);