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 .=
" ".((int) $user->id).
",";
374 $sql .=
" ".((int) $user->id).
",";
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);
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);
905 $object->context[
'createfromclone'] =
'createfromclone';
906 $result = $object->create($user);
910 $this->error = $object->error;
914 unset($object->context[
'createfromclone']);
921 $this->
db->rollback();
943 $this->classesname =
'';
944 $this->objectname =
'';
945 $this->methodename =
'';
947 $this->md5params =
'';
948 $this->module_name =
'';
949 $this->priority =
'';
950 $this->datelastrun =
'';
951 $this->datenextrun =
'';
953 $this->datestart =
'';
954 $this->datelastresult =
'';
955 $this->lastoutput =
'';
956 $this->lastresult =
'';
957 $this->unitfrequency =
'';
958 $this->frequency =
'';
960 $this->processing = 0;
962 $this->email_alert =
'';
963 $this->fk_user_author = 0;
964 $this->fk_user_mod = 0;
965 $this->note_private =
'';
982 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
984 global $db, $conf, $langs;
985 global $dolibarr_main_authentication, $dolibarr_main_demo;
988 if (!empty($conf->dol_no_mouse_hover)) {
994 $label =
img_picto(
'',
'object_'.$this->picto).
' <u>'.$langs->trans(
"CronTask").
'</u>';
995 if (isset($this->status)) {
996 $label .=
' '.$this->getLibStatut(5);
998 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
999 $label .=
'<br><b>'.$langs->trans(
'Title').
':</b> '.$langs->trans($this->label);
1000 if ($this->label != $langs->trans($this->label)) {
1001 $label .=
' <span class="opacitymedium">('.$this->label.
')</span>';
1003 if (!empty($this->datestart)) {
1004 $label .=
'<br><b>'.$langs->trans(
'CronDtStart').
':</b> '.
dol_print_date($this->datestart,
'dayhour',
'tzuserrel');
1006 if (!empty($this->dateend)) {
1007 $label .=
'<br><b>'.$langs->trans(
'CronDtEnd').
':</b> '.
dol_print_date($this->dateend,
'dayhour',
'tzuserrel');
1010 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
1012 if ($option !=
'nolink') {
1014 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1015 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1016 $add_save_lastsearch_values = 1;
1018 if ($add_save_lastsearch_values) {
1019 $url .=
'&save_lastsearch_values=1';
1024 if (empty($notooltip)) {
1025 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1026 $label = $langs->trans(
"ShowCronJob");
1027 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1029 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1030 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1032 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1035 $linkstart =
'<a href="'.$url.
'"';
1036 $linkstart .= $linkclose.
'>';
1039 $result .= $linkstart;
1041 $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);
1043 if ($withpicto != 2) {
1044 $result .= $this->ref;
1046 $result .= $linkend;
1062 $sql .=
" f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1063 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as f";
1064 $sql .=
" WHERE f.rowid = ".((int) $id);
1066 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1070 $obj = $this->
db->fetch_object(
$resql);
1071 $this->
id = $obj->rowid;
1073 $this->user_modification_id = $obj->fk_user_mod;
1074 $this->user_creation_id = $obj->fk_user_author;
1075 $this->date_creation = $this->
db->jdate($obj->datec);
1076 $this->date_modification = $this->
db->jdate($obj->tms);
1082 $this->error =
"Error ".$this->db->lasterror();
1100 global $langs, $conf, $hookmanager;
1102 $hookmanager->initHooks(array(
'cron'));
1108 $langs->load(
'cron');
1110 if (empty($userlogin)) {
1111 $this->error =
"User login is mandatory";
1112 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1118 if ($conf->entity != $this->entity && $this->entity > 0) {
1119 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1121 $savcurrententity = $conf->entity;
1122 $conf->setEntityValues($this->
db, $this->entity);
1123 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1125 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1126 $user =
new User($this->
db);
1127 $result = $user->fetch(
'', $userlogin);
1129 $this->error =
"User Error:".$user->error;
1130 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1131 $conf->setEntityValues($this->
db, $savcurrententity);
1134 if (empty($user->id)) {
1135 $this->error =
" User user login:".$userlogin.
" do not exists";
1136 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1137 $conf->setEntityValues($this->
db, $savcurrententity);
1142 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1145 $ExecTimeLimit = 600;
1146 if (!empty($ExecTimeLimit)) {
1147 $err = error_reporting();
1150 @set_time_limit($ExecTimeLimit);
1151 error_reporting($err);
1154 if (!empty($MemoryLimit)) {
1155 @ini_set(
'memory_limit', $MemoryLimit);
1159 $this->datelastrun = $now;
1160 $this->datelastresult =
null;
1161 $this->lastoutput =
'';
1162 $this->lastresult =
'';
1163 $this->processing = 1;
1164 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1165 $this->nbrun = $this->nbrun + 1;
1166 $result = $this->
update($user);
1168 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1169 $conf->setEntityValues($this->
db, $savcurrententity);
1174 if ($this->jobtype ==
'method') {
1178 if ($ret ===
false || (!class_exists($this->objectname))) {
1179 if ($ret ===
false) {
1180 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1182 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1184 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1185 $this->lastoutput = $this->error;
1186 $this->lastresult = -1;
1187 $retval = $this->lastresult;
1194 if (!method_exists($this->objectname, $this->methodename)) {
1195 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1196 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1197 $this->lastoutput = $this->error;
1198 $this->lastresult = -1;
1199 $retval = $this->lastresult;
1202 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1203 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1204 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1205 $this->lastoutput = $this->error;
1206 $this->lastresult = -1;
1207 $retval = $this->lastresult;
1214 $result = $langs->load($this->module_name);
1215 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1218 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1219 $this->error = $langs->error;
1220 $this->lastoutput = $this->error;
1221 $this->lastresult = -1;
1222 $retval = $this->lastresult;
1228 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1231 $nameofclass = $this->objectname;
1232 $object =
new $nameofclass($this->
db);
1233 if ($this->entity > 0) {
1234 $object->entity = $this->entity;
1237 $params_arr = array();
1238 if (!empty($this->params) || $this->params ===
'0') {
1239 $params_arr = array_map(
'trim', explode(
",", $this->params));
1242 if (!is_array($params_arr)) {
1243 $result = call_user_func(array($object, $this->methodename), $this->params);
1245 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1248 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1249 $langs->load(
"errors");
1252 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1253 $errmsg .= $object->error;
1255 if (is_array($object->errors) && count($object->errors)) {
1256 $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1258 if (empty($errmsg)) {
1259 $errmsg = $langs->trans(
'ErrorUnknown');
1262 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1264 $this->error = $errmsg;
1265 $this->lastoutput = (!empty($object->output) ? $object->output.
"\n" :
"").$errmsg;
1266 $this->lastresult = is_numeric($result) ? $result : -1;
1267 $retval = $this->lastresult;
1270 dol_syslog(get_class($this).
"::run_jobs END");
1271 $this->lastoutput = (!empty($object->output) ? $object->output :
"");
1272 $this->lastresult = var_export($result,
true);
1273 $retval = $this->lastresult;
1278 if ($this->jobtype ==
'function') {
1280 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1282 if ($ret ===
false) {
1283 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1284 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1285 $conf->setEntityValues($this->
db, $savcurrententity);
1290 $result = $langs->load($this->module_name);
1291 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1293 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1294 $conf->setEntityValues($this->
db, $savcurrententity);
1298 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1299 $params_arr = explode(
", ", $this->params);
1300 if (!is_array($params_arr)) {
1301 $result = call_user_func($this->methodename, $this->params);
1303 $result = call_user_func_array($this->methodename, $params_arr);
1306 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1307 $langs->load(
"errors");
1308 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1309 $this->error = $langs->trans(
'ErrorUnknown');
1310 $this->lastoutput = $this->error;
1311 $this->lastresult = is_numeric($result) ? $result : -1;
1312 $retval = $this->lastresult;
1315 $this->lastoutput = var_export($result,
true);
1316 $this->lastresult = var_export($result,
true);
1317 $retval = $this->lastresult;
1322 if ($this->jobtype ==
'command') {
1323 global $dolibarr_cron_allow_cli;
1325 if (empty($dolibarr_cron_allow_cli)) {
1326 $langs->load(
"errors");
1327 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1328 $this->lastoutput =
'';
1329 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1331 $outputdir = $conf->cron->dir_temp;
1332 if (empty($outputdir)) {
1333 $outputdir = $conf->cronjob->dir_temp;
1336 if (!empty($outputdir)) {
1338 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1341 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1342 $utils =
new Utils($this->
db);
1343 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1345 $retval = $arrayresult[
'result'];
1346 $this->error = $arrayresult[
'error'];
1347 $this->lastoutput = $arrayresult[
'output'];
1348 $this->lastresult = $arrayresult[
'result'];
1353 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1355 $this->datelastresult =
dol_now();
1356 $this->processing = 0;
1358 $result = $this->
update($user);
1360 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1361 $conf->setEntityValues($this->
db, $savcurrententity);
1365 $conf->setEntityValues($this->
db, $savcurrententity);
1367 if ($error && !empty($this->email_alert)) {
1368 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1369 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1370 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1372 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1373 $result = $cmailfile->sendfile();
1376 return $error ?-1 : 1;
1391 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1393 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1394 $user =
new User($this->
db);
1395 $result = $user->fetch(
'', $userlogin);
1397 $this->error =
"User Error : ".$user->error;
1398 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1401 if (empty($user->id)) {
1402 $this->error =
" User user login:".$userlogin.
" do not exists";
1403 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1408 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1410 if (empty($this->datenextrun)) {
1411 if (empty($this->datestart)) {
1412 if ($this->unitfrequency == 2678400) {
1415 $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1418 if ($this->unitfrequency == 2678400) {
1421 $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1426 if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
1428 while ($this->datenextrun < $now) {
1429 if ($this->unitfrequency == 2678400) {
1432 $this->datenextrun += ($this->frequency * $this->unitfrequency);
1436 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1441 if ($this->autodelete == 2) {
1442 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1443 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1444 $this->status = self::STATUS_ARCHIVED;
1445 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1449 $result = $this->
update($user);
1451 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1466 return $this->
LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1479 public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1482 $this->labelStatus = array();
1483 $this->labelStatusShort = array();
1485 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1487 $langs->load(
'users');
1491 $moretext =
' ('.$langs->trans(
"Running").
')';
1492 } elseif ($lastresult) {
1493 $moretext .=
' ('.$langs->trans(
"Error").
')';
1496 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled').$moretext;
1497 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled').$moretext;
1498 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled');
1499 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled');
1502 $statusType =
'status4';
1503 if ($status == 1 && $processing) {
1504 $statusType =
'status1';
1507 $statusType =
'status5';
1509 if ($this->lastresult) {
1510 $statusType =
'status8';
1513 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1546 public $classesname;
1548 public $methodename;
1551 public $module_name;
1553 public $datelastrun =
'';
1554 public $datenextrun =
'';
1555 public $dateend =
'';
1556 public $datestart =
'';
1557 public $datelastresult =
'';
1558 public $lastresult =
'';
1560 public $unitfrequency;
1572 public $fk_user_author;
1577 public $fk_user_mod;
1580 public $note_private;
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Parent class of all other business classes (invoices, contracts, proposals, orders,...
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user=null, $notrigger=0)
Update object into database.
fetch($id, $objectname='', $methodname='')
Load object in memory from the database.
info($id)
Load object information.
getLibStatut($mode=0)
Return label of status of user (active, inactive)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchAll($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='', $processing=-1)
Load list of cron jobs in a memory array from the database @TODO Use object CronJob and not CronJobLi...
LibStatut($status, $mode=0, $processing=0, $lastresult=0)
Renvoi le libelle d'un statut donne.
run_jobs($userlogin)
Run a job.
reprogram_jobs($userlogin, $now)
Reprogram a job.
create($user, $notrigger=0)
Create object into database.
__construct($db)
Constructor.
__construct()
Constructor.
Class to manage Dolibarr users.
Class to manage utility methods.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
$conf db
API class for accounts.