Completed
Pull Request — master (#647)
by Robbie
20:35 queued 18:30
created

GridFieldAddClassesButton   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 4
dl 0
loc 241
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setButtonName() 0 5 1
A getButtonName() 0 4 1
A getFragment() 0 4 1
A setFragment() 0 5 1
A getButtonClass() 0 4 1
A setButtonClass() 0 5 1
A getClasses() 0 4 1
A getClassesCreate() 0 13 3
A setClasses() 0 7 3
B getHTMLFragments() 0 33 4
A getActions() 0 6 1
A getAction() 0 4 1
A handleAction() 0 9 2
A handleAdd() 0 18 3
1
<?php
2
3
namespace SilverStripe\UserForms\Form;
4
5
use SilverStripe\Control\HTTPResponse_Exception;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\Forms\GridField\GridField_ActionProvider;
8
use SilverStripe\Forms\GridField\GridField_FormAction;
9
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
10
11
/**
12
 * A button which allows objects to be created with a specified classname(s)
13
 */
14
class GridFieldAddClassesButton implements GridField_HTMLProvider, GridField_ActionProvider
15
{
16
    /**
17
     * Name of fragment to insert into
18
     *
19
     * @var string
20
     */
21
    protected $targetFragment;
22
23
    /**
24
     * Button title
25
     *
26
     * @var string
27
     */
28
    protected $buttonName;
29
30
    /**
31
     * Additonal CSS classes for the button
32
     *
33
     * @var string
34
     */
35
    protected $buttonClass = null;
36
37
    /**
38
     * Class names
39
     *
40
     * @var array
41
     */
42
    protected $modelClasses = null;
43
44
    /**
45
     * @param array $classes Class or list of classes to create.
46
     * If you enter more than one class, each click of the "add" button will create one of each
47
     * @param string $targetFragment The fragment to render the button into
48
     */
49
    public function __construct($classes, $targetFragment = 'buttons-before-left')
50
    {
51
        $this->setClasses($classes);
52
        $this->setFragment($targetFragment);
53
    }
54
55
    /**
56
     * Change the button name
57
     *
58
     * @param string $name
59
     * @return $this
60
     */
61
    public function setButtonName($name)
62
    {
63
        $this->buttonName = $name;
64
        return $this;
65
    }
66
67
    /**
68
     * Get the button name
69
     *
70
     * @return string
71
     */
72
    public function getButtonName()
73
    {
74
        return $this->buttonName;
75
    }
76
77
    /**
78
     * Gets the fragment name this button is rendered into.
79
     *
80
     * @return string
81
     */
82
    public function getFragment()
83
    {
84
        return $this->targetFragment;
85
    }
86
87
    /**
88
     * Sets the fragment name this button is rendered into.
89
     *
90
     * @param string $fragment
91
     * @return GridFieldAddNewInlineButton $this
92
     */
93
    public function setFragment($fragment)
94
    {
95
        $this->targetFragment = $fragment;
96
        return $this;
97
    }
98
99
    /**
100
     * Get extra button class
101
     *
102
     * @return string
103
     */
104
    public function getButtonClass()
105
    {
106
        return $this->buttonClass;
107
    }
108
109
    /**
110
     * Sets extra CSS classes for this button
111
     *
112
     * @param string $buttonClass
113
     * @return $this
114
     */
115
    public function setButtonClass($buttonClass)
116
    {
117
        $this->buttonClass = $buttonClass;
118
        return $this;
119
    }
120
121
122
    /**
123
     * Get the classes of the objects to create
124
     *
125
     * @return array
126
     */
127
    public function getClasses()
128
    {
129
        return $this->modelClasses;
130
    }
131
132
    /**
133
     * Gets the list of classes which can be created, with checks for permissions.
134
     * Will fallback to the default model class for the given DataGrid
135
     *
136
     * @param DataGrid $grid
137
     * @return array
138
     */
139
    public function getClassesCreate($grid)
140
    {
141
        // Get explicit or fallback class list
142
        $classes = $this->getClasses();
143
        if (empty($classes) && $grid) {
144
            $classes = array($grid->getModelClass());
145
        }
146
147
        // Filter out classes without permission
148
        return array_filter($classes, function ($class) {
149
            return singleton($class)->canCreate();
150
        });
151
    }
152
153
    /**
154
     * Specify the classes to create
155
     *
156
     * @param array $classes
157
     */
158
    public function setClasses($classes)
159
    {
160
        if (!is_array($classes)) {
161
            $classes = $classes ? array($classes) : array();
162
        }
163
        $this->modelClasses = $classes;
164
    }
165
166
    public function getHTMLFragments($grid)
167
    {
168
        // Check create permission
169
        $singleton = singleton($grid->getModelClass());
170
        if (!$singleton->canCreate()) {
171
            return array();
172
        }
173
174
        // Get button name
175
        $buttonName = $this->getButtonName();
176
        if (!$buttonName) {
177
            // provide a default button name, can be changed by calling {@link setButtonName()} on this component
178
            $objectName = $singleton->i18n_singular_name();
179
            $buttonName = _t('SilverStripe\\Forms\\GridField\\GridField.Add', 'Add {name}', array('name' => $objectName));
180
        }
181
182
        $addAction = new GridField_FormAction(
183
            $grid,
184
            $this->getAction(),
185
            $buttonName,
186
            $this->getAction(),
187
            array()
188
        );
189
        $addAction->setAttribute('data-icon', 'add');
190
191
        if ($this->getButtonClass()) {
192
            $addAction->addExtraClass($this->getButtonClass());
193
        }
194
195
        return array(
196
            $this->targetFragment => $addAction->forTemplate()
197
        );
198
    }
199
200
    /**
201
     * {@inheritDoc}
202
     */
203
    public function getActions($gridField)
204
    {
205
        return array(
206
            $this->getAction()
207
        );
208
    }
209
210
    /**
211
     * Get the action suburl for this component
212
     *
213
     * @return string
214
     */
215
    protected function getAction()
216
    {
217
        return 'add-classes-' . strtolower(implode('-', $this->getClasses()));
218
    }
219
220
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
221
    {
222
        switch (strtolower($actionName)) {
223
            case $this->getAction():
224
                return $this->handleAdd($gridField);
225
            default:
226
                return null;
227
        }
228
    }
229
230
    /**
231
     * Handles adding a new instance of a selected class.
232
     *
233
     * @param GridField $grid
234
     * @return null
235
     */
236
    public function handleAdd($grid)
237
    {
238
        $classes = $this->getClassesCreate($grid);
0 ignored issues
show
Documentation introduced by
$grid is of type object<SilverStripe\Forms\GridField\GridField>, but the function expects a object<SilverStripe\UserForms\Form\DataGrid>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
239
        if (empty($classes)) {
240
            throw new HTTPResponse_Exception(400);
241
        }
242
243
        // Add item to gridfield
244
        $list = $grid->getList();
245
        foreach ($classes as $class) {
246
            $item = $class::create();
247
            $item->write();
248
            $list->add($item);
249
        }
250
251
        // Should trigger a simple reload
252
        return null;
253
    }
254
}
255