-
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathNavigationTreeController.php
More file actions
118 lines (89 loc) · 3.23 KB
/
Copy pathNavigationTreeController.php
File metadata and controls
118 lines (89 loc) · 3.23 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
116
117
118
<?php
namespace Statamic\Http\Controllers\CP\Navigation;
use Facades\Statamic\Structures\BranchIdGenerator;
use Illuminate\Http\Request;
use Statamic\Facades\Nav;
use Statamic\Facades\Site;
use Statamic\Fields\Blueprint;
use Statamic\Http\Controllers\CP\CpController;
use Statamic\Structures\TreeBuilder;
use Statamic\Support\Arr;
use function Statamic\trans as __;
class NavigationTreeController extends CpController
{
private $data;
private $generatedIds = [];
public function index(Request $request, $handle)
{
$nav = Nav::find($handle);
$site = $request->site ?? Site::selected()->handle();
$this->authorize('view', $nav->in($site), __('You are not authorized to view navs.'));
$nav->in($site)->ensureBranchIds();
$pages = (new TreeBuilder)->buildForController([
'structure' => $nav,
'include_home' => true,
'site' => $site,
]);
return ['pages' => $pages];
}
public function update(Request $request, $nav)
{
$nav = Nav::find($nav);
$tree = $nav->in($request->site);
$this->authorize('edit', $tree);
$this->data = $this->flattenExistingBranchData([], $tree->tree());
$blueprint = $nav->blueprint()
->ensureField('title', ['type' => 'text'])
->ensureField('url', ['type' => 'text']);
$this->updateData($request->data, $blueprint);
$tree = $this->reorderTree($request->pages);
$saved = $nav->in($request->site)->tree($tree)->save();
return [
'generatedIds' => $this->generatedIds,
'saved' => $saved,
];
}
private function updateData(array $data, Blueprint $blueprint)
{
collect($data)->each(function ($branch, $id) use ($blueprint) {
$data = $blueprint->fields()
->addValues($branch['values'] ?? [])
->process()
->values()
->only($branch['localizedFields'] ?? []);
if ($branch['new'] ?? false) {
$newId = BranchIdGenerator::generate();
$this->generatedIds[$id] = $newId;
$id = $newId;
}
$this->data[$id] = Arr::removeNullValues([
'id' => $id,
'entry' => $branch['entry'] ?? null,
'title' => $data->pull('title'),
'url' => $data->pull('url'),
'data' => $data->all(),
]);
});
}
private function flattenExistingBranchData($data, $branches)
{
foreach ($branches as $branch) {
$data[$branch['id']] = Arr::except($branch, 'children');
if ($children = $branch['children'] ?? false) {
$data = $data + $this->flattenExistingBranchData($data, $children);
}
}
return $data;
}
private function reorderTree($tree)
{
return collect($tree)->map(function ($branch) {
$id = $this->generatedIds[$branch['id']] ?? $branch['id'];
$item = $this->data[$id];
if ($children = $branch['children']) {
$item['children'] = $this->reorderTree($children);
}
return $item;
})->all();
}
}