-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathTreeBuilder.php
More file actions
131 lines (110 loc) · 4.28 KB
/
TreeBuilder.php
File metadata and controls
131 lines (110 loc) · 4.28 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
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Statamic\Structures;
use Statamic\Contracts\Structures\Nav;
use Statamic\Facades\Action;
use Statamic\Facades\Entry;
use Statamic\Facades\Structure;
use Statamic\Facades\User;
use Statamic\Support\Arr;
use Statamic\Support\Str;
class TreeBuilder
{
public function build($params)
{
if ($params['structure'] instanceof \Statamic\Contracts\Structures\Structure) {
$structure = $params['structure'];
} elseif (! $structure = Structure::find($params['structure'])) {
return null;
}
$from = $params['from'] ?? null;
if ($from && $structure instanceof Nav) {
throw new \Exception('Cannot get a nested starting position on a navigation structure.');
}
if (! $tree = $structure->in($params['site'])) {
return null;
}
$tree->withEntries();
$entry = null;
if ($from && $from !== '/') {
if (! $entry = Entry::findByUri(Str::start($from, '/'), $params['site'])) {
return [];
}
}
if ($entry) {
$page = $tree->find($entry->id());
$pages = $page->pages()->all();
} else {
$pages = $tree->pages()
->prependParent(Arr::get($params, 'include_home'))
->all();
}
return $this->toTree($pages, $params);
}
protected function toTree($pages, $params, $depth = 1)
{
$maxDepth = $params['max_depth'] ?? null;
$fields = $params['fields'] ?? null;
$query = $params['query'] ?? null;
if ($maxDepth && $depth > $maxDepth) {
return [];
}
if ($query && empty($pages = $query->withItems($pages)->get())) {
return [];
}
return $pages->map(function ($page) use ($fields, $params, $depth) {
if ($page->reference() && ! $page->referenceExists()) {
return null;
}
return [
'page' => $page->selectedQueryColumns($fields),
'depth' => $depth,
'children' => $this->toTree($page->pages()->all(), $params, $depth + 1),
];
})->filter()->values()->all();
}
public function buildForController($params)
{
$tree = $this->build($params);
return $this->transformTreeForController($tree);
}
protected function transformTreeForController($tree)
{
return collect($tree)->map(function ($item) {
$page = $item['page'];
$collection = $page->mountedCollection();
$referenceExists = $page->referenceExists();
$entry = $referenceExists ? $page->entry() : null;
$actionContext = ['view' => 'tree'];
if ($collection) {
$actionContext['collection'] = $collection->handle();
}
if ($entry) {
$actionContext['site'] = $entry->locale();
}
return [
'id' => $page->id(),
'entry' => $page->reference(),
'title' => $page->hasCustomTitle() ? $page->title() : null,
'entry_title' => $entry ? $entry->value('title') : null,
'entry_blueprint' => $entry ? [
'handle' => $entry->blueprint()->handle(),
'title' => $entry->blueprint()->title(),
] : null,
'url' => $page->url(),
'edit_url' => $page->editUrl(),
'can_delete' => $entry ? User::current()->can('delete', $entry) : true,
'slug' => $page->slug(),
'status' => $referenceExists ? $page->status() : null,
'redirect' => $entry ? $entry->get('redirect') : null,
'actions' => $entry ? Action::for($entry, $actionContext) : [],
'collection' => ! $collection ? null : [
'handle' => $collection->handle(),
'title' => $collection->title(),
'edit_url' => $collection->showUrl(),
'create_url' => $collection->createEntryUrl(),
],
'children' => (! empty($item['children'])) ? $this->transformTreeForController($item['children']) : [],
];
})->values()->all();
}
}