Passed
Push — master ( a7ad57...b00550 )
by Thomas
03:38 queued 14s
created

PureModalAction::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
    /**
57
     * @var boolean
58
     */
59
    protected $fillHeight = true;
60
61
    public function __construct($name, $title)
62
    {
63
        $name = 'doCustomAction[' . $name . ']';
64
        $this->title = $title;
65
        $this->name = $name;
66
67
        parent::__construct($name, $title);
68
    }
69
70
    public function getAttributes()
71
    {
72
        $attrs = [];
73
        // Move modal to body to avoid nesting issues
74
        $attrs['onclick'] = PureModal::getMoveModalScript();
75
        return $attrs;
76
    }
77
78
    /**
79
     * Get the title with icon if set
80
     *
81
     * @return string
82
     */
83
    protected function getButtonTitle()
84
    {
85
        $title = $this->title;
86
        if ($this->buttonIcon) {
87
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
88
        }
89
        return $title;
90
    }
91
92
    /**
93
     * Get the dialog button title with icon if set
94
     *
95
     * @return string
96
     */
97
    protected function getDialogButtonTitle()
98
    {
99
        $title = $this->dialogButtonTitle ?: $this->title;
100
        if ($this->buttonIcon) {
101
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
102
        }
103
        return $title;
104
    }
105
106
    /**
107
     * Set dialog button customised button title
108
     *
109
     * @return self
110
     */
111
    public function setDialogButtonTitle($value)
112
    {
113
        $this->dialogButtonTitle = $value;
114
        return $this;
115
    }
116
117
    /**
118
     * Get an icon for this button
119
     *
120
     * @return string
121
     */
122
    public function getButtonIcon()
123
    {
124
        return $this->buttonIcon;
125
    }
126
127
    /**
128
     * Set an icon for this button
129
     *
130
     * Feel free to use SilverStripeIcons constants
131
     *
132
     * @param string $buttonIcon An icon for this button
133
     * @return $this
134
     */
135
    public function setButtonIcon(string $buttonIcon)
136
    {
137
        $this->buttonIcon = $buttonIcon;
138
        return $this;
139
    }
140
141
    /**
142
     * Set a new type of btn-something. It will remove any existing btn- class
143
     * @param string $type Leave blank to simply remove default button type
144
     * @return $this
145
     */
146
    public function setButtonType($type = null)
147
    {
148
        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...
149
            foreach ($this->extraClasses as $k => $v) {
150
                if (strpos($k, 'btn-') !== false) {
151
                    unset($this->extraClasses[$k]);
152
                }
153
            }
154
        }
155
        if ($type) {
156
            $btn = "btn-$type";
157
            $this->extraClasses[$btn] = $btn;
158
        }
159
        return $this;
160
    }
161
162
    /**
163
     * Get whether it must display the dialog button
164
     *
165
     * @return boolean
166
     */
167
    protected function getShowDialogButton()
168
    {
169
        return $this->showDialogButton;
170
    }
171
172
    /**
173
     * Set whether it must display the dialog button
174
     *
175
     * @return self
176
     */
177
    public function setShowDialogButton($value)
178
    {
179
        $this->showDialogButton = !!$value;
180
        return $this;
181
    }
182
183
    /**
184
     * Get the value of fieldList
185
     * @return FieldList
186
     */
187
    public function getFieldList()
188
    {
189
        return $this->fieldList;
190
    }
191
192
    /**
193
     * Set the value of fieldList
194
     *
195
     * @param FieldList $fieldList
196
     * @return $this
197
     */
198
    public function setFieldList(FieldList $fieldList)
199
    {
200
        $this->fieldList = $fieldList;
201
        foreach ($fieldList->dataFields() as $f) {
202
            $f->addExtraClass('no-change-track');
203
        }
204
        return $this;
205
    }
206
207
    /**
208
     * Get the dropUp value
209
     * Called by ActionsGridFieldItemRequest to determine placement
210
     *
211
     * @see https://github.com/lekoala/silverstripe-cms-actions
212
     * @return bool
213
     */
214
    public function getDropUp()
215
    {
216
        return $this->dropUp;
217
    }
218
219
    /**
220
     * Set the value of dropUp
221
     * You might want to call also setButtonType(null) for better styles
222
     *
223
     * @see https://github.com/lekoala/silverstripe-cms-actions
224
     * @param bool $is
225
     * @return $this
226
     */
227
    public function setDropUp($is)
228
    {
229
        $this->dropUp = !!$is;
230
        return $this;
231
    }
232
233
    /**
234
     * Required for cms-actions
235
     * @return string
236
     */
237
    public function actionName()
238
    {
239
        return rtrim(str_replace('doCustomAction[', '', $this->name), ']');
240
    }
241
242
    /**
243
     * Get the value of shouldRefresh
244
     * @return mixed
245
     */
246
    public function getShouldRefresh()
247
    {
248
        return $this->shouldRefresh;
249
    }
250
251
    /**
252
     * Set the value of shouldRefresh
253
     *
254
     * @param mixed $shouldRefresh
255
     * @return $this
256
     */
257
    public function setShouldRefresh($shouldRefresh)
258
    {
259
        $this->shouldRefresh = $shouldRefresh;
260
        return $this;
261
    }
262
263
    public function getModalID()
264
    {
265
        return 'modal_' . $this->name;
266
    }
267
268
    public function getTitle()
269
    {
270
        return $this->title;
271
    }
272
273
    public function getFillHeight()
274
    {
275
        return $this->fillHeight;
276
    }
277
278
    public function setFillHeight($fillHeight)
279
    {
280
        $this->fillHeight = $fillHeight;
281
        return $this;
282
    }
283
}
284