211 public function create($user, $notrigger = 0)
213 global $conf, $langs;
220 if (isset($this->label)) {
221 $this->label = trim($this->label);
223 if (isset($this->jobtype)) {
224 $this->jobtype = trim($this->jobtype);
226 if (isset($this->command)) {
227 $this->command = trim($this->command);
229 if (isset($this->classesname)) {
230 $this->classesname = trim($this->classesname);
232 if (isset($this->objectname)) {
233 $this->objectname = trim($this->objectname);
235 if (isset($this->methodename)) {
236 $this->methodename = trim($this->methodename);
238 if (isset($this->params)) {
239 $this->params = trim($this->params);
241 if (isset($this->md5params)) {
242 $this->md5params = trim($this->md5params);
244 if (isset($this->module_name)) {
245 $this->module_name = trim($this->module_name);
247 if (isset($this->priority)) {
248 $this->priority = trim($this->priority);
250 if (isset($this->lastoutput)) {
251 $this->lastoutput = trim($this->lastoutput);
253 if (isset($this->lastresult)) {
254 $this->lastresult = trim($this->lastresult);
256 if (isset($this->unitfrequency)) {
257 $this->unitfrequency = trim($this->unitfrequency);
259 if (isset($this->frequency)) {
260 $this->frequency = trim($this->frequency);
262 if (isset($this->
status)) {
265 if (isset($this->note_private)) {
266 $this->note_private = trim($this->note_private);
268 if (isset($this->nbrun)) {
269 $this->nbrun = (int) $this->nbrun;
271 if (isset($this->maxrun)) {
272 $this->maxrun = (int) $this->maxrun;
274 if (isset($this->libname)) {
275 $this->libname = trim($this->libname);
277 if (isset($this->test)) {
278 $this->test = trim($this->test);
284 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
287 if (empty($this->label)) {
288 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
291 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
292 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
295 if (empty($this->unitfrequency)) {
296 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
299 if (($this->jobtype ==
'command') && (empty($this->command))) {
300 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
303 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
304 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
307 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
308 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
311 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
312 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
315 if (($this->jobtype ==
'function') && (empty($this->libname))) {
316 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
321 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
327 $sql .=
"classesname,";
328 $sql .=
"objectname,";
329 $sql .=
"methodename,";
331 $sql .=
"md5params,";
332 $sql .=
"module_name,";
334 $sql .=
"datelastrun,";
335 $sql .=
"datenextrun,";
337 $sql .=
"datestart,";
338 $sql .=
"lastresult,";
339 $sql .=
"datelastresult,";
340 $sql .=
"lastoutput,";
341 $sql .=
"unitfrequency,";
342 $sql .=
"frequency,";
344 $sql .=
"fk_user_author,";
345 $sql .=
"fk_user_mod,";
351 $sql .=
") VALUES (";
352 $sql .=
" ".(!isset($this->entity) ? $conf->entity : $this->db->escape($this->entity)).
",";
353 $sql .=
" '".$this->db->idate($now).
"',";
354 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
355 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
356 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
357 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
358 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
359 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
360 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
361 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
362 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
363 $sql .=
" ".(!isset($this->priority) ?
'0' : $this->priority).
",";
364 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
365 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
366 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
367 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
368 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
369 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
370 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
371 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
372 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
373 $sql .=
" ".(!isset($this->
status) ?
'0' : ((int) $this->
status)).
",";
374 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
375 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
376 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
377 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
378 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
379 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
380 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
385 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
386 $resql = $this->db->query($sql);
389 $this->errors[] =
"Error ".$this->db->lasterror();
393 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
398 $this->db->rollback();
415 public function fetch($id, $objectname =
'', $methodname =
'')
419 $sql .=
" t.entity,";
422 $sql .=
" t.jobtype,";
424 $sql .=
" t.command,";
425 $sql .=
" t.classesname,";
426 $sql .=
" t.objectname,";
427 $sql .=
" t.methodename,";
428 $sql .=
" t.params,";
429 $sql .=
" t.md5params,";
430 $sql .=
" t.module_name,";
431 $sql .=
" t.priority,";
432 $sql .=
" t.datelastrun,";
433 $sql .=
" t.datenextrun,";
434 $sql .=
" t.dateend,";
435 $sql .=
" t.datestart,";
436 $sql .=
" t.lastresult,";
437 $sql .=
" t.datelastresult,";
438 $sql .=
" t.lastoutput,";
439 $sql .=
" t.unitfrequency,";
440 $sql .=
" t.frequency,";
441 $sql .=
" t.status,";
442 $sql .=
" t.processing,";
444 $sql .=
" t.email_alert,";
445 $sql .=
" t.fk_user_author,";
446 $sql .=
" t.fk_user_mod,";
447 $sql .=
" t.note as note_private,";
449 $sql .=
" t.maxrun,";
450 $sql .=
" t.libname,";
452 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
454 $sql .=
" WHERE t.rowid = ".((int) $id);
456 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
457 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
458 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
461 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
462 $resql = $this->db->query($sql);
464 if ($this->db->num_rows($resql)) {
465 $obj = $this->db->fetch_object($resql);
467 $this->
id = $obj->rowid;
468 $this->
ref = $obj->rowid;
469 $this->entity = $obj->entity;
470 $this->tms = $this->db->jdate($obj->tms);
471 $this->datec = $this->db->jdate($obj->datec);
472 $this->label = $obj->label;
473 $this->jobtype = $obj->jobtype;
474 $this->command = $obj->command;
475 $this->classesname = $obj->classesname;
476 $this->objectname = $obj->objectname;
477 $this->methodename = $obj->methodename;
478 $this->params = $obj->params;
479 $this->md5params = $obj->md5params;
480 $this->module_name = $obj->module_name;
481 $this->priority = $obj->priority;
482 $this->datelastrun = $this->db->jdate($obj->datelastrun);
483 $this->datenextrun = $this->db->jdate($obj->datenextrun);
484 $this->dateend = $this->db->jdate($obj->dateend);
485 $this->datestart = $this->db->jdate($obj->datestart);
486 $this->lastresult = $obj->lastresult;
487 $this->lastoutput = $obj->lastoutput;
488 $this->datelastresult = $this->db->jdate($obj->datelastresult);
489 $this->unitfrequency = $obj->unitfrequency;
490 $this->frequency = $obj->frequency;
491 $this->
status = $obj->status;
492 $this->processing = $obj->processing;
493 $this->pid = $obj->pid;
494 $this->email_alert = $obj->email_alert;
495 $this->fk_user_author = $obj->fk_user_author;
496 $this->fk_user_mod = $obj->fk_user_mod;
497 $this->note_private = $obj->note_private;
498 $this->nbrun = $obj->nbrun;
499 $this->maxrun = $obj->maxrun;
500 $this->libname = $obj->libname;
501 $this->test = $obj->test;
503 $this->db->free($resql);
507 $this->error =
"Error ".$this->db->lasterror();
525 public function fetchAll($sortorder =
'DESC', $sortfield =
't.rowid', $limit = 0, $offset = 0, $status = 1, $filter =
'', $processing = -1)
527 $this->lines = array();
531 $sql .=
" t.entity,";
534 $sql .=
" t.jobtype,";
536 $sql .=
" t.command,";
537 $sql .=
" t.classesname,";
538 $sql .=
" t.objectname,";
539 $sql .=
" t.methodename,";
540 $sql .=
" t.params,";
541 $sql .=
" t.md5params,";
542 $sql .=
" t.module_name,";
543 $sql .=
" t.priority,";
544 $sql .=
" t.datelastrun,";
545 $sql .=
" t.datenextrun,";
546 $sql .=
" t.dateend,";
547 $sql .=
" t.datestart,";
548 $sql .=
" t.lastresult,";
549 $sql .=
" t.datelastresult,";
550 $sql .=
" t.lastoutput,";
551 $sql .=
" t.unitfrequency,";
552 $sql .=
" t.frequency,";
553 $sql .=
" t.status,";
554 $sql .=
" t.processing,";
556 $sql .=
" t.email_alert,";
557 $sql .=
" t.fk_user_author,";
558 $sql .=
" t.fk_user_mod,";
559 $sql .=
" t.note as note_private,";
561 $sql .=
" t.libname,";
563 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
564 $sql .=
" WHERE 1 = 1";
565 if ($processing >= 0) {
566 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
568 if ($status >= 0 && $status < 2) {
569 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
570 } elseif ($status == 2) {
571 $sql .=
" AND t.status = 2";
574 if (is_array($filter) && count($filter) > 0) {
575 foreach ($filter as $key => $value) {
576 if ($key ==
't.rowid') {
577 $sql .=
" AND ".$key.
" = ".((int) $value);
579 $sql .=
" AND ".$key.
" LIKE '%".$this->db->escape($value).
"%'";
584 $sql .= $this->db->order($sortfield, $sortorder);
585 if (!empty($limit) && !empty($offset)) {
586 $sql .= $this->db->plimit($limit + 1, $offset);
591 if (count($sqlwhere) > 0) {
592 $sql .=
" WHERE ".implode(
' AND ', $sqlwhere);
595 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
596 $resql = $this->db->query($sql);
598 $num = $this->db->num_rows($resql);
605 $obj = $this->db->fetch_object($resql);
607 $line->id = $obj->rowid;
608 $line->ref = $obj->rowid;
609 $line->entity = $obj->entity;
610 $line->tms = $this->db->jdate($obj->tms);
611 $line->datec = $this->db->jdate($obj->datec);
612 $line->label = $obj->label;
613 $line->jobtype = $obj->jobtype;
614 $line->command = $obj->command;
615 $line->classesname = $obj->classesname;
616 $line->objectname = $obj->objectname;
617 $line->methodename = $obj->methodename;
618 $line->params = $obj->params;
619 $line->md5params = $obj->md5params;
620 $line->module_name = $obj->module_name;
621 $line->priority = $obj->priority;
622 $line->datelastrun = $this->db->jdate($obj->datelastrun);
623 $line->datenextrun = $this->db->jdate($obj->datenextrun);
624 $line->dateend = $this->db->jdate($obj->dateend);
625 $line->datestart = $this->db->jdate($obj->datestart);
626 $line->lastresult = $obj->lastresult;
627 $line->datelastresult = $this->db->jdate($obj->datelastresult);
628 $line->lastoutput = $obj->lastoutput;
629 $line->unitfrequency = $obj->unitfrequency;
630 $line->frequency = $obj->frequency;
631 $line->status = $obj->status;
632 $line->processing = $obj->processing;
633 $line->pid = $obj->pid;
634 $line->email_alert = $obj->email_alert;
635 $line->fk_user_author = $obj->fk_user_author;
636 $line->fk_user_mod = $obj->fk_user_mod;
637 $line->note_private = $obj->note_private;
638 $line->nbrun = $obj->nbrun;
639 $line->libname = $obj->libname;
640 $line->test = $obj->test;
641 $this->lines[] = $line;
646 $this->db->free($resql);
650 $this->error =
"Error ".$this->db->lasterror();
663 public function update($user =
null, $notrigger = 0)
665 global $conf, $langs;
667 $langs->load(
'cron');
672 if (isset($this->label)) {
673 $this->label = trim($this->label);
675 if (isset($this->jobtype)) {
676 $this->jobtype = trim($this->jobtype);
678 if (isset($this->command)) {
679 $this->command = trim($this->command);
681 if (isset($this->classesname)) {
682 $this->classesname = trim($this->classesname);
684 if (isset($this->objectname)) {
685 $this->objectname = trim($this->objectname);
687 if (isset($this->methodename)) {
688 $this->methodename = trim($this->methodename);
690 if (isset($this->params)) {
691 $this->params = trim($this->params);
693 if (isset($this->md5params)) {
694 $this->md5params = trim($this->md5params);
696 if (isset($this->module_name)) {
697 $this->module_name = trim($this->module_name);
699 if (isset($this->priority)) {
700 $this->priority = trim($this->priority);
702 if (isset($this->lastoutput)) {
703 $this->lastoutput = trim($this->lastoutput);
705 if (isset($this->lastresult)) {
706 $this->lastresult = trim($this->lastresult);
708 if (isset($this->unitfrequency)) {
709 $this->unitfrequency = trim($this->unitfrequency);
711 if (isset($this->frequency)) {
712 $this->frequency = trim($this->frequency);
714 if (isset($this->
status)) {
717 if (isset($this->note_private)) {
718 $this->note_private = trim($this->note_private);
720 if (isset($this->nbrun)) {
721 $this->nbrun = trim($this->nbrun);
723 if (isset($this->libname)) {
724 $this->libname = trim($this->libname);
726 if (isset($this->test)) {
727 $this->test = trim($this->test);
730 if (empty($this->maxrun)) {
733 if (empty($this->processing)) {
734 $this->processing = 0;
736 if (empty($this->pid)) {
739 if (empty($this->email_alert)) {
740 $this->email_alert =
'';
742 if (empty($this->datenextrun)) {
743 $this->datenextrun =
dol_now();
748 if (
dol_strlen($this->datenextrun) == 0 && $this->
status == self::STATUS_ENABLED) {
749 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
752 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
753 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
756 if (empty($this->label)) {
757 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
760 if (empty($this->unitfrequency)) {
761 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
764 if (($this->jobtype ==
'command') && (empty($this->command))) {
765 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
768 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
769 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
772 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
773 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
776 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
777 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
781 if (($this->jobtype ==
'function') && (empty($this->libname))) {
782 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
788 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
789 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).
",";
790 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
791 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
792 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
793 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
794 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
795 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
796 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
797 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
798 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
799 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
800 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
801 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
802 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
803 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
804 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
805 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
806 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
807 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency :
"null").
",";
808 $sql .=
" frequency=".(isset($this->frequency) ? $this->frequency :
"null").
",";
809 $sql .=
" status=".(isset($this->
status) ? $this->
status :
"null").
",";
810 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
811 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
812 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
813 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
814 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
815 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
816 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
817 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
818 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
819 $sql .=
" WHERE rowid=".((int) $this->
id);
823 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
824 $resql = $this->db->query($sql);
827 $this->errors[] =
"Error ".$this->db->lasterror();
832 foreach ($this->errors as $errmsg) {
833 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
834 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
836 $this->db->rollback();
1036 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1038 global $db, $conf, $langs;
1040 if (!empty($conf->dol_no_mouse_hover)) {
1048 'objecttype' => $this->element,
1050 $classfortooltip =
'classfortooltip';
1053 $classfortooltip =
'classforajaxtooltip';
1054 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1060 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
1062 if ($option !=
'nolink') {
1064 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1065 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1066 $add_save_lastsearch_values = 1;
1068 if ($add_save_lastsearch_values) {
1069 $url .=
'&save_lastsearch_values=1';
1074 if (empty($notooltip)) {
1076 $label = $langs->trans(
"ShowCronJob");
1077 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1079 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1080 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1082 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1085 $linkstart =
'<a href="'.$url.
'"';
1086 $linkstart .= $linkclose.
'>';
1089 $result .= $linkstart;
1091 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1093 if ($withpicto != 2) {
1094 $result .= $this->ref;
1096 $result .= $linkend;
1151 global $langs, $conf, $hookmanager;
1153 $hookmanager->initHooks(array(
'cron'));
1159 $langs->load(
'cron');
1161 if (empty($userlogin)) {
1162 $this->error =
"User login is mandatory";
1163 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1169 if ($conf->entity != $this->entity && $this->entity > 0) {
1170 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1172 $savcurrententity = $conf->entity;
1173 $conf->setEntityValues($this->db, $this->entity);
1174 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1176 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1177 $user =
new User($this->db);
1178 $result = $user->fetch(
'', $userlogin);
1180 $this->error =
"User Error:".$user->error;
1181 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1182 $conf->setEntityValues($this->db, $savcurrententity);
1185 if (empty($user->id)) {
1186 $this->error =
"User login: ".$userlogin.
" does not exist";
1187 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1188 $conf->setEntityValues($this->db, $savcurrententity);
1193 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1196 $ExecTimeLimit = 600;
1197 if (!empty($ExecTimeLimit)) {
1198 $err = error_reporting();
1201 @set_time_limit($ExecTimeLimit);
1202 error_reporting($err);
1205 if (!empty($MemoryLimit)) {
1206 @ini_set(
'memory_limit', $MemoryLimit);
1210 $this->datelastrun = $now;
1211 $this->datelastresult =
null;
1212 $this->lastoutput =
'';
1213 $this->lastresult =
'';
1214 $this->processing = 1;
1215 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1216 $this->nbrun = $this->nbrun + 1;
1217 $result = $this->
update($user);
1219 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1220 $conf->setEntityValues($this->db, $savcurrententity);
1225 if ($this->jobtype ==
'method') {
1229 if ($ret ===
false || (!class_exists($this->objectname))) {
1230 if ($ret ===
false) {
1231 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1233 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1235 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1236 $this->lastoutput = $this->error;
1237 $this->lastresult = -1;
1238 $retval = $this->lastresult;
1245 if (!method_exists($this->objectname, $this->methodename)) {
1246 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1247 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1248 $this->lastoutput = $this->error;
1249 $this->lastresult = -1;
1250 $retval = $this->lastresult;
1253 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1254 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1255 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1256 $this->lastoutput = $this->error;
1257 $this->lastresult = -1;
1258 $retval = $this->lastresult;
1265 $result = $langs->load($this->module_name);
1266 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1269 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1270 $this->error = $langs->error;
1271 $this->lastoutput = $this->error;
1272 $this->lastresult = -1;
1273 $retval = $this->lastresult;
1279 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
"); !!! Log for job may be into a different log file...", LOG_DEBUG);
1282 $nameofclass = $this->objectname;
1283 $object =
new $nameofclass($this->db);
1284 if ($this->entity > 0) {
1285 $object->entity = $this->entity;
1288 $params_arr = array();
1289 if (!empty($this->params) || $this->params ===
'0') {
1290 $params_arr = array_map(
'trim', explode(
",", $this->params));
1293 if (!is_array($params_arr)) {
1294 $result = call_user_func(array($object, $this->methodename), $this->params);
1296 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1299 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1300 $langs->load(
"errors");
1302 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1303 $errmsg .= $object->error;
1305 if (is_array($object->errors) && count($object->errors)) {
1306 $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1308 if (empty($errmsg)) {
1309 $errmsg = $langs->trans(
'ErrorUnknown');
1312 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1314 $this->error = $errmsg;
1315 $this->lastoutput =
dol_substr((empty($object->output) ?
"" : $object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1316 $this->lastresult = is_numeric($result) ? $result : -1;
1317 $retval = $this->lastresult;
1320 dol_syslog(get_class($this).
"::run_jobs END");
1321 $this->lastoutput =
dol_substr((empty($object->output) ?
"" : $object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1322 $this->lastresult = var_export($result,
true);
1323 $retval = $this->lastresult;
1328 if ($this->jobtype ==
'function') {
1330 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1332 if ($ret ===
false) {
1333 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1334 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1335 $conf->setEntityValues($this->db, $savcurrententity);
1340 $result = $langs->load($this->module_name);
1341 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1343 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1344 $conf->setEntityValues($this->db, $savcurrententity);
1348 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1349 $params_arr = explode(
", ", $this->params);
1350 if (!is_array($params_arr)) {
1351 $result = call_user_func($this->methodename, $this->params);
1353 $result = call_user_func_array($this->methodename, $params_arr);
1356 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1357 $langs->load(
"errors");
1358 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1359 $this->error = $langs->trans(
'ErrorUnknown');
1360 $this->lastoutput = $this->error;
1361 $this->lastresult = is_numeric($result) ? $result : -1;
1362 $retval = $this->lastresult;
1365 $this->lastoutput = var_export($result,
true);
1366 $this->lastresult = var_export($result,
true);
1367 $retval = $this->lastresult;
1372 if ($this->jobtype ==
'command') {
1373 global $dolibarr_cron_allow_cli;
1375 if (empty($dolibarr_cron_allow_cli)) {
1376 $langs->load(
"errors");
1377 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1378 $this->lastoutput =
'';
1379 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1381 $outputdir = $conf->cron->dir_temp;
1382 if (empty($outputdir)) {
1383 $outputdir = $conf->cronjob->dir_temp;
1386 if (!empty($outputdir)) {
1388 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1391 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1392 $utils =
new Utils($this->db);
1393 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1395 $retval = $arrayresult[
'result'];
1396 $this->error = $arrayresult[
'error'];
1397 $this->lastoutput = $arrayresult[
'output'];
1398 $this->lastresult = $arrayresult[
'result'];
1403 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1405 $this->datelastresult =
dol_now();
1406 $this->processing = 0;
1408 $result = $this->
update($user);
1410 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1411 $conf->setEntityValues($this->db, $savcurrententity);
1415 $conf->setEntityValues($this->db, $savcurrententity);
1417 if ($error && !empty($this->email_alert)) {
1418 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1419 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1420 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1422 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1423 $result = $cmailfile->sendfile();
1426 return $error ? -1 : 1;