Passed
Push — master ( 6d5255...6f97b9 )
by Thomas
03:09
created

PureModalAction::getMoveModalScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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