/
jobs.php
257 lines (226 loc) · 8.9 KB
/
jobs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Optimization;
use Concrete\Core\Cache\Level\OverridesCache;
use Concrete\Core\Job\Job;
use Concrete\Core\Job\Set as JobSet;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Url\Resolver\Manager\ResolverManagerInterface;
class Jobs extends DashboardPageController
{
public function on_start()
{
parent::on_start();
// clear the environment overrides cache first
$this->app->make(OverridesCache::class)->flush();
$this->set('dh', $this->app->make('date'));
$this->set('urlResolver', $this->app->make(ResolverManagerInterface::class));
$this->set('availableJobs', Job::getAvailableList($this->canInstallCoreJobs()));
$this->set('installedJobs', Job::getList());
$this->set('defaultJobSet', JobSet::getDefault());
$this->set('jobSets', JobSet::getList());
$this->set('editingJobSet', null);
$this->set('auth', Job::generateAuth());
$this->addHeaderItem(
<<<'EOT'
<style>
#ccm-jobs-list td {
vertical-align: middle;
-webkit-transition-property: color, background-color;
-webkit-transition-duration: .9s, .9s;
-moz-transition-property: color, background-color;
-moz-transition-duration: .9s, .9s;
-o-transition-property: color, background-color;
-o-transition-duration: .9s, .9s;
-ms-transition-property: color, background-color;
-ms-transition-duration: .9s, .9s;
transition-property: color, background-color;
transition-duration: .9s, .9s;
}
</style>
EOT
);
}
public function view()
{
$this->set('activeTab', 'jobs');
}
public function view_sets()
{
$this->set('activeTab', 'jobSets');
}
public function reset($token = '')
{
if (!$this->token->validate('reset_jobs', $token)) {
$this->error->add($this->token->getErrorMessage());
return $this->view();
}
$jobs = Job::getList();
foreach ($jobs as $j) {
$j->reset();
}
$this->flash('success', t('All running jobs have been reset.'));
return $this->buildRedirect($this->action(''));
}
public function update_job_schedule($jID = '')
{
if (!$this->token->validate("update_job_schedule{$jID}")) {
$this->error->add($this->token->getErrorMessage());
} else {
$post = $this->request->request;
$J = Job::getByID($jID);
if ($J === null) {
$this->error->add(t('Job not found.'));
} else {
$isScheduled = (bool) $post->get("isScheduled{$J->getJobID()}");
if ($isScheduled) {
$intervalValue = (string) $post->get("value{$J->getJobID()}");
if ($intervalValue !== (string) (int) $intervalValue || (($intervalValue = (int) $intervalValue)) < 0) {
$this->error->add(t('Please specify the interval value.'));
}
$intervalUnit = $post->get("unit{$J->getJobID()}");
if (!in_array($intervalUnit, ['minutes', 'hours', 'days', 'weeks', 'months'], true)) {
$this->error->add(t('Please specify the interval unit.'));
}
} else {
$intervalValue = $J->scheduledValue;
$intervalUnit = $J->scheduledInterval;
}
if (!$this->error->has()) {
$J->setSchedule($isScheduled, $intervalUnit, $intervalValue);
$this->flash('success', t('Job schedule updated successfully.'));
return $this->buildRedirect($this->action(''));
}
}
}
return $this->view();
}
public function uninstall($job_id = null, $token = null)
{
if (!$this->token->validate('uninstall_job', $token)) {
$this->error->add($this->token->getErrorMessage());
} elseif (!$job_id) {
$this->error->add(t('No job specified.'));
} else {
$job = Job::getByID((int) $job_id);
if (!$job) {
$this->error->add(t('Job not found.'));
} else {
if ($job->jNotUninstallable) {
$this->error->add(t('This job cannot be uninstalled.'));
} else {
$job->uninstall();
$this->flash('success', t('Job successfully uninstalled.'));
return $this->buildRedirect($this->action(''));
}
}
}
return $this->view();
}
public function install($handle = null, $token = null)
{
if (!$this->token->validate('install_job', $token)) {
$this->error->add($this->token->getErrorMessage());
} elseif (!$handle) {
$this->error->add(t('No job specified.'));
} else {
Job::installByHandle($handle);
$this->flash('message', t('Job successfully installed.'));
return $this->buildRedirect($this->action(''));
}
return $this->view();
}
public function edit_set($jsID = false)
{
if ($jsID === 'new') {
$js = null;
} else {
$js = JobSet::getByID($jsID);
if ($js === null) {
$this->error->add(t('Invalid Job set.'));
return $this->buildRedirect($this->action(''));
}
}
$this->set('editingJobSet', $js ?: true);
$this->set('pageTitle', $js ? $js->getJobSetName() : t('New Job Set'));
return $this->view_sets();
}
public function update_set($jsID = '')
{
if ($jsID === 'new') {
$js = null;
} else {
$js = JobSet::getByID($jsID);
if ($js === null) {
$this->error->add(t('Invalid Job set.'));
return $this->view_sets();
}
}
if (!$this->token->validate("update_set{$jsID}")) {
$this->error->add($this->token->getErrorMessage());
} else {
$post = $this->request->request;
$name = trim($post->get('jsName'));
if ($name === '') {
$this->error->add(t('Specify a name for your Job set.'));
}
$jobIDs = $post->get('jID');
$jobIDs = is_array($jobIDs) ? array_unique(array_filter(array_map('intval', $jobIDs))) : [];
$isScheduled = (bool) $post->get('isScheduled');
if ($isScheduled) {
$intervalValue = (string) $post->get('value');
if ($intervalValue !== (string) (int) $intervalValue || (($intervalValue = (int) $intervalValue)) < 0) {
$this->error->add(t('Please specify the interval value.'));
}
$intervalUnit = $post->get('unit');
if (!in_array($intervalUnit, ['hours', 'days', 'weeks', 'months'], true)) {
$this->error->add(t('Please specify the interval unit.'));
}
} else {
$intervalValue = $js === null ? 0 : $js->scheduledValue;
$intervalUnit = $js === null ? 'days' : $js->scheduledInterval;
}
if (!$this->error->has()) {
if ($js === null) {
$js = JobSet::add($name);
} else {
$js->updateJobSetName($name);
$js->clearJobs();
}
foreach ($jobIDs as $jobID) {
$j = Job::getByID($jobID);
if ($j !== null) {
$js->addJob($j);
}
}
$js->setSchedule($isScheduled, $intervalUnit, $intervalValue);
$this->flash('success', $jsID === 'new' ? t('Job set added.') : t('Job Set updated successfully.'));
return $this->buildRedirect($this->action('view_sets'));
}
}
return $this->edit_set($jsID);
}
public function delete_set($jsID = '')
{
$js = null;
if ($this->token->validate("delete_set{$jsID}")) {
$js = JobSet::getByID($jsID);
if ($js === null) {
$this->error->add(t('Invalid Job set.'));
} elseif (!$js->canDelete()) {
$this->error->add(t('You cannot delete the default Job set.'));
}
if (!$this->error->has()) {
$js->delete();
$this->flash('success', t('Job set deleted successfully.'));
return $this->buildRedirect($this->action('view_sets'));
}
} else {
$this->error->add($this->token->getErrorMessage());
}
return $js === null ? $this->view_sets() : $this->edit_set($js->getJobSetID());
}
protected function canInstallCoreJobs(): bool
{
return false;
}
}