1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\Maintenance\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataList; |
6
|
|
|
use SilverStripe\View\Requirements; |
7
|
|
|
use SilverStripe\Forms\GridField\GridField_FormAction; |
8
|
|
|
use SilverStripe\View\ArrayData; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use SilverStripe\Core\Convert; |
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
12
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
13
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
14
|
|
|
use BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob; |
15
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
16
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider; |
17
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adds a "Refresh" button to the bottom or top of a GridField. |
21
|
|
|
* |
22
|
|
|
* @package forms |
23
|
|
|
* @subpackage fields-gridfield |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
class GridFieldRefreshButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
* @config |
31
|
|
|
*/ |
32
|
|
|
private static $allowed_actions = ["check"]; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Fragment to write the button to. |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $targetFragment; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $targetFragment The HTML fragment to write the button into |
42
|
|
|
*/ |
43
|
|
|
public function __construct($targetFragment) |
44
|
|
|
{ |
45
|
|
|
$this->targetFragment = $targetFragment; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param GridField $gridField |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getHTMLFragments($gridField) |
53
|
|
|
{ |
54
|
|
|
Requirements::javascript('bringyourownideas/silverstripe-maintenance: javascript/CheckForUpdates.js'); |
55
|
|
|
|
56
|
|
|
$button = GridField_FormAction::create( |
57
|
|
|
$gridField, |
58
|
|
|
'refresh', |
59
|
|
|
_t('GridFieldRefreshButton.REFRESH', 'Check for updates'), |
60
|
|
|
'refresh', |
61
|
|
|
null |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$button->addExtraClass('btn btn-secondary font-icon-sync'); |
65
|
|
|
|
66
|
|
|
$button->setAttribute('data-check', $gridField->Link('check')); |
67
|
|
|
$button->setAttribute( |
68
|
|
|
'data-message', |
69
|
|
|
_t( |
70
|
|
|
'GridFieldRefreshButton.MESSAGE', |
71
|
|
|
'Updating this list may take 2-3 minutes. You can continue to use the CMS while we run the update.' |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if ($this->hasActiveJob()) { |
76
|
|
|
$button->setTitle(_t('GridFieldRefreshButton.UPDATE', 'Updating...')); |
77
|
|
|
$button->setDisabled(true); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return [ |
81
|
|
|
$this->targetFragment => ArrayData::create(['Button' => $button->Field()]) |
82
|
|
|
->renderWith(__CLASS__) |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Refresh is an action button. |
88
|
|
|
* |
89
|
|
|
* @param GridField $gridField |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function getActions($gridField) |
94
|
|
|
{ |
95
|
|
|
return ['refresh']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Handle the refresh action. |
100
|
|
|
* |
101
|
|
|
* @param GridField $gridField |
102
|
|
|
* @param string $actionName |
103
|
|
|
* @param array $arguments |
104
|
|
|
* @param array $data |
105
|
|
|
* |
106
|
|
|
* @return null |
107
|
|
|
*/ |
108
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
109
|
|
|
{ |
110
|
|
|
if ($actionName == 'refresh') { |
111
|
|
|
return $this->handleRefresh($gridField); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Refresh is accessible via the url |
117
|
|
|
* |
118
|
|
|
* @param GridField $gridField |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getURLHandlers($gridField) |
122
|
|
|
{ |
123
|
|
|
return [ |
124
|
|
|
'check' => 'handleCheck' |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @see hasActiveJob |
130
|
|
|
* @return string JSON boolean |
131
|
|
|
*/ |
132
|
|
|
public function handleCheck() |
133
|
|
|
{ |
134
|
|
|
$isRunning = $this->hasActiveJob(); |
135
|
|
|
return Convert::raw2json($isRunning); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Check the queue for refresh jobs that are not 'done' |
140
|
|
|
* in one manner or another (e.g. stalled or cancelled) |
141
|
|
|
* |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
|
|
public function hasActiveJob() |
145
|
|
|
{ |
146
|
|
|
/** @var DataList $jobList */ |
147
|
|
|
$jobList = Injector::inst() |
148
|
|
|
->get(QueuedJobService::class) |
149
|
|
|
->getJobList(QueuedJob::QUEUED) |
150
|
|
|
->filter([ |
151
|
|
|
'Implementation' => CheckForUpdatesJob::class |
152
|
|
|
]) |
153
|
|
|
->exclude([ |
154
|
|
|
'JobStatus' => [ |
155
|
|
|
QueuedJob::STATUS_COMPLETE, |
156
|
|
|
QueuedJob::STATUS_CANCELLED, |
157
|
|
|
QueuedJob::STATUS_BROKEN |
158
|
|
|
] |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
return $jobList->exists(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Handle the refresh, for both the action button and the URL |
166
|
|
|
*/ |
167
|
|
|
public function handleRefresh() |
168
|
|
|
{ |
169
|
|
|
if (!$this->hasActiveJob()) { |
170
|
|
|
$injector = Injector::inst(); |
171
|
|
|
$injector->get(QueuedJobService::class)->queueJob($injector->create(CheckForUpdatesJob::class)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|