|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudButton; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
trait Buttons |
|
9
|
|
|
{ |
|
10
|
|
|
// ------------ |
|
11
|
|
|
// BUTTONS |
|
12
|
|
|
// ------------ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Order the CRUD buttons. If certain button names are missing from the given order array |
|
16
|
|
|
* they will be pushed to the end of the button collection. |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $stack Stack where the buttons belongs. Options: top, line, bottom. |
|
20
|
|
|
* @param array $order Ordered name of the buttons. ['update', 'delete', 'show'] |
|
21
|
|
|
*/ |
|
22
|
|
|
public function orderButtons(string $stack, array $order) |
|
23
|
|
|
{ |
|
24
|
|
|
$newButtons = collect([]); |
|
25
|
|
|
$otherButtons = collect([]); |
|
26
|
|
|
|
|
27
|
|
|
// we get the buttons that belong to the specified stack |
|
28
|
|
|
$stackButtons = $this->buttons()->reject(function ($item) use ($stack, $otherButtons) { |
|
29
|
|
|
if ($item->stack != $stack) { |
|
30
|
|
|
// if the button does not belong to this stack we just add it for merging later |
|
31
|
|
|
$otherButtons->push($item); |
|
32
|
|
|
|
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return false; |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
// we parse the ordered buttons |
|
40
|
|
|
collect($order)->each(function ($btnKey) use ($newButtons, $stackButtons) { |
|
41
|
|
|
if (! $button = $stackButtons->where('name', $btnKey)->first()) { |
|
42
|
|
|
abort(500, 'Button name [«'.$btnKey.'»] not found.'); |
|
43
|
|
|
} |
|
44
|
|
|
$newButtons->push($button); |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
// if the ordered buttons are less than the total number of buttons in the stack |
|
48
|
|
|
// we add the remaining buttons to the end of the ordered ones |
|
49
|
|
|
if (count($newButtons) < count($stackButtons)) { |
|
50
|
|
|
foreach ($stackButtons as $button) { |
|
51
|
|
|
if (! $newButtons->where('name', $button->name)->first()) { |
|
52
|
|
|
$newButtons->push($button); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->setOperationSetting('buttons', $newButtons->merge($otherButtons)); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Add a button to the CRUD table view. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $stack Where should the button be visible? Options: top, line, bottom. |
|
64
|
|
|
* @param string $name The name of the button. Unique. |
|
65
|
|
|
* @param string $type Type of button: view or model_function. |
|
66
|
|
|
* @param string $content The HTML for the button. |
|
67
|
|
|
* @param bool|string $position Position on the stack: beginning or end. If false, the position will be |
|
68
|
|
|
* 'beginning' for the line stack or 'end' otherwise. |
|
69
|
|
|
* @param bool $replaceExisting True if a button with the same name on the given stack should be replaced. |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Backpack\CRUD\app\Library\CrudPanel\CrudButton The new CRUD button. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true) |
|
74
|
|
|
{ |
|
75
|
|
|
if ($replaceExisting) { |
|
76
|
|
|
$this->removeButton($name, $stack); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new CrudButton($name, $stack, $type, $content, $position); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->addButton($stack, $name, 'model_function', $model_function_name, $position); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function addButtonFromView($stack, $name, $view, $position = false) |
|
88
|
|
|
{ |
|
89
|
|
|
$view = 'crud::buttons.'.$view; |
|
90
|
|
|
|
|
91
|
|
|
$this->addButton($stack, $name, 'view', $view, $position); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return Collection |
|
96
|
|
|
*/ |
|
97
|
|
|
public function buttons() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->getOperationSetting('buttons') ?? collect(); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Modify the attributes of a button. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $name The button name. |
|
106
|
|
|
* @param array $modifications The attributes and their new values. |
|
107
|
|
|
* |
|
108
|
|
|
* @return CrudButton The button that has suffered the changes, for daisychaining methods. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function modifyButton($name, $modifications = null) |
|
111
|
|
|
{ |
|
112
|
|
|
/** |
|
113
|
|
|
* @var CrudButton|null |
|
114
|
|
|
*/ |
|
115
|
|
|
$button = $this->buttons()->firstWhere('name', $name); |
|
116
|
|
|
|
|
117
|
|
|
if (! $button) { |
|
118
|
|
|
abort(500, 'CRUD Button "'.$name.'" not found. Please check the button exists before you modify it.'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (is_array($modifications)) { |
|
122
|
|
|
foreach ($modifications as $key => $value) { |
|
123
|
|
|
$button->{$key} = $value; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $button; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Remove a button from the CRUD panel. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $name Button name. |
|
134
|
|
|
* @param string $stack Optional stack name. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function removeButton($name, $stack = null) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->setOperationSetting('buttons', $this->buttons()->reject(function ($button) use ($name, $stack) { |
|
139
|
|
|
return $stack == null ? $button->name == $name : ($button->stack == $stack) && ($button->name == $name); |
|
|
|
|
|
|
140
|
|
|
})); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param array $names Button names |
|
145
|
|
|
* @param string|null $stack Optional stack name. |
|
146
|
|
|
*/ |
|
147
|
|
|
public function removeButtons($names, $stack = null) |
|
148
|
|
|
{ |
|
149
|
|
|
if (! empty($names)) { |
|
150
|
|
|
foreach ($names as $name) { |
|
151
|
|
|
$this->removeButton($name, $stack); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function removeAllButtons() |
|
157
|
|
|
{ |
|
158
|
|
|
$this->setOperationSetting('buttons', collect()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function removeAllButtonsFromStack($stack) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->setOperationSetting('buttons', $this->buttons()->reject(function ($button) use ($stack) { |
|
164
|
|
|
return $button->stack == $stack; |
|
165
|
|
|
})); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function removeButtonFromStack($name, $stack) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->setOperationSetting('buttons', $this->buttons()->reject(function ($button) use ($name, $stack) { |
|
171
|
|
|
return $button->name == $name && $button->stack == $stack; |
|
172
|
|
|
})); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Move the most recently added button before or after the given target button. Default is before. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string|array $target The target button name or array. |
|
179
|
|
|
* @param string|array $destination The destination button name or array. |
|
180
|
|
|
* @param bool $before If true, the button will be moved before the target button, otherwise it will be moved after it. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function moveButton($target, $where, $destination) |
|
183
|
|
|
{ |
|
184
|
|
|
$targetButton = $this->firstButtonWhere('name', $target); |
|
|
|
|
|
|
185
|
|
|
$destinationButton = $this->firstButtonWhere('name', $destination); |
|
186
|
|
|
$destinationKey = $this->getButtonKey($destination); |
|
187
|
|
|
$newDestinationKey = ($where == 'before' ? $destinationKey : $destinationKey + 1); |
|
188
|
|
|
$newButtons = $this->buttons()->filter(function ($value, $key) use ($target) { |
|
|
|
|
|
|
189
|
|
|
return $value->name != $target; |
|
190
|
|
|
}); |
|
191
|
|
|
|
|
192
|
|
|
if (! $targetButton) { |
|
193
|
|
|
return; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (! $destinationButton) { |
|
197
|
|
|
return; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$firstSlice = $newButtons->slice(0, $newDestinationKey); |
|
201
|
|
|
$lastSlice = $newButtons->slice($newDestinationKey, null); |
|
202
|
|
|
|
|
203
|
|
|
$newButtons = $firstSlice->push($targetButton); |
|
204
|
|
|
$lastSlice->each(function ($item, $key) use ($newButtons) { |
|
|
|
|
|
|
205
|
|
|
$newButtons->push($item); |
|
206
|
|
|
}); |
|
207
|
|
|
|
|
208
|
|
|
$this->setOperationSetting('buttons', $newButtons); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Check if a filter exists, by any given attribute. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $attribute Attribute name on that filter definition array. |
|
215
|
|
|
* @param string $value Value of that attribute on that filter definition array. |
|
216
|
|
|
* @return bool |
|
217
|
|
|
*/ |
|
218
|
|
|
public function hasButtonWhere($attribute, $value) |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->buttons()->contains($attribute, $value); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Get the first filter where a given attribute has the given value. |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $attribute Attribute name on that filter definition array. |
|
227
|
|
|
* @param string $value Value of that attribute on that filter definition array. |
|
228
|
|
|
* @return bool |
|
229
|
|
|
*/ |
|
230
|
|
|
public function firstButtonWhere($attribute, $value) |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->buttons()->firstWhere($attribute, $value); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function getButtonKey($buttonName) |
|
236
|
|
|
{ |
|
237
|
|
|
$array = $this->buttons()->toArray(); |
|
238
|
|
|
|
|
239
|
|
|
foreach ($array as $key => $value) { |
|
240
|
|
|
if ($value->name == $buttonName) { |
|
241
|
|
|
return $key; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Add a new button to the current CRUD operation. |
|
248
|
|
|
* |
|
249
|
|
|
* @param string|array $attributes Button name or array that contains name, stack, type and content. |
|
250
|
|
|
* @return \Backpack\CRUD\app\Library\CrudPanel\CrudButton |
|
251
|
|
|
*/ |
|
252
|
|
|
public function button($attributes = null) |
|
253
|
|
|
{ |
|
254
|
|
|
return new CrudButton($attributes); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Hide table buttons text. |
|
259
|
|
|
* |
|
260
|
|
|
* @param bool $value |
|
261
|
|
|
*/ |
|
262
|
|
|
public function hideTableButtonsText() |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->setOperationSetting('hideTableButtonsText', true); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Hide table buttons text. |
|
269
|
|
|
* |
|
270
|
|
|
* @param bool $value |
|
271
|
|
|
*/ |
|
272
|
|
|
public function showTableButtonsText() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->setOperationSetting('hideTableButtonsText', false); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Check if saved state is enabled for the table view. |
|
279
|
|
|
* |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getHideTableButtonsText() |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->getOperationSetting('hideTableButtonsText') ?? config('backpack.crud.operations.list.hideTableButtonsText'); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|