Passed
Push — master ( e35b69...147f3c )
by Thomas
03:36
created

GridFieldTableLink::setPromptDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use ReflectionClass;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
8
9
/**
10
 * Provide a simple way to declare links for GridField tables
11
 */
12
class GridFieldTableLink implements GridField_HTMLProvider
13
{
14
    use DefaultLink;
15
16
    /**
17
     * Fragment to write the button to
18
     * @var string
19
     */
20
    protected $targetFragment;
21
22
    /**
23
     * @var string
24
     */
25
    protected $actionName;
26
27
    /**
28
     * @var string
29
     */
30
    protected $buttonLabel;
31
32
    /**
33
     * @var string
34
     */
35
    protected $fontIcon;
36
37
    /**
38
     * @var string
39
     */
40
    protected $parentID;
41
42
    /**
43
     * @var string
44
     */
45
    protected $confirm;
46
47
    /**
48
     * @var string
49
     */
50
    protected $prompt;
51
52
    /**
53
     * @var string
54
     */
55
    protected $promptDefault;
56
57
    /**
58
     * @var array
59
     */
60
    protected $attributes = [];
61
62
    /**
63
     * @var boolean
64
     */
65
    protected $noAjax = false;
66
67
    /**
68
     * @param string $targetFragment The HTML fragment to write the button into
69
     * @param string $buttonLabel
70
     */
71
    public function __construct($targetFragment = "buttons-before-right", $buttonLabel = null, $actionName = null)
72
    {
73
        $this->targetFragment = $targetFragment;
74
        if ($buttonLabel) {
75
            $this->buttonLabel = $buttonLabel;
76
        }
77
        if ($actionName) {
78
            $this->actionName = $actionName;
79
        }
80
    }
81
82
    public function getActionName()
83
    {
84
        if ($this->actionName) {
85
            return $this->actionName;
86
        }
87
        $class = (new ReflectionClass(get_called_class()))->getShortName();
88
        // ! without lowercase, in does not work
89
        return strtolower(str_replace('Button', '', $class));
90
    }
91
92
    public function getButtonLabel()
93
    {
94
        return $this->buttonLabel;
95
    }
96
97
    /**
98
     * Place the export button in a <p> tag below the field
99
     */
100
    public function getHTMLFragments($gridField)
101
    {
102
        $action = $this->getActionName();
103
104
        $button = new CustomLink(
105
            $action,
106
            $this->getButtonLabel(),
107
        );
108
        $button->addExtraClass('btn btn-secondary action_' . $action);
109
        if ($this->fontIcon) {
110
            $button->addExtraClass('font-icon-' . $this->fontIcon);
111
        }
112
        if ($this->noAjax) {
113
            $button->setNoAjax($this->noAjax);
114
        }
115
        //TODO: replace prompt and confirm with inline js
116
        if ($this->prompt) {
117
            $button->setAttribute('data-prompt', $this->prompt);
118
            $promptDefault = $this->getPromptDefault();
119
            if ($promptDefault) {
120
                $button->setAttribute('data-prompt-default', $promptDefault);
121
            }
122
        }
123
        if ($this->confirm) {
124
            $button->setAttribute('data-confirm', $this->confirm);
125
        }
126
        if ($this->newWindow) {
127
            $button->setNewWindow($this->newWindow);
128
        }
129
        if ($this->link) {
130
            $button->setLink($this->link);
131
        }
132
        foreach ($this->attributes as $attributeName => $attributeValue) {
133
            $button->setAttribute($attributeName, $attributeValue);
134
        }
135
        $button->setForm($gridField->getForm());
136
        return array(
137
            $this->targetFragment => $button->Field()
138
        );
139
    }
140
141
    /**
142
     * @param string $name
143
     * @param string $value
144
     * @return $this
145
     */
146
    public function setAttribute($name, $value)
147
    {
148
        $this->attributes[$name] = $value;
149
        return $this;
150
    }
151
152
    /**
153
     * @param string $name
154
     * @return string
155
     */
156
    public function getAttribute($name)
157
    {
158
        if (isset($this->attributes[$name])) {
159
            return $this->attributes[$name];
160
        }
161
        return null;
162
    }
163
164
    public function getActions($gridField)
0 ignored issues
show
Unused Code introduced by
The parameter $gridField is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

164
    public function getActions(/** @scrutinizer ignore-unused */ $gridField)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
    {
166
        return array($this->getActionName());
167
    }
168
169
    /**
170
     * Get the value of fontIcon
171
     *
172
     * @return string
173
     */
174
    public function getFontIcon()
175
    {
176
        return $this->fontIcon;
177
    }
178
179
    /**
180
     * Set the value of fontIcon
181
     *
182
     * @param string $fontIcon
183
     *
184
     * @return $this
185
     */
186
    public function setFontIcon($fontIcon)
187
    {
188
        $this->fontIcon = $fontIcon;
189
190
        return $this;
191
    }
192
193
194
    /**
195
     * Get the parent record id
196
     *
197
     * @return int
198
     */
199
    public function getParentID()
200
    {
201
        return $this->parentID;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parentID returns the type string which is incompatible with the documented return type integer.
Loading history...
202
    }
203
204
    /**
205
     * Set the parent record id
206
     *
207
     * @param int $id
208
     * @return $this
209
     */
210
    public function setParentID($id)
211
    {
212
        $this->parentID = $id;
213
        return $this;
214
    }
215
216
    /**
217
     * Get the value of confirm
218
     *
219
     * @return string
220
     */
221
    public function getConfirm()
222
    {
223
        return $this->confirm;
224
    }
225
226
    /**
227
     * Set the value of confirm
228
     *
229
     * @param string $confirm
230
     * @return $this
231
     */
232
    public function setConfirm($confirm)
233
    {
234
        $this->confirm = $confirm;
235
        return $this;
236
    }
237
238
    /**
239
     * Get the value of prompt
240
     *
241
     * @return string
242
     */
243
    public function getPrompt()
244
    {
245
        return $this->prompt;
246
    }
247
248
    /**
249
     * Set the value of prompt
250
     *
251
     * @param string $prompt
252
     * @return $this
253
     */
254
    public function setPrompt($prompt)
255
    {
256
        $this->prompt = $prompt;
257
        return $this;
258
    }
259
260
    /**
261
     * Get the value of promptDefault
262
     *
263
     * @return string
264
     */
265
    public function getPromptDefault()
266
    {
267
        return $this->promptDefault;
268
    }
269
270
    /**
271
     * Set the value of promptDefault
272
     *
273
     * @param string $promptDefault
274
     * @return $this
275
     */
276
    public function setPromptDefault($promptDefault)
277
    {
278
        $this->promptDefault = $promptDefault;
279
        return $this;
280
    }
281
282
    /**
283
     * Get the value of noAjax
284
     * @return boolean
285
     */
286
    public function getNoAjax()
287
    {
288
        return $this->noAjax;
289
    }
290
291
    /**
292
     * Set the value of noAjax
293
     *
294
     * @param boolean $noAjax
295
     * @return $this
296
     */
297
    public function setNoAjax($noAjax)
298
    {
299
        $this->noAjax = $noAjax;
300
        return $this;
301
    }
302
}
303