1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Relations; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property string $child_type |
12
|
|
|
* @property string $child_id |
13
|
|
|
* @property string $parent_type |
14
|
|
|
* @property string $parent_id |
15
|
|
|
*/ |
16
|
|
|
class Relation extends Model |
17
|
|
|
{ |
18
|
|
|
public $timestamps = false; |
19
|
|
|
public $guarded = []; |
20
|
|
|
|
21
|
|
|
public static function find(string $parent_type, $parent_id, string $child_type, $child_id): ?Relation |
22
|
|
|
{ |
23
|
|
|
return static::query() |
24
|
|
|
->where('parent_type', $parent_type ) |
25
|
|
|
->where('parent_id', $parent_id ) |
26
|
|
|
->where('child_type', $child_type ) |
27
|
|
|
->where('child_id', $child_id ) |
28
|
|
|
->first(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function isOnline(): bool |
32
|
7 |
|
{ |
33
|
|
|
// Default is online, except explicitly set offline |
34
|
7 |
|
return ($this->online_status != 0); |
35
|
|
|
} |
36
|
|
|
|
37
|
34 |
|
public function isOffline(): bool |
38
|
|
|
{ |
39
|
34 |
|
return !$this->isOnline(); |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
/** |
43
|
|
|
* Set the keys for a save update query. |
44
|
7 |
|
* We override the default save setup since we do not have a primary key in relation table. |
45
|
7 |
|
* There should however always be the parent and child references so we can use |
46
|
7 |
|
* those to target the record in database. |
47
|
7 |
|
* |
48
|
|
|
* @param Builder $query |
49
|
|
|
* @return Builder |
50
|
6 |
|
*/ |
51
|
6 |
|
protected function setKeysForSaveQuery(Builder $query) |
52
|
6 |
|
{ |
53
|
7 |
|
$query->where('parent_type', $this->parent_type) |
54
|
|
|
->where('parent_id', $this->parent_id) |
55
|
|
|
->where('child_type', $this->child_type) |
56
|
65 |
|
->where('child_id', $this->child_id); |
57
|
|
|
|
58
|
65 |
|
return $query; |
59
|
65 |
|
} |
60
|
65 |
|
|
61
|
65 |
|
public function parent() |
62
|
|
|
{ |
63
|
|
|
return $this->morphTo('parent', 'parent_type', 'parent_id'); |
64
|
|
|
} |
65
|
|
|
|
66
|
33 |
|
public function child() |
67
|
|
|
{ |
68
|
|
|
return $this->morphTo('child', 'child_type', 'child_id'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function parents($child_type, $child_id) |
72
|
|
|
{ |
73
|
|
|
$relations = static::where('child_type', $child_type) |
74
|
|
|
->where('child_id', $child_id) |
75
|
|
|
->orderBy('sort', 'ASC') |
76
|
33 |
|
->get(); |
77
|
|
|
|
78
|
33 |
|
return $relations->map(function (Relation $relation) { |
79
|
65 |
|
$parent = $relation->parent; |
|
|
|
|
80
|
|
|
$parent->relation = $relation; |
81
|
|
|
|
82
|
|
|
return $parent; |
83
|
33 |
|
}); |
84
|
65 |
|
} |
85
|
65 |
|
|
86
|
|
|
public static function children($parent_type, $parent_id) |
87
|
|
|
{ |
88
|
2 |
|
$relations = static::where('parent_type', $parent_type) |
89
|
|
|
->where('parent_id', $parent_id) |
90
|
2 |
|
->with('child') |
91
|
2 |
|
->orderBy('sort', 'ASC') |
92
|
2 |
|
->get(); |
93
|
2 |
|
|
94
|
2 |
|
return $relations->map(function (Relation $relation) use ($parent_type, $parent_id) { |
|
|
|
|
95
|
|
|
|
96
|
|
|
// It could be that the child itself is archived, soft-deleted or removed. If this is the case, we will ignore it and move on. |
97
|
2 |
|
if (!$child = $relation->child) { |
|
|
|
|
98
|
|
|
return null; |
99
|
|
|
} |
100
|
2 |
|
|
101
|
2 |
|
$child->relation = $relation; |
102
|
|
|
|
103
|
2 |
|
return $child; |
104
|
2 |
|
}) |
105
|
2 |
|
|
106
|
|
|
// In case of soft-deleted entries, this will be null and should be ignored. We make sure that keys are reset in case of removed child |
107
|
2 |
|
->reject(function ($child) { |
108
|
1 |
|
return is_null($child); |
109
|
|
|
}) |
110
|
2 |
|
->values(); |
111
|
|
|
} |
112
|
2 |
|
|
113
|
|
|
public function delete() |
114
|
|
|
{ |
115
|
2 |
|
return static::where('parent_type', $this->parent_type) |
116
|
2 |
|
->where('parent_id', $this->parent_id) |
117
|
2 |
|
->where('child_type', $this->child_type) |
118
|
|
|
->where('child_id', $this->child_id) |
119
|
2 |
|
->delete(); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
2 |
|
public static function deleteRelationsOf($type, $id) |
123
|
|
|
{ |
124
|
2 |
|
$relations = static::where(function ($query) use ($type, $id) { |
125
|
|
|
return $query->where('parent_type', $type) |
126
|
|
|
->where('parent_id', $id); |
127
|
2 |
|
})->orWhere(function ($query) use ($type, $id) { |
128
|
2 |
|
return $query->where('child_type', $type) |
129
|
2 |
|
->where('child_id', $id); |
130
|
|
|
})->get(); |
131
|
2 |
|
|
132
|
|
|
foreach ($relations as $relation) { |
133
|
|
|
$relation->delete(); |
134
|
2 |
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function deleteAllChildRelationsOf($type, $id) |
138
|
|
|
{ |
139
|
|
|
$relations = static::where(function ($query) use ($type, $id) { |
140
|
|
|
return $query->where('parent_type', $type) |
141
|
|
|
->where('parent_id', $id); |
142
|
|
|
})->get(); |
143
|
|
|
|
144
|
|
|
foreach ($relations as $relation) { |
145
|
|
|
$relation->delete(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public static function deleteAllParentRelationsOf($type, $id) |
150
|
|
|
{ |
151
|
|
|
$relations = static::where(function ($query) use ($type, $id) { |
152
|
|
|
return $query->where('child_type', $type) |
153
|
|
|
->where('child_id', $id); |
154
|
|
|
})->get(); |
155
|
|
|
|
156
|
|
|
foreach ($relations as $relation) { |
157
|
|
|
$relation->delete(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|