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\Http\Request;
11 class TechniqueController extends Controller
14 public function byType(Request $request, $type) {
15 $techs = Technique::where('index', true)->where('type', '=', $type);
16 $this->applyFilter($request, $techs);
17 return $techs->get()->toJson();
20 public function byTypeAndName(Request $request, $type, $name) {
21 $tech = Technique::where('type', '=', $type)->where('name', '=', $name)->firstOrFail();
22 $this->authorize('view', $tech);
23 $tech->load(['chapters', 'relations']);
24 return $tech->toJson();
27 public function forMap($map) {
28 $techs = TechniqueMap::with(['technique', 'technique.relations'])->where('map', '=', $map);
30 return $techs->get()->toJson();
33 public function search(Request $request) {
34 $techs = Technique::where('index', '=', 1);
35 $this->applyFilter($request, $techs);
36 return $techs->get()->toJson();
39 public function single(Request $request, Technique $tech) {
40 $this->authorize('view', $tech);
41 $tech->load(['chapters', 'relations']);
42 return $tech->toJson();
45 public function update(Request $request, Technique $content) {
46 $this->authorize('update', $content);
48 $validatedData = $request->validate([
49 'attribution' => 'string',
50 'description' => 'string',
51 'language' => 'string|in:de,en',
52 'parent_id' => 'integer|exists:App\\Models\\Technique,id',
57 if ($validatedData['language'] == 'en') {
58 $this->applyLocalizedValues($validatedData, $content);
61 $translation = $this->getTranslation($content, $validatedData['language']);
62 $this->applyLocalizedValues($validatedData, $translation);
66 $result = isset($validatedData['parent_id']) ? Technique::findOrFail($validatedData['parent_id']) : $content->fresh();
67 $result->load(['chapters', 'relations']);
68 return $result->toJson();
71 private function applyLocalizedValues($validatedData, $content) {
72 foreach (['attribution', 'description', 'short', 'title'] as $name) {
73 if (isset($validatedData[$name])) {
74 $content->{$name} = $validatedData[$name];
79 private function getTranslation(Technique $content, $language) {
80 foreach ($content->translations as $translation) {
81 if ($translation->locale == $language) {
85 $translation = new TechniqueTranslation();
86 $translation->technique_id = $content->id;
87 $translation->locale = $language;
91 protected function applyFilter(Request $request, Builder $query) {
92 $validatedData = $request->validate([
93 'phrase' => 'string|nullable',
94 'ruleset' => 'array|nullable',
95 'ruleset.competitive' => 'boolean',
96 'ruleset.mg' => 'boolean',
97 'ruleset.nl' => 'boolean',
98 'ruleset.owg' => 'boolean',
99 'type' => 'string|nullable',
102 if (!empty($validatedData['type'])) {
103 $query->where('type', '=', $validatedData['type']);
106 if (!empty($validatedData['phrase'])) {
107 $search = $validatedData['phrase'];
108 $query->where(function (Builder $query) use ($search) {
109 $query->where('title', 'LIKE', '%'.$search.'%')
110 ->orWhere('short', 'LIKE', '%'.$search.'%');
114 if (isset($validatedData['ruleset'])) {
115 $com = isset($validatedData['ruleset']['competitive']) && $validatedData['ruleset']['competitive'];
116 $owg = isset($validatedData['ruleset']['owg']) && $validatedData['ruleset']['owg'];
117 $mg = isset($validatedData['ruleset']['mg']) && $validatedData['ruleset']['mg'];
118 $nl = isset($validatedData['ruleset']['nl']) && $validatedData['ruleset']['nl'];
119 $any = $com || $owg || $mg || $nl;
120 $all = $com && $owg && $mg && $nl;
122 $query->where(function(Builder $query) use ($com, $owg, $mg, $nl) {
123 $query->whereNull('rulesets');
125 $query->orWhere('rulesets->competitive', '=', true);
128 $query->orWhere('rulesets->owg', '=', true);
131 $query->orWhere('rulesets->mg', '=', true);
134 $query->orWhere('rulesets->nl', '=', true);