|
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 int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $perPage = 10; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return Grid |
|
38
|
|
|
*/ |
|
39
|
|
|
abstract public function make(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param bool $multiple |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function render($multiple = false) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->make(); |
|
48
|
|
|
|
|
49
|
|
|
$this->appendRemoveBtn(); |
|
50
|
|
|
|
|
51
|
|
|
$this->paginate($this->perPage) |
|
|
|
|
|
|
52
|
|
|
->expandFilter() |
|
53
|
|
|
->disableExport() |
|
54
|
|
|
->disableActions() |
|
55
|
|
|
->disableBatchActions() |
|
56
|
|
|
->disableCreateButton() |
|
57
|
|
|
->disableColumnSelector() |
|
58
|
|
|
->disablePerPageSelector(); |
|
59
|
|
|
|
|
60
|
|
|
$displayer = $multiple ? Checkbox::class : Radio::class; |
|
61
|
|
|
|
|
62
|
|
|
$this->prependColumn('__modal_selector__', ' ') |
|
|
|
|
|
|
63
|
|
|
->displayUsing($displayer, [$this->key]); |
|
64
|
|
|
|
|
65
|
|
|
return $this->grid->render(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function renderFormGrid($values, $multiple = false) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->make(); |
|
71
|
|
|
|
|
72
|
|
|
$this->appendRemoveBtn(false); |
|
73
|
|
|
|
|
74
|
|
|
$this->model()->whereKey(Arr::wrap($values)); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$this->disableFilter() |
|
|
|
|
|
|
77
|
|
|
->disableExport() |
|
78
|
|
|
->disableActions() |
|
79
|
|
|
->disableBatchActions() |
|
80
|
|
|
->disableCreateButton() |
|
81
|
|
|
->disableColumnSelector() |
|
82
|
|
|
->disablePerPageSelector() |
|
83
|
|
|
; |
|
84
|
|
|
|
|
85
|
|
|
if (!$multiple) { |
|
86
|
|
|
$this->disablePagination(); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->tools(function (Tools $tools) { |
|
|
|
|
|
|
90
|
|
|
$tools->append(new Grid\Selectable\BrowserBtn()); |
|
|
|
|
|
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
return $this->grid; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function appendRemoveBtn($hide = true) |
|
97
|
|
|
{ |
|
98
|
|
|
$hide = $hide ? 'hide' : ''; |
|
99
|
|
|
$key = $this->key; |
|
100
|
|
|
|
|
101
|
|
|
$this->column('__remove__', ' ')->display(function () use ($hide, $key) { |
|
|
|
|
|
|
102
|
|
|
return <<<BTN |
|
103
|
|
|
<a href="javascript:void(0);" class="grid-row-remove {$hide}" data-key="{$this->getAttribute($key)}"> |
|
|
|
|
|
|
104
|
|
|
<i class="fa fa-trash"></i> |
|
105
|
|
|
</a> |
|
106
|
|
|
BTN; |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function initGrid() |
|
111
|
|
|
{ |
|
112
|
|
|
if (!class_exists($this->model) || !is_subclass_of($this->model, Model::class)) { |
|
|
|
|
|
|
113
|
|
|
throw new \InvalidArgumentException("Invalid model [{$this->model}]"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** @var Model $model */ |
|
117
|
|
|
$model = new $this->model; |
|
118
|
|
|
|
|
119
|
|
|
$this->grid = new Grid(new $model); |
|
120
|
|
|
|
|
121
|
|
|
if (!$this->key) { |
|
122
|
|
|
$this->key = $model->getKeyName(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $method |
|
128
|
|
|
* @param array $arguments |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function __call(string $method, array $arguments = []) |
|
132
|
|
|
{ |
|
133
|
|
|
if (is_null($this->grid)) { |
|
134
|
|
|
$this->initGrid(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->grid->{$method}(...$arguments); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: