1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Field; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\URL; |
6
|
|
|
use Illuminate\Support\Facades\View; |
7
|
|
|
use Terranet\Administrator\Field\Traits\WorksWithModules; |
8
|
|
|
use Terranet\Administrator\Scaffolding; |
9
|
|
|
|
10
|
|
|
class BelongsTo extends Generic |
11
|
|
|
{ |
12
|
|
|
use WorksWithModules; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $column = 'name'; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
protected $searchable = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $column |
22
|
|
|
* |
23
|
|
|
* @return self |
24
|
|
|
*/ |
25
|
|
|
public function useForTitle(string $column): self |
26
|
|
|
{ |
27
|
|
|
$this->column = $column; |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function getColumn() |
36
|
|
|
{ |
37
|
|
|
return $this->column; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function searchable() |
44
|
|
|
{ |
45
|
|
|
$this->searchable = true; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
protected function onIndex(): array |
54
|
|
|
{ |
55
|
|
|
if ($relation = $this->model->{$this->id}) { |
56
|
|
|
$title = $relation->{$this->getColumn()}; |
57
|
|
|
$module = $this->firstWithModel($relation); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
'title' => $title ?? null, |
62
|
|
|
'relation' => $relation ?? null, |
63
|
|
|
'module' => $module ?? null, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
protected function onView(): array |
71
|
|
|
{ |
72
|
|
|
return $this->onIndex(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
protected function onEdit(): array |
79
|
|
|
{ |
80
|
|
|
if (method_exists($this->model, $this->id)) { |
81
|
|
|
$relation = call_user_func([$this->model, $this->id]); |
82
|
|
|
$eloquent = $relation->getRelated(); |
83
|
|
|
$related = $this->model->{$this->id}; |
84
|
|
|
|
85
|
|
|
$model = $this->firstWithModel($eloquent); |
86
|
|
|
$titleColumn = $model ? $model::$title : $this->getColumn(); |
87
|
|
|
|
88
|
|
|
if ($this->searchable) { |
89
|
|
|
if ($value = $this->value()) { |
90
|
|
|
$options = [ |
91
|
|
|
$value->getKey() => $value->getAttribute($titleColumn), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$options = $eloquent::pluck($titleColumn, $eloquent->getKeyName())->toArray(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return [ |
100
|
|
|
'options' => $options ?? [], |
101
|
|
|
'related' => $related ? get_class($related) : null, |
|
|
|
|
102
|
|
|
'searchable' => $this->searchable, |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function name() |
110
|
|
|
{ |
111
|
|
|
return $this->model->{$this->id}()->getForeignKey(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|