25 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
26 require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
37 public $element =
'cronjob';
42 public $table_element =
'cronjob';
47 public $picto =
'cron';
89 public $datelastrun =
'';
94 public $datenextrun =
'';
104 public $datestart =
'';
109 public $datelastresult =
'';
124 public $unitfrequency;
154 public $fk_user_author;
187 const STATUS_DISABLED = 0;
188 const STATUS_ENABLED = 1;
189 const STATUS_ARCHIVED = 2;
210 public function create($user, $notrigger = 0)
212 global $conf, $langs;
219 if (isset($this->label)) {
220 $this->label = trim($this->label);
222 if (isset($this->jobtype)) {
223 $this->jobtype = trim($this->jobtype);
225 if (isset($this->command)) {
226 $this->command = trim($this->command);
228 if (isset($this->classesname)) {
229 $this->classesname = trim($this->classesname);
231 if (isset($this->objectname)) {
232 $this->objectname = trim($this->objectname);
234 if (isset($this->methodename)) {
235 $this->methodename = trim($this->methodename);
237 if (isset($this->params)) {
238 $this->params = trim($this->params);
240 if (isset($this->md5params)) {
241 $this->md5params = trim($this->md5params);
243 if (isset($this->module_name)) {
244 $this->module_name = trim($this->module_name);
246 if (isset($this->priority)) {
247 $this->priority = trim($this->priority);
249 if (isset($this->lastoutput)) {
250 $this->lastoutput = trim($this->lastoutput);
252 if (isset($this->lastresult)) {
253 $this->lastresult = trim($this->lastresult);
255 if (isset($this->unitfrequency)) {
256 $this->unitfrequency = trim($this->unitfrequency);
258 if (isset($this->frequency)) {
259 $this->frequency = trim($this->frequency);
261 if (isset($this->status)) {
262 $this->status = trim($this->status);
264 if (isset($this->note_private)) {
265 $this->note_private = trim($this->note_private);
267 if (isset($this->nbrun)) {
268 $this->nbrun = (int) $this->nbrun;
270 if (isset($this->maxrun)) {
271 $this->maxrun = (int) $this->maxrun;
273 if (isset($this->libname)) {
274 $this->libname = trim($this->libname);
276 if (isset($this->test)) {
277 $this->test = trim($this->test);
283 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
286 if (empty($this->label)) {
287 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
290 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
291 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
294 if (empty($this->unitfrequency)) {
295 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
298 if (($this->jobtype ==
'command') && (empty($this->command))) {
299 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
302 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
303 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
306 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
307 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
310 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
311 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
314 if (($this->jobtype ==
'function') && (empty($this->libname))) {
315 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
320 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
326 $sql .=
"classesname,";
327 $sql .=
"objectname,";
328 $sql .=
"methodename,";
330 $sql .=
"md5params,";
331 $sql .=
"module_name,";
333 $sql .=
"datelastrun,";
334 $sql .=
"datenextrun,";
336 $sql .=
"datestart,";
337 $sql .=
"lastresult,";
338 $sql .=
"datelastresult,";
339 $sql .=
"lastoutput,";
340 $sql .=
"unitfrequency,";
341 $sql .=
"frequency,";
343 $sql .=
"fk_user_author,";
344 $sql .=
"fk_user_mod,";
350 $sql .=
") VALUES (";
351 $sql .=
" ".(!isset($this->entity) ? $conf->entity : $this->
db->escape($this->entity)).
",";
352 $sql .=
" '".$this->db->idate($now).
"',";
353 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
354 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
355 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
356 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
357 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
358 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
359 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
360 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
361 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
362 $sql .=
" ".(!isset($this->priority) ?
'0' : $this->priority).
",";
363 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
364 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
365 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
366 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
367 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
368 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
369 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
370 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
371 $sql .=
" ".(!isset($this->frequency) ?
'0' : $this->frequency).
",";
372 $sql .=
" ".(!isset($this->status) ?
'0' : $this->status).
",";
373 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
374 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
375 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
376 $sql .=
" ".(!isset($this->nbrun) ?
'0' : $this->
db->escape($this->nbrun)).
",";
377 $sql .=
" ".(empty($this->maxrun) ?
'0' : $this->
db->escape($this->maxrun)).
",";
378 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
379 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'").
"";
384 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
388 $this->errors[] =
"Error ".$this->db->lasterror();
392 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
397 $this->
db->rollback();
414 public function fetch($id, $objectname =
'', $methodname =
'')
418 $sql .=
" t.entity,";
421 $sql .=
" t.jobtype,";
423 $sql .=
" t.command,";
424 $sql .=
" t.classesname,";
425 $sql .=
" t.objectname,";
426 $sql .=
" t.methodename,";
427 $sql .=
" t.params,";
428 $sql .=
" t.md5params,";
429 $sql .=
" t.module_name,";
430 $sql .=
" t.priority,";
431 $sql .=
" t.datelastrun,";
432 $sql .=
" t.datenextrun,";
433 $sql .=
" t.dateend,";
434 $sql .=
" t.datestart,";
435 $sql .=
" t.lastresult,";
436 $sql .=
" t.datelastresult,";
437 $sql .=
" t.lastoutput,";
438 $sql .=
" t.unitfrequency,";
439 $sql .=
" t.frequency,";
440 $sql .=
" t.status,";
441 $sql .=
" t.processing,";
443 $sql .=
" t.email_alert,";
444 $sql .=
" t.fk_user_author,";
445 $sql .=
" t.fk_user_mod,";
446 $sql .=
" t.note as note_private,";
448 $sql .=
" t.maxrun,";
449 $sql .=
" t.libname,";
451 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
453 $sql .=
" WHERE t.rowid = ".((int) $id);
455 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
456 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
457 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
460 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
464 $obj = $this->
db->fetch_object(
$resql);
466 $this->
id = $obj->rowid;
467 $this->
ref = $obj->rowid;
468 $this->entity = $obj->entity;
469 $this->tms = $this->
db->jdate($obj->tms);
470 $this->datec = $this->
db->jdate($obj->datec);
471 $this->label = $obj->label;
472 $this->jobtype = $obj->jobtype;
473 $this->command = $obj->command;
474 $this->classesname = $obj->classesname;
475 $this->objectname = $obj->objectname;
476 $this->methodename = $obj->methodename;
477 $this->params = $obj->params;
478 $this->md5params = $obj->md5params;
479 $this->module_name = $obj->module_name;
480 $this->priority = $obj->priority;
481 $this->datelastrun = $this->
db->jdate($obj->datelastrun);
482 $this->datenextrun = $this->
db->jdate($obj->datenextrun);
483 $this->dateend = $this->
db->jdate($obj->dateend);
484 $this->datestart = $this->
db->jdate($obj->datestart);
485 $this->lastresult = $obj->lastresult;
486 $this->lastoutput = $obj->lastoutput;
487 $this->datelastresult = $this->
db->jdate($obj->datelastresult);
488 $this->unitfrequency = $obj->unitfrequency;
489 $this->frequency = $obj->frequency;
490 $this->status = $obj->status;
491 $this->processing = $obj->processing;
492 $this->pid = $obj->pid;
493 $this->email_alert = $obj->email_alert;
494 $this->fk_user_author = $obj->fk_user_author;
495 $this->fk_user_mod = $obj->fk_user_mod;
496 $this->note_private = $obj->note_private;
497 $this->nbrun = $obj->nbrun;
498 $this->maxrun = $obj->maxrun;
499 $this->libname = $obj->libname;
500 $this->test = $obj->test;
506 $this->error =
"Error ".$this->db->lasterror();
524 public function fetchAll($sortorder =
'DESC', $sortfield =
't.rowid', $limit = 0, $offset = 0, $status = 1, $filter =
'', $processing = -1)
526 $this->lines = array();
530 $sql .=
" t.entity,";
533 $sql .=
" t.jobtype,";
535 $sql .=
" t.command,";
536 $sql .=
" t.classesname,";
537 $sql .=
" t.objectname,";
538 $sql .=
" t.methodename,";
539 $sql .=
" t.params,";
540 $sql .=
" t.md5params,";
541 $sql .=
" t.module_name,";
542 $sql .=
" t.priority,";
543 $sql .=
" t.datelastrun,";
544 $sql .=
" t.datenextrun,";
545 $sql .=
" t.dateend,";
546 $sql .=
" t.datestart,";
547 $sql .=
" t.lastresult,";
548 $sql .=
" t.datelastresult,";
549 $sql .=
" t.lastoutput,";
550 $sql .=
" t.unitfrequency,";
551 $sql .=
" t.frequency,";
552 $sql .=
" t.status,";
553 $sql .=
" t.processing,";
555 $sql .=
" t.email_alert,";
556 $sql .=
" t.fk_user_author,";
557 $sql .=
" t.fk_user_mod,";
558 $sql .=
" t.note as note_private,";
560 $sql .=
" t.libname,";
562 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
563 $sql .=
" WHERE 1 = 1";
564 if ($processing >= 0) {
565 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
567 if ($status >= 0 && $status < 2) {
568 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
569 } elseif ($status == 2) {
570 $sql .=
" AND t.status = 2";
573 if (is_array($filter) && count($filter) > 0) {
574 foreach ($filter as $key => $value) {
575 if ($key ==
't.rowid') {
576 $sql .=
" AND ".$key.
" = ".((int) $value);
578 $sql .=
" AND ".$key.
" LIKE '%".$this->
db->escape($value).
"%'";
583 $sql .= $this->
db->order($sortfield, $sortorder);
584 if (!empty($limit) && !empty($offset)) {
585 $sql .= $this->
db->plimit($limit + 1, $offset);
590 if (count($sqlwhere) > 0) {
591 $sql .=
" WHERE ".implode(
' AND ', $sqlwhere);
594 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
604 $obj = $this->
db->fetch_object(
$resql);
606 $line->id = $obj->rowid;
607 $line->ref = $obj->rowid;
608 $line->entity = $obj->entity;
609 $line->tms = $this->
db->jdate($obj->tms);
610 $line->datec = $this->
db->jdate($obj->datec);
611 $line->label = $obj->label;
612 $line->jobtype = $obj->jobtype;
613 $line->command = $obj->command;
614 $line->classesname = $obj->classesname;
615 $line->objectname = $obj->objectname;
616 $line->methodename = $obj->methodename;
617 $line->params = $obj->params;
618 $line->md5params = $obj->md5params;
619 $line->module_name = $obj->module_name;
620 $line->priority = $obj->priority;
621 $line->datelastrun = $this->
db->jdate($obj->datelastrun);
622 $line->datenextrun = $this->
db->jdate($obj->datenextrun);
623 $line->dateend = $this->
db->jdate($obj->dateend);
624 $line->datestart = $this->
db->jdate($obj->datestart);
625 $line->lastresult = $obj->lastresult;
626 $line->datelastresult = $this->
db->jdate($obj->datelastresult);
627 $line->lastoutput = $obj->lastoutput;
628 $line->unitfrequency = $obj->unitfrequency;
629 $line->frequency = $obj->frequency;
630 $line->status = $obj->status;
631 $line->processing = $obj->processing;
632 $line->pid = $obj->pid;
633 $line->email_alert = $obj->email_alert;
634 $line->fk_user_author = $obj->fk_user_author;
635 $line->fk_user_mod = $obj->fk_user_mod;
636 $line->note_private = $obj->note_private;
637 $line->nbrun = $obj->nbrun;
638 $line->libname = $obj->libname;
639 $line->test = $obj->test;
640 $this->lines[] = $line;
649 $this->error =
"Error ".$this->db->lasterror();
662 public function update($user =
null, $notrigger = 0)
664 global $conf, $langs;
666 $langs->load(
'cron');
671 if (isset($this->label)) {
672 $this->label = trim($this->label);
674 if (isset($this->jobtype)) {
675 $this->jobtype = trim($this->jobtype);
677 if (isset($this->command)) {
678 $this->command = trim($this->command);
680 if (isset($this->classesname)) {
681 $this->classesname = trim($this->classesname);
683 if (isset($this->objectname)) {
684 $this->objectname = trim($this->objectname);
686 if (isset($this->methodename)) {
687 $this->methodename = trim($this->methodename);
689 if (isset($this->params)) {
690 $this->params = trim($this->params);
692 if (isset($this->md5params)) {
693 $this->md5params = trim($this->md5params);
695 if (isset($this->module_name)) {
696 $this->module_name = trim($this->module_name);
698 if (isset($this->priority)) {
699 $this->priority = trim($this->priority);
701 if (isset($this->lastoutput)) {
702 $this->lastoutput = trim($this->lastoutput);
704 if (isset($this->lastresult)) {
705 $this->lastresult = trim($this->lastresult);
707 if (isset($this->unitfrequency)) {
708 $this->unitfrequency = trim($this->unitfrequency);
710 if (isset($this->frequency)) {
711 $this->frequency = trim($this->frequency);
713 if (isset($this->status)) {
714 $this->status = trim($this->status);
716 if (isset($this->note_private)) {
717 $this->note_private = trim($this->note_private);
719 if (isset($this->nbrun)) {
720 $this->nbrun = trim($this->nbrun);
722 if (isset($this->libname)) {
723 $this->libname = trim($this->libname);
725 if (isset($this->test)) {
726 $this->test = trim($this->test);
729 if (empty($this->maxrun)) {
732 if (empty($this->processing)) {
733 $this->processing = 0;
735 if (empty($this->pid)) {
738 if (empty($this->email_alert)) {
739 $this->email_alert =
'';
745 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
748 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
749 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
752 if (empty($this->label)) {
753 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
756 if (empty($this->unitfrequency)) {
757 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
760 if (($this->jobtype ==
'command') && (empty($this->command))) {
761 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
764 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
765 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
768 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
769 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
772 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
773 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
777 if (($this->jobtype ==
'function') && (empty($this->libname))) {
778 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
784 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
785 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).
",";
786 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
787 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
788 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
789 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
790 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
791 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
792 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
793 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
794 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
795 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
796 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
797 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
798 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
799 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
800 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
801 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
802 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
803 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency :
"null").
",";
804 $sql .=
" frequency=".(isset($this->frequency) ? $this->frequency :
"null").
",";
805 $sql .=
" status=".(isset($this->status) ? $this->status :
"null").
",";
806 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
807 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
808 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
809 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
810 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
811 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
812 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
813 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
814 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
815 $sql .=
" WHERE rowid=".((int) $this->
id);
819 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
822 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
827 foreach ($this->errors as $errmsg) {
828 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
829 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
831 $this->
db->rollback();
847 public function delete($user, $notrigger = 0)
853 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"cronjob";
854 $sql .=
" WHERE rowid=".((int) $this->
id);
856 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
860 $this->errors[] =
"Error ".$this->db->lasterror();
865 foreach ($this->errors as $errmsg) {
866 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
867 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
869 $this->
db->rollback();
897 $object->fetch($fromid);
901 $object->status = self::STATUS_DISABLED;
902 $object->label = $langs->trans(
"CopyOf").
' '.$langs->trans($object->label);
903 $object->datelastrun =
null;
904 $object->lastresult =
'';
905 $object->datelastresult =
null;
906 $object->lastoutput =
'';
910 $object->context[
'createfromclone'] =
'createfromclone';
911 $result = $object->create($user);
915 $this->error = $object->error;
916 $this->errors = $object->errors;
920 unset($object->context[
'createfromclone']);
927 $this->
db->rollback();
949 $this->classesname =
'';
950 $this->objectname =
'';
951 $this->methodename =
'';
953 $this->md5params =
'';
954 $this->module_name =
'';
955 $this->priority =
'';
956 $this->datelastrun =
'';
957 $this->datenextrun =
'';
959 $this->datestart =
'';
960 $this->datelastresult =
'';
961 $this->lastoutput =
'';
962 $this->lastresult =
'';
963 $this->unitfrequency =
'';
964 $this->frequency =
'';
966 $this->processing = 0;
968 $this->email_alert =
'';
969 $this->fk_user_author = 0;
970 $this->fk_user_mod = 0;
971 $this->note_private =
'';
988 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
990 global $db, $conf, $langs;
991 global $dolibarr_main_authentication, $dolibarr_main_demo;
994 if (!empty($conf->dol_no_mouse_hover)) {
1000 $label =
img_picto(
'',
'object_'.$this->picto).
' <u>'.$langs->trans(
"CronTask").
'</u>';
1001 if (isset($this->status)) {
1002 $label .=
' '.$this->getLibStatut(5);
1004 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1005 $label .=
'<br><b>'.$langs->trans(
'Title').
':</b> '.$langs->trans($this->label);
1006 if ($this->label != $langs->trans($this->label)) {
1007 $label .=
' <span class="opacitymedium">('.$this->label.
')</span>';
1009 if (!empty($this->datestart)) {
1010 $label .=
'<br><b>'.$langs->trans(
'CronDtStart').
':</b> '.
dol_print_date($this->datestart,
'dayhour',
'tzuserrel');
1012 if (!empty($this->dateend)) {
1013 $label .=
'<br><b>'.$langs->trans(
'CronDtEnd').
':</b> '.
dol_print_date($this->dateend,
'dayhour',
'tzuserrel');
1016 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
1018 if ($option !=
'nolink') {
1020 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1021 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1022 $add_save_lastsearch_values = 1;
1024 if ($add_save_lastsearch_values) {
1025 $url .=
'&save_lastsearch_values=1';
1030 if (empty($notooltip)) {
1031 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1032 $label = $langs->trans(
"ShowCronJob");
1033 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1035 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1036 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1038 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1041 $linkstart =
'<a href="'.$url.
'"';
1042 $linkstart .= $linkclose.
'>';
1045 $result .= $linkstart;
1047 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1049 if ($withpicto != 2) {
1050 $result .= $this->ref;
1052 $result .= $linkend;
1068 $sql .=
" f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1069 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as f";
1070 $sql .=
" WHERE f.rowid = ".((int) $id);
1072 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1076 $obj = $this->
db->fetch_object(
$resql);
1077 $this->
id = $obj->rowid;
1079 $this->user_modification_id = $obj->fk_user_mod;
1080 $this->user_creation_id = $obj->fk_user_author;
1081 $this->date_creation = $this->
db->jdate($obj->datec);
1082 $this->date_modification = $this->
db->jdate($obj->tms);
1088 $this->error =
"Error ".$this->db->lasterror();
1106 global $langs, $conf, $hookmanager;
1108 $hookmanager->initHooks(array(
'cron'));
1114 $langs->load(
'cron');
1116 if (empty($userlogin)) {
1117 $this->error =
"User login is mandatory";
1118 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1124 if ($conf->entity != $this->entity && $this->entity > 0) {
1125 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1127 $savcurrententity = $conf->entity;
1128 $conf->setEntityValues($this->
db, $this->entity);
1129 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1131 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1132 $user =
new User($this->
db);
1133 $result = $user->fetch(
'', $userlogin);
1135 $this->error =
"User Error:".$user->error;
1136 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1137 $conf->setEntityValues($this->
db, $savcurrententity);
1140 if (empty($user->id)) {
1141 $this->error =
" User user login:".$userlogin.
" do not exists";
1142 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1143 $conf->setEntityValues($this->
db, $savcurrententity);
1148 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1151 $ExecTimeLimit = 600;
1152 if (!empty($ExecTimeLimit)) {
1153 $err = error_reporting();
1156 @set_time_limit($ExecTimeLimit);
1157 error_reporting($err);
1160 if (!empty($MemoryLimit)) {
1161 @ini_set(
'memory_limit', $MemoryLimit);
1165 $this->datelastrun = $now;
1166 $this->datelastresult =
null;
1167 $this->lastoutput =
'';
1168 $this->lastresult =
'';
1169 $this->processing = 1;
1170 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1171 $this->nbrun = $this->nbrun + 1;
1172 $result = $this->
update($user);
1174 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1175 $conf->setEntityValues($this->
db, $savcurrententity);
1180 if ($this->jobtype ==
'method') {
1184 if ($ret ===
false || (!class_exists($this->objectname))) {
1185 if ($ret ===
false) {
1186 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1188 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1190 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1191 $this->lastoutput = $this->error;
1192 $this->lastresult = -1;
1193 $retval = $this->lastresult;
1200 if (!method_exists($this->objectname, $this->methodename)) {
1201 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1202 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1203 $this->lastoutput = $this->error;
1204 $this->lastresult = -1;
1205 $retval = $this->lastresult;
1208 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1209 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1210 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1211 $this->lastoutput = $this->error;
1212 $this->lastresult = -1;
1213 $retval = $this->lastresult;
1220 $result = $langs->load($this->module_name);
1221 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1224 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1225 $this->error = $langs->error;
1226 $this->lastoutput = $this->error;
1227 $this->lastresult = -1;
1228 $retval = $this->lastresult;
1234 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1237 $nameofclass = $this->objectname;
1238 $object =
new $nameofclass($this->
db);
1239 if ($this->entity > 0) {
1240 $object->entity = $this->entity;
1243 $params_arr = array();
1244 if (!empty($this->params) || $this->params ===
'0') {
1245 $params_arr = array_map(
'trim', explode(
",", $this->params));
1248 if (!is_array($params_arr)) {
1249 $result = call_user_func(array($object, $this->methodename), $this->params);
1251 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1254 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1255 $langs->load(
"errors");
1258 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1259 $errmsg .= $object->error;
1261 if (is_array($object->errors) && count($object->errors)) {
1262 $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1264 if (empty($errmsg)) {
1265 $errmsg = $langs->trans(
'ErrorUnknown');
1268 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1270 $this->error = $errmsg;
1271 $this->lastoutput = (!empty($object->output) ? $object->output.
"\n" :
"").$errmsg;
1272 $this->lastresult = is_numeric($result) ? $result : -1;
1273 $retval = $this->lastresult;
1276 dol_syslog(get_class($this).
"::run_jobs END");
1277 $this->lastoutput = (!empty($object->output) ? $object->output :
"");
1278 $this->lastresult = var_export($result,
true);
1279 $retval = $this->lastresult;
1284 if ($this->jobtype ==
'function') {
1286 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1288 if ($ret ===
false) {
1289 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1290 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1291 $conf->setEntityValues($this->
db, $savcurrententity);
1296 $result = $langs->load($this->module_name);
1297 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1299 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1300 $conf->setEntityValues($this->
db, $savcurrententity);
1304 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1305 $params_arr = explode(
", ", $this->params);
1306 if (!is_array($params_arr)) {
1307 $result = call_user_func($this->methodename, $this->params);
1309 $result = call_user_func_array($this->methodename, $params_arr);
1312 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1313 $langs->load(
"errors");
1314 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1315 $this->error = $langs->trans(
'ErrorUnknown');
1316 $this->lastoutput = $this->error;
1317 $this->lastresult = is_numeric($result) ? $result : -1;
1318 $retval = $this->lastresult;
1321 $this->lastoutput = var_export($result,
true);
1322 $this->lastresult = var_export($result,
true);
1323 $retval = $this->lastresult;
1328 if ($this->jobtype ==
'command') {
1329 global $dolibarr_cron_allow_cli;
1331 if (empty($dolibarr_cron_allow_cli)) {
1332 $langs->load(
"errors");
1333 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1334 $this->lastoutput =
'';
1335 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1337 $outputdir = $conf->cron->dir_temp;
1338 if (empty($outputdir)) {
1339 $outputdir = $conf->cronjob->dir_temp;
1342 if (!empty($outputdir)) {
1344 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1347 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1348 $utils =
new Utils($this->
db);
1349 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1351 $retval = $arrayresult[
'result'];
1352 $this->error = $arrayresult[
'error'];
1353 $this->lastoutput = $arrayresult[
'output'];
1354 $this->lastresult = $arrayresult[
'result'];
1359 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1361 $this->datelastresult =
dol_now();
1362 $this->processing = 0;
1364 $result = $this->
update($user);
1366 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1367 $conf->setEntityValues($this->
db, $savcurrententity);
1371 $conf->setEntityValues($this->
db, $savcurrententity);
1373 if ($error && !empty($this->email_alert)) {
1374 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1375 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1376 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1378 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1379 $result = $cmailfile->sendfile();
1382 return $error ?-1 : 1;
1397 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1399 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1400 $user =
new User($this->
db);
1401 $result = $user->fetch(
'', $userlogin);
1403 $this->error =
"User Error : ".$user->error;
1404 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1407 if (empty($user->id)) {
1408 $this->error =
" User user login:".$userlogin.
" do not exists";
1409 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1414 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1416 if (empty($this->datenextrun)) {
1417 if (empty($this->datestart)) {
1418 if ($this->unitfrequency == 2678400) {
1421 $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1424 if ($this->unitfrequency == 2678400) {
1427 $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1432 if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
1434 while ($this->datenextrun < $now) {
1435 if ($this->unitfrequency == 2678400) {
1438 $this->datenextrun += ($this->frequency * $this->unitfrequency);
1442 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1447 if ($this->autodelete == 2) {
1448 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1449 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1450 $this->status = self::STATUS_ARCHIVED;
1451 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1455 $result = $this->
update($user);
1457 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1472 return $this->
LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1485 public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1488 $this->labelStatus = array();
1489 $this->labelStatusShort = array();
1491 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1493 $langs->load(
'users');
1497 $moretext =
' ('.$langs->trans(
"Running").
')';
1498 } elseif ($lastresult) {
1499 $moretext .=
' ('.$langs->trans(
"Error").
')';
1502 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled').$moretext;
1503 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled').$moretext;
1504 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled');
1505 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled');
1508 $statusType =
'status4';
1509 if ($status == 1 && $processing) {
1510 $statusType =
'status1';
1513 $statusType =
'status5';
1515 if ($this->lastresult) {
1516 $statusType =
'status8';
1519 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1552 public $classesname;
1554 public $methodename;
1557 public $module_name;
1559 public $datelastrun =
'';
1560 public $datenextrun =
'';
1561 public $dateend =
'';
1562 public $datestart =
'';
1563 public $datelastresult =
'';
1564 public $lastresult =
'';
1566 public $unitfrequency;
1578 public $fk_user_author;
1583 public $fk_user_mod;
1586 public $note_private;