1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
trait Buttons |
8
|
|
|
{ |
9
|
|
|
// ------------ |
10
|
|
|
// BUTTONS |
11
|
|
|
// ------------ |
12
|
|
|
|
13
|
|
|
// TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Add a button to the CRUD table view. |
17
|
|
|
* |
18
|
|
|
* @param string $stack Where should the button be visible? Options: top, line, bottom. |
19
|
|
|
* @param string $name The name of the button. Unique. |
20
|
|
|
* @param string $type Type of button: view or model_function. |
21
|
|
|
* @param string $content The HTML for the button. |
22
|
|
|
* @param bool|string $position Position on the stack: beginning or end. If false, the position will be |
23
|
|
|
* 'beginning' for the line stack or 'end' otherwise. |
24
|
|
|
* @param bool $replaceExisting True if a button with the same name on the given stack should be replaced. |
25
|
|
|
* |
26
|
|
|
* @return \Backpack\CRUD\PanelTraits\CrudButton The new CRUD button. |
27
|
|
|
*/ |
28
|
202 |
|
public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true) |
29
|
|
|
{ |
30
|
202 |
|
if ($position == false) { |
31
|
202 |
|
switch ($stack) { |
32
|
202 |
|
case 'line': |
33
|
1 |
|
$position = 'beginning'; |
34
|
1 |
|
break; |
35
|
|
|
|
36
|
|
|
default: |
37
|
202 |
|
$position = 'end'; |
38
|
202 |
|
break; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
202 |
|
if ($replaceExisting) { |
43
|
202 |
|
$this->removeButton($name, $stack); |
44
|
|
|
} |
45
|
|
|
|
46
|
202 |
|
$button = new CrudButton($stack, $name, $type, $content); |
47
|
202 |
|
switch ($position) { |
48
|
202 |
|
case 'beginning': |
49
|
2 |
|
$this->buttons->prepend($button); |
|
|
|
|
50
|
2 |
|
break; |
51
|
|
|
|
52
|
|
|
default: |
53
|
202 |
|
$this->buttons->push($button); |
54
|
202 |
|
break; |
55
|
|
|
} |
56
|
|
|
|
57
|
202 |
|
return $button; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false) |
61
|
|
|
{ |
62
|
|
|
$this->addButton($stack, $name, 'model_function', $model_function_name, $position); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function addButtonFromView($stack, $name, $view, $position = false) |
66
|
|
|
{ |
67
|
1 |
|
$view = 'crud::buttons.'.$view; |
68
|
|
|
|
69
|
1 |
|
$this->addButton($stack, $name, 'view', $view, $position); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Collection |
74
|
|
|
*/ |
75
|
|
|
public function buttons() |
76
|
|
|
{ |
77
|
|
|
return $this->buttons; |
78
|
|
|
} |
79
|
|
|
|
80
|
202 |
|
public function initButtons() |
81
|
|
|
{ |
82
|
202 |
|
$this->buttons = collect(); |
83
|
|
|
|
84
|
|
|
// line stack |
85
|
202 |
|
$this->addButton('line', 'show', 'view', 'crud::buttons.show', 'end'); |
86
|
202 |
|
$this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
87
|
202 |
|
$this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end'); |
88
|
202 |
|
$this->addButton('line', 'clone', 'view', 'crud::buttons.clone', 'end'); |
89
|
202 |
|
$this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end'); |
90
|
|
|
|
91
|
|
|
// top stack |
92
|
202 |
|
$this->addButton('top', 'create', 'view', 'crud::buttons.create'); |
93
|
202 |
|
$this->addButton('top', 'reorder', 'view', 'crud::buttons.reorder'); |
94
|
202 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Modify the attributes of a button. |
98
|
|
|
* |
99
|
|
|
* @param string $name The button name. |
100
|
|
|
* @param array $modifications The attributes and their new values. |
|
|
|
|
101
|
|
|
* |
102
|
|
|
* @return CrudButton The button that has suffered the changes, for daisychaining methods. |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function modifyButton($name, $modifications = null) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
/** |
107
|
|
|
* @var CrudButton|null |
108
|
|
|
*/ |
109
|
|
|
$button = $this->buttons()->firstWhere('name', $name); |
110
|
|
|
|
111
|
|
|
if (! $button) { |
112
|
|
|
abort(500, 'CRUD Button "'.$name.'" not found. Please check the button exists before you modify it.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (is_array($modifications)) { |
116
|
|
|
foreach ($modifications as $key => $value) { |
117
|
|
|
$button->{$key} = $value; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $button; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Remove a button from the CRUD panel. |
126
|
|
|
* |
127
|
|
|
* @param string $name Button name. |
128
|
|
|
* @param string $stack Optional stack name. |
|
|
|
|
129
|
|
|
*/ |
130
|
202 |
|
public function removeButton($name, $stack = null) |
131
|
|
|
{ |
132
|
|
|
$this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) { |
133
|
202 |
|
return $stack == null ? $button->name == $name : ($button->stack == $stack) && ($button->name == $name); |
|
|
|
|
134
|
202 |
|
}); |
135
|
202 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array $names Button names |
139
|
|
|
* @param string|null $stack Optional stack name. |
140
|
|
|
*/ |
141
|
2 |
|
public function removeButtons($names, $stack = null) |
142
|
|
|
{ |
143
|
2 |
|
if (! empty($names)) { |
144
|
2 |
|
foreach ($names as $name) { |
145
|
2 |
|
$this->removeButton($name, $stack); |
146
|
|
|
} |
147
|
|
|
} |
148
|
2 |
|
} |
149
|
|
|
|
150
|
1 |
|
public function removeAllButtons() |
151
|
|
|
{ |
152
|
1 |
|
$this->buttons = collect([]); |
153
|
1 |
|
} |
154
|
|
|
|
155
|
2 |
|
public function removeAllButtonsFromStack($stack) |
156
|
|
|
{ |
157
|
|
|
$this->buttons = $this->buttons->reject(function ($button) use ($stack) { |
158
|
2 |
|
return $button->stack == $stack; |
159
|
2 |
|
}); |
160
|
2 |
|
} |
161
|
|
|
|
162
|
3 |
|
public function removeButtonFromStack($name, $stack) |
163
|
|
|
{ |
164
|
|
|
$this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) { |
165
|
3 |
|
return $button->name == $name && $button->stack == $stack; |
166
|
3 |
|
}); |
167
|
3 |
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
class CrudButton |
171
|
|
|
{ |
172
|
|
|
public $stack; |
173
|
|
|
public $name; |
174
|
|
|
public $type = 'view'; |
175
|
|
|
public $content; |
176
|
|
|
|
177
|
202 |
|
public function __construct($stack, $name, $type, $content) |
178
|
|
|
{ |
179
|
202 |
|
$this->stack = $stack; |
180
|
202 |
|
$this->name = $name; |
181
|
202 |
|
$this->type = $type; |
182
|
202 |
|
$this->content = $content; |
183
|
202 |
|
} |
184
|
|
|
} |
185
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: