-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGlobalsController.php
More file actions
92 lines (66 loc) · 2.45 KB
/
Copy pathGlobalsController.php
File metadata and controls
92 lines (66 loc) · 2.45 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
<?php
namespace Tv2regionerne\StatamicPrivateApi\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Statamic\Facades;
use Statamic\Http\Controllers\CP\Globals\GlobalsController as CpController;
use Statamic\Query\ItemQueryBuilder;
use Tv2regionerne\StatamicPrivateApi\Http\Resources\GlobalResource;
use Tv2regionerne\StatamicPrivateApi\Traits\VerifiesPrivateAPI;
class GlobalsController extends ApiController
{
use VerifiesPrivateAPI;
public function index()
{
abort_if(! $this->resourcesAllowed('globals', ''), 404);
$query = (new ItemQueryBuilder)->withItems(Facades\GlobalSet::all());
return GlobalResource::collection(
$this->filterSortAndPaginate($query)
);
}
public function store(Request $request)
{
abort_if(! $this->resourcesAllowed('globals', ''), 404);
try {
(new CpController($request))->store($request);
$global = $this->globalFromHandle($request->input('handle'));
return GlobalResource::make($global);
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
}
public function show($global)
{
$global = $this->globalFromHandle($global);
return GlobalResource::make($global);
}
public function update(Request $request, $handle)
{
$global = $this->globalFromHandle($handle);
try {
$mergedData = collect($this->show($handle)->toArray($request))->merge($request->all());
$request->merge($mergedData->all());
(new CpController($request))->update($request, $global->handle());
$global = $this->globalFromHandle($handle);
return GlobalResource::make($global);
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
}
public function destroy(Request $request, $global)
{
$global = $this->globalFromHandle($global);
abort_if(! $this->resourcesAllowed('globals', $global->handle()), 404);
$global->delete();
return response('', 204);
}
private function globalFromHandle($global)
{
$global = is_string($global) ? Facades\GlobalSet::find($global) : $global;
if (! $global) {
abort(404);
}
abort_if(! $this->resourcesAllowed('globals', $global->handle()), 404);
return $global;
}
}