Completed
Push — master ( c5ed18...ed0fce )
by Nathan
07:36
created

GridFieldQueuedJobExecute::augmentColumns()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
namespace Symbiote\QueuedJobs\Forms;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\Forms\GridField\GridField_ActionProvider;
7
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
8
use SilverStripe\Forms\GridField\GridField_FormAction;
9
use Symbiote\QueuedJobs\Services\QueuedJob;
10
use SilverStripe\View\Requirements;
11
12
/**
13
 * This class is a {@link GridField} component that adds a delete action for objects.
14
 *
15
 * This component also supports unlinking a relation instead of deleting the object.
16
 * Use the {@link $removeRelation} property set in the constructor.
17
 *
18
 * <code>
19
 * $action = new GridFieldDeleteAction(); // delete objects permanently
20
 * $action = new GridFieldDeleteAction(true); // removes the relation to object, instead of deleting
21
 * </code>
22
 *
23
 * @package queuedjobs
24
 * @subpackage forms
25
 */
26
class GridFieldQueuedJobExecute implements GridField_ColumnProvider, GridField_ActionProvider
27
{
28
29
    protected $action = 'execute';
30
31
    /**
32
     * @var array
33
     */
34
    protected $icons = array(
35
        'execute' => 'navigation',
36
        'pause'   => 'minus-circle_disabled',
37
        'resume'  => 'arrow-circle-double',
38
    );
39
40
    /**
41
     * Call back to see if the record's action icon should be shown.
42
     *
43
     * @var closure
44
     */
45
    protected $viewCheck;
46
47
    /**
48
     * @param string   $action
49
     * @param callable $check
50
     */
51
    public function __construct($action = 'execute', $check = null)
52
    {
53
        $this->action = $action;
54
        if (!$check) {
55
            $check = function ($record) {
56
                return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_NEW;
57
            };
58
        }
59
60
        $this->viewCheck = $check;
0 ignored issues
show
Documentation Bug introduced by
It seems like $check of type object<Closure> is incompatible with the declared type object<Symbiote\QueuedJobs\Forms\closure> of property $viewCheck.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    /**
64
     * Add a column 'Delete'
65
     *
66
     * @param GridField $gridField
67
     * @param array     $columns
68
     */
69
    public function augmentColumns($gridField, &$columns)
70
    {
71
        if (!in_array('Actions', $columns)) {
72
            $columns[] = 'Actions';
73
        }
74
    }
75
76
    /**
77
     * Return any special attributes that will be used for FormField::createTag()
78
     *
79
     * @param GridField $gridField
80
     * @param DataObject $record
81
     * @param string $columnName
82
     * @return array
83
     */
84
    public function getColumnAttributes($gridField, $record, $columnName)
85
    {
86
        return array('class' => 'col-buttons');
87
    }
88
89
    /**
90
     * Add the title
91
     *
92
     * @param GridField $gridField
93
     * @param string $columnName
94
     * @return array
95
     */
96
    public function getColumnMetadata($gridField, $columnName)
97
    {
98
        if ($columnName == 'Actions') {
99
            return array('title' => '');
100
        }
101
    }
102
103
    /**
104
     * Which columns are handled by this component
105
     *
106
     * @param GridField $gridField
107
     * @return array
108
     */
109
    public function getColumnsHandled($gridField)
110
    {
111
        return array('Actions');
112
    }
113
114
    /**
115
     * Which GridField actions are this component handling
116
     *
117
     * @param GridField $gridField
118
     * @return array
119
     */
120
    public function getActions($gridField)
121
    {
122
        return array('execute', 'pause', 'resume');
123
    }
124
125
    /**
126
     * @param GridField $gridField
127
     * @param DataObject $record
128
     * @param string $columnName
129
     * @return string|void - the HTML for the column
130
     */
131
    public function getColumnContent($gridField, $record, $columnName)
132
    {
133
        $icon = $this->icons[$this->action];
134
135
        if ($this->viewCheck) {
136
            $func = $this->viewCheck;
137
            if (!$func($record)) {
138
                return;
139
            }
140
        }
141
142
        $field = GridField_FormAction::create(
143
            $gridField,
144
            'ExecuteJob' . $record->ID,
145
            false,
146
            $this->action,
147
            array('RecordID' => $record->ID)
148
        );
149
        $field->addExtraClass('gridfield-button-job' . $this->action)
150
            ->setAttribute('title', ucfirst($this->action))
151
            ->setAttribute('data-icon', $icon);
152
        return $field->Field();
153
    }
154
155
    /**
156
     * Handle the actions and apply any changes to the GridField
157
     *
158
     * @param GridField $gridField
159
     * @param string $actionName
160
     * @param mixed $arguments
161
     * @param array $data - form data
162
     * @return void
163
     */
164
    public function handleAction(GridField $gridField, $actionName, $arguments, $data)
165
    {
166
        $actions = $this->getActions(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<SilverStripe\Forms\GridField\GridField>.

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...
167
        if (in_array($actionName, $actions)) {
168
            $item = $gridField->getList()->byID($arguments['RecordID']);
169
            if (!$item) {
170
                return;
171
            }
172
            $item->$actionName();
173
            Requirements::clear();
174
        }
175
    }
176
}
177