-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtons.php
387 lines (315 loc) · 8.6 KB
/
Buttons.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php declare(strict_types=1);
namespace dophp\buttons;
require_once 'Widgets.php';
/**
* Defines the button bar, a button collection
*/
class ButtonBar {
protected $_buttons = [];
/**
* Appends a button to this bar
*/
public function add(Button $btn) {
if (array_key_exists($btn->id, $this->_buttons))
throw new \InvalidArgumentException('Duplicate button ' . $btn->id);
$this->_buttons[$btn->id] = $btn;
}
/**
* Insert a button or an array of buttons at the beginning of this bar
*
* @param $btns Button/array: a single button or an array of Button
*/
public function insert($btns) {
if ($btns instanceof Button)
$btns = [ $btns ];
elseif (is_array($btns)) {
// Nothing
} else
throw new \InvalidArgumentException('Invalid $btns type');
$barr = [];
foreach ($btns as $b) {
if (array_key_exists($b->id, $this->_buttons))
throw new \InvalidArgumentException('Duplicate button ' . $b->id);
$barr[$b->id] = $b;
}
$this->_buttons = $barr + $this->_buttons;
}
/**
* Removes a button from bar
*
* @param $id string: ID of the button to be deleted
* @param $ignoreMissing bool: If true, do not trigger error when missing
* @return True if button has been found and removed
*/
public function del(string $id, bool $ignoreMissing=true) {
if( ! array_key_exists($id, $this->_buttons) ) {
if( $ignoreMissing )
return false;
throw new \UnexpectedValueException("Trying to remove missing button \"$id\"");
}
unset( $this->_buttons[$id] );
return true;
}
/**
* Returns buttons array
*/
public function buttons(): array {
return $this->_buttons;
}
/**
* Returns a signle button by ID
*/
public function button(string $id): Button {
return $this->_buttons[$id];
}
/**
* Enables the given button by ID
*/
public function enable(string $id) {
$this->_buttons[$id]->enable();
}
/**
* Disables the given button by ID
*/
public function disable(string $id) {
$this->_buttons[$id]->disable();
}
/**
* Shows the given button by ID
*/
public function show(string $id) {
$this->_buttons[$id]->show();
}
/**
* Hides the given button by ID
*/
public function hide(string $id) {
$this->_buttons[$id]->hide();
}
}
/**
* Defines a button for a ButtonBar
*/
abstract class Button extends \dophp\widgets\BaseWidget {
const DEFAULT_TYPE = 'button';
const DEFAULT_CLASS = 'btn-secondary';
/** True if this is a dropdown button */
const DROPDOWN = false;
public $id;
public $type;
public $class;
public $confirm = '';
public $icon;
public $label;
public $enabled = false;
public $hidden = false;
/**
* true if this button should be disabled in an editing context
* (i.e. edited and non-saved form)
*/
public $disabledOnFormDirty = true;
/**
* Constructs the button
*
* @param $id string: Unique button ID
* @param $label string: Button description for user
* @param $icon string: FA-icon
* @param $options array: array of optional options:
* - type: string button type, default 'button'
* - class: string button class, default 'btn-secondary'
* - confirm: A string to ask confirmation before proceeding
* - disabledOnFormDirty: boolean defining if the button is disabled
* when on a modified context (i.e. an unsaved form)
*/
public function __construct(string $id, string $label, string $icon, array $options=[]) {
if( isset($options['class']) && ! is_string($options['class']) )
throw new \UnexpectedValueException('class must be string');
if( isset($options['type']) && ! is_string($options['type']) )
throw new \UnexpectedValueException('type must be string');
parent::__construct();
$this->id = $id;
$this->label = $label;
$this->icon = $icon;
$this->type = $options['type'] ?? self::DEFAULT_TYPE;
$this->class = $options['class'] ?? self::DEFAULT_CLASS;
if( array_key_exists('disabledOnFormDirty', $options) )
$this->disabledOnFormDirty = $options['disabledOnFormDirty'];
}
public function enable() {
$this->enabled = true;
}
public function disable() {
$this->enabled = false;
}
public function hide() {
$this->hidden = true;
}
public function show() {
$this->hidden = false;
}
/**
* Returns true if this button is a dropdown
*/
public function isDropdown(): bool {
return static::DROPDOWN;
}
/**
* Returns button's PHP class name, all lowercase
*/
public function phpclass(): string {
$pts = explode('\\', strtolower(get_class($this)));
return end($pts);
}
/**
* Returns html data- attributes
*/
public function htmldata(): array {
return [
'confirm' => $this->confirm,
];
}
}
/**
* A button containing dropdown links
*/
class DropdownButton extends Button {
const DROPDOWN = true;
protected $_childs = [];
/**
* Appends a child link to this button
*/
public function add(DropdownButtonChild $child) {
if (array_key_exists($child->id, $this->_childs))
throw new \InvalidArgumentException('Duplicate child ' . $child->id);
$this->_childs[$child->id] = $child;
}
/**
* Returns childs array
*/
public function childs(): array {
return $this->_childs;
}
}
/**
* A child for a dropdown button
*/
class DropdownButtonChild extends \dophp\widgets\BaseWidget {
public $id;
public $label;
public $icon = null;
public $url = null;
public $post = null;
/**
* Constructs the child
*
* @param $id string: Unique button ID
* @param $label string: Button description for user
* @param $icon string: FA-icon
* @param $url string: The link URL
* @param $post array: If given, will be a POST button
*/
public function __construct(string $id, string $label, string $icon=null, string $url=null, $post=null) {
parent::__construct();
$this->id = $id;
$this->label = $label;
$this->icon = $icon;
$this->url = $url;
$this->post = $post;
}
}
/**
* The sandard submit button
*/
class SaveButton extends Button {
const DEFAULT_ID = 'save';
public $disabledOnFormDirty = false;
public function __construct(string $id=self::DEFAULT_ID, string $label=null,
string $icon='fa-floppy-o', array $options=['type'=>'submit']) {
if( $label === null )
$label = _('Save');
parent::__construct($id, $label, $icon, $options);
}
}
/**
* The standard form reset button
*/
class CancelButton extends Button {
const DEFAULT_ID = 'cancel';
public $disabledOnFormDirty = false;
public function __construct(string $id=self::DEFAULT_ID, string $label=null,
string $icon='fa-undo', array $options=[]) {
if( $label === null )
$label = _('Cancel');
parent::__construct($id, $label, $icon, $options);
}
}
/**
* The standard record delete button
*/
class DeleteButton extends Button {
const DEFAULT_ID = 'delete';
public function __construct(string $id=self::DEFAULT_ID, string $label=null,
string $icon='fa-trash', array $options=[]) {
if( $label === null )
$label = _('Delete');
if( ! isset($options['class']) )
$options['class'] = 'btn-danger';
parent::__construct($id, $label, $icon, $options);
}
}
/**
* A button to open a link
*/
class LinkButton extends Button {
public $url;
public $newtab;
/**
* @param $url string: The url to send the user to
* @param $options array: Array of options:
* - newtab: If true, opens the url in new tab
*/
public function __construct(string $id, string $label, string $icon, string $url, array $options=[]) {
parent::__construct($id, $label, $icon, $options);
$this->url = $url;
$this->newtab = isset($options['newtab']) ? (bool)$options['newtab'] : false;
}
public function htmldata(): array {
$data = parent::htmldata();
$data['url'] = $this->url;
$data['newtab'] = $this->newtab ? '1' : '';
return $data;
}
}
/**
* A button to open a link and POST to it
*/
class PostButton extends LinkButton {
/** POST data array */
public $post;
/** Opens a modal */
public $prompt = null;
/**
* @param $url string: The url to send the user to
* @param $post array: Post data array
* @param $options array: Array of options:
* - newtab: If true, opens the url in new tab
* - prompt: str, prompt message
*/
public function __construct(string $id, string $label, string $icon, string $url, array $post=[], array $options=[]) {
parent::__construct($id, $label, $icon, $url, $options);
$this->post = $post;
if( isset($options['prompt']) && $options['prompt'] )
$this->prompt = $options['prompt'];
}
public function htmldata(): array {
$data = parent::htmldata();
$data['post'] = json_encode($this->post);
$data['prompt'] = json_encode($this->prompt);
return $data;
}
}
/**
* A button handled by custom javascript in template code
*/
class JsButton extends Button {
}