1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Displayers; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Actions\RowAction; |
6
|
|
|
use Encore\Admin\Admin; |
7
|
|
|
use Encore\Admin\Grid\Actions\Delete; |
8
|
|
|
use Encore\Admin\Grid\Actions\Edit; |
9
|
|
|
use Encore\Admin\Grid\Actions\Show; |
10
|
|
|
|
11
|
|
|
class DropdownActions extends Actions |
12
|
|
|
{ |
13
|
|
|
protected $view = 'admin::grid.actions.dropdown'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $custom = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $default = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $defaultClass = [Edit::class, Show::class, Delete::class]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param RowAction $action |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function add(RowAction $action) |
36
|
|
|
{ |
37
|
|
|
$this->prepareAction($action); |
38
|
|
|
|
39
|
|
|
array_push($this->custom, $action); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Prepend default `edit` `view` `delete` actions. |
46
|
|
|
*/ |
47
|
|
|
protected function prependDefaultActions() |
48
|
|
|
{ |
49
|
|
|
foreach ($this->defaultClass as $class) { |
50
|
|
|
/** @var RowAction $action */ |
51
|
|
|
$action = new $class(); |
52
|
|
|
|
53
|
|
|
$this->prepareAction($action); |
54
|
|
|
|
55
|
|
|
array_push($this->default, $action); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param RowAction $action |
61
|
|
|
*/ |
62
|
|
|
protected function prepareAction(RowAction $action) |
63
|
|
|
{ |
64
|
|
|
$action->setGrid($this->grid) |
65
|
|
|
->setColumn($this->column) |
66
|
|
|
->setRow($this->row); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Disable view action. |
71
|
|
|
* |
72
|
|
|
* @param bool $disable |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function disableView(bool $disable = true) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if ($disable) { |
79
|
|
|
array_delete($this->defaultClass, Show::class); |
80
|
|
|
} elseif (!in_array(Show::class, $this->defaultClass)) { |
81
|
|
|
array_push($this->defaultClass, Show::class); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Disable delete. |
89
|
|
|
* |
90
|
|
|
* @param bool $disable |
91
|
|
|
* |
92
|
|
|
* @return $this. |
|
|
|
|
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function disableDelete(bool $disable = true) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
if ($disable) { |
97
|
|
|
array_delete($this->defaultClass, Delete::class); |
98
|
|
|
} elseif (!in_array(Delete::class, $this->defaultClass)) { |
99
|
|
|
array_push($this->defaultClass, Delete::class); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Disable edit. |
107
|
|
|
* |
108
|
|
|
* @param bool $disable |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public function disableEdit(bool $disable = true) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
if ($disable) { |
115
|
|
|
array_delete($this->defaultClass, Edit::class); |
116
|
|
|
} elseif (!in_array(Edit::class, $this->defaultClass)) { |
117
|
|
|
array_push($this->defaultClass, Edit::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param null|\Closure $callback |
125
|
|
|
* |
126
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string |
127
|
|
|
*/ |
128
|
|
|
public function display($callback = null) |
129
|
|
|
{ |
130
|
|
|
if ($callback instanceof \Closure) { |
131
|
|
|
$callback->call($this, $this); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($this->disableAll) { |
135
|
|
|
return ''; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->prependDefaultActions(); |
139
|
|
|
|
140
|
|
|
$variables = [ |
141
|
|
|
'default' => $this->default, |
142
|
|
|
'custom' => $this->custom, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
if (empty($variables['default']) && empty($variables['custom'])) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return Admin::component($this->view, $variables); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.