Passed
Pull Request — master (#2)
by Sergey
01:57
created

PureModalAction::setDialogButtonTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
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
    public function __construct($name, $title)
44
    {
45
        $name = 'doCustomAction[' . $name . ']';
46
        $this->title = $title;
47
        $this->name = $name;
48
    }
49
50
    public function FieldHolder($properties = array())
51
    {
52
        Requirements::javascript('lekoala/silverstripe-pure-modal: client/pure-modal.js');
53
        Requirements::css('lekoala/silverstripe-pure-modal: client/pure-modal.css');
54
55
        $modalContent = '';
56
        foreach ($this->fieldList as $field) {
57
            $modalContent .= $field->FieldHolder();
58
        }
59
60
        $attrs = '';
61
        $modalID = 'modal_' . $this->name;
62
63
        $content = '';
64
        $content .= '<label for="' . $modalID . '" class="btn btn-info"' . $attrs . '>';
65
        $content .= $this->getButtonTitle();
66
        $content .= '</label>';
67
        $content .= '<div class="pure-modal from-top">';
68
        // This is how we show the modal
69
        $content .= '<input id="' . $modalID . '" class="checkbox" type="checkbox">';
70
        $content .= '<div class="pure-modal-overlay">';
71
        // Close in overlay
72
        $content .= '<label for="' . $modalID . '" class="o-close"></label>';
73
        $content .= '<div class="pure-modal-wrap">';
74
        // Close icon
75
        $content .= '<label for="' . $modalID . '" class="close">&#10006;</label>';
76
        $content .= $modalContent;
77
78
        // Add actual button
79
        if($this->showDialogButton) {
80
            $content .= '<button type="submit" name="action_' . $this->name . '" class="btn action btn btn-info custom-action">
81
                <span class="btn__title">' . $this->getButtonTitle() . '</span>
82
            </button>';
83
        }
84
85
        $content .= '</div>';
86
        $content .= '</div>';
87
        $content .= '</div>';
88
        $this->content = $content;
89
90
        return parent::FieldHolder($properties);
91
    }
92
93
    /**
94
     * Get the title with icon if set
95
     *
96
     * @return string
97
     */
98
    protected function getButtonTitle()
99
    {
100
        $title = $this->title;
101
        if ($this->buttonIcon) {
102
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
103
        }
104
        return $title;
105
    }
106
107
    /**
108
     * Get the dialog button title with icon if set
109
     *
110
     * @return string
111
     */
112
    protected function getDialogButtonTitle()
113
    {
114
        $title = $this->buttonTitle ?: $this->title;
115
        if ($this->buttonIcon) {
116
            $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title;
117
        }
118
        return $title;
119
    }
120
121
    /**
122
     * Set dialog button customised button title
123
     *
124
     * @return self
125
     */
126
    public function setDialogButtonTitle($value)
127
    {
128
        $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...
129
        return $this;
130
    }
131
132
133
    /**
134
     * Get an icon for this button
135
     *
136
     * @return string
137
     */
138
    public function getButtonIcon()
139
    {
140
        return $this->buttonIcon;
141
    }
142
143
    /**
144
     * Set an icon for this button
145
     *
146
     * Feel free to use SilverStripeIcons constants
147
     *
148
     * @param string $buttonIcon An icon for this button
149
     * @return $this
150
     */
151
    public function setButtonIcon(string $buttonIcon)
152
    {
153
        $this->buttonIcon = $buttonIcon;
154
        return $this;
155
    }
156
157
    /**
158
     * Get whether it must display the dialog button
159
     *
160
     * @return boolean
161
     */
162
    protected function getShowDialogButton()
163
    {
164
        return $this->showDialogButton;
165
    }
166
167
    /**
168
     * Set whether it must display the dialog button
169
     *
170
     * @return self
171
     */
172
    public function setShowDialogButton($value)
173
    {
174
        $this->showDialogButton = !!$value;
175
        return $this;
176
    }
177
178
179
180
    /**
181
     * Get the value of fieldList
182
     * @return FieldList
183
     */
184
    public function getFieldList()
185
    {
186
        return $this->fieldList;
187
    }
188
189
    /**
190
     * Set the value of fieldList
191
     *
192
     * @param FieldList $fieldList
193
     * @return $this
194
     */
195
    public function setFieldList(FieldList $fieldList)
196
    {
197
        $this->fieldList = $fieldList;
198
        return $this;
199
    }
200
201
    /**
202
     * Required for cms-actions
203
     * @return string
204
     */
205
    public function actionName()
206
    {
207
        return rtrim(str_replace('doCustomAction[', '', $this->name), ']');
208
    }
209
210
    /**
211
     * Get the value of shouldRefresh
212
     * @return mixed
213
     */
214
    public function getShouldRefresh()
215
    {
216
        return $this->shouldRefresh;
217
    }
218
219
    /**
220
     * Set the value of shouldRefresh
221
     *
222
     * @param mixed $shouldRefresh
223
     * @return $this
224
     */
225
    public function setShouldRefresh($shouldRefresh)
226
    {
227
        $this->shouldRefresh = $shouldRefresh;
228
        return $this;
229
    }
230
}
231