|
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 SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\View\Requirements; |
|
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
|
12
|
|
|
|
|
13
|
|
|
class GridFieldQueuedJobExecute implements GridField_ColumnProvider, GridField_ActionProvider |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
protected $action = 'execute'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* CSS icon class names for each action (see silverstripe-admin fonts) |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $icons = [ |
|
24
|
|
|
'execute' => 'font-icon-block-media', |
|
25
|
|
|
'pause' => 'font-icon-cancel-circled', |
|
26
|
|
|
'resume' => 'font-icon-sync', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Call back to see if the record's action icon should be shown. |
|
31
|
|
|
* |
|
32
|
|
|
* @var callable |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $viewCheck; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $action |
|
38
|
|
|
* @param callable $check |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($action = 'execute', $check = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->action = $action; |
|
43
|
|
|
if (!$check) { |
|
44
|
|
|
$check = function ($record) { |
|
45
|
|
|
return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_NEW; |
|
46
|
|
|
}; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->viewCheck = $check; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Add a column 'Delete' |
|
54
|
|
|
* |
|
55
|
|
|
* @param GridField $gridField |
|
56
|
|
|
* @param array $columns |
|
57
|
|
|
*/ |
|
58
|
|
|
public function augmentColumns($gridField, &$columns) |
|
59
|
|
|
{ |
|
60
|
|
|
if (!in_array('Actions', $columns)) { |
|
61
|
|
|
$columns[] = 'Actions'; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return any special attributes that will be used for FormField::createTag() |
|
67
|
|
|
* |
|
68
|
|
|
* @param GridField $gridField |
|
69
|
|
|
* @param DataObject $record |
|
70
|
|
|
* @param string $columnName |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getColumnAttributes($gridField, $record, $columnName) |
|
74
|
|
|
{ |
|
75
|
|
|
return array('class' => 'grid-field__col-compact'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Add the title |
|
80
|
|
|
* |
|
81
|
|
|
* @param GridField $gridField |
|
82
|
|
|
* @param string $columnName |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getColumnMetadata($gridField, $columnName) |
|
86
|
|
|
{ |
|
87
|
|
|
if ($columnName == 'Actions') { |
|
88
|
|
|
return array('title' => ''); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Which columns are handled by this component |
|
94
|
|
|
* |
|
95
|
|
|
* @param GridField $gridField |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getColumnsHandled($gridField) |
|
99
|
|
|
{ |
|
100
|
|
|
return array('Actions'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Which GridField actions are this component handling |
|
105
|
|
|
* |
|
106
|
|
|
* @param GridField $gridField |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getActions($gridField) |
|
110
|
|
|
{ |
|
111
|
|
|
return array('execute', 'pause', 'resume'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param GridField $gridField |
|
116
|
|
|
* @param DataObject $record |
|
117
|
|
|
* @param string $columnName |
|
118
|
|
|
* @return string|void - the HTML for the column |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getColumnContent($gridField, $record, $columnName) |
|
121
|
|
|
{ |
|
122
|
|
|
$icon = $this->icons[$this->action]; |
|
123
|
|
|
|
|
124
|
|
|
if ($this->viewCheck) { |
|
125
|
|
|
$func = $this->viewCheck; |
|
126
|
|
|
if (!$func($record)) { |
|
127
|
|
|
return; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$field = GridField_FormAction::create( |
|
132
|
|
|
$gridField, |
|
133
|
|
|
'ExecuteJob' . $record->ID, |
|
134
|
|
|
false, |
|
135
|
|
|
$this->action, |
|
136
|
|
|
array('RecordID' => $record->ID) |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$humanTitle = ucfirst($this->action); |
|
140
|
|
|
$title = _t(__CLASS__ . '.' . $humanTitle, $humanTitle); |
|
141
|
|
|
|
|
142
|
|
|
$field |
|
143
|
|
|
->addExtraClass('gridfield-button-job' . $this->action) |
|
144
|
|
|
->addExtraClass($icon) |
|
145
|
|
|
->addExtraClass('btn--icon-md btn--no-text grid-field__icon-action') |
|
146
|
|
|
->setAttribute('title', $title) |
|
147
|
|
|
->setDescription($title); |
|
148
|
|
|
|
|
149
|
|
|
return $field->Field(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Handle the actions and apply any changes to the GridField |
|
154
|
|
|
* |
|
155
|
|
|
* @param GridField $gridField |
|
156
|
|
|
* @param string $actionName |
|
157
|
|
|
* @param mixed $arguments |
|
158
|
|
|
* @param array $data - form data |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
162
|
|
|
{ |
|
163
|
|
|
$actions = $this->getActions(null); |
|
|
|
|
|
|
164
|
|
|
if (in_array($actionName, $actions)) { |
|
165
|
|
|
$item = $gridField->getList()->byID($arguments['RecordID']); |
|
166
|
|
|
if (!$item) { |
|
167
|
|
|
return; |
|
168
|
|
|
} |
|
169
|
|
|
$item->$actionName(); |
|
170
|
|
|
Requirements::clear(); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
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: