|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
|
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
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $columnName = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param int $multiple |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function getLoadUrl($multiple = 0) |
|
30
|
|
|
{ |
|
31
|
|
|
$selectable = str_replace('\\', '_', $this->selectable); |
|
32
|
|
|
|
|
33
|
|
|
return route('admin.handle-selectable', compact('selectable', 'multiple')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
View Code Duplication |
public function addHtml() |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$trans = [ |
|
42
|
|
|
'choose' => admin_trans('admin.choose'), |
|
43
|
|
|
'cancal' => admin_trans('admin.cancel'), |
|
44
|
|
|
'submit' => admin_trans('admin.submit'), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
$html = <<<HTML |
|
48
|
|
|
<div class="modal fade" id="{$this->modalID}" tabindex="-1" role="dialog"> |
|
49
|
|
|
<div class="modal-dialog modal-lg" role="document"> |
|
50
|
|
|
<div class="modal-content" style="border-radius: 5px;"> |
|
51
|
|
|
<div class="modal-header"> |
|
52
|
|
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> |
|
53
|
|
|
<span aria-hidden="true">×</span> |
|
54
|
|
|
</button> |
|
55
|
|
|
<h4 class="modal-title">{$trans['choose']}</h4> |
|
56
|
|
|
</div> |
|
57
|
|
|
<div class="modal-body"> |
|
58
|
|
|
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> |
|
59
|
|
|
</div> |
|
60
|
|
|
<div class="modal-footer"> |
|
61
|
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">{$trans['cancal']}</button> |
|
62
|
|
|
<button type="button" class="btn btn-primary submit">{$trans['submit']}</button> |
|
63
|
|
|
</div> |
|
64
|
|
|
</div> |
|
65
|
|
|
</div> |
|
66
|
|
|
</div> |
|
67
|
|
|
HTML; |
|
68
|
|
|
Admin::html($html); |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function addStyle() |
|
77
|
|
|
{ |
|
78
|
|
|
$style = <<<STYLE |
|
79
|
|
|
#{$this->modalID} tr { |
|
80
|
|
|
cursor: pointer; |
|
81
|
|
|
} |
|
82
|
|
|
#{$this->modalID} .box { |
|
83
|
|
|
border-top: none; |
|
84
|
|
|
margin-bottom: 0; |
|
85
|
|
|
box-shadow: none; |
|
86
|
|
|
} |
|
87
|
|
|
STYLE; |
|
88
|
|
|
|
|
89
|
|
|
Admin::style($style); |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $selectable |
|
96
|
|
|
* @param string $column |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function display($selectable = null, $column = '') |
|
100
|
|
|
{ |
|
101
|
|
View Code Duplication |
if (!class_exists($selectable) || !is_subclass_of($selectable, Selectable::class)) { |
|
|
|
|
|
|
102
|
|
|
throw new \InvalidArgumentException( |
|
103
|
|
|
"[Class [{$selectable}] must be a sub class of Encore\Admin\Grid\Selectable" |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->columnName = $column ?: $this->getName(); |
|
|
|
|
|
|
108
|
|
|
$this->selectable = $selectable; |
|
109
|
|
|
$this->modalID = sprintf('modal-grid-selector-%s', $this->getClassName()); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$this->addHtml()->addScript()->addStyle(); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
return <<<HTML |
|
114
|
|
|
<span class="grid-selector" data-toggle="modal" data-target="#{$this->modalID}" key="{$this->getKey()}" data-val="{$this->getOriginalData()}"> |
|
|
|
|
|
|
115
|
|
|
<a href="javascript:void(0)" class="text-muted"> |
|
116
|
|
|
<i class="fa fa-check-square-o"></i> |
|
117
|
|
|
<span class="text">{$this->value}</span> |
|
|
|
|
|
|
118
|
|
|
</a> |
|
119
|
|
|
</span> |
|
120
|
|
|
HTML; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.