Passed
Push — master ( ebdfbb...20570e )
by
unknown
02:32
created

code/Form/GridFieldAddClassesButton.php (7 issues)

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
0 ignored issues
show
The type SilverStripe\UserForms\F...FieldAddNewInlineButton was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
     */
93
    public function setFragment($fragment)
94
    {
95
        $this->targetFragment = $fragment;
96
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type SilverStripe\UserForms\F...idFieldAddClassesButton which is incompatible with the documented return type SilverStripe\UserForms\F...FieldAddNewInlineButton.
Loading history...
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
0 ignored issues
show
The type SilverStripe\UserForms\Form\DataGrid was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {
0 ignored issues
show
The condition empty($classes) && $grid can never be true.
Loading history...
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)) {
0 ignored issues
show
The condition ! is_array($classes) can never be true.
Loading history...
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}', ['name' => $objectName]);
180
        }
181
182
        $addAction = GridField_FormAction::create(
183
            $grid,
184
            $this->getAction(),
0 ignored issues
show
$this->getAction() of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

184
            /** @scrutinizer ignore-type */ $this->getAction(),
Loading history...
185
            $buttonName,
186
            $this->getAction(),
187
            array()
188
        );
189
        $addAction->addExtraClass('font-icon-plus btn');
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);
0 ignored issues
show
Are you sure the usage of $this->handleAdd($gridField) targeting SilverStripe\UserForms\F...ssesButton::handleAdd() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
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