|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Common\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class RelatedCollection extends Collection |
|
8
|
|
|
{ |
|
9
|
3 |
|
public static function availableChildren(ActsAsParent $parent): self |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
3 |
|
$available_children_types = config('thinktomorrow.chief.relations.children', []); |
|
12
|
|
|
|
|
13
|
3 |
|
$collection = new static(); |
|
14
|
|
|
|
|
15
|
3 |
|
foreach ($available_children_types as $type) { |
|
16
|
3 |
|
$collection = $collection->merge((new $type)->all()); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
3 |
|
return $collection; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public static function relationIds(Collection $collection): Collection |
|
23
|
|
|
{ |
|
24
|
|
|
return $collection->map(function ($entry) { |
|
25
|
|
|
return $entry->getRelationId(); |
|
26
|
|
|
}); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function flattenForSelect() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->map(function (ActsAsChild $child) { |
|
32
|
|
|
return [ |
|
33
|
1 |
|
'id' => $child->getRelationId(), |
|
34
|
1 |
|
'label' => $child->getRelationLabel(), |
|
35
|
1 |
|
'group' => $child->getRelationGroup(), |
|
36
|
|
|
]; |
|
37
|
2 |
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function flattenForGroupedSelect(): Collection |
|
41
|
|
|
{ |
|
42
|
1 |
|
$grouped = []; |
|
43
|
|
|
|
|
44
|
|
|
$this->flattenForSelect()->each(function ($entry) use (&$grouped) { |
|
45
|
|
|
if (isset($grouped[$entry['group']])) { |
|
46
|
|
|
$grouped[$entry['group']]['values'][] = $entry; |
|
47
|
|
|
} else { |
|
48
|
|
|
$grouped[$entry['group']] = ['group' => $entry['group'], 'values' => [$entry]]; |
|
49
|
|
|
} |
|
50
|
1 |
|
}); |
|
51
|
|
|
|
|
52
|
|
|
// We remove the group key as we need to have non-assoc array for the multiselect options. |
|
53
|
1 |
|
return collect(array_values($grouped)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public static function inflate(array $relateds = []): self |
|
57
|
|
|
{ |
|
58
|
2 |
|
if (count($relateds) == 1 && is_null(reset($relateds))) { |
|
59
|
|
|
$relateds = []; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return (new static($relateds))->map(function ($related) { |
|
63
|
1 |
|
list($type, $id) = explode('@', $related); |
|
64
|
1 |
|
return (new $type)->find($id); |
|
65
|
2 |
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.