Passed
Push — master ( 2fa425...8b71f9 )
by Thomas
02:16
created

PureModalAction::setButtonType()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 9.6111
1
<?php
2
3
namespace LeKoala\PureModal;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\DatalessField;
7
8
/**
9
 * Custom modal action
10
 * Requires cms-actions to work out of the box
11
 */
12
class PureModalAction extends DatalessField
13
{
14
    /**
15
     * Default classes applied in constructor by the FormField
16
     * @config
17
     * @var array
18
     */
19
    private static $default_classes = ["btn", "btn-info"];
0 ignored issues
show
introduced by
The private property $default_classes is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var FieldList
23
     */
24
    protected $fieldList;
25
26
    /**
27
     * A custom title for the dialog button
28
     * @var string
29
     */
30
    protected $dialogButtonTitle;
31
32
    /**
33
     * Should it show the dialog button
34
     * @var boolean
35
     */
36
    protected $showDialogButton = true;
37
38
    /**
39
     * An icon for this button
40
     * @var string
41
     */
42
    protected $buttonIcon;
43
44
    /**
45
     * @var boolean
46
     */
47
    protected $shouldRefresh = false;
48
49
    /**
50
     * Whether to place the button in a dot-menu.
51
     * @see https://github.com/lekoala/silverstripe-cms-actions
52
     * @var bool
53
     */
54
    protected $dropUp = false;
55
56
    public function __construct($name, $title)
57
    {
58
        $name = 'doCustomAction[' . $name . ']';
59
        $this->title = $title;
60
        $this->name = $name;
61
62
        parent::__construct($name, $title);
63
    }
64
65
    /**
66
     * Get the title with icon if set
67
     *
68
     * @return string
69
     */
70
    protected function getButtonTitle()
71
    {
72
        $title = $this->title;
73
        if ($this->buttonIcon) {
74
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
75
        }
76
        return $title;
77
    }
78
79
    /**
80
     * Get the dialog button title with icon if set
81
     *
82
     * @return string
83
     */
84
    protected function getDialogButtonTitle()
85
    {
86
        $title = $this->buttonTitle ?: $this->title;
87
        if ($this->buttonIcon) {
88
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
89
        }
90
        return $title;
91
    }
92
93
    /**
94
     * Set dialog button customised button title
95
     *
96
     * @return self
97
     */
98
    public function setDialogButtonTitle($value)
99
    {
100
        $this->buttonTitle = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property buttonTitle does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
101
        return $this;
102
    }
103
104
    /**
105
     * Get an icon for this button
106
     *
107
     * @return string
108
     */
109
    public function getButtonIcon()
110
    {
111
        return $this->buttonIcon;
112
    }
113
114
    /**
115
     * Set an icon for this button
116
     *
117
     * Feel free to use SilverStripeIcons constants
118
     *
119
     * @param string $buttonIcon An icon for this button
120
     * @return $this
121
     */
122
    public function setButtonIcon(string $buttonIcon)
123
    {
124
        $this->buttonIcon = $buttonIcon;
125
        return $this;
126
    }
127
128
    /**
129
     * Set a new type of btn-something. It will remove any existing btn- class
130
     * @param string $type Leave blank to simply remove default button type
131
     * @return $this
132
     */
133
    public function setButtonType($type = null)
134
    {
135
        if ($this->extraClasses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->extraClasses of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
136
            foreach ($this->extraClasses as $k => $v) {
137
                if (strpos($k, 'btn-') !== false) {
138
                    unset($this->extraClasses[$k]);
139
                }
140
            }
141
        }
142
        if ($type) {
143
            $btn = "btn-$type";
144
            $this->extraClasses[$btn] = $btn;
145
        }
146
        return $this;
147
    }
148
149
    /**
150
     * Get whether it must display the dialog button
151
     *
152
     * @return boolean
153
     */
154
    protected function getShowDialogButton()
155
    {
156
        return $this->showDialogButton;
157
    }
158
159
    /**
160
     * Set whether it must display the dialog button
161
     *
162
     * @return self
163
     */
164
    public function setShowDialogButton($value)
165
    {
166
        $this->showDialogButton = !!$value;
167
        return $this;
168
    }
169
170
    /**
171
     * Get the value of fieldList
172
     * @return FieldList
173
     */
174
    public function getFieldList()
175
    {
176
        return $this->fieldList;
177
    }
178
179
    /**
180
     * Set the value of fieldList
181
     *
182
     * @param FieldList $fieldList
183
     * @return $this
184
     */
185
    public function setFieldList(FieldList $fieldList)
186
    {
187
        $this->fieldList = $fieldList;
188
        foreach ($fieldList->dataFields() as $f) {
189
            $f->addExtraClass('no-change-track');
190
        }
191
        return $this;
192
    }
193
194
    /**
195
     * Get the dropUp value
196
     * Called by ActionsGridFieldItemRequest to determine placement
197
     *
198
     * @see https://github.com/lekoala/silverstripe-cms-actions
199
     * @return bool
200
     */
201
    public function getDropUp()
202
    {
203
        return $this->dropUp;
204
    }
205
206
    /**
207
     * Set the value of dropUp
208
     * You might want to call also setButtonType(null) for better styles
209
     *
210
     * @see https://github.com/lekoala/silverstripe-cms-actions
211
     * @param bool $is
212
     * @return $this
213
     */
214
    public function setDropUp($is)
215
    {
216
        $this->dropUp = !!$is;
217
        return $this;
218
    }
219
220
    /**
221
     * Required for cms-actions
222
     * @return string
223
     */
224
    public function actionName()
225
    {
226
        return rtrim(str_replace('doCustomAction[', '', $this->name), ']');
227
    }
228
229
    /**
230
     * Get the value of shouldRefresh
231
     * @return mixed
232
     */
233
    public function getShouldRefresh()
234
    {
235
        return $this->shouldRefresh;
236
    }
237
238
    /**
239
     * Set the value of shouldRefresh
240
     *
241
     * @param mixed $shouldRefresh
242
     * @return $this
243
     */
244
    public function setShouldRefresh($shouldRefresh)
245
    {
246
        $this->shouldRefresh = $shouldRefresh;
247
        return $this;
248
    }
249
250
    public function getModalID()
251
    {
252
        return 'modal_' . $this->name;
253
    }
254
255
    public function getTitle()
256
    {
257
        return $this->title;
258
    }
259
}
260