1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Fluent; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
|
8
|
|
|
class Button extends Fluent implements Arrayable |
9
|
|
|
{ |
10
|
|
|
use HasAuthorizations; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Make a new button instance. |
14
|
|
|
* |
15
|
|
|
* @param string|array $options |
16
|
|
|
* @return Button |
17
|
|
|
*/ |
18
|
|
|
public static function make($options = []) |
19
|
|
|
{ |
20
|
|
|
if (is_string($options)) { |
21
|
|
|
return new static(['extend' => $options]); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return new static($options); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Make a raw button that does not extend anything. |
29
|
|
|
* |
30
|
|
|
* @param array $options |
31
|
|
|
* @return \Yajra\DataTables\Html\Button |
32
|
|
|
*/ |
33
|
|
|
public static function raw($options = []) |
34
|
|
|
{ |
35
|
|
|
if (is_string($options)) { |
36
|
|
|
return new static(['text' => $options]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return new static($options); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set extend option value. |
44
|
|
|
* |
45
|
|
|
* @param string $value |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function extend($value) |
49
|
|
|
{ |
50
|
|
|
$this->attributes['extend'] = $value; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set editor option value. |
57
|
|
|
* |
58
|
|
|
* @param string $value |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function editor($value) |
62
|
|
|
{ |
63
|
|
|
$this->attributes['editor'] = $value; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $buttons |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function buttons(array $buttons) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
foreach ($buttons as $key => $button) { |
75
|
|
|
if ($button instanceof Arrayable) { |
76
|
|
|
$buttons[$key] = $button->toArray(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->attributes['buttons'] = $buttons; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $buttons |
87
|
|
|
* @return $this |
88
|
|
|
* @see https://editor.datatables.net/examples/api/cancelButton |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function formButtons(array $buttons) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
foreach ($buttons as $key => $button) { |
93
|
|
|
if ($button instanceof Arrayable) { |
94
|
|
|
$buttons[$key] = $button->toArray(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->attributes['formButtons'] = $buttons; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set className option value. |
105
|
|
|
* |
106
|
|
|
* @param string $value |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function className($value) |
110
|
|
|
{ |
111
|
|
|
$this->attributes['className'] = $value; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set text option value. |
118
|
|
|
* |
119
|
|
|
* @param string $value |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function text($value) |
123
|
|
|
{ |
124
|
|
|
$this->attributes['text'] = $value; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set name option value. |
131
|
|
|
* |
132
|
|
|
* @param string $value |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function name($value) |
136
|
|
|
{ |
137
|
|
|
$this->attributes['name'] = $value; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set columns option value. |
144
|
|
|
* |
145
|
|
|
* @param mixed $value |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function columns($value) |
149
|
|
|
{ |
150
|
|
|
$this->attributes['columns'] = $value; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Set exportOptions option value. |
157
|
|
|
* |
158
|
|
|
* @param mixed $value |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function exportOptions($value) |
162
|
|
|
{ |
163
|
|
|
$this->attributes['exportOptions'] = $value; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set action to submit the form. |
170
|
|
|
* |
171
|
|
|
* @return \Yajra\DataTables\Html\Button |
172
|
|
|
*/ |
173
|
|
|
public function actionSubmit() |
174
|
|
|
{ |
175
|
|
|
return $this->action('function() { this.submit(); }'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Set action option value. |
180
|
|
|
* |
181
|
|
|
* @param string $value |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function action($value) |
185
|
|
|
{ |
186
|
|
|
$this->attributes['action'] = $value; |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Set editor class action handler. |
193
|
|
|
* |
194
|
|
|
* @param string $action |
195
|
|
|
* @return \Yajra\DataTables\Html\Button |
196
|
|
|
*/ |
197
|
|
|
public function actionHandler($action) |
198
|
|
|
{ |
199
|
|
|
return $this->action("function() { this.submit(null, null, function(data) { data.action = '{$action}'; return data; }) }"); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set action to close the form. |
204
|
|
|
* |
205
|
|
|
* @return \Yajra\DataTables\Html\Button |
206
|
|
|
*/ |
207
|
|
|
public function actionClose() |
208
|
|
|
{ |
209
|
|
|
return $this->action('function() { this.close(); }'); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
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.