ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 {
18  function executeCommand()
19  {
20  global $ilCtrl, $lng;
21 
22  $lng->loadLanguageModule("cron");
23 
24  $cmd = $ilCtrl->getCmd("render");
25  $this->$cmd();
26 
27  return true;
28  }
29 
30  function render()
31  {
32  global $tpl, $ilSetting, $lng;
33 
34  if($ilSetting->get('last_cronjob_start_ts'))
35  {
36  $tstamp = ilDatePresentation::formatDate(new ilDateTime($ilSetting->get('last_cronjob_start_ts'), IL_CAL_UNIX));
37  }
38  else
39  {
40  $tstamp = $lng->txt('cronjob_last_start_unknown');
41  }
42  ilUtil::sendInfo($lng->txt('cronjob_last_start').": ".$tstamp);
43 
44  include_once "Services/Cron/classes/class.ilCronManagerTableGUI.php";
45  $tbl = new ilCronManagerTableGUI($this, "render");
46  $tpl->setContent($tbl->getHTML());
47  }
48 
49  function edit(ilPropertyFormGUI $a_form = null)
50  {
51  global $ilCtrl, $tpl;
52 
53  $id = $_REQUEST["jid"];
54  if(!$id)
55  {
56  $ilCtrl->redirect($this, "render");
57  }
58 
59  if(!$a_form)
60  {
61  $a_form = $this->initEditForm($id);
62  }
63 
64  $tpl->setContent($a_form->getHTML());
65  }
66 
67  function initEditForm($a_job_id)
68  {
69  global $ilCtrl, $lng;
70 
71  $job = ilCronManager::getJobInstanceById($a_job_id);
72  if(!$job)
73  {
74  $ilCtrl->redirect($this, "render");
75  }
76 
77  $ilCtrl->setParameter($this, "jid", $a_job_id);
78 
79  $data = array_pop(ilCronManager::getCronJobData($job->getId()));
80 
81  include_once("Services/Cron/classes/class.ilCronJob.php");
82  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
83  $form = new ilPropertyFormGUI();
84  $form->setFormAction($ilCtrl->getFormAction($this, "update"));
85  $form->setTitle($lng->txt("cron_action_edit").': "'.$job->getTitle().'"');
86 
87  if($job->hasFlexibleSchedule())
88  {
89  $type = new ilRadioGroupInputGUI($lng->txt("cron_schedule_type"), "type");
90  $type->setRequired(true);
91  $type->setValue($data["schedule_type"]);
92  $type->addOption(new ilRadioOption($lng->txt("cron_schedule_daily"), ilCronJob::SCHEDULE_TYPE_DAILY));
93  $type->addOption(new ilRadioOption($lng->txt("cron_schedule_weekly"), ilCronJob::SCHEDULE_TYPE_WEEKLY));
94  $type->addOption(new ilRadioOption($lng->txt("cron_schedule_monthly"), ilCronJob::SCHEDULE_TYPE_MONTHLY));
95  $type->addOption(new ilRadioOption($lng->txt("cron_schedule_quarterly"), ilCronJob::SCHEDULE_TYPE_QUARTERLY));
96  $type->addOption(new ilRadioOption($lng->txt("cron_schedule_yearly"), ilCronJob::SCHEDULE_TYPE_YEARLY));
97 
98  $min = new ilRadioOption(sprintf($lng->txt("cron_schedule_in_minutes"), "x"),
100  $mini = new ilNumberInputGUI($lng->txt("cron_schedule_value"), "smini");
101  $mini->setRequired(true);
102  $mini->setSize(5);
103  if($data["schedule_type"] == ilCronJob::SCHEDULE_TYPE_IN_MINUTES)
104  {
105  $mini->setValue($data["schedule_value"]);
106  }
107  $min->addSubItem($mini);
108  $type->addOption($min);
109 
110  $hr = new ilRadioOption(sprintf($lng->txt("cron_schedule_in_hours"), "x"),
112  $hri = new ilNumberInputGUI($lng->txt("cron_schedule_value"), "shri");
113  $hri->setRequired(true);
114  $hri->setSize(5);
115  if($data["schedule_type"] == ilCronJob::SCHEDULE_TYPE_IN_HOURS)
116  {
117  $hri->setValue($data["schedule_value"]);
118  }
119  $hr->addSubItem($hri);
120  $type->addOption($hr);
121 
122  $dy = new ilRadioOption(sprintf($lng->txt("cron_schedule_in_days"), "x"),
124  $dyi = new ilNumberInputGUI($lng->txt("cron_schedule_value"), "sdyi");
125  $dyi->setRequired(true);
126  $dyi->setSize(5);
127  if($data["schedule_type"] == ilCronJob::SCHEDULE_TYPE_IN_DAYS)
128  {
129  $dyi->setValue($data["schedule_value"]);
130  }
131  $dy->addSubItem($dyi);
132  $type->addOption($dy);
133 
134  $form->addItem($type);
135  }
136 
137  if($job->hasCustomSettings())
138  {
139  $job->addCustomSettingsToForm($form);
140  }
141 
142  $form->addCommandButton("update", $lng->txt("save"));
143  $form->addCommandButton("render", $lng->txt("cancel"));
144 
145  return $form;
146  }
147 
148  function update()
149  {
150  global $ilCtrl, $lng;
151 
152  $id = $_REQUEST["jid"];
153  if(!$id)
154  {
155  $ilCtrl->redirect($this, "render");
156  }
157 
158  $form = $this->initEditForm($id);
159  if($form->checkInput())
160  {
162  if($job)
163  {
164  $valid = true;
165  if($job->hasCustomSettings() &&
166  !$job->saveCustomSettings($form))
167  {
168  $valid = false;
169  }
170 
171  if($valid && $job->hasFlexibleSchedule())
172  {
173  $type = $form->getInput("type");
174  switch($type)
175  {
177  $value = $form->getInput("smini");
178  break;
179 
181  $value = $form->getInput("shri");
182  break;
183 
185  $value = $form->getInput("sdyi");
186  break;
187 
188  default:
189  $value = null;
190  }
191 
192  ilCronManager::updateJobSchedule($job, $type, $value);
193  }
194  if($valid)
195  {
196  ilUtil::sendSuccess($lng->txt("cron_action_edit_success"), true);
197  $ilCtrl->redirect($this, "render");
198  }
199  }
200  }
201 
202  $form->setValuesByPost();
203  $this->edit($form);
204  }
205 
206  function run()
207  {
208  $this->confirm("run");
209  }
210 
211  function confirmedRun()
212  {
213  global $ilCtrl, $lng;
214 
215  $job_id = $_GET["jid"];
216  if($job_id)
217  {
218  if(ilCronManager::runJobManual($job_id))
219  {
220  ilUtil::sendSuccess($lng->txt("cron_action_run_success"), true);
221  }
222  else
223  {
224  ilUtil::sendFailure($lng->txt("cron_action_run_fail"), true);
225  }
226  }
227 
228  $ilCtrl->redirect($this, "render");
229  }
230 
231  function activate()
232  {
233  $this->confirm("activate");
234  }
235 
236  function confirmedActivate()
237  {
238  global $ilCtrl, $lng;
239 
240  $jobs = $this->getMultiActionData();
241  if($jobs)
242  {
243  foreach($jobs as $job)
244  {
245  if(ilCronManager::isJobInactive($job->getId()))
246  {
248  ilCronManager::activateJob($job, true);
249  }
250  }
251 
252  ilUtil::sendSuccess($lng->txt("cron_action_activate_success"), true);
253  }
254 
255  $ilCtrl->redirect($this, "render");
256  }
257 
258  function deactivate()
259  {
260  $this->confirm("deactivate");
261  }
262 
264  {
265  global $ilCtrl, $lng;
266 
267  $jobs = $this->getMultiActionData();
268  if($jobs)
269  {
270  foreach($jobs as $job)
271  {
272  if(ilCronManager::isJobActive($job->getId()))
273  {
274  ilCronManager::deactivateJob($job, true);
275  }
276  }
277 
278  ilUtil::sendSuccess($lng->txt("cron_action_deactivate_success"), true);
279  }
280 
281  $ilCtrl->redirect($this, "render");
282  }
283 
284  function reset()
285  {
286  $this->confirm("reset");
287  }
288 
289  function confirmedReset()
290  {
291  global $ilCtrl, $lng;
292 
293  $jobs = $this->getMultiActionData();
294  if($jobs)
295  {
296  foreach($jobs as $job)
297  {
298  if(ilCronManager::isJobActive($job->getId()))
299  {
301  }
302  }
303  ilUtil::sendSuccess($lng->txt("cron_action_reset_success"), true);
304  }
305 
306  $ilCtrl->redirect($this, "render");
307  }
308 
309  protected function getMultiActionData()
310  {
311  $res = array();
312 
313  if($_REQUEST["jid"])
314  {
315  $job_id = trim($_REQUEST["jid"]);
316  $job = ilCronManager::getJobInstanceById($job_id);
317  if($job)
318  {
319  $res[$job_id] = $job;
320  }
321  }
322  else if(is_array($_REQUEST["mjid"]))
323  {
324  foreach($_REQUEST["mjid"] as $job_id)
325  {
326  $job = ilCronManager::getJobInstanceById($job_id);
327  if($job)
328  {
329  $res[$job_id] = $job;
330  }
331  }
332  }
333 
334  return $res;
335  }
336 
337  protected function confirm($a_action)
338  {
339  global $ilCtrl, $tpl, $lng;
340 
341  $jobs = $this->getMultiActionData();
342  if(!$jobs)
343  {
344  $ilCtrl->redirect($this, "render");
345  }
346 
347  if('run' == $a_action)
348  {
349  // Filter jobs which are not indented to be executed manually
350  $jobs = array_filter($jobs, function ($job) {
354  return $job->isManuallyExecutable();
355  });
356 
357  if(0 == count($jobs))
358  {
359  ilUtil::sendFailure($lng->txt('cron_no_executable_job_selected'), true);
360  $ilCtrl->redirect($this, 'render');
361  }
362  }
363 
364  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
365  $cgui = new ilConfirmationGUI();
366 
367  if(sizeof($jobs) == 1)
368  {
369  $job_id = array_pop(array_keys($jobs));
370  $job = array_pop($jobs);
371  $title = $job->getTitle();
372  if(!$title)
373  {
374  $title = preg_replace("[^A-Za-z0-9_\-]", "", $job->getId());
375  }
376 
377  $cgui->setHeaderText(sprintf($lng->txt("cron_action_".$a_action."_sure"),
378  $title));
379 
380  $ilCtrl->setParameter($this, "jid", $job_id);
381  }
382  else
383  {
384  $cgui->setHeaderText($lng->txt("cron_action_".$a_action."_sure_multi"));
385 
386  foreach($jobs as $job_id => $job)
387  {
388  $cgui->addItem("mjid[]", $job_id, $job->getTitle());
389  }
390  }
391 
392  $cgui->setFormAction($ilCtrl->getFormAction($this, "confirmed".ucfirst($a_action)));
393  $cgui->setCancel($lng->txt("cancel"), "render");
394  $cgui->setConfirm($lng->txt("cron_action_".$a_action), "confirmed".ucfirst($a_action));
395 
396  $tpl->setContent($cgui->getHTML());
397  }
398 
399  public function addToExternalSettingsForm($a_form_id)
400  {
401  $fields = array();
402 
404  foreach($data as $item)
405  {
406  $job = ilCronManager::getJobInstance($item["job_id"],
407  $item["component"], $item["class"], $item["path"]);
408 
409  if(method_exists($job, "addToExternalSettingsForm"))
410  {
411  $job->addToExternalSettingsForm($a_form_id, $fields, $item["job_status"]);
412  }
413  }
414 
415  if(sizeof($fields))
416  {
417  return array("cron_jobs"=>array("jumpToCronJobs", $fields));
418  }
419  }
420 }
421 
422 ?>
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
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.
This class represents a property form user interface.
$_GET["client_id"]
$tbl
Definition: example_048.php:81
static deactivateJob(ilCronJob $a_job, $a_manual=false)
Deactivate cron job.
$valid
$cmd
Definition: sahs_server.php:35
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
const SCHEDULE_TYPE_WEEKLY
static resetJob(ilCronJob $a_job)
Reset job.
global $tpl
Definition: ilias.php:8
global $ilCtrl
Definition: ilias.php:18
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.
static getJobInstanceById($a_job_id)
Get job instance (by job id)
This class represents a number property in a property form.
static formatDate(ilDateTime $date)
Format a date public.
const SCHEDULE_TYPE_IN_DAYS
Date and time handling
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
const SCHEDULE_TYPE_YEARLY
global $ilSetting
Definition: privfeed.php:17
global $lng
Definition: privfeed.php:17
edit(ilPropertyFormGUI $a_form=null)
const SCHEDULE_TYPE_DAILY
const SCHEDULE_TYPE_QUARTERLY
Class ilCronManagerGUI.
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
setRequired($a_required)
Set Required.
List all active cron jobs.
Confirmation screen class.