1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Grid; |
6
|
|
|
use Encore\Admin\Grid\Selectable\Checkbox; |
7
|
|
|
use Encore\Admin\Grid\Selectable\Radio; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @mixin Grid |
13
|
|
|
*/ |
14
|
|
|
abstract class Selectable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $model; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Grid |
23
|
|
|
*/ |
24
|
|
|
protected $grid; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $key = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $multiple = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $perPage = 10; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Selectable constructor. |
43
|
|
|
* @param $key |
44
|
|
|
* @param $multiple |
45
|
|
|
*/ |
46
|
|
|
public function __construct($multiple = false, $key = '') |
47
|
|
|
{ |
48
|
|
|
$this->key = $key; |
49
|
|
|
$this->multiple = $multiple; |
50
|
|
|
|
51
|
|
|
$this->initGrid(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return Grid |
56
|
|
|
*/ |
57
|
|
|
abstract public function make(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param bool $multiple |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function render() |
65
|
|
|
{ |
66
|
|
|
$this->make(); |
67
|
|
|
|
68
|
|
|
$this->paginate($this->perPage) |
|
|
|
|
69
|
|
|
->expandFilter() |
70
|
|
|
->disableExport() |
71
|
|
|
->disableActions() |
72
|
|
|
->disableBatchActions() |
73
|
|
|
->disableCreateButton() |
74
|
|
|
->disableColumnSelector() |
75
|
|
|
->disablePerPageSelector(); |
76
|
|
|
|
77
|
|
|
$displayer = $this->multiple ? Checkbox::class : Radio::class; |
78
|
|
|
|
79
|
|
|
$this->prependColumn('__modal_selector__', ' ') |
|
|
|
|
80
|
|
|
->displayUsing($displayer, [$this->key]); |
81
|
|
|
|
82
|
|
|
return $this->grid->render(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function renderFormGrid($values) |
86
|
|
|
{ |
87
|
|
|
$this->make(); |
88
|
|
|
|
89
|
|
|
$this->appendRemoveBtn(false); |
90
|
|
|
|
91
|
|
|
$this->model()->whereKey(Arr::wrap($values)); |
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->disableFilter() |
|
|
|
|
94
|
|
|
->disableExport() |
95
|
|
|
->disableActions() |
96
|
|
|
->disableBatchActions() |
97
|
|
|
->disableCreateButton() |
98
|
|
|
->disableColumnSelector() |
99
|
|
|
->disablePerPageSelector(); |
100
|
|
|
|
101
|
|
|
if (!$this->multiple) { |
102
|
|
|
$this->disablePagination(); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->tools(function (Tools $tools) { |
|
|
|
|
106
|
|
|
$tools->append(new Grid\Selectable\BrowserBtn()); |
|
|
|
|
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
return $this->grid; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function appendRemoveBtn($hide = true) |
113
|
|
|
{ |
114
|
|
|
$hide = $hide ? 'hide' : ''; |
115
|
|
|
$key = $this->key; |
116
|
|
|
|
117
|
|
|
$this->column('__remove__', ' ')->display(function () use ($hide, $key) { |
|
|
|
|
118
|
|
|
return <<<BTN |
119
|
|
|
<a href="javascript:void(0);" class="grid-row-remove {$hide}" data-key="{$this->getAttribute($key)}"> |
|
|
|
|
120
|
|
|
<i class="fa fa-trash"></i> |
121
|
|
|
</a> |
122
|
|
|
BTN; |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function initGrid() |
127
|
|
|
{ |
128
|
|
|
if (!class_exists($this->model) || !is_subclass_of($this->model, Model::class)) { |
|
|
|
|
129
|
|
|
throw new \InvalidArgumentException("Invalid model [{$this->model}]"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @var Model $model */ |
133
|
|
|
$model = new $this->model(); |
134
|
|
|
|
135
|
|
|
$this->grid = new Grid(new $model()); |
136
|
|
|
|
137
|
|
|
if (!$this->key) { |
138
|
|
|
$this->key = $model->getKeyName(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $method |
144
|
|
|
* @param array $arguments |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function __call(string $method, array $arguments = []) |
149
|
|
|
{ |
150
|
|
|
return $this->grid->{$method}(...$arguments); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: