-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTaxonomyTermsController.php
More file actions
115 lines (85 loc) · 3.31 KB
/
Copy pathTaxonomyTermsController.php
File metadata and controls
115 lines (85 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Tv2regionerne\StatamicPrivateApi\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Statamic\Facades;
use Statamic\Http\Controllers\CP\Taxonomies\TermsController as CpController;
use Statamic\Http\Resources\API\TermResource;
use Tv2regionerne\StatamicPrivateApi\Traits\VerifiesPrivateAPI;
class TaxonomyTermsController extends ApiController
{
use VerifiesPrivateAPI;
public function index($taxonomy)
{
$taxonomy = $this->taxonomyFromHandle($taxonomy);
$with = $taxonomy->termBlueprints()
->flatMap(fn ($blueprint) => $blueprint->fields()->all())
->filter->isRelationship()->keys()->all();
return app(TermResource::class)::collection(
$this->filterSortAndPaginate($taxonomy->queryTerms()->with($with))
);
}
public function show($taxonomy, $term)
{
$taxonomy = $this->taxonomyFromHandle($taxonomy);
$term = $this->termFromSlug($term, $taxonomy);
$this->abortIfInvalid($term, $taxonomy);
return app(TermResource::class)::make($term);
}
public function store(Request $request, $taxonomy)
{
$taxonomy = $this->taxonomyFromHandle($taxonomy);
try {
$response = (new CpController($request))->store($request, $taxonomy, Facades\Site::current());
return app(TermResource::class)::make(Facades\Term::find($response->id()));
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
}
public function update(Request $request, $taxonomyHandle, $termHandle)
{
$taxonomy = $this->taxonomyFromHandle($taxonomyHandle);
$term = $this->termFromSlug($termHandle, $taxonomy);
$this->abortIfInvalid($term, $taxonomy);
try {
$data = json_decode($this->show($taxonomyHandle, $termHandle)->toJson(), true);
$mergedData = collect($data)->merge($request->all());
$request->merge($mergedData->all());
(new CpController($request))->update($request, $taxonomy, $term, Facades\Site::current());
return app(TermResource::class)::make($term->fresh());
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
}
public function destroy(Request $request, $taxonomy, $term)
{
$taxonomy = $this->taxonomyFromHandle($taxonomy);
$term = $this->termFromSlug($term, $taxonomy);
$this->abortIfInvalid($term, $taxonomy);
$term->delete();
return response('', 204);
}
private function abortIfInvalid($term, $taxonomy)
{
if (! $term || $term->taxonomy()->handle() !== $taxonomy->handle()) {
abort(404);
}
}
private function taxonomyFromHandle($taxonomy)
{
$taxonomy = is_string($taxonomy) ? Facades\Taxonomy::find($taxonomy) : $taxonomy;
if (! $taxonomy) {
abort(404);
}
abort_if(! $this->resourcesAllowed('taxonomies', $taxonomy->handle()), 404);
return $taxonomy;
}
private function termFromSlug($term, $taxonomy)
{
$term = is_string($term) ? Facades\Term::find($taxonomy->handle().'::'.$term) : $term;
if (! $term) {
abort(404);
}
return $term;
}
}