Passed
Push — master ( 6f97b9...649976 )
by Thomas
03:10
created

PureModalAction::SubmitOnClickScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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