-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGlobalVariablesController.php
More file actions
70 lines (51 loc) · 2.03 KB
/
Copy pathGlobalVariablesController.php
File metadata and controls
70 lines (51 loc) · 2.03 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
<?php
namespace Tv2regionerne\StatamicPrivateApi\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Statamic\Facades;
use Tv2regionerne\StatamicPrivateApi\Http\Resources\GlobalVariablesResource;
use Tv2regionerne\StatamicPrivateApi\Traits\VerifiesPrivateAPI;
class GlobalVariablesController extends ApiController
{
use VerifiesPrivateAPI;
public function show($global, ?string $site = null)
{
$global = $this->globalFromHandle($global);
$site = $site ? Facades\Site::get($site) : Facades\Site::default();
if (! $site) {
abort(404);
}
return GlobalVariablesResource::make($global->in($site->handle()));
}
public function update(Request $request, $handle, ?string $site = null)
{
$global = $this->globalFromHandle($handle);
$site = $site ? Facades\Site::get($site) : Facades\Site::default();
$set = $global->in($site->handle());
try {
$data = $this->show($handle, $site->handle())->toArray($request)['data'] ?? [];
$mergedData = collect($data)->merge($request->all());
$fields = $set->blueprint()->fields()->addValues($mergedData->toArray());
$fields->validate();
$values = $fields->process()->values();
if ($set->hasOrigin()) {
$values = $values->only($request->input('_localized'));
}
$set->data($values);
$set->save();
$global = $this->globalFromHandle($handle);
return GlobalVariablesResource::make($global->in($site->handle()));
} catch (ValidationException $e) {
return $this->returnValidationErrors($e);
}
}
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;
}
}