25 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
36 public $element =
'cronjob';
41 public $table_element =
'cronjob';
46 public $picto =
'cron';
88 public $datelastrun =
'';
93 public $datenextrun =
'';
103 public $datestart =
'';
108 public $datelastresult =
'';
123 public $unitfrequency;
143 public $fk_user_author;
176 const STATUS_DISABLED = 0;
177 const STATUS_ENABLED = 1;
178 const STATUS_ARCHIVED = 2;
199 public function create($user, $notrigger = 0)
201 global $conf, $langs;
208 if (isset($this->label)) {
209 $this->label = trim($this->label);
211 if (isset($this->jobtype)) {
212 $this->jobtype = trim($this->jobtype);
214 if (isset($this->command)) {
215 $this->command = trim($this->command);
217 if (isset($this->classesname)) {
218 $this->classesname = trim($this->classesname);
220 if (isset($this->objectname)) {
221 $this->objectname = trim($this->objectname);
223 if (isset($this->methodename)) {
224 $this->methodename = trim($this->methodename);
226 if (isset($this->params)) {
227 $this->params = trim($this->params);
229 if (isset($this->md5params)) {
230 $this->md5params = trim($this->md5params);
232 if (isset($this->module_name)) {
233 $this->module_name = trim($this->module_name);
235 if (isset($this->priority)) {
236 $this->priority = trim($this->priority);
238 if (isset($this->lastoutput)) {
239 $this->lastoutput = trim($this->lastoutput);
241 if (isset($this->lastresult)) {
242 $this->lastresult = trim($this->lastresult);
244 if (isset($this->unitfrequency)) {
245 $this->unitfrequency = trim($this->unitfrequency);
247 if (isset($this->frequency)) {
248 $this->frequency = trim($this->frequency);
250 if (isset($this->status)) {
251 $this->status = trim($this->status);
253 if (isset($this->note_private)) {
254 $this->note_private = trim($this->note_private);
256 if (isset($this->nbrun)) {
257 $this->nbrun = (int) $this->nbrun;
259 if (isset($this->maxrun)) {
260 $this->maxrun = (int) $this->maxrun;
262 if (isset($this->libname)) {
263 $this->libname = trim($this->libname);
265 if (isset($this->test)) {
266 $this->test = trim($this->test);
272 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
275 if (empty($this->label)) {
276 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
279 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
280 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
283 if (empty($this->unitfrequency)) {
284 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
287 if (($this->jobtype ==
'command') && (empty($this->command))) {
288 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
291 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
292 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
295 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
296 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
299 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
300 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
303 if (($this->jobtype ==
'function') && (empty($this->libname))) {
304 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
309 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
315 $sql .=
"classesname,";
316 $sql .=
"objectname,";
317 $sql .=
"methodename,";
319 $sql .=
"md5params,";
320 $sql .=
"module_name,";
322 $sql .=
"datelastrun,";
323 $sql .=
"datenextrun,";
325 $sql .=
"datestart,";
326 $sql .=
"lastresult,";
327 $sql .=
"datelastresult,";
328 $sql .=
"lastoutput,";
329 $sql .=
"unitfrequency,";
330 $sql .=
"frequency,";
332 $sql .=
"fk_user_author,";
333 $sql .=
"fk_user_mod,";
339 $sql .=
") VALUES (";
340 $sql .=
" ".(!isset($this->entity) ? $conf->entity : $this->
db->escape($this->entity)).
",";
341 $sql .=
" '".$this->db->idate($now).
"',";
342 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
343 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
344 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
345 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
346 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
347 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
348 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
349 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
350 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
351 $sql .=
" ".(!isset($this->priority) ?
'0' : $this->priority).
",";
352 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
353 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
354 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
355 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
356 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
357 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
358 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
359 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
360 $sql .=
" ".(!isset($this->frequency) ?
'0' : $this->frequency).
",";
361 $sql .=
" ".(!isset($this->status) ?
'0' : $this->status).
",";
362 $sql .=
" ".$user->id.
",";
363 $sql .=
" ".$user->id.
",";
364 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
365 $sql .=
" ".(!isset($this->nbrun) ?
'0' : $this->
db->escape($this->nbrun)).
",";
366 $sql .=
" ".(empty($this->maxrun) ?
'0' : $this->
db->escape($this->maxrun)).
",";
367 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
368 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'").
"";
373 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
377 $this->errors[] =
"Error ".$this->db->lasterror();
381 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
386 $this->
db->rollback();
403 public function fetch($id, $objectname =
'', $methodname =
'')
407 $sql .=
" t.entity,";
410 $sql .=
" t.jobtype,";
412 $sql .=
" t.command,";
413 $sql .=
" t.classesname,";
414 $sql .=
" t.objectname,";
415 $sql .=
" t.methodename,";
416 $sql .=
" t.params,";
417 $sql .=
" t.md5params,";
418 $sql .=
" t.module_name,";
419 $sql .=
" t.priority,";
420 $sql .=
" t.datelastrun,";
421 $sql .=
" t.datenextrun,";
422 $sql .=
" t.dateend,";
423 $sql .=
" t.datestart,";
424 $sql .=
" t.lastresult,";
425 $sql .=
" t.datelastresult,";
426 $sql .=
" t.lastoutput,";
427 $sql .=
" t.unitfrequency,";
428 $sql .=
" t.frequency,";
429 $sql .=
" t.status,";
430 $sql .=
" t.processing,";
431 $sql .=
" t.fk_user_author,";
432 $sql .=
" t.fk_user_mod,";
433 $sql .=
" t.note as note_private,";
435 $sql .=
" t.maxrun,";
436 $sql .=
" t.libname,";
438 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
440 $sql .=
" WHERE t.rowid = ".((int) $id);
442 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
443 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
444 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
447 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
451 $obj = $this->
db->fetch_object(
$resql);
453 $this->
id = $obj->rowid;
454 $this->
ref = $obj->rowid;
455 $this->entity = $obj->entity;
456 $this->tms = $this->
db->jdate($obj->tms);
457 $this->datec = $this->
db->jdate($obj->datec);
458 $this->label = $obj->label;
459 $this->jobtype = $obj->jobtype;
460 $this->command = $obj->command;
461 $this->classesname = $obj->classesname;
462 $this->objectname = $obj->objectname;
463 $this->methodename = $obj->methodename;
464 $this->params = $obj->params;
465 $this->md5params = $obj->md5params;
466 $this->module_name = $obj->module_name;
467 $this->priority = $obj->priority;
468 $this->datelastrun = $this->
db->jdate($obj->datelastrun);
469 $this->datenextrun = $this->
db->jdate($obj->datenextrun);
470 $this->dateend = $this->
db->jdate($obj->dateend);
471 $this->datestart = $this->
db->jdate($obj->datestart);
472 $this->lastresult = $obj->lastresult;
473 $this->lastoutput = $obj->lastoutput;
474 $this->datelastresult = $this->
db->jdate($obj->datelastresult);
475 $this->unitfrequency = $obj->unitfrequency;
476 $this->frequency = $obj->frequency;
477 $this->status = $obj->status;
478 $this->processing = $obj->processing;
479 $this->fk_user_author = $obj->fk_user_author;
480 $this->fk_user_mod = $obj->fk_user_mod;
481 $this->note_private = $obj->note_private;
482 $this->nbrun = $obj->nbrun;
483 $this->maxrun = $obj->maxrun;
484 $this->libname = $obj->libname;
485 $this->test = $obj->test;
491 $this->error =
"Error ".$this->db->lasterror();
509 public function fetchAll($sortorder =
'DESC', $sortfield =
't.rowid', $limit = 0, $offset = 0, $status = 1, $filter =
'', $processing = -1)
511 $this->lines = array();
515 $sql .=
" t.entity,";
518 $sql .=
" t.jobtype,";
520 $sql .=
" t.command,";
521 $sql .=
" t.classesname,";
522 $sql .=
" t.objectname,";
523 $sql .=
" t.methodename,";
524 $sql .=
" t.params,";
525 $sql .=
" t.md5params,";
526 $sql .=
" t.module_name,";
527 $sql .=
" t.priority,";
528 $sql .=
" t.datelastrun,";
529 $sql .=
" t.datenextrun,";
530 $sql .=
" t.dateend,";
531 $sql .=
" t.datestart,";
532 $sql .=
" t.lastresult,";
533 $sql .=
" t.datelastresult,";
534 $sql .=
" t.lastoutput,";
535 $sql .=
" t.unitfrequency,";
536 $sql .=
" t.frequency,";
537 $sql .=
" t.status,";
538 $sql .=
" t.processing,";
539 $sql .=
" t.fk_user_author,";
540 $sql .=
" t.fk_user_mod,";
541 $sql .=
" t.note as note_private,";
543 $sql .=
" t.libname,";
545 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
546 $sql .=
" WHERE 1 = 1";
547 if ($processing >= 0) {
548 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
550 if ($status >= 0 && $status < 2) {
551 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
552 } elseif ($status == 2) {
553 $sql .=
" AND t.status = 2";
556 if (is_array($filter) && count($filter) > 0) {
557 foreach ($filter as $key => $value) {
558 if ($key ==
't.rowid') {
559 $sql .=
" AND ".$key.
" = ".((int) $value);
561 $sql .=
" AND ".$key.
" LIKE '%".$this->
db->escape($value).
"%'";
566 $sql .= $this->
db->order($sortfield, $sortorder);
567 if (!empty($limit) && !empty($offset)) {
568 $sql .= $this->
db->plimit($limit + 1, $offset);
573 if (count($sqlwhere) > 0) {
574 $sql .=
" WHERE ".implode(
' AND ', $sqlwhere);
577 dol_syslog(get_class($this).
"::fetch_all", LOG_DEBUG);
587 $obj = $this->
db->fetch_object(
$resql);
589 $line->id = $obj->rowid;
590 $line->ref = $obj->rowid;
591 $line->entity = $obj->entity;
592 $line->tms = $this->
db->jdate($obj->tms);
593 $line->datec = $this->
db->jdate($obj->datec);
594 $line->label = $obj->label;
595 $line->jobtype = $obj->jobtype;
596 $line->command = $obj->command;
597 $line->classesname = $obj->classesname;
598 $line->objectname = $obj->objectname;
599 $line->methodename = $obj->methodename;
600 $line->params = $obj->params;
601 $line->md5params = $obj->md5params;
602 $line->module_name = $obj->module_name;
603 $line->priority = $obj->priority;
604 $line->datelastrun = $this->
db->jdate($obj->datelastrun);
605 $line->datenextrun = $this->
db->jdate($obj->datenextrun);
606 $line->dateend = $this->
db->jdate($obj->dateend);
607 $line->datestart = $this->
db->jdate($obj->datestart);
608 $line->lastresult = $obj->lastresult;
609 $line->datelastresult = $this->
db->jdate($obj->datelastresult);
610 $line->lastoutput = $obj->lastoutput;
611 $line->unitfrequency = $obj->unitfrequency;
612 $line->frequency = $obj->frequency;
613 $line->status = $obj->status;
614 $line->processing = $obj->processing;
615 $line->fk_user_author = $obj->fk_user_author;
616 $line->fk_user_mod = $obj->fk_user_mod;
617 $line->note_private = $obj->note_private;
618 $line->nbrun = $obj->nbrun;
619 $line->libname = $obj->libname;
620 $line->test = $obj->test;
621 $this->lines[] = $line;
630 $this->error =
"Error ".$this->db->lasterror();
643 public function update($user =
null, $notrigger = 0)
645 global $conf, $langs;
647 $langs->load(
'cron');
652 if (isset($this->label)) {
653 $this->label = trim($this->label);
655 if (isset($this->jobtype)) {
656 $this->jobtype = trim($this->jobtype);
658 if (isset($this->command)) {
659 $this->command = trim($this->command);
661 if (isset($this->classesname)) {
662 $this->classesname = trim($this->classesname);
664 if (isset($this->objectname)) {
665 $this->objectname = trim($this->objectname);
667 if (isset($this->methodename)) {
668 $this->methodename = trim($this->methodename);
670 if (isset($this->params)) {
671 $this->params = trim($this->params);
673 if (isset($this->md5params)) {
674 $this->md5params = trim($this->md5params);
676 if (isset($this->module_name)) {
677 $this->module_name = trim($this->module_name);
679 if (isset($this->priority)) {
680 $this->priority = trim($this->priority);
682 if (isset($this->lastoutput)) {
683 $this->lastoutput = trim($this->lastoutput);
685 if (isset($this->lastresult)) {
686 $this->lastresult = trim($this->lastresult);
688 if (isset($this->unitfrequency)) {
689 $this->unitfrequency = trim($this->unitfrequency);
691 if (isset($this->frequency)) {
692 $this->frequency = trim($this->frequency);
694 if (isset($this->status)) {
695 $this->status = trim($this->status);
697 if (isset($this->note_private)) {
698 $this->note_private = trim($this->note_private);
700 if (isset($this->nbrun)) {
701 $this->nbrun = trim($this->nbrun);
703 if (isset($this->libname)) {
704 $this->libname = trim($this->libname);
706 if (isset($this->test)) {
707 $this->test = trim($this->test);
710 if (empty($this->maxrun)) {
713 if (empty($this->processing)) {
714 $this->processing = 0;
720 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
723 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
724 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
727 if (empty($this->label)) {
728 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
731 if (empty($this->unitfrequency)) {
732 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
735 if (($this->jobtype ==
'command') && (empty($this->command))) {
736 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
739 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
740 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
743 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
744 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
747 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
748 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
752 if (($this->jobtype ==
'function') && (empty($this->libname))) {
753 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
759 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
760 $sql .=
" entity=".(isset($this->entity) ? $this->
db->escape($this->entity) : $conf->entity).
",";
761 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
762 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
763 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
764 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
765 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
766 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
767 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
768 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
769 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
770 $sql .=
" priority=".(isset($this->priority) ? $this->priority :
"null").
",";
771 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
772 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
773 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
774 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
775 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
776 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
777 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
778 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency :
"null").
",";
779 $sql .=
" frequency=".(isset($this->frequency) ? $this->frequency :
"null").
",";
780 $sql .=
" status=".(isset($this->status) ? $this->status :
"null").
",";
781 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
782 $sql .=
" fk_user_mod=".$user->id.
",";
783 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
784 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
785 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
786 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
787 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
788 $sql .=
" WHERE rowid=".((int) $this->
id);
792 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
795 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
800 foreach ($this->errors as $errmsg) {
801 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
802 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
804 $this->
db->rollback();
820 public function delete($user, $notrigger = 0)
826 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"cronjob";
827 $sql .=
" WHERE rowid=".((int) $this->
id);
829 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
833 $this->errors[] =
"Error ".$this->db->lasterror();
838 foreach ($this->errors as $errmsg) {
839 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
840 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
842 $this->
db->rollback();
870 $object->fetch($fromid);
874 $object->status = self::STATUS_DISABLED;
875 $object->label = $langs->trans(
"CopyOf").
' '.$langs->trans($object->label);
878 $object->context[
'createfromclone'] =
'createfromclone';
879 $result = $object->create($user);
883 $this->error = $object->error;
887 unset($object->context[
'createfromclone']);
894 $this->
db->rollback();
916 $this->classesname =
'';
917 $this->objectname =
'';
918 $this->methodename =
'';
920 $this->md5params =
'';
921 $this->module_name =
'';
922 $this->priority =
'';
923 $this->datelastrun =
'';
924 $this->datenextrun =
'';
926 $this->datestart =
'';
927 $this->datelastresult =
'';
928 $this->lastoutput =
'';
929 $this->lastresult =
'';
930 $this->unitfrequency =
'';
931 $this->frequency =
'';
933 $this->processing = 0;
934 $this->fk_user_author = 0;
935 $this->fk_user_mod = 0;
936 $this->note_private =
'';
953 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
955 global $db, $conf, $langs;
956 global $dolibarr_main_authentication, $dolibarr_main_demo;
959 if (!empty($conf->dol_no_mouse_hover)) {
965 $label =
img_picto(
'',
'object_'.$this->picto).
' <u>'.$langs->trans(
"CronTask").
'</u>';
966 if (isset($this->status)) {
967 $label .=
' '.$this->getLibStatut(5);
969 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
970 $label .=
'<br><b>'.$langs->trans(
'Title').
':</b> '.$langs->trans($this->label);
971 if ($this->label != $langs->trans($this->label)) {
972 $label .=
' <span class="opacitymedium">('.$this->label.
')</span>';
974 if (!empty($this->datestart)) {
975 $label .=
'<br><b>'.$langs->trans(
'CronDtStart').
':</b> '.
dol_print_date($this->datestart,
'dayhour',
'tzuserrel');
977 if (!empty($this->dateend)) {
978 $label .=
'<br><b>'.$langs->trans(
'CronDtEnd').
':</b> '.
dol_print_date($this->dateend,
'dayhour',
'tzuserrel');
981 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
983 if ($option !=
'nolink') {
985 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
986 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
987 $add_save_lastsearch_values = 1;
989 if ($add_save_lastsearch_values) {
990 $url .=
'&save_lastsearch_values=1';
995 if (empty($notooltip)) {
996 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
997 $label = $langs->trans(
"ShowCronJob");
998 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1000 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1001 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
1003 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1006 $linkstart =
'<a href="'.$url.
'"';
1007 $linkstart .= $linkclose.
'>';
1010 $result .= $linkstart;
1012 $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);
1014 if ($withpicto != 2) {
1015 $result .= $this->ref;
1017 $result .= $linkend;
1033 $sql .=
" f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1034 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as f";
1035 $sql .=
" WHERE f.rowid = ".((int) $id);
1037 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1041 $obj = $this->
db->fetch_object(
$resql);
1042 $this->
id = $obj->rowid;
1044 $this->user_modification_id = $obj->fk_user_mod;
1045 $this->user_creation_id = $obj->fk_user_author;
1046 $this->date_creation = $this->
db->jdate($obj->datec);
1047 $this->date_modification = $this->
db->jdate($obj->tms);
1053 $this->error =
"Error ".$this->db->lasterror();
1071 global $langs, $conf, $hookmanager;
1073 $hookmanager->initHooks(array(
'cron'));
1079 $langs->load(
'cron');
1081 if (empty($userlogin)) {
1082 $this->error =
"User login is mandatory";
1083 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1089 if ($conf->entity != $this->entity && $this->entity > 0) {
1090 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1092 $savcurrententity = $conf->entity;
1093 $conf->setEntityValues($this->
db, $this->entity);
1094 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1096 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1097 $user =
new User($this->
db);
1098 $result = $user->fetch(
'', $userlogin);
1100 $this->error =
"User Error:".$user->error;
1101 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1102 $conf->setEntityValues($this->
db, $savcurrententity);
1105 if (empty($user->id)) {
1106 $this->error =
" User user login:".$userlogin.
" do not exists";
1107 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1108 $conf->setEntityValues($this->
db, $savcurrententity);
1113 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1116 $ExecTimeLimit = 600;
1117 if (!empty($ExecTimeLimit)) {
1118 $err = error_reporting();
1121 @set_time_limit($ExecTimeLimit);
1122 error_reporting($err);
1124 if (!empty($MemoryLimit)) {
1125 @ini_set(
'memory_limit', $MemoryLimit);
1129 $this->datelastrun = $now;
1130 $this->datelastresult =
null;
1131 $this->lastoutput =
'';
1132 $this->lastresult =
'';
1133 $this->processing = 1;
1134 $this->nbrun = $this->nbrun + 1;
1135 $result = $this->
update($user);
1137 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1138 $conf->setEntityValues($this->
db, $savcurrententity);
1143 if ($this->jobtype ==
'method') {
1147 if ($ret ===
false || (!class_exists($this->objectname))) {
1148 if ($ret ===
false) {
1149 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1151 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1153 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1154 $this->lastoutput = $this->error;
1155 $this->lastresult = -1;
1156 $retval = $this->lastresult;
1163 if (!method_exists($this->objectname, $this->methodename)) {
1164 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1165 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1166 $this->lastoutput = $this->error;
1167 $this->lastresult = -1;
1168 $retval = $this->lastresult;
1171 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1172 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1173 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1174 $this->lastoutput = $this->error;
1175 $this->lastresult = -1;
1176 $retval = $this->lastresult;
1183 $result = $langs->load($this->module_name);
1184 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1187 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1188 $this->error = $langs->error;
1189 $this->lastoutput = $this->error;
1190 $this->lastresult = -1;
1191 $retval = $this->lastresult;
1197 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1200 $nameofclass = $this->objectname;
1201 $object =
new $nameofclass($this->
db);
1202 if ($this->entity > 0) {
1203 $object->entity = $this->entity;
1206 $params_arr = array();
1207 if (!empty($this->params) || $this->params ===
'0') {
1208 $params_arr = array_map(
'trim', explode(
",", $this->params));
1211 if (!is_array($params_arr)) {
1212 $result = call_user_func(array($object, $this->methodename), $this->params);
1214 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1217 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1218 $langs->load(
"errors");
1221 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1222 $errmsg .= $object->error;
1224 if (is_array($object->errors) && count($object->errors)) {
1225 $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1227 if (empty($errmsg)) {
1228 $errmsg = $langs->trans(
'ErrorUnknown');
1231 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1233 $this->error = $errmsg;
1234 $this->lastoutput = ($object->output ? $object->output.
"\n" :
"").$errmsg;
1235 $this->lastresult = is_numeric($result) ? $result : -1;
1236 $retval = $this->lastresult;
1239 dol_syslog(get_class($this).
"::run_jobs END");
1240 $this->lastoutput = $object->output;
1241 $this->lastresult = var_export($result,
true);
1242 $retval = $this->lastresult;
1247 if ($this->jobtype ==
'function') {
1249 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1251 if ($ret ===
false) {
1252 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1253 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1254 $conf->setEntityValues($this->
db, $savcurrententity);
1259 $result = $langs->load($this->module_name);
1260 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1262 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1263 $conf->setEntityValues($this->
db, $savcurrententity);
1267 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1268 $params_arr = explode(
", ", $this->params);
1269 if (!is_array($params_arr)) {
1270 $result = call_user_func($this->methodename, $this->params);
1272 $result = call_user_func_array($this->methodename, $params_arr);
1275 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1276 $langs->load(
"errors");
1277 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1278 $this->error = $langs->trans(
'ErrorUnknown');
1279 $this->lastoutput = $this->error;
1280 $this->lastresult = is_numeric($result) ? $result : -1;
1281 $retval = $this->lastresult;
1284 $this->lastoutput = var_export($result,
true);
1285 $this->lastresult = var_export($result,
true);
1286 $retval = $this->lastresult;
1291 if ($this->jobtype ==
'command') {
1292 global $dolibarr_cron_allow_cli;
1294 if (empty($dolibarr_cron_allow_cli)) {
1295 $langs->load(
"errors");
1296 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1297 $this->lastoutput =
'';
1298 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1300 $outputdir = $conf->cron->dir_temp;
1301 if (empty($outputdir)) {
1302 $outputdir = $conf->cronjob->dir_temp;
1305 if (!empty($outputdir)) {
1307 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1310 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1311 $utils =
new Utils($this->
db);
1312 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1314 $retval = $arrayresult[
'result'];
1315 $this->error = $arrayresult[
'error'];
1316 $this->lastoutput = $arrayresult[
'output'];
1317 $this->lastresult = $arrayresult[
'result'];
1322 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1324 $this->datelastresult =
dol_now();
1325 $this->processing = 0;
1326 $result = $this->
update($user);
1328 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1329 $conf->setEntityValues($this->
db, $savcurrententity);
1333 $conf->setEntityValues($this->
db, $savcurrententity);
1334 return $error ?-1 : 1;
1349 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1351 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1352 $user =
new User($this->
db);
1353 $result = $user->fetch(
'', $userlogin);
1355 $this->error =
"User Error : ".$user->error;
1356 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1359 if (empty($user->id)) {
1360 $this->error =
" User user login:".$userlogin.
" do not exists";
1361 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1366 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1368 if (empty($this->datenextrun)) {
1369 if (empty($this->datestart)) {
1370 $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1372 $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1376 if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
1378 while ($this->datenextrun < $now) {
1379 $this->datenextrun += ($this->frequency * $this->unitfrequency);
1385 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1390 if ($this->autodelete == 2) {
1391 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1392 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1393 $this->status = self::STATUS_ARCHIVED;
1394 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1398 $result = $this->
update($user);
1400 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1415 return $this->
LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1428 public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1431 $this->labelStatus = array();
1432 $this->labelStatusShort = array();
1434 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1436 $langs->load(
'users');
1440 $moretext =
' ('.$langs->trans(
"Running").
')';
1441 } elseif ($lastresult) {
1442 $moretext .=
' ('.$langs->trans(
"Error").
')';
1445 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled').$moretext;
1446 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled').$moretext;
1447 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled');
1448 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Scheduled');
1451 $statusType =
'status4';
1452 if ($status == 1 && $processing) {
1453 $statusType =
'status1';
1456 $statusType =
'status5';
1458 if ($this->lastresult) {
1459 $statusType =
'status8';
1462 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1495 public $classesname;
1497 public $methodename;
1500 public $module_name;
1502 public $datelastrun =
'';
1503 public $datenextrun =
'';
1504 public $dateend =
'';
1505 public $datestart =
'';
1506 public $lastresult =
'';
1508 public $unitfrequency;
1519 public $fk_user_author;
1524 public $fk_user_mod;