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 showAs(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 null|\Illuminate\Contracts\View\View |
52
|
|
|
*/ |
53
|
|
|
protected function onIndex() |
54
|
|
|
{ |
55
|
|
|
$method = $this->id; |
56
|
|
|
|
57
|
|
|
if ($hasRelation = method_exists($this->model, $method)) { |
|
|
|
|
58
|
|
|
$relation = $this->model->{$method}; |
59
|
|
|
if ($relation) { |
60
|
|
|
$module = $this->firstWithModel($relation); |
61
|
|
|
$title = $relation->{$this->getColumn()}; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return [ |
|
|
|
|
66
|
|
|
'title' => $title ?? null, |
67
|
|
|
'relation' => $relation ?? null, |
68
|
|
|
'module' => $module ?? null, |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return null|\Illuminate\Contracts\View\View |
74
|
|
|
*/ |
75
|
|
|
protected function onView() |
76
|
|
|
{ |
77
|
|
|
return $this->onIndex(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return \Illuminate\Contracts\View\View |
82
|
|
|
*/ |
83
|
|
|
protected function onEdit() |
84
|
|
|
{ |
85
|
|
|
$method = $this->relation ?: $this->id; |
|
|
|
|
86
|
|
|
$hasRelation = method_exists($this->model, $method); |
87
|
|
|
|
88
|
|
|
if ($hasRelation && $relation = $this->model->{$method}()) { |
89
|
|
|
$related = $relation->getRelated(); |
90
|
|
|
|
91
|
|
|
if ($this->searchable) { |
92
|
|
|
$searchable = $this->firstWithModel($related); |
93
|
|
|
$attributes = [ |
94
|
|
|
'data-type' => 'livesearch', |
95
|
|
|
'data-url' => route('scaffold.search', ['searchable' => get_class($related)]), |
96
|
|
|
'placeholder' => 'Search...', |
97
|
|
|
]; |
98
|
|
|
$options = [$this->value()->getKey() => $this->value()->getAttribute($searchable::$title)]; |
99
|
|
|
} else { |
100
|
|
|
$options = $related::pluck($this->getColumn(), $related->getKeyName())->toArray(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return [ |
105
|
|
|
'name' => $relation ? $relation->getForeignKey() : $this->id(), |
|
|
|
|
106
|
|
|
'attributes' => $attributes ?? [], |
107
|
|
|
'options' => $options ?? [], |
108
|
|
|
'related' => $related ? get_class($related) : null, |
|
|
|
|
109
|
|
|
'searchable' => $searchable ?? null, |
110
|
|
|
'value' => $this->value()->getKey(), |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|