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