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