ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilCronManagerGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "Services/Cron/classes/class.ilCronManager.php";
6 
17 {
21  protected $lng;
22 
26  protected $ctrl;
27 
31  protected $settings;
32 
36  protected $tpl;
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 
50  $this->lng->loadLanguageModule('cron');
51  }
52 
53  public function executeCommand()
54  {
55  $class = $this->ctrl->getNextClass($this);
56 
57  switch ($class) {
58  case "ilpropertyformgui":
59  $form = $this->initEditForm($_REQUEST['jid']);
60  $this->ctrl->forwardCommand($form);
61  break;
62  }
63  $cmd = $this->ctrl->getCmd("render");
64  $this->$cmd();
65 
66  return true;
67  }
68 
69  protected function render()
70  {
71  if ($this->settings->get('last_cronjob_start_ts')) {
72  $tstamp = ilDatePresentation::formatDate(new ilDateTime($this->settings->get('last_cronjob_start_ts'), IL_CAL_UNIX));
73  } else {
74  $tstamp = $this->lng->txt('cronjob_last_start_unknown');
75  }
76  ilUtil::sendInfo($this->lng->txt('cronjob_last_start') . ": " . $tstamp);
77 
78  include_once "Services/Cron/classes/class.ilCronManagerTableGUI.php";
79  $tbl = new ilCronManagerTableGUI($this, "render");
80  $this->tpl->setContent($tbl->getHTML());
81  }
82 
83  public function edit(ilPropertyFormGUI $a_form = null)
84  {
85  $id = $_REQUEST["jid"];
86  if (!$id) {
87  $this->ctrl->redirect($this, "render");
88  }
89 
90  if (!$a_form) {
91  $a_form = $this->initEditForm($id);
92  }
93 
94  $this->tpl->setContent($a_form->getHTML());
95  }
96 
102  protected function getScheduleTypeFormElementName(int $scheduleTypeId)
103  {
104  switch ($scheduleTypeId) {
106  return $this->lng->txt('cron_schedule_daily');
107 
109  return $this->lng->txt('cron_schedule_weekly');
110 
112  return $this->lng->txt('cron_schedule_monthly');
113 
115  return $this->lng->txt('cron_schedule_quarterly');
116 
118  return $this->lng->txt('cron_schedule_yearly');
119 
121  return sprintf($this->lng->txt('cron_schedule_in_minutes'), 'x');
122 
124  return sprintf($this->lng->txt('cron_schedule_in_hours'), 'x');
125 
127  return sprintf($this->lng->txt('cron_schedule_in_days'), 'x');
128  }
129 
130  throw new \InvalidArgumentException(sprintf('The passed argument %s is invalid!', var_export($scheduleTypeId, 1)));
131  }
132 
138  protected function getScheduleValueFormElementName(int $scheduleTypeId)
139  {
140  switch ($scheduleTypeId) {
142  return 'smini';
143 
145  return 'shri';
146 
148  return 'sdyi';
149  }
150 
151  throw new \InvalidArgumentException(sprintf('The passed argument %s is invalid!', var_export($scheduleTypeId, 1)));
152  }
153 
158  protected function hasScheduleValue(int $scheduleTypeId) : bool
159  {
160  return in_array(
161  $scheduleTypeId,
162  [
166  ]
167  );
168  }
169 
170  protected function initEditForm($a_job_id)
171  {
172  $job = ilCronManager::getJobInstanceById($a_job_id);
173  if (!$job) {
174  $this->ctrl->redirect($this, "render");
175  }
176 
177  $this->ctrl->setParameter($this, "jid", $a_job_id);
178 
179  $data = array_pop(ilCronManager::getCronJobData($job->getId()));
180 
181  include_once("Services/Cron/classes/class.ilCronJob.php");
182  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
183  $form = new ilPropertyFormGUI();
184  $form->setFormAction($this->ctrl->getFormAction($this, "update"));
185  $form->setTitle($this->lng->txt("cron_action_edit") . ': "' . $job->getTitle() . '"');
186 
187  if ($job->hasFlexibleSchedule()) {
188  $type = new ilRadioGroupInputGUI($this->lng->txt('cron_schedule_type'), 'type');
189  $type->setRequired(true);
190  $type->setValue($data['schedule_type']);
191 
192  foreach ($job->getAllScheduleTypes() as $typeId) {
193  if (!in_array($typeId, $job->getValidScheduleTypes())) {
194  continue;
195  }
196 
197  $option = new ilRadioOption(
198  $this->getScheduleTypeFormElementName($typeId),
199  $typeId
200  );
201  $type->addOption($option);
202 
203  if (in_array($typeId, $job->getScheduleTypesWithValues())) {
204  $scheduleValue = new ilNumberInputGUI(
205  $this->lng->txt('cron_schedule_value'),
206  $this->getScheduleValueFormElementName($typeId)
207  );
208  $scheduleValue->allowDecimals(false);
209  $scheduleValue->setRequired(true);
210  $scheduleValue->setSize(5);
211  if ($data['schedule_type'] == $typeId) {
212  $scheduleValue->setValue($data['schedule_value']);
213  }
214  $option->addSubItem($scheduleValue);
215  }
216  }
217 
218  $form->addItem($type);
219  }
220 
221  if ($job->hasCustomSettings()) {
222  $job->addCustomSettingsToForm($form);
223  }
224 
225  $form->addCommandButton("update", $this->lng->txt("save"));
226  $form->addCommandButton("render", $this->lng->txt("cancel"));
227 
228  return $form;
229  }
230 
231  public function update()
232  {
233  $id = $_REQUEST["jid"];
234  if (!$id) {
235  $this->ctrl->redirect($this, "render");
236  }
237 
238  $form = $this->initEditForm($id);
239  if ($form->checkInput()) {
241  if ($job) {
242  $valid = true;
243  if ($job->hasCustomSettings() &&
244  !$job->saveCustomSettings($form)) {
245  $valid = false;
246  }
247 
248  if ($valid && $job->hasFlexibleSchedule()) {
249  $type = $form->getInput("type");
250  switch (true) {
251  case $this->hasScheduleValue($type):
252  $value = $form->getInput($this->getScheduleValueFormElementName($type));
253  break;
254 
255  default:
256  $value = null;
257  break;
258  }
259 
261  }
262  if ($valid) {
263  ilUtil::sendSuccess($this->lng->txt("cron_action_edit_success"), true);
264  $this->ctrl->redirect($this, "render");
265  }
266  }
267  }
268 
269  $form->setValuesByPost();
270  $this->edit($form);
271  }
272 
273  public function run()
274  {
275  $this->confirm("run");
276  }
277 
278  public function confirmedRun()
279  {
280  $job_id = $_GET["jid"];
281  if ($job_id) {
282  if (ilCronManager::runJobManual($job_id)) {
283  ilUtil::sendSuccess($this->lng->txt("cron_action_run_success"), true);
284  } else {
285  ilUtil::sendFailure($this->lng->txt("cron_action_run_fail"), true);
286  }
287  }
288 
289  $this->ctrl->redirect($this, "render");
290  }
291 
292  public function activate()
293  {
294  $this->confirm("activate");
295  }
296 
297  public function confirmedActivate()
298  {
299  $jobs = $this->getMultiActionData();
300  if ($jobs) {
301  foreach ($jobs as $job) {
302  if (ilCronManager::isJobInactive($job->getId())) {
304  ilCronManager::activateJob($job, true);
305  }
306  }
307 
308  ilUtil::sendSuccess($this->lng->txt("cron_action_activate_success"), true);
309  }
310 
311  $this->ctrl->redirect($this, "render");
312  }
313 
314  public function deactivate()
315  {
316  $this->confirm("deactivate");
317  }
318 
319  public function confirmedDeactivate()
320  {
321  $jobs = $this->getMultiActionData();
322  if ($jobs) {
323  foreach ($jobs as $job) {
324  if (ilCronManager::isJobActive($job->getId())) {
325  ilCronManager::deactivateJob($job, true);
326  }
327  }
328 
329  ilUtil::sendSuccess($this->lng->txt("cron_action_deactivate_success"), true);
330  }
331 
332  $this->ctrl->redirect($this, "render");
333  }
334 
335  public function reset()
336  {
337  $this->confirm("reset");
338  }
339 
340  public function confirmedReset()
341  {
342  $jobs = $this->getMultiActionData();
343  if ($jobs) {
344  foreach ($jobs as $job) {
346  }
347  ilUtil::sendSuccess($this->lng->txt("cron_action_reset_success"), true);
348  }
349 
350  $this->ctrl->redirect($this, "render");
351  }
352 
353  protected function getMultiActionData()
354  {
355  $res = array();
356 
357  if ($_REQUEST["jid"]) {
358  $job_id = trim($_REQUEST["jid"]);
359  $job = ilCronManager::getJobInstanceById($job_id);
360  if ($job) {
361  $res[$job_id] = $job;
362  }
363  } elseif (is_array($_REQUEST["mjid"])) {
364  foreach ($_REQUEST["mjid"] as $job_id) {
365  $job = ilCronManager::getJobInstanceById($job_id);
366  if ($job) {
367  $res[$job_id] = $job;
368  }
369  }
370  }
371 
372  return $res;
373  }
374 
375  protected function confirm($a_action)
376  {
377  $jobs = $this->getMultiActionData();
378  if (!$jobs) {
379  $this->ctrl->redirect($this, "render");
380  }
381 
382  if ('run' == $a_action) {
383  // Filter jobs which are not indented to be executed manually
384  $jobs = array_filter($jobs, function ($job) {
388  return $job->isManuallyExecutable();
389  });
390 
391  if (0 == count($jobs)) {
392  ilUtil::sendFailure($this->lng->txt('cron_no_executable_job_selected'), true);
393  $this->ctrl->redirect($this, 'render');
394  }
395  }
396 
397  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
398  $cgui = new ilConfirmationGUI();
399 
400  if (sizeof($jobs) == 1) {
401  $job_id = array_pop(array_keys($jobs));
402  $job = array_pop($jobs);
403  $title = $job->getTitle();
404  if (!$title) {
405  $title = preg_replace("[^A-Za-z0-9_\-]", "", $job->getId());
406  }
407 
408  $cgui->setHeaderText(sprintf(
409  $this->lng->txt("cron_action_" . $a_action . "_sure"),
410  $title
411  ));
412 
413  $this->ctrl->setParameter($this, "jid", $job_id);
414  } else {
415  $cgui->setHeaderText($this->lng->txt("cron_action_" . $a_action . "_sure_multi"));
416 
417  foreach ($jobs as $job_id => $job) {
418  $cgui->addItem("mjid[]", $job_id, $job->getTitle());
419  }
420  }
421 
422  $cgui->setFormAction($this->ctrl->getFormAction($this, "confirmed" . ucfirst($a_action)));
423  $cgui->setCancel($this->lng->txt("cancel"), "render");
424  $cgui->setConfirm($this->lng->txt("cron_action_" . $a_action), "confirmed" . ucfirst($a_action));
425 
426  $this->tpl->setContent($cgui->getHTML());
427  }
428 
429  public function addToExternalSettingsForm($a_form_id)
430  {
431  $fields = array();
432 
434  foreach ($data as $item) {
436  $item["job_id"],
437  $item["component"],
438  $item["class"],
439  $item["path"]
440  );
441 
442  if (method_exists($job, "addToExternalSettingsForm")) {
443  $job->addToExternalSettingsForm($a_form_id, $fields, $item["job_status"]);
444  }
445  }
446 
447  if (sizeof($fields)) {
448  return array("cron_jobs" => array("jumpToCronJobs", $fields));
449  }
450  }
451 }
static getJobInstance($a_id, $a_component, $a_class, $a_path=null)
Get job instance (by job data)
This class represents an option in a radio group.
settings()
Definition: settings.php:2
getScheduleValueFormElementName(int $scheduleTypeId)
This class represents a property form user interface.
$type
global $DIC
Definition: saml.php:7
$_GET["client_id"]
$tbl
Definition: example_048.php:81
static deactivateJob(ilCronJob $a_job, $a_manual=false)
Deactivate cron job.
$valid
if(!array_key_exists('StateId', $_REQUEST)) $id
const SCHEDULE_TYPE_IN_MINUTES
static activateJob(ilCronJob $a_job, $a_manual=false)
Activate cron job.
static isJobInactive($a_job_id)
Check if given job is currently inactive.
static runJobManual($a_job_id)
Run single job manually.
const IL_CAL_UNIX
const SCHEDULE_TYPE_MONTHLY
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
const SCHEDULE_TYPE_WEEKLY
static resetJob(ilCronJob $a_job)
Reset job.
allowDecimals($a_value)
Toggle Decimals.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
addToExternalSettingsForm($a_form_id)
static getCronJobData($a_id=null, $a_include_inactive=true)
Get cron job configuration/execution data.
This class represents a property in a property form.
foreach($_POST as $key=> $value) $res
static getJobInstanceById($a_job_id)
Get job instance (by job id)
if(isset($_POST['submit'])) $form
This class represents a number property in a property form.
const SCHEDULE_TYPE_IN_DAYS
Date and time handling
hasScheduleValue(int $scheduleTypeId)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
__construct()
ilCronManagerGUI constructor.
const SCHEDULE_TYPE_YEARLY
edit(ilPropertyFormGUI $a_form=null)
const SCHEDULE_TYPE_DAILY
const SCHEDULE_TYPE_QUARTERLY
Class ilCronManagerGUI.
getScheduleTypeFormElementName(int $scheduleTypeId)
static updateJobSchedule(ilCronJob $a_job, $a_schedule_type, $a_schedule_value)
Update job schedule.
static isJobActive($a_job_id)
Check if given job is currently active.
const SCHEDULE_TYPE_IN_HOURS
List all active cron jobs.
Confirmation screen class.
$data
Definition: bench.php:6