diff --git a/htdocs/cron/card.php b/htdocs/cron/card.php index ac28bc631a4..87a054c699f 100644 --- a/htdocs/cron/card.php +++ b/htdocs/cron/card.php @@ -141,6 +141,7 @@ if ($action == 'add') { $object->unitfrequency = GETPOST('unitfrequency', 'int'); $object->frequency = GETPOST('nbfrequency', 'int'); $object->maxrun = GETPOST('maxrun', 'int'); + $object->email_alert = GETPOST('email_alert'); // Add cron task $result = $object->create($user); @@ -175,6 +176,7 @@ if ($action == 'update') { $object->unitfrequency = GETPOST('unitfrequency', 'int'); $object->frequency = GETPOST('nbfrequency', 'int'); $object->maxrun = GETPOST('maxrun', 'int'); + $object->email_alert = GETPOST('email_alert'); // Add cron task $result = $object->update($user); @@ -415,6 +417,15 @@ if (($action == "create") || ($action == "edit")) { print ""; print "\n"; + print ''; + print $langs->trans('EmailIfError').""; + print ' '; + print ""; + print ""; + //print $form->textwithpicto('', $langs->trans("CronCommandHelp"), 1, 'help'); + print ""; + print "\n"; + print ''; print $langs->trans('CronEvery').""; print ""; @@ -578,32 +589,32 @@ if (($action == "create") || ($action == "edit")) { print ''; print $langs->trans('CronModule').""; - print $object->module_name; + print dol_escape_htmltag($object->module_name); print ""; print ''; print $langs->trans('CronClassFile').""; - print $object->classesname; + print dol_escape_htmltag($object->classesname); print ""; print ''; print $langs->trans('CronObject').""; - print $object->objectname; + print dol_escape_htmltag($object->objectname); print ""; print ''; print $langs->trans('CronMethod').""; - print $object->methodename; + print dol_escape_htmltag($object->methodename); print ""; print ''; print $langs->trans('CronArgs').""; - print $object->params; + print dol_escape_htmltag($object->params); print ""; print ''; print $langs->trans('CronCommand').""; - print $object->command; + print dol_escape_htmltag($object->command); print ""; print ''; @@ -613,6 +624,11 @@ if (($action == "create") || ($action == "edit")) { } print ""; + print ''; + print $langs->trans('EmailIfError').""; + print dol_escape_htmltag($object->email_alert); + print ""; + if (isModEnabled('multicompany')) { print ''; print $langs->trans('Entity').""; diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index 033e6512bd8..afbaa06aa6a 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -142,6 +142,11 @@ class Cronjob extends CommonObject */ public $pid; + /** + * @var string Email when an error occurs + */ + public $email_alert; + /** * @var int User ID of creation */ @@ -428,6 +433,7 @@ class Cronjob extends CommonObject $sql .= " t.status,"; $sql .= " t.processing,"; $sql .= " t.pid,"; + $sql .= " t.email_alert,"; $sql .= " t.fk_user_author,"; $sql .= " t.fk_user_mod,"; $sql .= " t.note as note_private,"; @@ -477,6 +483,7 @@ class Cronjob extends CommonObject $this->status = $obj->status; $this->processing = $obj->processing; $this->pid = $obj->pid; + $this->email_alert = $obj->email_alert; $this->fk_user_author = $obj->fk_user_author; $this->fk_user_mod = $obj->fk_user_mod; $this->note_private = $obj->note_private; @@ -538,6 +545,7 @@ class Cronjob extends CommonObject $sql .= " t.status,"; $sql .= " t.processing,"; $sql .= " t.pid,"; + $sql .= " t.email_alert,"; $sql .= " t.fk_user_author,"; $sql .= " t.fk_user_mod,"; $sql .= " t.note as note_private,"; @@ -615,6 +623,7 @@ class Cronjob extends CommonObject $line->status = $obj->status; $line->processing = $obj->processing; $line->pid = $obj->pid; + $line->email_alert = $obj->email_alert; $line->fk_user_author = $obj->fk_user_author; $line->fk_user_mod = $obj->fk_user_mod; $line->note_private = $obj->note_private; @@ -716,10 +725,12 @@ class Cronjob extends CommonObject if (empty($this->processing)) { $this->processing = 0; } - if (empty($this->pid)) { $this->pid = null; } + if (empty($this->email_alert)) { + $this->email_alert = ''; + } // Check parameters // Put here code to add a control on parameters values @@ -764,7 +775,7 @@ class Cronjob extends CommonObject // Update request $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET"; - $sql .= " entity=".(isset($this->entity) ? $this->db->escape($this->entity) : $conf->entity).","; + $sql .= " entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).","; $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").","; $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").","; $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").","; @@ -774,7 +785,7 @@ class Cronjob extends CommonObject $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").","; $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").","; $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").","; - $sql .= " priority=".(isset($this->priority) ? $this->priority : "null").","; + $sql .= " priority=".(isset($this->priority) ? ((int) $this->priority) : "null").","; $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').","; $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').","; $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').","; @@ -786,8 +797,9 @@ class Cronjob extends CommonObject $sql .= " frequency=".(isset($this->frequency) ? $this->frequency : "null").","; $sql .= " status=".(isset($this->status) ? $this->status : "null").","; $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").","; - $sql .= " pid=".(isset($this->pid) ? $this->pid : "null").","; - $sql .= " fk_user_mod=".$user->id.","; + $sql .= " pid=".(isset($this->pid) ? ((int) $this->pid) : "null").","; + $sql .= " email_alert = ".(isset($this->email_alert) ? "'".$this->db->escape($this->email_alert)."'" : "null").","; + $sql .= " fk_user_mod = ".((int) $user->id).","; $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").","; $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").","; $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").","; @@ -940,6 +952,7 @@ class Cronjob extends CommonObject $this->status = 0; $this->processing = 0; $this->pid = null; + $this->email_alert = ''; $this->fk_user_author = 0; $this->fk_user_mod = 0; $this->note_private = ''; @@ -1130,6 +1143,7 @@ class Cronjob extends CommonObject @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64 error_reporting($err); } + $MemoryLimit = 0; if (!empty($MemoryLimit)) { @ini_set('memory_limit', $MemoryLimit); } @@ -1341,6 +1355,16 @@ class Cronjob extends CommonObject } $conf->setEntityValues($this->db, $savcurrententity); + + if ($error && !empty($this->email_alert)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php'; + $subject = $langs->trans("ErrorInBatch", $this->label); + $msg = $langs->trans("ErrorInBatch", $this->label); + $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM'); + $cmailfile = new CMailFile($subject, $this->email_alert, $from, $msg); + $result = $cmailfile->sendfile(); // Do not test result + } + return $error ?-1 : 1; } diff --git a/htdocs/langs/en_US/cron.lang b/htdocs/langs/en_US/cron.lang index d9bdd2691eb..70fb9e387b9 100644 --- a/htdocs/langs/en_US/cron.lang +++ b/htdocs/langs/en_US/cron.lang @@ -89,6 +89,9 @@ CleanUnfinishedCronjob=Clean cronjob stuck in processing when the process is no WarningCronDelayed=Attention, for performance purpose, whatever is next date of execution of enabled jobs, your jobs may be delayed to a maximum of %s hours, before being run. DATAPOLICYJob=Data cleaner and anonymizer JobXMustBeEnabled=Job %s must be enabled +EmailIfError=Email for warning on error +ErrorInBatch=Error when running the job %s + # Cron Boxes LastExecutedScheduledJob=Last executed scheduled job NextScheduledJobExecute=Next scheduled job to execute