GridFieldRowButton::getColumnsHandled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\CmsActions;
4
5
use ReflectionClass;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\Forms\GridField\GridField;
9
use SilverStripe\Forms\GridField\GridField_ActionProvider;
10
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
11
use SilverStripe\Forms\GridField\GridField_FormAction;
12
use SilverStripe\ORM\DataObject;
13
14
/**
15
 * A boilerplate to create row level buttons
16
 *
17
 * It creates the "Actions" (or custom) column if it doesn't exist yet
18
 *
19
 * @link https://docs.silverstripe.org/en/4/developer_guides/forms/how_tos/create_a_gridfield_actionprovider/
20
 */
21
abstract class GridFieldRowButton implements GridField_ColumnProvider, GridField_ActionProvider
22
{
23
    /**
24
     * Column name
25
     *
26
     * @var string
27
     */
28
    protected $columnName = 'Actions';
29
30
    /**
31
     * A silverstripe icon
32
     *
33
     * @var string
34
     */
35
    protected $fontIcon;
36
37
    /**
38
     * Adds class grid-field__icon-action--hidden-on-hover if set
39
     *
40
     * @var boolean
41
     */
42
    protected $hiddenOnHover = true;
43
44
    /**
45
     * Adds class no-ajax if false
46
     *
47
     * @var boolean
48
     */
49
    protected $ajax = false;
50
51
    /**
52
     * Adds Bootstrap style class if not $fontIcon (eg btn-primary / btn-dark / btn-warning etc)
53
     *
54
     * @var string one of the btn-XXX styles (Bootstrap)
55
     */
56
    protected $btnStyleClass = 'dark';
57
58
    /**
59
     * @var int
60
     */
61
    protected $parentID;
62
63
    /**
64
     * @param string $columnName name of the column for this button (default null -> 'Actions')
65
     */
66
    public function __construct($columnName = null)
67
    {
68
        if ($columnName) {
69
            $this->columnName = $columnName;
70
        }
71
    }
72
73
    /**
74
     * @param GridField $gridField
75
     * @param DataObject $record
76
     * @param string $columnName
77
     * @return string Label for the gridfield button
78
     */
79
    abstract public function getButtonLabel(GridField $gridField, $record, $columnName);
80
81
    /**
82
     * @param GridField $gridField
83
     * @param string $actionName
84
     * @param array<mixed> $arguments
85
     * @param array<mixed> $data
86
     * @return mixed
87
     */
88
    abstract public function doHandle(GridField $gridField, $actionName, $arguments, $data);
89
90
    /**
91
     * @return string
92
     */
93
    public function getActionName()
94
    {
95
        $class = (new ReflectionClass(get_called_class()))->getShortName();
96
97
        // ! without lowercase, in does not work
98
        return strtolower(str_replace('Button', '', $class));
99
    }
100
101
    /**
102
     * @param GridField $gridField
103
     * @param array<string> $columns
104
     * @return void
105
     */
106
    public function augmentColumns($gridField, &$columns)
107
    {
108
        if (!in_array($this->columnName, $columns)) {
109
            $columns[] = $this->columnName;
110
        }
111
    }
112
113
    /**
114
     * Return any special attributes that will be used for FormField::create_tag()
115
     *
116
     * @param GridField $gridField
117
     * @param DataObject $record
118
     * @param string $columnName
119
     * @return array<string,string>
120
     */
121
    public function getColumnAttributes($gridField, $record, $columnName)
122
    {
123
        // right-align if this column contains icon-buttons
124
        return ['class' => $this->fontIcon ? 'grid-field__col-compact' : ''];
125
    }
126
127
    /**
128
     * Add the title
129
     *
130
     * @param GridField $gridField
131
     * @param string $columnName
132
     * @return array<string,string>
133
     */
134
    public function getColumnMetadata($gridField, $columnName)
135
    {
136
        // No titles for action column IF icon button
137
        if ($columnName == $this->columnName && $this->fontIcon) {
138
            return ['title' => ''];
139
        }
140
141
        return ['title' => $columnName];
142
    }
143
144
    /**
145
     * Which columns are handled by this component
146
     *
147
     * @param GridField $gridField
148
     * @return array<string>
149
     */
150
    public function getColumnsHandled($gridField)
151
    {
152
        return [$this->columnName];
153
    }
154
155
    /**
156
     * Which GridField actions are this component handling
157
     *
158
     * @param GridField $gridField
159
     * @return array<string>
160
     */
161
    public function getActions($gridField)
162
    {
163
        return [$this->getActionName()];
164
    }
165
166
    /**
167
     *
168
     * @param GridField $gridField
169
     * @param DataObject $record
170
     * @param string $columnName
171
     * @return string - the HTML for the column
172
     */
173
    public function getColumnContent($gridField, $record, $columnName)
174
    {
175
        $actionName = $this->getActionName();
176
        $actionLabel = $this->getButtonLabel($gridField, $record, $columnName);
177
178
        $field = GridField_FormAction::create(
179
            $gridField,
180
            sprintf('%s_%s', $actionName, $record->ID),
181
            ($this->fontIcon ? false : $actionLabel),
182
            $actionName,
183
            ['RecordID' => $record->ID, 'ParentID' => $this->parentID]
184
        )
185
            ->addExtraClass('gridfield-button-' . $actionName)
186
            ->setAttribute('title', $actionLabel);
187
188
        if (!$this->ajax) {
189
            $field->addExtraClass('no-ajax');
190
        }
191
192
        if ($this->hiddenOnHover) {
193
            $field->addExtraClass('grid-field__icon-action--hidden-on-hover');
194
        }
195
196
        if ($this->fontIcon) {
197
            $field->addExtraClass('grid-field__icon-action btn--icon-large font-icon-' . $this->fontIcon);
198
        } else {
199
            // Add a regular button
200
            $field->addExtraClass('btn btn-' . $this->btnStyleClass);
201
        }
202
203
        return $field->Field();
204
    }
205
206
    /**
207
     * Handle the actions and apply any changes to the GridField
208
     *
209
     * @param GridField $gridField
210
     * @param string $actionName
211
     * @param mixed $arguments
212
     * @param array<string,mixed> $data - form data
213
     * @return HTTPResponse|void
214
     */
215
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
216
    {
217
        if ($actionName == $this->getActionName()) {
218
            $result = $this->doHandle($gridField, $actionName, $arguments, $data);
219
            if ($result) {
220
                return $result;
221
            }
222
223
            // Do something!
224
            return Controller::curr()->redirectBack();
225
        }
226
    }
227
228
    /**
229
     * Get the parent record id
230
     *
231
     * @return int
232
     */
233
    public function getParentID()
234
    {
235
        return $this->parentID;
236
    }
237
238
    /**
239
     * Set the parent record id
240
     *
241
     * This will be passed as ParentID along RecordID in the arguments parameter
242
     *
243
     * @param int $id
244
     * @return $this
245
     */
246
    public function setParentID($id)
247
    {
248
        $this->parentID = $id;
249
250
        return $this;
251
    }
252
253
    /**
254
     * Get a silverstripe icon
255
     *
256
     * @return  string
257
     */
258
    public function getFontIcon()
259
    {
260
        return $this->fontIcon;
261
    }
262
263
    /**
264
     * Set a silverstripe icon
265
     *
266
     * @param string $fontIcon A silverstripe icon
267
     *
268
     * @return $this
269
     */
270
    public function setFontIcon(string $fontIcon)
271
    {
272
        $this->fontIcon = $fontIcon;
273
274
        return $this;
275
    }
276
277
    /**
278
     * Get adds class grid-field__icon-action--hidden-on-hover if set
279
     *
280
     * @return  boolean
281
     */
282
    public function getHiddenOnHover()
283
    {
284
        return $this->hiddenOnHover;
285
    }
286
287
    /**
288
     * Set adds class grid-field__icon-action--hidden-on-hover if set
289
     *
290
     * @param boolean $hiddenOnHover Adds class grid-field__icon-action--hidden-on-hover if set
291
     *
292
     * @return $this
293
     */
294
    public function setHiddenOnHover(bool $hiddenOnHover)
295
    {
296
        $this->hiddenOnHover = $hiddenOnHover;
297
298
        return $this;
299
    }
300
}
301