Passed
Push — master ( 47a487...43d762 )
by Thomas
02:38
created

PureModalAction::getOverlayTriggersClose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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