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