mb_substr($name !== '' ? $name : $command, 0, 64), 'Enabled' => empty($posted['Enabled']) ? 0 : 1, 'Schedule' => $schedule, 'Command' => $command, 'ServerId' => empty($posted['ServerId']) ? null : validInt($posted['ServerId']), ); $id = empty($_REQUEST['id']) ? 0 : validInt($_REQUEST['id']); $job = $id ? ZM\CronJob::find_one(array('Id'=>$id)) : new ZM\CronJob(); if (!$job) { $cron_error = 'The scheduled task no longer exists'; } else if ($job->Id()) { if ($job->save($values)) { ZM\Info('Updated scheduled task '.$job->Id().': '.$schedule.' '.$command); } else { $cron_error = $job->get_last_error() ?: 'Unable to save the scheduled task'; } } else if ($job->insert($values)) { ZM\Info('Added scheduled task '.$job->Id().': '.$schedule.' '.$command); } else { $cron_error = $job->get_last_error() ?: 'Unable to save the scheduled task'; } } } else if ($action == 'delete') { if (!empty($_REQUEST['markIds']) && is_array($_REQUEST['markIds'])) { foreach ($_REQUEST['markIds'] as $id) { $id = validInt($id); if (!$id) continue; ZM\Info('Deleting scheduled task '.$id); if (dbQuery('DELETE FROM CronJobs WHERE Id=?', array($id)) === null) { $delete_error .= ($delete_error === '' ? '' : ' ').'Unable to delete scheduled task '.$id.': '.dbLastError(); } } } } if (in_array($action, array('save', 'delete')) and ($cron_error === '')) { // The crontab is a projection of the table, rebuilt whenever the table // changes, so the two cannot drift. if (cronRebuild() === false) $cron_error = translate('CronWriteFailed'); } // Reported after the rebuild, so the crontab still drops the tasks that were deleted. if ($delete_error !== '') { $cron_error = ($cron_error === '' ? '' : $cron_error.' ').$delete_error; } if ($cron_error !== '') { ZM\Error('Scheduled tasks: '.$cron_error); $redirect = '?view=cron&error='.urlencode($cron_error); } else { $redirect = '?view=cron'; } ?>