Repository navigation
Expand file tree
/
Copy pathCollectionStructure.php
More file actions
156 lines (124 loc) · 4.43 KB
/
Copy pathCollectionStructure.php
File metadata and controls
156 lines (124 loc) · 4.43 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Statamic\Structures;
use Statamic\Contracts\Structures\CollectionTree;
use Statamic\Contracts\Structures\CollectionTreeRepository;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
class CollectionStructure extends Structure
{
protected $showSlugs = false;
public function title($title = null)
{
if (func_num_args() === 1) {
throw new \LogicException('Title cannot be set.');
}
return $this->collection()->title();
}
public function collection()
{
return Blink::once('collection-structure-collection-'.$this->handle(), function () {
return Collection::findByHandle($this->handle());
});
}
private function flattenedPages($entry)
{
return Blink::once('collection-structure-flattened-pages-collection'.$this->handle().'-'.$entry->locale(), function () use ($entry) {
return $this->in($entry->locale())->flattenedPages();
});
}
public function entryUri($entry)
{
if (! $this->route($entry->locale())) {
return null;
}
$page = $this->flattenedPages($entry)
->keyBy->reference()
->get($entry->id());
return optional($page)->uri();
}
public function collections($collections = null)
{
// return collect([$this->collection]);
}
public function route(string $site): ?string
{
return $this->collection()->route($site);
}
public function newTreeInstance()
{
return app(CollectionTree::class);
}
public function validateTree(array $tree, string $locale): array
{
parent::validateTree($tree, $locale);
$entryIds = $this->getEntryIdsFromTree($tree);
if ($entryId = $entryIds->duplicates()->first()) {
throw new \Exception("Duplicate entry [{$entryId}] in [{$this->collection()->handle()}] collection's structure.");
}
$thisCollectionsEntries = Blink::once('collection-structure-tree-entries::'.$this->handle().'::'.$locale, fn () => $this->collection()->queryEntries())
->where('site', $locale)->pluck('id');
$otherCollectionEntries = $entryIds->diff($thisCollectionsEntries);
if ($otherCollectionEntries->isNotEmpty()) {
$tree = $this->removeEntryReferencesFromTree($tree, $otherCollectionEntries);
}
$missingEntries = $thisCollectionsEntries->diff($entryIds)->map(function ($id) {
return ['entry' => $id];
})->values()->all();
return array_merge($tree, $missingEntries);
}
protected function getEntryIdsFromTree($tree)
{
return collect($tree)
->map(function ($item) {
return [
'entry' => $item['entry'] ?? null,
'children' => isset($item['children']) ? $this->getEntryIdsFromTree($item['children']) : null,
];
})
->flatten()
->filter();
}
protected function removeEntryReferencesFromTree($tree, $entries)
{
return collect($tree)
->reject(function ($branch) use ($entries) {
return $entries->contains($branch['entry']);
})
->map(function ($branch) use ($entries) {
if (isset($branch['children'])) {
$branch['children'] = $this->removeEntryReferencesFromTree($branch['children'], $entries);
}
return $branch;
})
->all();
}
public function save()
{
$this->collection()->structure($this)->save();
return true;
}
public function trees()
{
return $this->collection()->sites()->mapWithKeys(function ($site) {
return [$site => $this->in($site)];
});
}
public function in($site)
{
return Blink::once("collection-structure-tree-{$this->handle()}-{$site}", function () use ($site) {
$tree = app(CollectionTreeRepository::class)->find($this->collection()->handle(), $site);
if (! $tree && $this->existsIn($site)) {
$tree = $this->makeTree($site);
}
return $tree;
});
}
public function existsIn($site)
{
return $this->collection()->sites()->contains($site);
}
public function showSlugs($showSlugs = null)
{
return $this->fluentlyGetOrSet('showSlugs')->args(func_get_args());
}
}