|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
use Encore\Admin\Grid\Selectable; |
|
7
|
|
|
|
|
8
|
|
|
trait BelongsToRelation |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $modalID; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $selectable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* BelongsToRelation constructor. |
|
22
|
|
|
* @param string $column |
|
23
|
|
|
* @param array $arguments |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($column, $arguments = []) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->setSelectable($arguments[0]); |
|
28
|
|
|
|
|
29
|
|
|
parent::__construct($column, array_slice($arguments, 1)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $selectable |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function setSelectable($selectable) |
|
36
|
|
|
{ |
|
37
|
|
View Code Duplication |
if (!class_exists($selectable) || !is_subclass_of($selectable, Selectable::class)) { |
|
|
|
|
|
|
38
|
|
|
throw new \InvalidArgumentException( |
|
39
|
|
|
"[Class [{$selectable}] must be a sub class of Encore\Admin\Grid\Selectable" |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->selectable = $selectable; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getSelectable() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->selectable; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param int $multiple |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getLoadUrl($multiple = 0) |
|
59
|
|
|
{ |
|
60
|
|
|
$selectable = str_replace('\\', '_', $this->selectable); |
|
61
|
|
|
|
|
62
|
|
|
return route('admin.handle-selectable', compact('selectable', 'multiple')); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
View Code Duplication |
public function addHtml() |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$trans = [ |
|
71
|
|
|
'choose' => admin_trans('admin.choose'), |
|
72
|
|
|
'cancal' => admin_trans('admin.cancel'), |
|
73
|
|
|
'submit' => admin_trans('admin.submit'), |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
$html = <<<HTML |
|
77
|
|
|
<div class="modal fade" id="{$this->modalID}" tabindex="-1" role="dialog"> |
|
78
|
|
|
<div class="modal-dialog modal-lg" role="document"> |
|
79
|
|
|
<div class="modal-content" style="border-radius: 5px;"> |
|
80
|
|
|
<div class="modal-header"> |
|
81
|
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> |
|
82
|
|
|
<span aria-hidden="true">×</span> |
|
83
|
|
|
</button> |
|
84
|
|
|
<h4 class="modal-title">{$trans['choose']}</h4> |
|
85
|
|
|
</div> |
|
86
|
|
|
<div class="modal-body"> |
|
87
|
|
|
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> |
|
88
|
|
|
</div> |
|
89
|
|
|
<div class="modal-footer"> |
|
90
|
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">{$trans['cancal']}</button> |
|
91
|
|
|
<button type="button" class="btn btn-primary submit">{$trans['submit']}</button> |
|
92
|
|
|
</div> |
|
93
|
|
|
</div> |
|
94
|
|
|
</div> |
|
95
|
|
|
</div> |
|
96
|
|
|
HTML; |
|
97
|
|
|
Admin::html($html); |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return $this |
|
104
|
|
|
*/ |
|
105
|
|
|
public function addStyle() |
|
106
|
|
|
{ |
|
107
|
|
|
$style = <<<STYLE |
|
108
|
|
|
#{$this->modalID} tr { |
|
109
|
|
|
cursor: pointer; |
|
110
|
|
|
} |
|
111
|
|
|
#{$this->modalID} .box { |
|
112
|
|
|
border-top: none; |
|
113
|
|
|
margin-bottom: 0; |
|
114
|
|
|
box-shadow: none; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
.grid-table .empty-grid { |
|
118
|
|
|
padding: 20px !important; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
.grid-table .empty-grid svg { |
|
122
|
|
|
width: 60px !important; |
|
123
|
|
|
height: 60px !important; |
|
124
|
|
|
} |
|
125
|
|
|
.grid-box .box-footer { |
|
126
|
|
|
border-top: none !important; |
|
127
|
|
|
} |
|
128
|
|
|
STYLE; |
|
129
|
|
|
|
|
130
|
|
|
Admin::style($style); |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return \Encore\Admin\Grid |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function makeGrid() |
|
139
|
|
|
{ |
|
140
|
|
|
/** @var Selectable $selectable */ |
|
141
|
|
|
$selectable = new $this->selectable; |
|
142
|
|
|
|
|
143
|
|
|
return $selectable->renderFormGrid($this->value()); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* {@inheritdoc} |
|
148
|
|
|
*/ |
|
149
|
|
|
public function render() |
|
150
|
|
|
{ |
|
151
|
|
|
$this->modalID = sprintf('modal-selector-%s', $this->getElementClassString()); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$this->addScript()->addHtml()->addStyle(); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$this->addVariables([ |
|
|
|
|
|
|
156
|
|
|
'grid' => $this->makeGrid(), |
|
157
|
|
|
'options' => $this->getOptions(), |
|
|
|
|
|
|
158
|
|
|
]); |
|
159
|
|
|
|
|
160
|
|
|
return parent::fieldRender(); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|