1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Management; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\Morphables; |
9
|
|
|
use Thinktomorrow\Chief\Urls\UrlRecord; |
10
|
|
|
|
11
|
|
|
class Managers |
12
|
|
|
{ |
13
|
|
|
/** @var Register */ |
14
|
210 |
|
private $register; |
15
|
|
|
|
16
|
210 |
|
public function __construct(Register $register) |
17
|
210 |
|
{ |
18
|
|
|
$this->register = $register; |
19
|
202 |
|
} |
20
|
|
|
|
21
|
202 |
|
public function findByKey($key, $id = null): Manager |
22
|
|
|
{ |
23
|
202 |
|
$registration = $this->register->filterByKey($key)->first(); |
24
|
|
|
|
25
|
|
|
return $this->instance($registration, $id); |
26
|
11 |
|
} |
27
|
|
|
|
28
|
|
|
public function findByModel($model, $id = null): Manager |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* If an instance is passed as model, we try to extract the class and id |
32
|
11 |
|
* ourselves and assume we can read the id value via the id property |
33
|
11 |
|
*/ |
34
|
|
|
if (null === $id && !is_string($model)) { |
35
|
11 |
|
$modelClass = get_class($model); |
36
|
|
|
|
37
|
9 |
|
$registration = $this->register->filterByModel($modelClass)->first(); |
38
|
|
|
|
39
|
|
|
return $this->instanceModel($registration, $model); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$registration = $this->register->filterByModel($model)->first(); |
43
|
|
|
|
44
|
|
|
return $this->instance($registration, $id); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function findByUrl(string $slug, string $locale): Manager |
48
|
|
|
{ |
49
|
|
|
$urlRecord = UrlRecord::findBySlug($slug, $locale); |
50
|
|
|
|
51
|
25 |
|
$model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id); |
52
|
|
|
|
53
|
25 |
|
return $this->findByModel($model); |
54
|
|
|
} |
55
|
|
|
|
56
|
23 |
|
/** |
57
|
25 |
|
* Enlist all details for a certain tag group of managed models |
58
|
|
|
* |
59
|
|
|
* @param $tag |
60
|
|
|
* @return Collection of Managers |
61
|
|
|
*/ |
62
|
|
|
public function findByTag($tag): Collection |
63
|
|
|
{ |
64
|
|
|
$registrations = collect($this->register->filterByTag($tag)->all()); |
65
|
|
|
|
66
|
|
|
return $registrations->map(function ($registration) { |
|
|
|
|
67
|
|
|
return $this->instance($registration); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Enlist all details for a certain tag group of managed models |
73
|
|
|
* |
74
|
|
|
* @param $tag |
75
|
|
|
* @return Collection of ManagedModelDetails |
76
|
|
|
*/ |
77
|
|
|
public function findDetailsByTag($tag): Collection |
78
|
|
|
{ |
79
|
|
|
$registrations = collect($this->register->filterByTag($tag)->all()); |
80
|
|
|
|
81
|
|
|
return $registrations->map(function ($registration) { |
|
|
|
|
82
|
|
|
return $this->instance($registration)->details(); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function findByTagForSelect($tag) |
87
|
|
|
{ |
88
|
|
|
$managers = $this->findByTag($tag); |
89
|
|
|
//return array with group name and values |
90
|
|
|
$grouped = []; |
|
|
|
|
91
|
|
|
|
92
|
25 |
|
$managers = $managers->map(function (Manager $item) { |
93
|
|
|
return [ |
94
|
25 |
|
'id' => $item->details()->id, |
|
|
|
|
95
|
|
|
'group' => $item->details()->plural, |
|
|
|
|
96
|
|
|
]; |
97
|
25 |
|
})->toArray(); |
98
|
25 |
|
|
99
|
|
|
// We remove the group key as we need to have non-assoc array for the multiselect options. |
100
|
|
|
return collect(array_values($managers)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function all(): Collection |
104
|
|
|
{ |
105
|
|
|
$registrations = collect($this->register->all()); |
106
|
|
|
|
107
|
|
|
return $registrations->map(function ($registration) { |
|
|
|
|
108
|
|
|
return $this->instance($registration); |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
24 |
|
public function allAssistantClassNames(): Collection |
113
|
|
|
{ |
114
|
24 |
|
$assistants = collect(); |
115
|
|
|
|
116
|
|
|
foreach ($this->all() as $manager) { |
117
|
|
|
$assistants = $assistants->merge($manager->assistants(false)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $assistants->unique(); |
|
|
|
|
121
|
|
|
} |
122
|
202 |
|
|
123
|
|
|
public function anyRegisteredByTag($tag) |
124
|
202 |
|
{ |
125
|
|
|
return !empty($this->register->filterByTag($tag)->all()); |
126
|
202 |
|
} |
127
|
|
|
|
128
|
202 |
|
/** |
129
|
140 |
|
* @param $registration |
130
|
202 |
|
* @param $id |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
private function instance(Registration $registration, $id = null) |
134
|
|
|
{ |
135
|
|
|
$managerClass = $registration->class(); |
136
|
|
|
|
137
|
|
|
$manager = new $managerClass($registration); |
138
|
9 |
|
|
139
|
|
|
return $id |
140
|
9 |
|
? $manager->findManaged($id) |
141
|
|
|
: $manager; |
142
|
9 |
|
} |
143
|
|
|
|
144
|
9 |
|
/** |
145
|
|
|
* @param $registration |
146
|
|
|
* @param $model |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
private function instanceModel(Registration $registration, $model) |
150
|
|
|
{ |
151
|
|
|
$managerClass = $registration->class(); |
152
|
|
|
|
153
|
|
|
$manager = new $managerClass($registration); |
154
|
|
|
|
155
|
|
|
return $manager->manage($model); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|