211 public function create($user, $notrigger = 0)
213 global $conf, $langs;
220 if (isset($this->label)) {
221 $this->label = trim($this->label);
223 if (isset($this->jobtype)) {
224 $this->jobtype = trim($this->jobtype);
226 if (isset($this->command)) {
227 $this->command = trim($this->command);
229 if (isset($this->classesname)) {
230 $this->classesname = trim($this->classesname);
232 if (isset($this->objectname)) {
233 $this->objectname = trim($this->objectname);
235 if (isset($this->methodename)) {
236 $this->methodename = trim($this->methodename);
238 if (isset($this->params)) {
239 $this->params = trim($this->params);
241 if (isset($this->md5params)) {
242 $this->md5params = trim($this->md5params);
244 if (isset($this->module_name)) {
245 $this->module_name = trim($this->module_name);
247 if (isset($this->priority)) {
248 $this->priority = trim($this->priority);
250 if (isset($this->lastoutput)) {
251 $this->lastoutput = trim($this->lastoutput);
253 if (isset($this->lastresult)) {
254 $this->lastresult = trim($this->lastresult);
256 if (isset($this->unitfrequency)) {
257 $this->unitfrequency = trim($this->unitfrequency);
259 if (isset($this->frequency)) {
260 $this->frequency = trim($this->frequency);
262 if (isset($this->status)) {
263 $this->status = trim($this->status);
265 if (isset($this->note_private)) {
266 $this->note_private = trim($this->note_private);
268 if (isset($this->nbrun)) {
269 $this->nbrun = (int) $this->nbrun;
271 if (isset($this->maxrun)) {
272 $this->maxrun = (int) $this->maxrun;
274 if (isset($this->libname)) {
275 $this->libname = trim($this->libname);
277 if (isset($this->test)) {
278 $this->test = trim($this->test);
284 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
287 if (empty($this->label)) {
288 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
291 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
292 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
295 if (empty($this->unitfrequency)) {
296 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
299 if (($this->jobtype ==
'command') && (empty($this->command))) {
300 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
303 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
304 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
307 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
308 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
311 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
312 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
315 if (($this->jobtype ==
'function') && (empty($this->libname))) {
316 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
321 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
327 $sql .=
"classesname,";
328 $sql .=
"objectname,";
329 $sql .=
"methodename,";
331 $sql .=
"md5params,";
332 $sql .=
"module_name,";
334 $sql .=
"datelastrun,";
335 $sql .=
"datenextrun,";
337 $sql .=
"datestart,";
338 $sql .=
"lastresult,";
339 $sql .=
"datelastresult,";
340 $sql .=
"lastoutput,";
341 $sql .=
"unitfrequency,";
342 $sql .=
"frequency,";
344 $sql .=
"fk_user_author,";
345 $sql .=
"fk_user_mod,";
351 $sql .=
") VALUES (";
352 $sql .=
" ".(!isset($this->entity) ? $conf->entity : $this->db->escape($this->entity)).
",";
353 $sql .=
" '".$this->db->idate($now).
"',";
354 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
355 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
356 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
357 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
358 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
359 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
360 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
361 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
362 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
363 $sql .=
" ".(!isset($this->priority) ?
'0' : $this->priority).
",";
364 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
365 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
366 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
367 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
368 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
369 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
370 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
371 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
372 $sql .=
" ".(!isset($this->frequency) ?
'0' : ((int) $this->frequency)).
",";
373 $sql .=
" ".(!isset($this->status) ?
'0' : ((int) $this->status)).
",";
374 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
375 $sql .=
" ".($user->id ? (int) $user->id :
"NULL").
",";
376 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
377 $sql .=
" ".(!isset($this->nbrun) ?
'0' : ((int) $this->nbrun)).
",";
378 $sql .=
" ".(empty($this->maxrun) ?
'0' : ((int) $this->maxrun)).
",";
379 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
380 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'");
385 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
386 $resql = $this->db->query($sql);
389 $this->errors[] =
"Error ".$this->db->lasterror();
393 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
398 $this->db->rollback();
415 public function fetch($id, $objectname =
'', $methodname =
'')
419 $sql .=
" t.entity,";
422 $sql .=
" t.jobtype,";
424 $sql .=
" t.command,";
425 $sql .=
" t.classesname,";
426 $sql .=
" t.objectname,";
427 $sql .=
" t.methodename,";
428 $sql .=
" t.params,";
429 $sql .=
" t.md5params,";
430 $sql .=
" t.module_name,";
431 $sql .=
" t.priority,";
432 $sql .=
" t.datelastrun,";
433 $sql .=
" t.datenextrun,";
434 $sql .=
" t.dateend,";
435 $sql .=
" t.datestart,";
436 $sql .=
" t.lastresult,";
437 $sql .=
" t.datelastresult,";
438 $sql .=
" t.lastoutput,";
439 $sql .=
" t.unitfrequency,";
440 $sql .=
" t.frequency,";
441 $sql .=
" t.status,";
442 $sql .=
" t.processing,";
444 $sql .=
" t.email_alert,";
445 $sql .=
" t.fk_user_author,";
446 $sql .=
" t.fk_user_mod,";
447 $sql .=
" t.note as note_private,";
449 $sql .=
" t.maxrun,";
450 $sql .=
" t.libname,";
452 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
454 $sql .=
" WHERE t.rowid = ".((int) $id);
456 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
457 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
458 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
461 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
462 $resql = $this->db->query($sql);
464 if ($this->db->num_rows($resql)) {
465 $obj = $this->db->fetch_object($resql);
467 $this->
id = $obj->rowid;
468 $this->
ref = $obj->rowid;
469 $this->entity = $obj->entity;
470 $this->tms = $this->db->jdate($obj->tms);
471 $this->datec = $this->db->jdate($obj->datec);
472 $this->label = $obj->label;
473 $this->jobtype = $obj->jobtype;
474 $this->command = $obj->command;
475 $this->classesname = $obj->classesname;
476 $this->objectname = $obj->objectname;
477 $this->methodename = $obj->methodename;
478 $this->params = $obj->params;
479 $this->md5params = $obj->md5params;
480 $this->module_name = $obj->module_name;
481 $this->priority = $obj->priority;
482 $this->datelastrun = $this->db->jdate($obj->datelastrun);
483 $this->datenextrun = $this->db->jdate($obj->datenextrun);
484 $this->dateend = $this->db->jdate($obj->dateend);
485 $this->datestart = $this->db->jdate($obj->datestart);
486 $this->lastresult = $obj->lastresult;
487 $this->lastoutput = $obj->lastoutput;
488 $this->datelastresult = $this->db->jdate($obj->datelastresult);
489 $this->unitfrequency = $obj->unitfrequency;
490 $this->frequency = $obj->frequency;
491 $this->status = $obj->status;
492 $this->processing = $obj->processing;
493 $this->pid = $obj->pid;
494 $this->email_alert = $obj->email_alert;
495 $this->fk_user_author = $obj->fk_user_author;
496 $this->fk_user_mod = $obj->fk_user_mod;
497 $this->note_private = $obj->note_private;
498 $this->nbrun = $obj->nbrun;
499 $this->maxrun = $obj->maxrun;
500 $this->libname = $obj->libname;
501 $this->test = $obj->test;
503 $this->db->free($resql);
507 $this->error =
"Error ".$this->db->lasterror();
525 public function fetchAll($sortorder =
'DESC', $sortfield =
't.rowid', $limit = 0, $offset = 0, $status = 1, $filter =
'', $processing = -1)
527 $this->lines = array();
531 $sql .=
" t.entity,";
534 $sql .=
" t.jobtype,";
536 $sql .=
" t.command,";
537 $sql .=
" t.classesname,";
538 $sql .=
" t.objectname,";
539 $sql .=
" t.methodename,";
540 $sql .=
" t.params,";
541 $sql .=
" t.md5params,";
542 $sql .=
" t.module_name,";
543 $sql .=
" t.priority,";
544 $sql .=
" t.datelastrun,";
545 $sql .=
" t.datenextrun,";
546 $sql .=
" t.dateend,";
547 $sql .=
" t.datestart,";
548 $sql .=
" t.lastresult,";
549 $sql .=
" t.datelastresult,";
550 $sql .=
" t.lastoutput,";
551 $sql .=
" t.unitfrequency,";
552 $sql .=
" t.frequency,";
553 $sql .=
" t.status,";
554 $sql .=
" t.processing,";
556 $sql .=
" t.email_alert,";
557 $sql .=
" t.fk_user_author,";
558 $sql .=
" t.fk_user_mod,";
559 $sql .=
" t.note as note_private,";
561 $sql .=
" t.libname,";
563 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
564 $sql .=
" WHERE 1 = 1";
565 if ($processing >= 0) {
566 $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
568 if ($status >= 0 && $status < 2) {
569 $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
570 } elseif ($status == 2) {
571 $sql .=
" AND t.status = 2";
574 if (is_array($filter) && count($filter) > 0) {
575 foreach ($filter as $key => $value) {
576 if ($key ==
't.rowid') {
577 $sql .=
" AND ".$key.
" = ".((int) $value);
579 $sql .=
" AND ".$key.
" LIKE '%".$this->db->escape($value).
"%'";
584 $sql .= $this->db->order($sortfield, $sortorder);
585 if (!empty($limit) && !empty($offset)) {
586 $sql .= $this->db->plimit($limit + 1, $offset);
591 if (count($sqlwhere) > 0) {
592 $sql .=
" WHERE ".implode(
' AND ', $sqlwhere);
595 dol_syslog(get_class($this).
"::fetchAll", LOG_DEBUG);
596 $resql = $this->db->query($sql);
598 $num = $this->db->num_rows($resql);
605 $obj = $this->db->fetch_object($resql);
607 $line->id = $obj->rowid;
608 $line->ref = $obj->rowid;
609 $line->entity = $obj->entity;
610 $line->tms = $this->db->jdate($obj->tms);
611 $line->datec = $this->db->jdate($obj->datec);
612 $line->label = $obj->label;
613 $line->jobtype = $obj->jobtype;
614 $line->command = $obj->command;
615 $line->classesname = $obj->classesname;
616 $line->objectname = $obj->objectname;
617 $line->methodename = $obj->methodename;
618 $line->params = $obj->params;
619 $line->md5params = $obj->md5params;
620 $line->module_name = $obj->module_name;
621 $line->priority = $obj->priority;
622 $line->datelastrun = $this->db->jdate($obj->datelastrun);
623 $line->datenextrun = $this->db->jdate($obj->datenextrun);
624 $line->dateend = $this->db->jdate($obj->dateend);
625 $line->datestart = $this->db->jdate($obj->datestart);
626 $line->lastresult = $obj->lastresult;
627 $line->datelastresult = $this->db->jdate($obj->datelastresult);
628 $line->lastoutput = $obj->lastoutput;
629 $line->unitfrequency = $obj->unitfrequency;
630 $line->frequency = $obj->frequency;
631 $line->status = $obj->status;
632 $line->processing = $obj->processing;
633 $line->pid = $obj->pid;
634 $line->email_alert = $obj->email_alert;
635 $line->fk_user_author = $obj->fk_user_author;
636 $line->fk_user_mod = $obj->fk_user_mod;
637 $line->note_private = $obj->note_private;
638 $line->nbrun = $obj->nbrun;
639 $line->libname = $obj->libname;
640 $line->test = $obj->test;
641 $this->lines[] = $line;
646 $this->db->free($resql);
650 $this->error =
"Error ".$this->db->lasterror();
663 public function update($user =
null, $notrigger = 0)
665 global $conf, $langs;
667 $langs->load(
'cron');
672 if (isset($this->label)) {
673 $this->label = trim($this->label);
675 if (isset($this->jobtype)) {
676 $this->jobtype = trim($this->jobtype);
678 if (isset($this->command)) {
679 $this->command = trim($this->command);
681 if (isset($this->classesname)) {
682 $this->classesname = trim($this->classesname);
684 if (isset($this->objectname)) {
685 $this->objectname = trim($this->objectname);
687 if (isset($this->methodename)) {
688 $this->methodename = trim($this->methodename);
690 if (isset($this->params)) {
691 $this->params = trim($this->params);
693 if (isset($this->md5params)) {
694 $this->md5params = trim($this->md5params);
696 if (isset($this->module_name)) {
697 $this->module_name = trim($this->module_name);
699 if (isset($this->priority)) {
700 $this->priority = trim($this->priority);
702 if (isset($this->lastoutput)) {
703 $this->lastoutput = trim($this->lastoutput);
705 if (isset($this->lastresult)) {
706 $this->lastresult = trim($this->lastresult);
708 if (isset($this->unitfrequency)) {
709 $this->unitfrequency = trim($this->unitfrequency);
711 if (isset($this->frequency)) {
712 $this->frequency = trim($this->frequency);
714 if (isset($this->status)) {
715 $this->status = trim($this->status);
717 if (isset($this->note_private)) {
718 $this->note_private = trim($this->note_private);
720 if (isset($this->nbrun)) {
721 $this->nbrun = trim($this->nbrun);
723 if (isset($this->libname)) {
724 $this->libname = trim($this->libname);
726 if (isset($this->test)) {
727 $this->test = trim($this->test);
730 if (empty($this->maxrun)) {
733 if (empty($this->processing)) {
734 $this->processing = 0;
736 if (empty($this->pid)) {
739 if (empty($this->email_alert)) {
740 $this->email_alert =
'';
745 if (
dol_strlen($this->datenextrun) == 0 && $this->status == self::STATUS_ENABLED) {
746 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtNextLaunch'));
749 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
750 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
753 if (empty($this->label)) {
754 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
757 if (empty($this->unitfrequency)) {
758 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
761 if (($this->jobtype ==
'command') && (empty($this->command))) {
762 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
765 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
766 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
769 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
770 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
773 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
774 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
778 if (($this->jobtype ==
'function') && (empty($this->libname))) {
779 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
785 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
786 $sql .=
" entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).
",";
787 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
788 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
789 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
790 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
791 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
792 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
793 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
794 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
795 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
796 $sql .=
" priority=".(isset($this->priority) ? ((int) $this->priority) :
"null").
",";
797 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
798 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
799 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
800 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
801 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
802 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
803 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
804 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency :
"null").
",";
805 $sql .=
" frequency=".(isset($this->frequency) ? $this->frequency :
"null").
",";
806 $sql .=
" status=".(isset($this->status) ? $this->status :
"null").
",";
807 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
808 $sql .=
" pid=".(isset($this->pid) ? ((int) $this->pid) :
"null").
",";
809 $sql .=
" email_alert = ".(isset($this->email_alert) ?
"'".$this->db->escape($this->email_alert).
"'" :
"null").
",";
810 $sql .=
" fk_user_mod = ".((int) $user->id).
",";
811 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
812 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
813 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
814 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
815 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
816 $sql .=
" WHERE rowid=".((int) $this->
id);
820 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
821 $resql = $this->db->query($sql);
823 $error++; $this->errors[] =
"Error ".$this->db->lasterror();
828 foreach ($this->errors as $errmsg) {
829 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
830 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
832 $this->db->rollback();
1032 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
1034 global $db, $conf, $langs;
1036 if (!empty($conf->dol_no_mouse_hover)) {
1044 'objecttype' => $this->element,
1046 $classfortooltip =
'classfortooltip';
1049 $classfortooltip =
'classforajaxtooltip';
1050 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
1056 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
1058 if ($option !=
'nolink') {
1060 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1061 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1062 $add_save_lastsearch_values = 1;
1064 if ($add_save_lastsearch_values) {
1065 $url .=
'&save_lastsearch_values=1';
1070 if (empty($notooltip)) {
1071 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1072 $label = $langs->trans(
"ShowCronJob");
1073 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1075 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
1076 $linkclose .= $dataparams.
' class="'.$classfortooltip.($morecss ?
' '.$morecss :
'').
'"';
1078 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
1081 $linkstart =
'<a href="'.$url.
'"';
1082 $linkstart .= $linkclose.
'>';
1085 $result .= $linkstart;
1087 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), (($withpicto != 2) ?
'class="paddingright"' :
''), 0, 0, $notooltip ? 0 : 1);
1089 if ($withpicto != 2) {
1090 $result .= $this->ref;
1092 $result .= $linkend;
1146 global $langs, $conf, $hookmanager;
1148 $hookmanager->initHooks(array(
'cron'));
1154 $langs->load(
'cron');
1156 if (empty($userlogin)) {
1157 $this->error =
"User login is mandatory";
1158 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1164 if ($conf->entity != $this->entity && $this->entity > 0) {
1165 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
1167 $savcurrententity = $conf->entity;
1168 $conf->setEntityValues($this->db, $this->entity);
1169 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
1171 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1172 $user =
new User($this->db);
1173 $result = $user->fetch(
'', $userlogin);
1175 $this->error =
"User Error:".$user->error;
1176 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1177 $conf->setEntityValues($this->db, $savcurrententity);
1180 if (empty($user->id)) {
1181 $this->error =
"User login: ".$userlogin.
" does not exist";
1182 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1183 $conf->setEntityValues($this->db, $savcurrententity);
1188 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
1191 $ExecTimeLimit = 600;
1192 if (!empty($ExecTimeLimit)) {
1193 $err = error_reporting();
1196 @set_time_limit($ExecTimeLimit);
1197 error_reporting($err);
1200 if (!empty($MemoryLimit)) {
1201 @ini_set(
'memory_limit', $MemoryLimit);
1205 $this->datelastrun = $now;
1206 $this->datelastresult =
null;
1207 $this->lastoutput =
'';
1208 $this->lastresult =
'';
1209 $this->processing = 1;
1210 $this->pid = function_exists(
'getmypid') ? getmypid() :
null;
1211 $this->nbrun = $this->nbrun + 1;
1212 $result = $this->
update($user);
1214 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1215 $conf->setEntityValues($this->db, $savcurrententity);
1220 if ($this->jobtype ==
'method') {
1224 if ($ret ===
false || (!class_exists($this->objectname))) {
1225 if ($ret ===
false) {
1226 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1228 $this->error = $langs->transnoentitiesnoconv(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1230 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1231 $this->lastoutput = $this->error;
1232 $this->lastresult = -1;
1233 $retval = $this->lastresult;
1240 if (!method_exists($this->objectname, $this->methodename)) {
1241 $this->error = $langs->transnoentitiesnoconv(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1242 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1243 $this->lastoutput = $this->error;
1244 $this->lastresult = -1;
1245 $retval = $this->lastresult;
1248 if (in_array(strtolower(trim($this->methodename)), array(
'executecli'))) {
1249 $this->error = $langs->transnoentitiesnoconv(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1250 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1251 $this->lastoutput = $this->error;
1252 $this->lastresult = -1;
1253 $retval = $this->lastresult;
1260 $result = $langs->load($this->module_name);
1261 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1264 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1265 $this->error = $langs->error;
1266 $this->lastoutput = $this->error;
1267 $this->lastresult = -1;
1268 $retval = $this->lastresult;
1274 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1277 $nameofclass = $this->objectname;
1278 $object =
new $nameofclass($this->db);
1279 if ($this->entity > 0) {
1280 $object->entity = $this->entity;
1283 $params_arr = array();
1284 if (!empty($this->params) || $this->params ===
'0') {
1285 $params_arr = array_map(
'trim', explode(
",", $this->params));
1288 if (!is_array($params_arr)) {
1289 $result = call_user_func(array($object, $this->methodename), $this->params);
1291 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1294 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1295 $langs->load(
"errors");
1298 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1299 $errmsg .= $object->error;
1301 if (is_array($object->errors) && count($object->errors)) {
1302 $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1304 if (empty($errmsg)) {
1305 $errmsg = $langs->trans(
'ErrorUnknown');
1308 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1310 $this->error = $errmsg;
1311 $this->lastoutput =
dol_substr((empty($object->output) ?
"" : $object->output.
"\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1312 $this->lastresult = is_numeric($result) ? $result : -1;
1313 $retval = $this->lastresult;
1316 dol_syslog(get_class($this).
"::run_jobs END");
1317 $this->lastoutput =
dol_substr((empty($object->output) ?
"" : $object->output.
"\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD,
'UTF-8', 1);
1318 $this->lastresult = var_export($result,
true);
1319 $retval = $this->lastresult;
1324 if ($this->jobtype ==
'function') {
1326 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1328 if ($ret ===
false) {
1329 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1330 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1331 $conf->setEntityValues($this->db, $savcurrententity);
1336 $result = $langs->load($this->module_name);
1337 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1339 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1340 $conf->setEntityValues($this->db, $savcurrententity);
1344 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1345 $params_arr = explode(
", ", $this->params);
1346 if (!is_array($params_arr)) {
1347 $result = call_user_func($this->methodename, $this->params);
1349 $result = call_user_func_array($this->methodename, $params_arr);
1352 if ($result ===
false || (!is_bool($result) && $result != 0)) {
1353 $langs->load(
"errors");
1354 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1355 $this->error = $langs->trans(
'ErrorUnknown');
1356 $this->lastoutput = $this->error;
1357 $this->lastresult = is_numeric($result) ? $result : -1;
1358 $retval = $this->lastresult;
1361 $this->lastoutput = var_export($result,
true);
1362 $this->lastresult = var_export($result,
true);
1363 $retval = $this->lastresult;
1368 if ($this->jobtype ==
'command') {
1369 global $dolibarr_cron_allow_cli;
1371 if (empty($dolibarr_cron_allow_cli)) {
1372 $langs->load(
"errors");
1373 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1374 $this->lastoutput =
'';
1375 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1377 $outputdir = $conf->cron->dir_temp;
1378 if (empty($outputdir)) {
1379 $outputdir = $conf->cronjob->dir_temp;
1382 if (!empty($outputdir)) {
1384 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1387 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1388 $utils =
new Utils($this->db);
1389 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1391 $retval = $arrayresult[
'result'];
1392 $this->error = $arrayresult[
'error'];
1393 $this->lastoutput = $arrayresult[
'output'];
1394 $this->lastresult = $arrayresult[
'result'];
1399 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1401 $this->datelastresult =
dol_now();
1402 $this->processing = 0;
1404 $result = $this->
update($user);
1406 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1407 $conf->setEntityValues($this->db, $savcurrententity);
1411 $conf->setEntityValues($this->db, $savcurrententity);
1413 if ($error && !empty($this->email_alert)) {
1414 include_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1415 $subject = $langs->trans(
"ErrorInBatch", $this->label);
1416 $msg = $langs->trans(
"ErrorInBatch", $this->label);
1418 $cmailfile =
new CMailFile($subject, $this->email_alert, $from, $msg);
1419 $result = $cmailfile->sendfile();
1422 return $error ?-1 : 1;