1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Field; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Illuminate\Database\Query\Builder; |
8
|
|
|
use Illuminate\Support\Facades\View; |
9
|
|
|
use Terranet\Administrator\Contracts\Module; |
10
|
|
|
use Terranet\Administrator\Field\Traits\HandlesRelation; |
11
|
|
|
use Terranet\Administrator\Field\Traits\WorksWithModules; |
12
|
|
|
use Terranet\Administrator\Modules\Faked; |
13
|
|
|
use Terranet\Administrator\Scaffolding; |
14
|
|
|
use Terranet\Administrator\Services\CrudActions; |
15
|
|
|
use Terranet\Administrator\Traits\Module\HasColumns; |
16
|
|
|
|
17
|
|
|
class HasMany extends Generic |
18
|
|
|
{ |
19
|
|
|
use WorksWithModules, HandlesRelation; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $icon = 'list-ul'; |
23
|
|
|
|
24
|
|
|
/** @var Builder */ |
25
|
|
|
protected $query; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Closure $query |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function withQuery(\Closure $query) |
33
|
|
|
{ |
34
|
|
|
$this->query = $query; |
|
|
|
|
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $icon |
41
|
|
|
* |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
|
|
public function setIcon(string $icon): self |
45
|
|
|
{ |
46
|
|
|
$this->icon = $icon; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected function onIndex(): array |
55
|
|
|
{ |
56
|
|
|
$relation = $this->relation(); |
57
|
|
|
$module = $this->firstWithModel($related = $relation->getRelated()); |
|
|
|
|
58
|
|
|
|
59
|
|
|
// apply a query |
60
|
|
|
if ($this->query) { |
61
|
|
|
$relation = call_user_func_array($this->query, [$relation]); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($module = $this->firstWithModel($related)) { |
65
|
|
|
$url = route('scaffold.view', [ |
66
|
|
|
'module' => $module->url(), |
67
|
|
|
$related->getKeyName() => $related->getKey(), |
68
|
|
|
$relation->getForeignKeyName() => $this->model->getKey(), |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return [ |
73
|
|
|
'icon' => $this->icon, |
74
|
|
|
'module' => $module, |
75
|
|
|
'count' => $relation->count(), |
76
|
|
|
'url' => $url ?? null, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function onView(): array |
84
|
|
|
{ |
85
|
|
|
$relation = $this->relation(); |
86
|
|
|
$related = $relation->getRelated(); |
87
|
|
|
|
88
|
|
|
if (!$module = $this->relationModule()) { |
89
|
|
|
// Build a runtime module |
90
|
|
|
$module = Faked::make($related); |
91
|
|
|
} |
92
|
|
|
$columns = $module->columns()->each->disableSorting(); |
93
|
|
|
$actions = $module->actionsManager(); |
94
|
|
|
|
95
|
|
|
return [ |
96
|
|
|
'module' => $module ?? null, |
97
|
|
|
'columns' => $columns ?? null, |
98
|
|
|
'actions' => $actions ?? null, |
99
|
|
|
'relation' => $relation ?? null, |
100
|
|
|
'items' => $relation ? $relation->getResults() : null, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function onEdit(): array |
108
|
|
|
{ |
109
|
|
|
$relation = $this->relation(); |
110
|
|
|
|
111
|
|
|
if (static::MODE_CHECKBOXES === $this->editMode && $this->completeList) { |
|
|
|
|
112
|
|
|
$values = $relation->getRelated()->all(); |
113
|
|
|
} else { |
114
|
|
|
$values = $this->value(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return [ |
118
|
|
|
'relation' => $relation, |
119
|
|
|
'searchable' => get_class($relation->getRelated()), |
120
|
|
|
'values' => $values, |
121
|
|
|
'completeList' => $this->completeList, |
122
|
|
|
'titleField' => $this->titleField, |
123
|
|
|
'editMode' => $this->editMode, |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..