ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilCronManagerGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
6
14{
16 protected $lng;
18 protected $ctrl;
20 protected $settings;
22 protected $tpl;
24 private $uiFactory;
26 private $uiRenderer;
28 private $uiService;
30 private $repository;
32 private $rbac;
34 private $error;
36 private $dic;
37
41 public function __construct()
42 {
43 global $DIC;
44
45 $this->lng = $DIC->language();
46 $this->ctrl = $DIC->ctrl();
47 $this->settings = $DIC->settings();
48 $this->tpl = $DIC->ui()->mainTemplate();
49 $this->uiFactory = $DIC->ui()->factory();
50 $this->uiRenderer = $DIC->ui()->renderer();
51 $this->uiRenderer = $DIC->ui()->renderer();
52 $this->uiService = $DIC->uiService();
53 $this->dic = $DIC;
54 $this->rbac = $DIC->rbac();
55 $this->error = $DIC['ilErr'];
57
58 $this->lng->loadLanguageModule('cron');
59 $this->lng->loadLanguageModule('cmps');
60 }
61
62 public function executeCommand() : void
63 {
64 if (!$this->rbac->system()->checkAccess('visible,read', SYSTEM_FOLDER_ID)) {
65 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
66 }
67
68 $class = $this->ctrl->getNextClass($this);
69
70 switch (strtolower($class)) {
71 case strtolower(ilPropertyFormGUI::class):
72 $form = $this->initEditForm(ilUtil::stripSlashes($_REQUEST['jid']));
73 $this->ctrl->forwardCommand($form);
74 break;
75 }
76
77 $cmd = $this->ctrl->getCmd('render');
78 $this->$cmd();
79 }
80
81 protected function render() : void
82 {
83 $tstamp = $this->lng->txt('cronjob_last_start_unknown');
84 if ($this->settings->get('last_cronjob_start_ts')) {
85 $tstamp = ilDatePresentation::formatDate(new ilDateTime($this->settings->get('last_cronjob_start_ts'), IL_CAL_UNIX));
86 }
87
88 $message = $this->uiFactory->messageBox()->info($this->lng->txt('cronjob_last_start') . ': ' . $tstamp);
89
90 $cronJobs = $this->repository->findAll();
91
92 $tableFilterMediator = new ilCronManagerTableFilterMediator(
93 $cronJobs,
94 $this->uiFactory,
95 $this->uiService,
96 $this->lng
97 );
98 $filter = $tableFilterMediator->filter($this->ctrl->getFormAction(
99 $this,
100 'render',
101 '',
102 true
103 ));
104
105 $tbl = new ilCronManagerTableGUI(
106 $this,
107 'render',
108 $this->dic,
109 $this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)
110 );
111 $this->tpl->setContent(implode('', [
112 $this->uiRenderer->render([$message, $filter]),
113 $tbl->populate(
114 $tableFilterMediator->filteredJobs(
115 $filter
116 )
117 )->getHTML()
118 ]));
119 }
120
121 public function edit(ilPropertyFormGUI $a_form = null)
122 {
123 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
124 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
125 }
126
127 $id = $_REQUEST["jid"];
128 if (!$id) {
129 $this->ctrl->redirect($this, "render");
130 }
131
132 if (!$a_form) {
133 $a_form = $this->initEditForm($id);
134 }
135
136 $this->tpl->setContent($a_form->getHTML());
137 }
138
144 protected function getScheduleTypeFormElementName(int $scheduleTypeId)
145 {
146 switch ($scheduleTypeId) {
148 return $this->lng->txt('cron_schedule_daily');
149
151 return $this->lng->txt('cron_schedule_weekly');
152
154 return $this->lng->txt('cron_schedule_monthly');
155
157 return $this->lng->txt('cron_schedule_quarterly');
158
160 return $this->lng->txt('cron_schedule_yearly');
161
163 return sprintf($this->lng->txt('cron_schedule_in_minutes'), 'x');
164
166 return sprintf($this->lng->txt('cron_schedule_in_hours'), 'x');
167
169 return sprintf($this->lng->txt('cron_schedule_in_days'), 'x');
170 }
171
172 throw new \InvalidArgumentException(sprintf('The passed argument %s is invalid!', var_export($scheduleTypeId, 1)));
173 }
174
180 protected function getScheduleValueFormElementName(int $scheduleTypeId)
181 {
182 switch ($scheduleTypeId) {
184 return 'smini';
185
187 return 'shri';
188
190 return 'sdyi';
191 }
192
193 throw new \InvalidArgumentException(sprintf('The passed argument %s is invalid!', var_export($scheduleTypeId, 1)));
194 }
195
200 protected function hasScheduleValue(int $scheduleTypeId) : bool
201 {
202 return in_array(
203 $scheduleTypeId,
204 [
208 ]
209 );
210 }
211
212 protected function initEditForm($a_job_id)
213 {
214 $job = ilCronManager::getJobInstanceById($a_job_id);
215 if (!$job) {
216 $this->ctrl->redirect($this, "render");
217 }
218
219 $this->ctrl->setParameter($this, "jid", $a_job_id);
220
221 $jobData = ilCronManager::getCronJobData($job->getId());
222 $data = array_pop($jobData);
223
224 include_once("Services/Cron/classes/class.ilCronJob.php");
225 include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
226 $form = new ilPropertyFormGUI();
227 $form->setFormAction($this->ctrl->getFormAction($this, "update"));
228 $form->setTitle($this->lng->txt("cron_action_edit") . ': "' . $job->getTitle() . '"');
229
230 if ($job->hasFlexibleSchedule()) {
231 $type = new ilRadioGroupInputGUI($this->lng->txt('cron_schedule_type'), 'type');
232 $type->setRequired(true);
233 $type->setValue($data['schedule_type']);
234
235 foreach ($job->getAllScheduleTypes() as $typeId) {
236 if (!in_array($typeId, $job->getValidScheduleTypes())) {
237 continue;
238 }
239
240 $option = new ilRadioOption(
241 $this->getScheduleTypeFormElementName($typeId),
242 $typeId
243 );
244 $type->addOption($option);
245
246 if (in_array($typeId, $job->getScheduleTypesWithValues())) {
247 $scheduleValue = new ilNumberInputGUI(
248 $this->lng->txt('cron_schedule_value'),
249 $this->getScheduleValueFormElementName($typeId)
250 );
251 $scheduleValue->allowDecimals(false);
252 $scheduleValue->setRequired(true);
253 $scheduleValue->setSize(5);
254 if ($data['schedule_type'] == $typeId) {
255 $scheduleValue->setValue($data['schedule_value']);
256 }
257 $option->addSubItem($scheduleValue);
258 }
259 }
260
261 $form->addItem($type);
262 }
263
264 if ($job->hasCustomSettings()) {
265 $job->addCustomSettingsToForm($form);
266 }
267
268 $form->addCommandButton("update", $this->lng->txt("save"));
269 $form->addCommandButton("render", $this->lng->txt("cancel"));
270
271 return $form;
272 }
273
274 public function update()
275 {
276 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
277 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
278 }
279
280 $id = $_REQUEST["jid"];
281 if (!$id) {
282 $this->ctrl->redirect($this, "render");
283 }
284
285 $form = $this->initEditForm($id);
286 if ($form->checkInput()) {
288 if ($job) {
289 $valid = true;
290 if ($job->hasCustomSettings() &&
291 !$job->saveCustomSettings($form)) {
292 $valid = false;
293 }
294
295 if ($valid && $job->hasFlexibleSchedule()) {
296 $type = $form->getInput("type");
297 switch (true) {
298 case $this->hasScheduleValue($type):
299 $value = $form->getInput($this->getScheduleValueFormElementName($type));
300 break;
301
302 default:
303 $value = null;
304 break;
305 }
306
308 }
309 if ($valid) {
310 ilUtil::sendSuccess($this->lng->txt("cron_action_edit_success"), true);
311 $this->ctrl->redirect($this, "render");
312 }
313 }
314 }
315
316 $form->setValuesByPost();
317 $this->edit($form);
318 }
319
320 public function run()
321 {
322 $this->confirm("run");
323 }
324
325 public function confirmedRun()
326 {
327 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
328 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
329 }
330
331 $job_id = $_GET["jid"];
332 if ($job_id) {
333 if (ilCronManager::runJobManual($job_id)) {
334 ilUtil::sendSuccess($this->lng->txt("cron_action_run_success"), true);
335 } else {
336 ilUtil::sendFailure($this->lng->txt("cron_action_run_fail"), true);
337 }
338 }
339
340 $this->ctrl->redirect($this, "render");
341 }
342
343 public function activate()
344 {
345 $this->confirm("activate");
346 }
347
348 public function confirmedActivate()
349 {
350 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
351 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
352 }
353
354 $jobs = $this->getMultiActionData();
355 if ($jobs) {
356 foreach ($jobs as $job) {
357 if (ilCronManager::isJobInactive($job->getId())) {
359 ilCronManager::activateJob($job, true);
360 }
361 }
362
363 ilUtil::sendSuccess($this->lng->txt("cron_action_activate_success"), true);
364 }
365
366 $this->ctrl->redirect($this, "render");
367 }
368
369 public function deactivate()
370 {
371 $this->confirm("deactivate");
372 }
373
374 public function confirmedDeactivate()
375 {
376 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
377 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
378 }
379
380 $jobs = $this->getMultiActionData();
381 if ($jobs) {
382 foreach ($jobs as $job) {
383 if (ilCronManager::isJobActive($job->getId())) {
385 }
386 }
387
388 ilUtil::sendSuccess($this->lng->txt("cron_action_deactivate_success"), true);
389 }
390
391 $this->ctrl->redirect($this, "render");
392 }
393
394 public function reset()
395 {
396 $this->confirm("reset");
397 }
398
399 public function confirmedReset()
400 {
401 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
402 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
403 }
404
405 $jobs = $this->getMultiActionData();
406 if ($jobs) {
407 foreach ($jobs as $job) {
409 }
410 ilUtil::sendSuccess($this->lng->txt("cron_action_reset_success"), true);
411 }
412
413 $this->ctrl->redirect($this, "render");
414 }
415
416 protected function getMultiActionData()
417 {
418 $res = array();
419
420 if ($_REQUEST["jid"]) {
421 $job_id = trim($_REQUEST["jid"]);
422 $job = ilCronManager::getJobInstanceById($job_id);
423 if ($job) {
424 $res[$job_id] = $job;
425 }
426 } elseif (is_array($_REQUEST["mjid"])) {
427 foreach ($_REQUEST["mjid"] as $job_id) {
428 $job = ilCronManager::getJobInstanceById($job_id);
429 if ($job) {
430 $res[$job_id] = $job;
431 }
432 }
433 }
434
435 return $res;
436 }
437
438 protected function confirm($a_action)
439 {
440 if (!$this->rbac->system()->checkAccess('write', SYSTEM_FOLDER_ID)) {
441 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
442 }
443
444 $jobs = $this->getMultiActionData();
445 if (!$jobs) {
446 $this->ctrl->redirect($this, "render");
447 }
448
449 if ('run' == $a_action) {
450 // Filter jobs which are not indented to be executed manually
451 $jobs = array_filter($jobs, function ($job) {
455 return $job->isManuallyExecutable();
456 });
457
458 if (0 == count($jobs)) {
459 ilUtil::sendFailure($this->lng->txt('cron_no_executable_job_selected'), true);
460 $this->ctrl->redirect($this, 'render');
461 }
462 }
463
464 include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
465 $cgui = new ilConfirmationGUI();
466
467 if (sizeof($jobs) == 1) {
468 $jobKeys = array_keys($jobs);
469 $job_id = array_pop($jobKeys);
470 $job = array_pop($jobs);
471 $title = $job->getTitle();
472 if (!$title) {
473 $title = preg_replace("[^A-Za-z0-9_\-]", "", $job->getId());
474 }
475
476 $cgui->setHeaderText(sprintf(
477 $this->lng->txt("cron_action_" . $a_action . "_sure"),
478 $title
479 ));
480
481 $this->ctrl->setParameter($this, "jid", $job_id);
482 } else {
483 $cgui->setHeaderText($this->lng->txt("cron_action_" . $a_action . "_sure_multi"));
484
485 foreach ($jobs as $job_id => $job) {
486 $cgui->addItem("mjid[]", $job_id, $job->getTitle());
487 }
488 }
489
490 $cgui->setFormAction($this->ctrl->getFormAction($this, "confirmed" . ucfirst($a_action)));
491 $cgui->setCancel($this->lng->txt("cancel"), "render");
492 $cgui->setConfirm($this->lng->txt("cron_action_" . $a_action), "confirmed" . ucfirst($a_action));
493
494 $this->tpl->setContent($cgui->getHTML());
495 }
496
497 public function addToExternalSettingsForm($a_form_id)
498 {
499 $fields = array();
500
502 foreach ($data as $item) {
504 $item["job_id"],
505 $item["component"],
506 $item["class"],
507 $item["path"]
508 );
509
510 if (method_exists($job, "addToExternalSettingsForm")) {
511 $job->addToExternalSettingsForm($a_form_id, $fields, $item["job_status"]);
512 }
513 }
514
515 if (sizeof($fields)) {
516 return array("cron_jobs" => array("jumpToCronJobs", $fields));
517 }
518 }
519}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
error($a_errmsg)
set error message @access public
Confirmation screen class.
Class ilCronJobRepositoryImpl.
const SCHEDULE_TYPE_IN_DAYS
const SCHEDULE_TYPE_IN_HOURS
const SCHEDULE_TYPE_IN_MINUTES
const SCHEDULE_TYPE_WEEKLY
const SCHEDULE_TYPE_YEARLY
const SCHEDULE_TYPE_DAILY
const SCHEDULE_TYPE_QUARTERLY
const SCHEDULE_TYPE_MONTHLY
Class ilCronManagerGUI.
addToExternalSettingsForm($a_form_id)
getScheduleTypeFormElementName(int $scheduleTypeId)
edit(ilPropertyFormGUI $a_form=null)
getScheduleValueFormElementName(int $scheduleTypeId)
__construct()
ilCronManagerGUI constructor.
hasScheduleValue(int $scheduleTypeId)
List all active cron jobs.
static getCronJobData($a_id=null, $a_include_inactive=true)
Get cron job configuration/execution data.
static isJobInactive($a_job_id)
Check if given job is currently inactive.
static getJobInstanceById($a_job_id)
Get job instance (by job id)
static runJobManual($a_job_id)
Run single job manually.
static activateJob(ilCronJob $a_job, $a_manual=false)
Activate cron job.
static getJobInstance($a_id, $a_component, $a_class, $a_path=null)
Get job instance (by job data)
static isJobActive($a_job_id)
Check if given job is currently active.
static deactivateJob(ilCronJob $a_job, $a_manual=false)
Deactivate cron job.
static resetJob(ilCronJob $a_job)
Reset job.
static updateJobSchedule(ilCronJob $a_job, $a_schedule_type, $a_schedule_value)
Update job schedule.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
@classDescription Date and time handling
This class represents a number property in a property form.
This class represents a property form user interface.
This class represents a property in a property form.
This class represents an option in a radio group.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
const SYSTEM_FOLDER_ID
Definition: constants.php:33
$valid
global $DIC
Definition: goto.php:24
This is how the factory for UI elements looks.
Definition: Factory.php:18
An entity that renders components to a string output.
Definition: Renderer.php:15
$type
repository()
Definition: repository.php:5
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2
$data
Definition: storeScorm.php:23
$message
Definition: xapiexit.php:14