1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Buttons |
6
|
|
|
{ |
7
|
|
|
// ------------ |
8
|
|
|
// BUTTONS |
9
|
|
|
// ------------ |
10
|
|
|
|
11
|
|
|
// TODO: $this->crud->reorderButtons('stack_name', ['one', 'two']); |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Add a button to the CRUD table view. |
15
|
|
|
* |
16
|
|
|
* @param string $stack Where should the button be visible? Options: top, line, bottom. |
17
|
|
|
* @param string $name The name of the button. Unique. |
18
|
|
|
* @param string $type Type of button: view or model_function. |
19
|
|
|
* @param string $content The HTML for the button. |
20
|
|
|
* @param bool|string $position Position on the stack: beginning or end. If false, the position will be |
21
|
|
|
* 'beginning' for the line stack or 'end' otherwise. |
22
|
|
|
* @param bool $replaceExisting True if a button with the same name on the given stack should be replaced. |
23
|
|
|
* @return \Backpack\CRUD\PanelTraits\CrudButton The new CRUD button. |
24
|
|
|
*/ |
25
|
159 |
|
public function addButton($stack, $name, $type, $content, $position = false, $replaceExisting = true) |
26
|
|
|
{ |
27
|
159 |
|
if ($position == false) { |
28
|
|
|
switch ($stack) { |
29
|
159 |
|
case 'line': |
30
|
1 |
|
$position = 'beginning'; |
31
|
1 |
|
break; |
32
|
|
|
|
33
|
|
|
default: |
34
|
159 |
|
$position = 'end'; |
35
|
159 |
|
break; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
159 |
|
if ($replaceExisting) { |
40
|
159 |
|
$this->removeButton($name, $stack); |
41
|
|
|
} |
42
|
|
|
|
43
|
159 |
|
$button = new CrudButton($stack, $name, $type, $content); |
44
|
|
|
switch ($position) { |
45
|
159 |
|
case 'beginning': |
46
|
2 |
|
$this->buttons->prepend($button); |
|
|
|
|
47
|
2 |
|
break; |
48
|
|
|
|
49
|
|
|
default: |
50
|
159 |
|
$this->buttons->push($button); |
51
|
159 |
|
break; |
52
|
|
|
} |
53
|
|
|
|
54
|
159 |
|
return $button; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function addButtonFromModelFunction($stack, $name, $model_function_name, $position = false) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->addButton($stack, $name, 'model_function', $model_function_name, $position); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function addButtonFromView($stack, $name, $view, $position = false) |
63
|
|
|
{ |
64
|
1 |
|
$view = 'vendor.backpack.crud.buttons.'.$view; |
65
|
|
|
|
66
|
1 |
|
$this->addButton($stack, $name, 'view', $view, $position); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
public function buttons() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
return $this->buttons; |
72
|
|
|
} |
73
|
|
|
|
74
|
159 |
|
public function initButtons() |
75
|
|
|
{ |
76
|
159 |
|
$this->buttons = collect(); |
77
|
|
|
|
78
|
|
|
// line stack |
79
|
159 |
|
$this->addButton('line', 'preview', 'view', 'crud::buttons.preview', 'end'); |
80
|
159 |
|
$this->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); |
81
|
159 |
|
$this->addButton('line', 'revisions', 'view', 'crud::buttons.revisions', 'end'); |
82
|
159 |
|
$this->addButton('line', 'delete', 'view', 'crud::buttons.delete', 'end'); |
83
|
|
|
|
84
|
|
|
// top stack |
85
|
159 |
|
$this->addButton('top', 'create', 'view', 'crud::buttons.create'); |
86
|
159 |
|
$this->addButton('top', 'reorder', 'view', 'crud::buttons.reorder'); |
87
|
159 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Remove a button from the CRUD panel. |
91
|
|
|
* |
92
|
|
|
* @param string $name Button name. |
93
|
|
|
* @param string $stack Optional stack name. |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
public function removeButton($name, $stack = null) |
96
|
|
|
{ |
97
|
159 |
|
$this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) { |
98
|
159 |
|
return $stack == null ? $button->name == $name : ($button->stack == $stack) && ($button->name == $name); |
|
|
|
|
99
|
159 |
|
}); |
100
|
159 |
|
} |
101
|
|
|
|
102
|
1 |
|
public function removeAllButtons() |
103
|
|
|
{ |
104
|
1 |
|
$this->buttons = collect([]); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
public function removeAllButtonsFromStack($stack) |
108
|
|
|
{ |
109
|
2 |
|
$this->buttons = $this->buttons->reject(function ($button) use ($stack) { |
110
|
2 |
|
return $button->stack == $stack; |
111
|
2 |
|
}); |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
public function removeButtonFromStack($name, $stack) |
115
|
|
|
{ |
116
|
3 |
|
$this->buttons = $this->buttons->reject(function ($button) use ($name, $stack) { |
117
|
3 |
|
return $button->name == $name && $button->stack == $stack; |
118
|
3 |
|
}); |
119
|
3 |
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
class CrudButton |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
public $stack; |
125
|
|
|
public $name; |
126
|
|
|
public $type = 'view'; |
127
|
|
|
public $content; |
128
|
|
|
|
129
|
159 |
|
public function __construct($stack, $name, $type, $content) |
130
|
|
|
{ |
131
|
159 |
|
$this->stack = $stack; |
132
|
159 |
|
$this->name = $name; |
133
|
159 |
|
$this->type = $type; |
134
|
159 |
|
$this->content = $content; |
135
|
159 |
|
} |
136
|
|
|
} |
137
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.