|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Relations; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
8
|
|
|
use Thinktomorrow\Chief\PageBuilder\PresentSections; |
|
9
|
|
|
|
|
10
|
|
|
trait ActingAsParent |
|
11
|
|
|
{ |
|
12
|
|
|
protected $loadedChildRelations; |
|
13
|
|
|
|
|
14
|
|
|
public function children(): Collection |
|
15
|
|
|
{ |
|
16
|
|
|
if ($this->areChildRelationsLoaded()) { |
|
17
|
|
|
return $this->loadedChildRelations; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
return $this->loadedChildRelations = $this->freshChildren(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function freshChildren(): Collection |
|
24
|
|
|
{ |
|
25
|
|
|
$this->loadedChildRelations = null; |
|
26
|
|
|
|
|
27
|
|
|
return new Collection(Relation::children($this->getMorphClass(), $this->getKey())->all()); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function adoptChild(ActsAsChild $child, array $attributes = []) |
|
31
|
|
|
{ |
|
32
|
|
|
// Reset cached relation |
|
33
|
|
|
$this->loadedChildRelations = null; |
|
34
|
|
|
|
|
35
|
|
|
$this->attachChild($child->getMorphClass(), $child->getKey(), $attributes); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function rejectChild(ActsAsChild $child) |
|
39
|
|
|
{ |
|
40
|
|
|
// Reset cached relation |
|
41
|
|
|
$this->loadedChildRelations = null; |
|
42
|
|
|
|
|
43
|
|
|
$this->detachChild($child->getMorphClass(), $child->getKey()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function renderChildren(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->presentChildren()->implode(''); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function presentChildren(): \Illuminate\Support\Collection |
|
52
|
|
|
{ |
|
53
|
|
|
return (new PresentSections())($this, $this->children()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function relationWithChild(ActsAsChild $child): Relation |
|
57
|
|
|
{ |
|
58
|
|
|
return Relation::query() |
|
|
|
|
|
|
59
|
|
|
->where('parent_type', $this->getMorphClass()) |
|
60
|
|
|
->where('parent_id', $this->getKey()) |
|
61
|
|
|
->where('child_type', $child->getMorphClass()) |
|
62
|
|
|
->where('child_id', $child->getKey()) |
|
63
|
|
|
->first(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function sortChild(ActsAsChild $child, $sort = 0) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->loadedChildRelations = null; |
|
69
|
|
|
|
|
70
|
|
|
Relation::query() |
|
71
|
|
|
->where('parent_type', $this->getMorphClass()) |
|
72
|
|
|
->where('parent_id', $this->getKey()) |
|
73
|
|
|
->where('child_type', $child->getMorphClass()) |
|
74
|
|
|
->where('child_id', $child->getKey()) |
|
75
|
|
|
->update(['sort' => $sort]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function detachAllChildRelations() |
|
79
|
|
|
{ |
|
80
|
|
|
Relation::deleteAllChildRelationsOf($this->getMorphClass(), $this->getKey()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function attachChild($child_type, $child_id, array $attributes = []) |
|
84
|
|
|
{ |
|
85
|
|
|
Relation::firstOrCreate([ |
|
86
|
|
|
'parent_type' => $this->getMorphClass(), |
|
87
|
|
|
'parent_id' => $this->getKey(), |
|
88
|
|
|
'child_type' => $child_type, |
|
89
|
|
|
'child_id' => $child_id, |
|
90
|
|
|
], $attributes); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function detachChild($child_type, $child_id) |
|
94
|
|
|
{ |
|
95
|
|
|
Relation::query() |
|
96
|
|
|
->where('parent_type', $this->getMorphClass()) |
|
97
|
|
|
->where('parent_id', $this->getKey()) |
|
98
|
|
|
->where('child_type', $child_type) |
|
99
|
|
|
->where('child_id', $child_id) |
|
100
|
|
|
->delete(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function areChildRelationsLoaded(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return !is_null($this->loadedChildRelations); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function breakChildRelationCache() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->loadedChildRelations = null; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|