1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Common button functionnality that is shared between CustomAction and CustomLink |
7
|
|
|
*/ |
8
|
|
|
trait CustomButton |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Default classes applied in constructor |
12
|
|
|
* @config |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private static $default_classes = [ |
16
|
|
|
'btn', 'btn-info' |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An icon for this button |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $buttonIcon; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The confirmation message |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $confirmation; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the title of the link |
33
|
|
|
* Called by ActionsGridFieldItemRequest to build default message |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getTitle() |
38
|
|
|
{ |
39
|
|
|
return $this->title; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set the value of title |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setTitle($title) |
48
|
|
|
{ |
49
|
|
|
$this->title = $title; |
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $type |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function setButtonType($type) |
59
|
|
|
{ |
60
|
|
|
if ($this->extraClasses) { |
61
|
|
|
foreach ($this->extraClasses as $k => $v) { |
62
|
|
|
if (strpos($k, 'btn-') !== false) { |
63
|
|
|
unset($this->extraClasses[$k]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$btn = "btn-$type"; |
69
|
|
|
$this->extraClasses[$btn] = $btn; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the title with icon if set |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function getButtonTitle() |
78
|
|
|
{ |
79
|
|
|
$title = $this->title; |
80
|
|
|
if ($this->buttonIcon) { |
81
|
|
|
$title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title; |
82
|
|
|
} |
83
|
|
|
return $title; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get an icon for this button |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getButtonIcon() |
92
|
|
|
{ |
93
|
|
|
return $this->buttonIcon; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set an icon for this button |
98
|
|
|
* |
99
|
|
|
* Feel free to use SilverStripeIcons constants |
100
|
|
|
* |
101
|
|
|
* @param string $buttonIcon An icon for this button |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setButtonIcon(string $buttonIcon) |
105
|
|
|
{ |
106
|
|
|
$this->buttonIcon = $buttonIcon; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the value of confirmation |
112
|
|
|
*/ |
113
|
|
|
public function getConfirmation() |
114
|
|
|
{ |
115
|
|
|
return $this->confirmation; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the value of confirmation |
120
|
|
|
* |
121
|
|
|
* @param string|bool A confirm message or true for a generic message |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setConfirmation($confirmation) |
125
|
|
|
{ |
126
|
|
|
if ($confirmation === true) { |
127
|
|
|
$confirmation = _t('CustomButton.CONFIRM_MESSAGE', 'Are you sure?'); |
128
|
|
|
} |
129
|
|
|
$this->confirmation = $confirmation; |
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|