Passed
Push — master ( c64afd...d17d6b )
by Thomas
02:21
created

CustomButton::setDropUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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