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' : ((int) $this->frequency)).
",";
372 $sql .=
" ".(!isset($this->status) ?
'0' : ((int) $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' : ((int) $this->nbrun)).
",";
377 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $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);
385 $resql = $this->db->query(
$sql);
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);
461 $resql = $this->db->query(
$sql);
463 if ($this->db->num_rows($resql)) {
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;
502 $this->db->free($resql);
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);
595 $resql = $this->db->query(
$sql);
597 $num = $this->db->num_rows($resql);
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;
645 $this->db->free($resql);
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 =
'';
744 if (
dol_strlen($this->datenextrun) == 0 && $this->status == self::STATUS_ENABLED) {
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);
820 $resql = $this->db->query(
$sql);
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);
857 $resql = $this->db->query(
$sql);
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();
892 $object =
new Cronjob($this->db);
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 $langs->load(
'cron');
991 $datas[
'picto'] =
img_picto(
'',
'object_'.$this->picto).
' <u>'.$langs->trans(
"CronTask").
'</u>';
992 if (isset($this->status)) {
993 $datas[
'picto'] .=
' '.$this->getLibStatut(5);
996 $datas[
'label'] =
'<br><b>'.$langs->trans(
'Title').
':</b> '.$langs->trans($this->label);
997 if ($this->label != $langs->trans($this->label)) {
998 $datas[
'label'] .=
' <span class="opacitymedium">('.$this->label.
')</span>';
1000 if (!empty($this->params)) {
1001 $datas[
'params'] =
'<br><b>'.$langs->trans(
'Parameters').
':</b> '.
dol_escape_htmltag($this->params);
1003 $datas[
'space'] =
'<br>';
1005 if (!empty($this->datestart) && $this->datestart >=
dol_now()) {
1006 $datas[
'crondtstart'] =
'<br><b>'.$langs->trans(
'CronDtStart').
':</b> '.
dol_print_date($this->datestart,
'dayhour',
'tzuserrel');
1008 if (!empty($this->dateend)) {
1009 $datas[
'crondtend'] =
'<br><b>'.$langs->trans(
'CronDtEnd').
':</b> '.
dol_print_date($this->dateend,
'dayhour',
'tzuserrel');
1011 if (!empty($this->datelastrun)) {
1012 $datas[
'cronlastlaunch'] =
'<br><b>'.$langs->trans(
'CronDtLastLaunch').
':</b> '.
dol_print_date($this->datelastrun,
'dayhour',
'tzuserrel');
1014 if (!empty($this->datenextrun)) {
1015 $datas[
'crondtnextlaunch'] =
'<br><b>'.$langs->trans(
'CronDtNextLaunch').
':</b> '.
dol_print_date($this->datenextrun,
'dayhour',
'tzuserrel');
1031 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1033 global $db, $conf, $langs;
1035 if (!empty($conf->dol_no_mouse_hover)) {
1043 'objecttype' => $this->element,
1045 $classfortooltip =
'classfortooltip';
1048 $classfortooltip =
'classforajaxtooltip';
1049 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1055 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
1057 if ($option !=
'nolink') {
1059 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1060 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1061 $add_save_lastsearch_values = 1;
1063 if ($add_save_lastsearch_values) {
1064 $url .=
'&save_lastsearch_values=1';
1069 if (empty($notooltip)) {
1070 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1071 $label = $langs->trans(
"ShowCronJob");
1072 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1074 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1075 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1077 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1080 $linkstart =
'<a href="'.$url.
'"';
1081 $linkstart .= $linkclose.
'>';
1084 $result .= $linkstart;
1086 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1088 if ($withpicto != 2) {
1089 $result .= $this->ref;
1091 $result .= $linkend;
1107 $sql .=
" f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1108 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as f";
1109 $sql .=
" WHERE f.rowid = ".((int) $id);
1111 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1112 $resql = $this->db->query(
$sql);
1114 if ($this->db->num_rows($resql)) {
1115 $obj = $this->db->fetch_object($resql);
1116 $this->
id = $obj->rowid;
1118 $this->user_modification_id = $obj->fk_user_mod;
1119 $this->user_creation_id = $obj->fk_user_author;
1120 $this->date_creation = $this->db->jdate($obj->datec);
1121 $this->date_modification = $this->db->jdate($obj->tms);
1123 $this->db->free($resql);
1127 $this->error =
"Error ".$this->db->lasterror();
1145 global $langs, $conf, $hookmanager;
1147 $hookmanager->initHooks(array(
'cron'));
1153 $langs->load(
'cron');
1155 if (empty($userlogin)) {
1156 $this->error =
"User login is mandatory";
1157 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1163 if ($conf->entity != $this->entity && $this->entity > 0) {
1164 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1166 $savcurrententity = $conf->entity;
1167 $conf->setEntityValues($this->db, $this->entity);
1168 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1170 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1171 $user =
new User($this->db);
1172 $result = $user->fetch(
'', $userlogin);
1174 $this->error =
"User Error:".$user->error;
1175 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1176 $conf->setEntityValues($this->db, $savcurrententity);
1179 if (empty($user->id)) {
1180 $this->error =
"User login: ".$userlogin.
" does not exist";
1181 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1182 $conf->setEntityValues($this->db, $savcurrententity);
1187 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1190 $ExecTimeLimit = 600;
1191 if (!empty($ExecTimeLimit)) {
1192 $err = error_reporting();
1195 @set_time_limit($ExecTimeLimit);
1196 error_reporting($err);
1199 if (!empty($MemoryLimit)) {
1200 @ini_set(
'memory_limit', $MemoryLimit);
1204 $this->datelastrun = $now;
1205 $this->datelastresult =
null;
1206 $this->lastoutput =
'';
1207 $this->lastresult =
'';
1208 $this->processing = 1;
1209 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1210 $this->nbrun = $this->nbrun + 1;
1211 $result = $this->
update($user);
1213 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1214 $conf->setEntityValues($this->db, $savcurrententity);
1219 if ($this->jobtype ==
'method') {
1223 if ($ret ===
false || (!class_exists($this->objectname))) {
1224 if ($ret ===
false) {
1225 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1227 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1229 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1230 $this->lastoutput = $this->error;
1231 $this->lastresult = -1;
1232 $retval = $this->lastresult;
1239 if (!method_exists($this->objectname, $this->methodename)) {
1240 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1241 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1242 $this->lastoutput = $this->error;
1243 $this->lastresult = -1;
1244 $retval = $this->lastresult;
1247 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1248 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1249 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1250 $this->lastoutput = $this->error;
1251 $this->lastresult = -1;
1252 $retval = $this->lastresult;
1259 $result = $langs->load($this->module_name);
1260 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1263 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1264 $this->error = $langs->error;
1265 $this->lastoutput = $this->error;
1266 $this->lastresult = -1;
1267 $retval = $this->lastresult;
1273 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1276 $nameofclass = $this->objectname;
1277 $object =
new $nameofclass($this->db);
1278 if ($this->entity > 0) {
1279 $object->entity = $this->entity;
1282 $params_arr = array();
1283 if (!empty($this->params) || $this->params ===
'0') {
1284 $params_arr = array_map(
'trim', explode(
",", $this->params));
1287 if (!is_array($params_arr)) {
1288 $result = call_user_func(array($object, $this->methodename), $this->params);
1290 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1293 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1294 $langs->load(
"errors");
1297 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1298 $errmsg .= $object->error;
1300 if (is_array($object->errors) && count($object->errors)) {
1301 $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1303 if (empty($errmsg)) {
1304 $errmsg = $langs->trans(
'ErrorUnknown');
1307 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1309 $this->error = $errmsg;
1310 $this->lastoutput = (!empty($object->output) ? $object->output.
"\n" :
"").$errmsg;
1311 $this->lastresult = is_numeric($result) ? $result : -1;
1312 $retval = $this->lastresult;
1315 dol_syslog(get_class($this).
"::run_jobs END");
1316 $this->lastoutput = (!empty($object->output) ? $object->output :
"");
1317 $this->lastresult = var_export($result,
true);
1318 $retval = $this->lastresult;
1323 if ($this->jobtype ==
'function') {
1325 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1327 if ($ret ===
false) {
1328 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1329 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1330 $conf->setEntityValues($this->db, $savcurrententity);
1335 $result = $langs->load($this->module_name);
1336 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1338 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1339 $conf->setEntityValues($this->db, $savcurrententity);
1343 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1344 $params_arr = explode(
", ", $this->params);
1345 if (!is_array($params_arr)) {
1346 $result = call_user_func($this->methodename, $this->params);
1348 $result = call_user_func_array($this->methodename, $params_arr);
1351 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1352 $langs->load(
"errors");
1353 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1354 $this->error = $langs->trans(
'ErrorUnknown');
1355 $this->lastoutput = $this->error;
1356 $this->lastresult = is_numeric($result) ? $result : -1;
1357 $retval = $this->lastresult;
1360 $this->lastoutput = var_export($result,
true);
1361 $this->lastresult = var_export($result,
true);
1362 $retval = $this->lastresult;
1367 if ($this->jobtype ==
'command') {
1368 global $dolibarr_cron_allow_cli;
1370 if (empty($dolibarr_cron_allow_cli)) {
1371 $langs->load(
"errors");
1372 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1373 $this->lastoutput =
'';
1374 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1376 $outputdir = $conf->cron->dir_temp;
1377 if (empty($outputdir)) {
1378 $outputdir = $conf->cronjob->dir_temp;
1381 if (!empty($outputdir)) {
1383 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1386 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1387 $utils =
new Utils($this->db);
1388 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1390 $retval = $arrayresult[
'result'];
1391 $this->error = $arrayresult[
'error'];
1392 $this->lastoutput = $arrayresult[
'output'];
1393 $this->lastresult = $arrayresult[
'result'];
1398 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1400 $this->datelastresult =
dol_now();
1401 $this->processing = 0;
1403 $result = $this->
update($user);
1405 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1406 $conf->setEntityValues($this->db, $savcurrententity);
1410 $conf->setEntityValues($this->db, $savcurrententity);
1412 if ($error && !empty($this->email_alert)) {
1413 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1414 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1415 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1417 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1418 $result = $cmailfile->sendfile();
1421 return $error ?-1 : 1;
1436 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1438 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1439 $user =
new User($this->db);
1440 $result = $user->fetch(
'', $userlogin);
1442 $this->error =
"User Error : ".$user->error;
1443 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1446 if (empty($user->id)) {
1447 $this->error =
" User user login:".$userlogin.
" do not exists";
1448 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1453 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1455 if (empty($this->datenextrun)) {
1456 if (empty($this->datestart)) {
1457 if ($this->unitfrequency == 2678400) {
1460 $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1463 if ($this->unitfrequency == 2678400) {
1466 $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1471 if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
1473 while ($this->datenextrun < $now) {
1474 if ($this->unitfrequency == 2678400) {
1477 $this->datenextrun += ($this->frequency * $this->unitfrequency);
1481 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1486 if ($this->autodelete == 2) {
1487 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1488 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1489 $this->status = self::STATUS_ARCHIVED;
1490 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1494 $result = $this->
update($user);
1496 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1511 return $this->
LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1524 public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1527 $this->labelStatus = array();
1528 $this->labelStatusShort = array();
1530 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1532 $langs->load(
'users');
1536 $moretext =
' ('.$langs->trans(
"Running").
')';
1537 } elseif ($lastresult) {
1538 $moretext .=
' ('.$langs->trans(
"Error").
')';
1541 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled').$moretext;
1542 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled').$moretext;
1543 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled');
1544 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled');
1547 $statusType =
'status4';
1548 if ($status == 1 && $processing) {
1549 $statusType =
'status1';
1552 $statusType =
'status5';
1554 if ($this->lastresult) {
1555 $statusType =
'status8';
1558 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1591 public $classesname;
1593 public $methodename;
1596 public $module_name;
1598 public $datelastrun =
'';
1599 public $datenextrun =
'';
1600 public $dateend =
'';
1601 public $datestart =
'';
1602 public $datelastresult =
'';
1603 public $lastresult =
'';
1605 public $unitfrequency;
1617 public $fk_user_author;
1622 public $fk_user_mod;
1625 public $note_private;