Passed
Push — master ( 5adf80...3e8f57 )
by Thomas
02:43 queued 12s
created

PureModalAction::getDialogButtonTitle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
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\View\Requirements;
7
use SilverStripe\Forms\LiteralField;
8
9
/**
10
 * Custom modal action
11
 * Requires cms-actions to work out of the box
12
 */
13
class PureModalAction extends LiteralField
14
{
15
    /**
16
     * @var FieldList
17
     */
18
    protected $fieldList;
19
20
    /**
21
     * A custom title for the dialog button
22
     * @var string
23
     */
24
    protected $dialogButtonTitle;
25
26
    /**
27
     * Should it show the dialog button
28
     * @var boolean
29
     */
30
    protected $showDialogButton = true;
31
32
    /**
33
     * An icon for this button
34
     * @var string
35
     */
36
    protected $buttonIcon;
37
38
    /**
39
     * @var boolean
40
     */
41
    protected $shouldRefresh = false;
42
43
    /**
44
     * Whether to place the button in a dot-menu.
45
     * @see https://github.com/lekoala/silverstripe-cms-actions
46
     * @var bool
47
     */
48
    protected $dropUp = false;
49
50
    public function __construct($name, $title)
51
    {
52
        $name = 'doCustomAction[' . $name . ']';
53
        $this->title = $title;
54
        $this->name = $name;
55
    }
56
57
    public function FieldHolder($properties = array())
58
    {
59
        Requirements::javascript('lekoala/silverstripe-pure-modal: client/pure-modal.js');
60
        Requirements::css('lekoala/silverstripe-pure-modal: client/pure-modal.css');
61
62
        $modalContent = '';
63
        foreach ($this->fieldList as $field) {
64
            $modalContent .= $field->FieldHolder();
65
        }
66
67
        $attrs = '';
68
        $modalID = 'modal_' . $this->name;
69
70
        $content = '';
71
        $content .= '<label for="' . $modalID . '" class="btn btn-info"' . $attrs . '>';
72
        $content .= $this->getButtonTitle();
73
        $content .= '</label>';
74
        $content .= '<div class="pure-modal from-top">';
75
        // This is how we show the modal
76
        $content .= '<input id="' . $modalID . '" class="checkbox" type="checkbox">';
77
        $content .= '<div class="pure-modal-overlay">';
78
        // Close in overlay
79
        $content .= '<label for="' . $modalID . '" class="o-close"></label>';
80
        $content .= '<div class="pure-modal-wrap">';
81
        // Close icon
82
        $content .= '<label for="' . $modalID . '" class="close">&#10006;</label>';
83
        $content .= $modalContent;
84
85
        // Add actual button
86
        if($this->showDialogButton) {
87
            $content .= '<button type="submit" name="action_' . $this->name . '" class="btn action btn btn-info custom-action">
88
                <span class="btn__title">' . $this->getButtonTitle() . '</span>
89
            </button>';
90
        }
91
92
        $content .= '</div>';
93
        $content .= '</div>';
94
        $content .= '</div>';
95
        $this->content = $content;
96
97
        return parent::FieldHolder($properties);
98
    }
99
100
    /**
101
     * Get the title with icon if set
102
     *
103
     * @return string
104
     */
105
    protected function getButtonTitle()
106
    {
107
        $title = $this->title;
108
        if ($this->buttonIcon) {
109
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
110
        }
111
        return $title;
112
    }
113
114
    /**
115
     * Get the dialog button title with icon if set
116
     *
117
     * @return string
118
     */
119
    protected function getDialogButtonTitle()
120
    {
121
        $title = $this->buttonTitle ?: $this->title;
122
        if ($this->buttonIcon) {
123
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
124
        }
125
        return $title;
126
    }
127
128
    /**
129
     * Set dialog button customised button title
130
     *
131
     * @return self
132
     */
133
    public function setDialogButtonTitle($value)
134
    {
135
        $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...
136
        return $this;
137
    }
138
139
140
    /**
141
     * Get an icon for this button
142
     *
143
     * @return string
144
     */
145
    public function getButtonIcon()
146
    {
147
        return $this->buttonIcon;
148
    }
149
150
    /**
151
     * Set an icon for this button
152
     *
153
     * Feel free to use SilverStripeIcons constants
154
     *
155
     * @param string $buttonIcon An icon for this button
156
     * @return $this
157
     */
158
    public function setButtonIcon(string $buttonIcon)
159
    {
160
        $this->buttonIcon = $buttonIcon;
161
        return $this;
162
    }
163
164
    /**
165
     * Get whether it must display the dialog button
166
     *
167
     * @return boolean
168
     */
169
    protected function getShowDialogButton()
170
    {
171
        return $this->showDialogButton;
172
    }
173
174
    /**
175
     * Set whether it must display the dialog button
176
     *
177
     * @return self
178
     */
179
    public function setShowDialogButton($value)
180
    {
181
        $this->showDialogButton = !!$value;
182
        return $this;
183
    }
184
185
186
187
    /**
188
     * Get the value of fieldList
189
     * @return FieldList
190
     */
191
    public function getFieldList()
192
    {
193
        return $this->fieldList;
194
    }
195
196
    /**
197
     * Set the value of fieldList
198
     *
199
     * @param FieldList $fieldList
200
     * @return $this
201
     */
202
    public function setFieldList(FieldList $fieldList)
203
    {
204
        $this->fieldList = $fieldList;
205
        return $this;
206
    }
207
208
    /**
209
     * Get the dropUp value
210
     * Called by ActionsGridFieldItemRequest to determine placement
211
     *
212
     * @see https://github.com/lekoala/silverstripe-cms-actions
213
     * @return bool
214
     */
215
    public function getDropUp()
216
    {
217
        return $this->dropUp;
218
    }
219
220
    /**
221
     * Set the value of dropUp
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