Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_edit_fns.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
18 
31 {
32 
33 
39  function __construct()
40  {
41  parent::__construct();
42 
43  $this->static_screens = Array(
44  'details' => Array(
45  'name' => translate('details'),
46  'force_unlock' => TRUE,
47  ),
48  'logs' => Array(
49  'name' => translate('logs'),
50  'force_unlock' => TRUE,
51  ),
52  );
53 
54  }//end constructor
55 
56 
69  public function _paintWhenBox(Cron_Job $asset, Backend_Outputter $o, $prefix, $when='', $form_extra_elements='')
70  {
71  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
72  if (is_null($cron_mgr)) return FALSE;
73 
74  if ((int) $cron_mgr->attr('refresh_time') <= 0) {
75  echo translate('cron_manager_refresh_unknown');
76  return;
77  }
78 
79  $write_access = $asset->writeAccess('attributes');
80  $prefix .= '_when';
81  $one_off = ($asset->attr('type') == 'one_off');
82  $repeating = !$one_off; // for easier reading
83 
84  $when_str = ($when == '') ? $asset->attr('when') : $when;
85  $when_arr = Cron_Job::getWhenArr($when_str);
86  $now = getdate();
87 
88  // work out the allowed hour/minute based on the cron manager refresh time
89  $cron_mgr_edit_fns = $cron_mgr->getEditFns();
90  $hours_mins = $cron_mgr_edit_fns->getWhenBoxHoursMins($cron_mgr, $one_off);
91  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($cron_mgr);
92 
93  // prepare the drop down box for a repeating cron job with time interval (TI)
94  if ($repeating) {
95 
96  $time_unit = Array(
97  60 => translate('minutes'),
98  3600 => translate('hours'),
99  86400 => translate('days'),
100  );
101 
102  $mins = Array('--' => '--');
103  $hours = Array('--' => '--');
104 
105  // setup the interval in minute
106  if (count($hours_mins['mins']) > 1) {
107  foreach ($hours_mins['mins'] as $i) {
108  $mins[$i] = $i;
109  }
110  unset($mins['0']);
111  } else {
112  unset($time_unit[60]);
113  }
114 
115  // setup the interval in hour
116  if (count($hours_mins['hours']) > 1) {
117  foreach ($hours_mins['hours'] as $i) {
118  $hours[$i] = $i;
119  }
120  unset($hours['0']);
121  } else {
122  unset($time_unit[3600]);
123  }
124 
125  combo_box($prefix.'_interval_mins', $mins, FALSE, $mins['--'], NULL, $form_extra_elements);
126  combo_box($prefix.'_interval_hours', $hours, FALSE, $hours['--'], NULL, $form_extra_elements);
127  text_box($prefix.'_interval_days', '-', 2, '', TRUE, $form_extra_elements);
128  echo '&nbsp;&nbsp;';
129 
130  // time interval unit selection, minute/hour/day (based on cron manager's refresh time)
131  // javascript will show/hide selection based on the refresh time setting
132  $onChange = 'onChange="switchInterval(this.value)"';
133  combo_box($prefix.'_interval_unit', $time_unit, FALSE, current($time_unit), NULL, $onChange.$form_extra_elements);
134  echo '<br />';
135 
136  // javascipt to show/hide input based on the current time unit selection
137  ?>
138  <script type="text/javascript">
139  var prefix = '<?php echo $prefix; ?>';
140  var value = document.getElementById(prefix + '_interval_unit').value;
141  switchInterval(value);
142 
143  function switchInterval(interval) {
144  var prefix = '<?php echo $prefix; ?>';
145  if (interval == '60') {
146  document.getElementById(prefix + '_interval_mins').style['display'] = '';
147  document.getElementById(prefix + '_interval_hours').style['display'] = 'none';
148  document.getElementById(prefix + '_interval_days').style['display'] = 'none';
149  } else if (interval == '3600') {
150  document.getElementById(prefix + '_interval_mins').style['display'] = 'none';
151  document.getElementById(prefix + '_interval_hours').style['display'] = '';
152  document.getElementById(prefix + '_interval_days').style['display'] = 'none';
153  } else if (interval == '86400') {
154  document.getElementById(prefix + '_interval_mins').style['display'] = 'none';
155  document.getElementById(prefix + '_interval_hours').style['display'] = 'none';
156  document.getElementById(prefix + '_interval_days').style['display'] = '';
157  }
158  }
159  </script>
160  <?php
161 
162  } else if ($one_off) {
163 
164  // day (1 - 31)
165  $days = Array('--' => '--');
166  for ($i = 1; $i <= 31; $i++) {
167  $days[str_pad($i, 2, '0', STR_PAD_LEFT)] = $i;
168  }
169  if ($write_access) {
170  combo_box($prefix.'_day', $days, FALSE, $when_arr['day'], NULL, $form_extra_elements);
171  } else {
172  echo $days[$when_arr['day']];
173  }
174 
175  // month (Jan - Dec)
176  echo '&nbsp;/&nbsp;';
177  if ($write_access) {
178  $months = Array('--' => '--');
179  for ($i = 1; $i <= 12; $i++) {
180  $months[str_pad($i, 2, '0', STR_PAD_LEFT)] = date('M', mktime(0,0,0,$i,1,2000));
181  }
182  combo_box($prefix.'_month', $months, FALSE, $when_arr['month'], NULL, $form_extra_elements);
183  } else {
184  echo $when_arr['month'];
185  }
186 
187  // year (this year - specified years later)
188  echo '&nbsp;/&nbsp;';
189  if ($write_access) {
190  $years = Array('--' => '--');
191  $max_years = (int) $cron_mgr->attr('max_years');
192  $end_year = max($when_arr['year'], $now['year']) + ($max_years > 0 ? $max_years : 10);
193  for ($i = $now['year']; $i <= $end_year; $i++) {
194  $years[str_pad($i, 4, '0', STR_PAD_LEFT)] = str_pad($i, 4, '0', STR_PAD_LEFT);
195  }
196  combo_box($prefix.'_year', $years, FALSE, $when_arr['year'], NULL, $form_extra_elements);
197  } else {
198  echo $when_arr['year'];
199  }
200 
201  // hours (based on the values from getWhenBoxHoursMins())
202  echo '&nbsp;&nbsp;&nbsp;';
203  if ($write_access) {
204  if (count($hours_mins['hours']) > 1) {
205  $hours = Array('--' => '--');
206  foreach ($hours_mins['hours'] as $i) {
207  $hours[$i] = $i;
208  }
209  combo_box($prefix.'_hour', $hours, FALSE, $when_arr['hour'], NULL, $form_extra_elements);
210  } else {
211  echo $hours_mins['hours'][0];
212  hidden_field($prefix.'_hour', $hours_mins['hours'][0]);
213  }
214  } else {
215  echo $when_arr['hour'];
216  }
217 
218  // minutes (based on the values from getWhenBoxHoursMins())
219  echo '&nbsp;:&nbsp;';
220  if ($write_access) {
221  if (count($hours_mins['mins']) > 1) {
222  $mins = Array('--' => '--');
223  foreach ($hours_mins['mins'] as $i) {
224  $mins[$i] = $i;
225  }
226  combo_box($prefix.'_minute', $mins, FALSE, $when_arr['minute'], NULL, $form_extra_elements);
227  } else {
228  echo $hours_mins['mins'][0];
229  hidden_field($prefix.'_minute', $hours_mins['mins'][0]);
230  }
231  } else {
232  echo $when_arr['minute'];
233  }
234 
235  if ($write_access) {
236  $next_run = $cron_mgr->timeOfNextRun()
237  ?>
238  <script language="javascript">
239  var TIME_UNITS = new Array();
240  <?php
241  $units = Array(
242  'Y' => translate('year'),
243  'n' => translate('month'),
244  'j' => translate('day'),
245  'G' => translate('hour'),
246  'i' => translate('minute'),
247  );
248 
249  foreach ($units as $php_unit => $js_unit) {
250  echo 'TIME_UNITS[\''.$js_unit.'\'] = \''.str_pad(date($php_unit, $next_run), 2, '0', STR_PAD_LEFT).'\';';
251  }
252  ?>
253 
254  function setCurrentTime(prefix) {
255  for (u in TIME_UNITS) {
256  var id = prefix + '_' + u;
257  var unit = document.getElementById(id);
258 
259  if (unit.type == 'text') {
260  unit.value = TIME_UNITS[u];
261  } else {
262  for (var i = 0; i < unit.options.length; i++) {
263  if (TIME_UNITS[u] == unit.options[i].value) {
264  unit.selectedIndex = i;
265  }
266  }
267  }
268  }
269  }
270  </script>
271  <?php
272  if ( $form_extra_elements != 'disabled') {
273  normal_button($prefix.'_set_now', translate('next_run'), "setCurrentTime('$prefix');");
274  }
275  echo ' '.translate('or_in').' ';
276  text_box($prefix.'_duration', ($when_arr['duration'] <> '' ? $when_arr['duration'] : '-'), 2, '', TRUE, $form_extra_elements)
277  ?>
278  &nbsp;
279  <?php
280  $periods = Array(
281  'i' => translate('minutes'),
282  'h' => translate('hours'),
283  'd' => translate('days'),
284  'w' => translate('weeks'),
285  'm' => translate('months'),
286  'y' => translate('years'),
287  );
288 
289  combo_box($prefix.'_period', $periods, FALSE, ($when_arr['period'] <> '' ? $when_arr['period'] : '-'), NULL, $form_extra_elements);
290  }//end if $write_access
291 
292  }//end if one off job
293 
294  }//end _paintWhenBox()
295 
296 
307  public function _processWhenBox(Cron_Job $asset, Backend_Outputter $o, $prefix)
308  {
309  $one_off = ($asset->attr('type') == 'one_off');
310  $repeating = !$one_off; // for easier reading
311  $valid = FALSE;
312  $fields = Array();
313  $prefix .= '_when';
314 
315  // one-off job, make sure form elements have the correct values
316  if ($one_off) {
317  $has_day = isset($_POST[$prefix.'_day']) && ($_POST[$prefix.'_day'] != '--');
318  $has_month = isset($_POST[$prefix.'_month']) && ($_POST[$prefix.'_month'] != '--');
319  $has_year = isset($_POST[$prefix.'_year']) && ($_POST[$prefix.'_year'] != '--');
320  $has_hour = isset($_POST[$prefix.'_hour']) && ($_POST[$prefix.'_hour'] != '--');
321  $has_minute = isset($_POST[$prefix.'_minute']) && ($_POST[$prefix.'_minute'] != '--');
322  $has_duration = isset($_POST[$prefix.'_duration']) && ($_POST[$prefix.'_duration'] != '-');
323  $valid = (($has_day && $has_month && $has_year && $has_hour && $has_minute) || $has_duration);
324  foreach (Array('year', 'month', 'day', 'hour', 'minute', 'period', 'duration') as $field) {
325  if (!isset($_POST[$prefix.'_'.$field])) continue;
326  $fields[$field] = $_POST[$prefix.'_'.$field];
327  }
328 
329  // repeating job
330  } else if ($repeating) {
331  $has_interval = isset($_POST[$prefix.'_interval_unit']);
332  $has_mins = isset($_POST[$prefix.'_interval_mins']) && ($_POST[$prefix.'_interval_mins'] != '--');
333  $has_hours = isset($_POST[$prefix.'_interval_hours']) && ($_POST[$prefix.'_interval_hours'] != '--');
334  $has_days = isset($_POST[$prefix.'_interval_days']) && ($_POST[$prefix.'_interval_days'] != '-');
335  $valid_min = $has_interval && $has_mins && $_POST[$prefix.'_interval_unit'] == '60';
336  $valid_hour = $has_interval && $has_hours && $_POST[$prefix.'_interval_unit'] == '3600';
337  $valid_day = $has_interval && $has_days && $_POST[$prefix.'_interval_unit'] == '86400';
338  $valid = (($valid_min) || ($valid_hour) || ($valid_day));
339  }
340 
341  if (!$valid) return FALSE;
342 
343  $when_str = '';
344  $when = '';
345  $type_prefix = ($one_off) ? 'OO' : '';
346 
347  if ($one_off) {
348 
349  if (isset($_POST[$prefix.'_duration']) && $_POST[$prefix.'_duration'] <> '-') {
350  // user chose a duration instead of a fixed date, so lets return that
351  $when = $type_prefix.'!'.$fields['period'].$fields['duration'];
352 
353  // display notice if user chose a specific date/time as well
354  foreach (Array('year', 'month', 'day', 'hour', 'minute') as $field) {
355  if ($fields[$field] != '--') {
356  trigger_localised_error('CRON0044', E_USER_NOTICE);
357  break;
358  }
359  }
360 
361  } else {
362  // usr chose a specific date/time, so return that
363  foreach (Array('year', 'month', 'day', 'hour', 'minute') as $field) {
364  if ($fields[$field] != '--') {
365  switch ($field) {
366  case 'year' :
367  $when_str .= $fields[$field].'-';
368  break;
369  case 'month' :
370  $when_str .= $fields[$field].'-';
371  if (!$type_prefix) $type_prefix = 'YR';
372  break;
373  case 'day' :
374  $when_str .= $fields[$field].' ';
375  if (!$type_prefix) {
376  $type_prefix = (strlen($_POST[$prefix.'_'.$field]) == 2) ? 'MT' : 'WK';
377  }
378  break;
379  case 'hour' :
380  $when_str .= $fields[$field].':';
381  if (!$type_prefix) $type_prefix = 'DL';
382  break;
383  case 'minute' :
384  $when_str .= $fields[$field];
385  if (!$type_prefix) $type_prefix = 'HR';
386  break;
387  }//end switch
388 
389  } else if ($type_prefix) {
390  trigger_localised_error('CRON0040', E_USER_WARNING, Cron_Job::whenTypeName($type_prefix), $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($asset->type(), 'name'), $field);
391  return FALSE;
392  }//end if
393 
394  }//end foreach
395 
396  // do some date parsing like with datetime fields so we get a valid date
397  $timestamp = mktime(
398  (int) substr($when_str,11,2),
399  (int) substr($when_str,14,2),
400  (int) substr($when_str,17,2),
401  (int) substr($when_str,5,2),
402  (int) substr($when_str,8,2),
403  (int) substr($when_str,0,4)
404  );
405  $parsed_date = date('Y-m-d', $timestamp).substr($when_str, 10);
406 
407  if (strcmp($parsed_date, $when_str) != 0) {
408  trigger_localised_error('SYS0311', E_USER_NOTICE, $when_str);
409  $when_str = $parsed_date;
410  }
411 
412  $when = $type_prefix.'='.$when_str;
413  }//end else
414 
415  } else if ($repeating) {
416 
417  if (isset($_POST[$prefix.'_interval_unit'])) {
418  // 60, 3600 or 86400 sec
419  $interval_unit = (int) $_POST[$prefix.'_interval_unit'];
420  $interval = 0;
421  if ($interval_unit == 60) {
422  $interval = (int) $_POST[$prefix.'_interval_mins'];
423  } else if ($interval_unit == 3600) {
424  $interval = (int) $_POST[$prefix.'_interval_hours'];
425  } else if ($interval_unit == 86400) {
426  $interval = (int) $_POST[$prefix.'_interval_days'];
427  }
428 
429  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
430  $next_run = $cron_mgr->timeOfNextRun();
431  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($cron_mgr);
432  // default next run = next run of cron manager
433  // new next run = next run + (interval * interval unit)
434  $when = "TI=$next_run,$interval,$interval_unit";
435  }
436  }//end else if
437 
438  return $when;
439 
440  }//end _processWhenBox()
441 
442 
443 }//end class
444 ?>