Issues (66)

src/CustomButton.php (2 issues)

1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use SilverStripe\View\HTML;
6
7
/**
8
 * Common button functionnality that is shared between CustomAction and CustomLink
9
 */
10
trait CustomButton
11
{
12
    /**
13
     * Default classes applied in constructor
14
     * @config
15
     * @var array
16
     */
17
    private static $default_classes = [
18
        'btn',
19
        'btn-info'
20
    ];
21
22
    /**
23
     * Whether to place the button in a dot-menu
24
     * @var bool
25
     */
26
    protected $dropUp = false;
27
28
    /**
29
     * An icon for this button
30
     * @var string|null
31
     */
32
    protected $buttonIcon;
33
34
    /**
35
     * An icon using l-i element
36
     * @var array
37
     */
38
    protected $lastIcon = [];
39
40
    /**
41
     * The confirmation message
42
     * @var string
43
     */
44
    protected $confirmation;
45
46
    /**
47
     * Get the title of the link
48
     * Called by ActionsGridFieldItemRequest to build default message
49
     *
50
     * @return string
51
     */
52
    public function getTitle()
53
    {
54
        return $this->title;
55
    }
56
57
    /**
58
     * Set the value of title
59
     *
60
     * @param string $title
61
     * @return $this
62
     */
63
    public function setTitle($title)
64
    {
65
        $this->title = $title;
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
66
67
        return $this;
68
    }
69
70
    /**
71
     * Get the dropUp value
72
     * Called by ActionsGridFieldItemRequest to determine placement
73
     *
74
     * @return bool
75
     */
76
    public function getDropUp()
77
    {
78
        return $this->dropUp;
79
    }
80
81
    /**
82
     * Set the value of dropUp
83
     *
84
     * @param bool $is
85
     * @return $this
86
     */
87
    public function setDropUp($is)
88
    {
89
        $this->dropUp = !!$is;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $type
96
     * @return $this
97
     */
98
    public function setButtonType($type)
99
    {
100
        if ($this->extraClasses) {
101
            foreach ($this->extraClasses as $k => $v) {
102
                if (strpos($k, 'btn-') !== false) {
103
                    unset($this->extraClasses[$k]);
104
                }
105
            }
106
        }
107
108
        $btn = sprintf('btn-%s', $type);
109
        $this->extraClasses[$btn] = $btn;
0 ignored issues
show
Bug Best Practice introduced by
The property extraClasses does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get the title with icon if set
116
     *
117
     * @return string
118
     */
119
    protected function getButtonTitle()
120
    {
121
        return $this->title;
122
    }
123
124
    /**
125
     * Get an icon for this button
126
     *
127
     * @return string
128
     */
129
    public function getButtonIcon()
130
    {
131
        return $this->buttonIcon;
132
    }
133
134
    /**
135
     * Set an icon for this button
136
     *
137
     * Feel free to use SilverStripeIcons constants
138
     *
139
     * @param string $buttonIcon An icon for this button
140
     * @return $this
141
     */
142
    public function setButtonIcon(string $buttonIcon)
143
    {
144
        $this->buttonIcon = $buttonIcon;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Get an icon for this button
151
     *
152
     * @return array
153
     */
154
    public function getLastIcon()
155
    {
156
        return $this->lastIcon;
157
    }
158
159
    /**
160
     * Set an icon for this button
161
     *
162
     * @param string|array $lastIcon An icon for this button
163
     * @param string $set
164
     * @param string $type
165
     * @param string $size
166
     * @return $this
167
     */
168
    public function setLastIcon($lastIcon, $set = null, $type = null, $size = null)
169
    {
170
        if (is_string($lastIcon)) {
171
            $lastIcon = [
172
                'name' => $lastIcon
173
            ];
174
        }
175
        if ($set) {
176
            $lastIcon['set'] = $set;
177
        }
178
        if ($type) {
179
            $lastIcon['type'] = $type;
180
        }
181
        if ($size) {
182
            $lastIcon['size'] = $size;
183
        }
184
        $this->lastIcon = $lastIcon;
185
186
        return $this;
187
    }
188
189
    /**
190
     * @return boolean
191
     */
192
    public function hasLastIcon()
193
    {
194
        return !empty($this->lastIcon['name']);
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function renderLastIcon()
201
    {
202
        if (!$this->hasLastIcon()) {
203
            return '';
204
        }
205
        return HTML::createTag('l-i', $this->lastIcon);
206
    }
207
208
    /**
209
     * Get the value of confirmation
210
     * @return bool|string
211
     */
212
    public function getConfirmation()
213
    {
214
        return $this->confirmation;
215
    }
216
217
    /**
218
     * Set the value of confirmation
219
     *
220
     * @param string|bool $confirmation A confirm message or true for a generic message
221
     * @return $this
222
     */
223
    public function setConfirmation($confirmation)
224
    {
225
        if ($confirmation === true) {
226
            $confirmation = _t('CustomButton.CONFIRM_MESSAGE', 'Are you sure?');
227
        }
228
        $this->confirmation = $confirmation;
229
230
        return $this;
231
    }
232
}
233