17 require_once SQ_INCLUDE_PATH.
'/asset_edit/asset_edit_fns.inc';
41 parent::__construct();
43 $this->static_screens = Array(
45 'name' => translate(
'details'),
46 'force_unlock' => TRUE,
49 'name' => translate(
'logs'),
50 'force_unlock' => TRUE,
71 $cron_mgr = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cron_manager');
72 if (is_null($cron_mgr))
return FALSE;
74 if ((
int) $cron_mgr->attr(
'refresh_time') <= 0) {
75 echo translate(
'cron_manager_refresh_unknown');
81 $one_off = ($asset->
attr(
'type') ==
'one_off');
82 $repeating = !$one_off;
84 $when_str = ($when ==
'') ? $asset->
attr(
'when') : $when;
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);
97 60 => translate(
'minutes'),
98 3600 => translate(
'hours'),
99 86400 => translate(
'days'),
102 $mins = Array(
'--' =>
'--');
103 $hours = Array(
'--' =>
'--');
106 if (count($hours_mins[
'mins']) > 1) {
107 foreach ($hours_mins[
'mins'] as $i) {
112 unset($time_unit[60]);
116 if (count($hours_mins[
'hours']) > 1) {
117 foreach ($hours_mins[
'hours'] as $i) {
122 unset($time_unit[3600]);
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);
132 $onChange =
'onChange="switchInterval(this.value)"';
133 combo_box($prefix.
'_interval_unit', $time_unit, FALSE, current($time_unit), NULL, $onChange.$form_extra_elements);
138 <script type=
"text/javascript">
139 var prefix =
'<?php echo $prefix; ?>';
140 var value = document.getElementById(prefix +
'_interval_unit').value;
141 switchInterval(value);
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'] =
'';
162 }
else if ($one_off) {
165 $days = Array(
'--' =>
'--');
166 for ($i = 1; $i <= 31; $i++) {
167 $days[str_pad($i, 2,
'0', STR_PAD_LEFT)] = $i;
170 combo_box($prefix.
'_day', $days, FALSE, $when_arr[
'day'], NULL, $form_extra_elements);
172 echo $days[$when_arr[
'day']];
176 echo
' / ';
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));
182 combo_box($prefix.
'_month', $months, FALSE, $when_arr[
'month'], NULL, $form_extra_elements);
184 echo $when_arr[
'month'];
188 echo
' / ';
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);
196 combo_box($prefix.
'_year', $years, FALSE, $when_arr[
'year'], NULL, $form_extra_elements);
198 echo $when_arr[
'year'];
202 echo
' ';
204 if (count($hours_mins[
'hours']) > 1) {
205 $hours = Array(
'--' =>
'--');
206 foreach ($hours_mins[
'hours'] as $i) {
209 combo_box($prefix.
'_hour', $hours, FALSE, $when_arr[
'hour'], NULL, $form_extra_elements);
211 echo $hours_mins[
'hours'][0];
212 hidden_field($prefix.
'_hour', $hours_mins[
'hours'][0]);
215 echo $when_arr[
'hour'];
219 echo
' : ';
221 if (count($hours_mins[
'mins']) > 1) {
222 $mins = Array(
'--' =>
'--');
223 foreach ($hours_mins[
'mins'] as $i) {
226 combo_box($prefix.
'_minute', $mins, FALSE, $when_arr[
'minute'], NULL, $form_extra_elements);
228 echo $hours_mins[
'mins'][0];
229 hidden_field($prefix.
'_minute', $hours_mins[
'mins'][0]);
232 echo $when_arr[
'minute'];
236 $next_run = $cron_mgr->timeOfNextRun()
238 <script language=
"javascript">
239 var TIME_UNITS =
new Array();
242 'Y' => translate(
'year'),
243 'n' => translate(
'month'),
244 'j' => translate(
'day'),
245 'G' => translate(
'hour'),
246 'i' => translate(
'minute'),
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).
'\';
';
254 function setCurrentTime(prefix) {
255 for (u in TIME_UNITS) {
256 var id = prefix + '_
' + u;
257 var unit = document.getElementById(id);
259 if (unit.type == 'text
') {
260 unit.value = TIME_UNITS[u];
262 for (var i = 0; i < unit.options.length; i++) {
263 if (TIME_UNITS[u] == unit.options[i].value) {
264 unit.selectedIndex = i;
272 if ( $form_extra_elements != 'disabled
') {
273 normal_button($prefix.'_set_now
', translate('next_run
'), "setCurrentTime('$prefix
');");
275 echo ' '.translate('or_in
').' ';
276 text_box($prefix.'_duration
', ($when_arr['duration
'] <> '' ? $when_arr['duration
'] : '-
'), 2, '', TRUE, $form_extra_elements)
281 'i
' => translate('minutes
'),
282 'h
' => translate('hours
'),
283 'd
' => translate('days
'),
284 'w
' => translate('weeks
'),
285 'm
' => translate('months
'),
286 'y
' => translate('years
'),
289 combo_box($prefix.'_period
', $periods, FALSE, ($when_arr['period
'] <> '' ? $when_arr['period
'] : '-
'), NULL, $form_extra_elements);
290 }//end if $write_access
292 }//end if one off job
294 }//end _paintWhenBox()
307 public function _processWhenBox(Cron_Job $asset, Backend_Outputter $o, $prefix)
309 $one_off = ($asset->attr('type
') == 'one_off
');
310 $repeating = !$one_off; // for easier reading
315 // one-off job, make sure form elements have the correct values
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];
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));
341 if (!$valid) return FALSE;
345 $type_prefix = ($one_off) ? 'OO
' : '';
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
'];
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);
362 // usr chose a specific date/time, so return that
363 foreach (Array('year
', 'month
', 'day
', 'hour
', 'minute
') as $field) {
364 if ($fields[$field] != '--
') {
367 $when_str .= $fields[$field].'-
';
370 $when_str .= $fields[$field].'-
';
371 if (!$type_prefix) $type_prefix = 'YR
';
374 $when_str .= $fields[$field].' ';
376 $type_prefix = (strlen($_POST[$prefix.'_
'.$field]) == 2) ? 'MT
' : 'WK
';
380 $when_str .= $fields[$field].':
';
381 if (!$type_prefix) $type_prefix = 'DL
';
384 $when_str .= $fields[$field];
385 if (!$type_prefix) $type_prefix = 'HR
';
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);
396 // do some date parsing like with datetime fields so we get a valid date
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)
405 $parsed_date = date('Y-m-d
', $timestamp).substr($when_str, 10);
407 if (strcmp($parsed_date, $when_str) != 0) {
408 trigger_localised_error('SYS0311
', E_USER_NOTICE, $when_str);
409 $when_str = $parsed_date;
412 $when = $type_prefix.'=
'.$when_str;
415 } else if ($repeating) {
417 if (isset($_POST[$prefix.'_interval_unit
'])) {
418 // 60, 3600 or 86400 sec
419 $interval_unit = (int) $_POST[$prefix.'_interval_unit
'];
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
'];
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";
440 }//end _processWhenBox()