25 require_once DOL_DOCUMENT_ROOT.
"/core/class/commonobject.class.php";
36 public $element =
'cronjob';
41 public $table_element =
'cronjob';
46 public $picto =
'cron';
73 public $datelastrun =
'';
77 public $datenextrun =
'';
79 public $datestart =
'';
80 public $datelastresult =
'';
83 public $unitfrequency;
96 public $fk_user_author;
107 const STATUS_DISABLED = 0;
108 const STATUS_ENABLED = 1;
109 const STATUS_ARCHIVED = 2;
130 public function create($user, $notrigger = 0)
132 global $conf, $langs;
139 if (isset($this->label)) $this->label = trim($this->label);
140 if (isset($this->jobtype)) $this->jobtype = trim($this->jobtype);
141 if (isset($this->command)) $this->command = trim($this->command);
142 if (isset($this->classesname)) $this->classesname = trim($this->classesname);
143 if (isset($this->objectname)) $this->objectname = trim($this->objectname);
144 if (isset($this->methodename)) $this->methodename = trim($this->methodename);
145 if (isset($this->params)) $this->params = trim($this->params);
146 if (isset($this->md5params)) $this->md5params = trim($this->md5params);
147 if (isset($this->module_name)) $this->module_name = trim($this->module_name);
148 if (isset($this->priority)) $this->priority = trim($this->priority);
149 if (isset($this->lastoutput)) $this->lastoutput = trim($this->lastoutput);
150 if (isset($this->lastresult)) $this->lastresult = trim($this->lastresult);
151 if (isset($this->unitfrequency)) $this->unitfrequency = trim($this->unitfrequency);
152 if (isset($this->frequency)) $this->frequency = trim($this->frequency);
153 if (isset($this->status)) $this->status = trim($this->status);
154 if (isset($this->note_private)) $this->note_private = trim($this->note_private);
155 if (isset($this->nbrun)) $this->nbrun = trim($this->nbrun);
156 if (isset($this->libname)) $this->libname = trim($this->libname);
157 if (isset($this->test)) $this->test = trim($this->test);
162 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtStart'));
165 if (empty($this->label)) {
166 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
169 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
170 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
173 if (empty($this->unitfrequency)) {
174 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
177 if (($this->jobtype ==
'command') && (empty($this->command))) {
178 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
181 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
182 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
185 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
186 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
189 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
190 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
193 if (($this->jobtype ==
'function') && (empty($this->libname))) {
194 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
199 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"cronjob(";
205 $sql .=
"classesname,";
206 $sql .=
"objectname,";
207 $sql .=
"methodename,";
209 $sql .=
"md5params,";
210 $sql .=
"module_name,";
212 $sql .=
"datelastrun,";
213 $sql .=
"datenextrun,";
215 $sql .=
"datestart,";
216 $sql .=
"lastresult,";
217 $sql .=
"datelastresult,";
218 $sql .=
"lastoutput,";
219 $sql .=
"unitfrequency,";
220 $sql .=
"frequency,";
222 $sql .=
"fk_user_author,";
223 $sql .=
"fk_user_mod,";
229 $sql .=
") VALUES (";
230 $sql .=
" ".(!isset($this->entity) ? $conf->entity : $this->
db->escape($this->entity)).
",";
231 $sql .=
" '".$this->db->idate($now).
"',";
232 $sql .=
" ".(!isset($this->jobtype) ?
'NULL' :
"'".$this->db->escape($this->jobtype).
"'").
",";
233 $sql .=
" ".(!isset($this->label) ?
'NULL' :
"'".$this->db->escape($this->label).
"'").
",";
234 $sql .=
" ".(!isset($this->command) ?
'NULL' :
"'".$this->db->escape($this->command).
"'").
",";
235 $sql .=
" ".(!isset($this->classesname) ?
'NULL' :
"'".$this->db->escape($this->classesname).
"'").
",";
236 $sql .=
" ".(!isset($this->objectname) ?
'NULL' :
"'".$this->db->escape($this->objectname).
"'").
",";
237 $sql .=
" ".(!isset($this->methodename) ?
'NULL' :
"'".$this->db->escape($this->methodename).
"'").
",";
238 $sql .=
" ".(!isset($this->params) ?
'NULL' :
"'".$this->db->escape($this->params).
"'").
",";
239 $sql .=
" ".(!isset($this->md5params) ?
'NULL' :
"'".$this->db->escape($this->md5params).
"'").
",";
240 $sql .=
" ".(!isset($this->module_name) ?
'NULL' :
"'".$this->db->escape($this->module_name).
"'").
",";
241 $sql .=
" ".(!isset($this->priority) ?
'0' : $this->priority).
",";
242 $sql .=
" ".(!isset($this->datelastrun) ||
dol_strlen($this->datelastrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastrun).
"'").
",";
243 $sql .=
" ".(!isset($this->datenextrun) ||
dol_strlen($this->datenextrun) == 0 ?
'NULL' :
"'".$this->db->idate($this->datenextrun).
"'").
",";
244 $sql .=
" ".(!isset($this->dateend) ||
dol_strlen($this->dateend) == 0 ?
'NULL' :
"'".$this->db->idate($this->dateend).
"'").
",";
245 $sql .=
" ".(!isset($this->datestart) ||
dol_strlen($this->datestart) == 0 ?
'NULL' :
"'".$this->db->idate($this->datestart).
"'").
",";
246 $sql .=
" ".(!isset($this->lastresult) ?
'NULL' :
"'".$this->db->escape($this->lastresult).
"'").
",";
247 $sql .=
" ".(!isset($this->datelastresult) ||
dol_strlen($this->datelastresult) == 0 ?
'NULL' :
"'".$this->db->idate($this->datelastresult).
"'").
",";
248 $sql .=
" ".(!isset($this->lastoutput) ?
'NULL' :
"'".$this->db->escape($this->lastoutput).
"'").
",";
249 $sql .=
" ".(!isset($this->unitfrequency) ?
'NULL' :
"'".$this->db->escape($this->unitfrequency).
"'").
",";
250 $sql .=
" ".(!isset($this->frequency) ?
'0' : $this->frequency).
",";
251 $sql .=
" ".(!isset($this->status) ?
'0' : $this->status).
",";
252 $sql .=
" ".$user->id.
",";
253 $sql .=
" ".$user->id.
",";
254 $sql .=
" ".(!isset($this->note_private) ?
'NULL' :
"'".$this->db->escape($this->note_private).
"'").
",";
255 $sql .=
" ".(!isset($this->nbrun) ?
'0' : $this->
db->escape($this->nbrun)).
",";
256 $sql .=
" ".(empty($this->maxrun) ?
'0' : $this->
db->escape($this->maxrun)).
",";
257 $sql .=
" ".(!isset($this->libname) ?
'NULL' :
"'".$this->db->escape($this->libname).
"'").
",";
258 $sql .=
" ".(!isset($this->test) ?
'NULL' :
"'".$this->db->escape($this->test).
"'").
"";
263 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
265 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
269 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"cronjob");
275 foreach ($this->errors as $errmsg)
277 dol_syslog(get_class($this).
"::create ".$errmsg, LOG_ERR);
278 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
280 $this->
db->rollback();
297 public function fetch($id, $objectname =
'', $methodname =
'')
301 $sql .=
" t.entity,";
304 $sql .=
" t.jobtype,";
306 $sql .=
" t.command,";
307 $sql .=
" t.classesname,";
308 $sql .=
" t.objectname,";
309 $sql .=
" t.methodename,";
310 $sql .=
" t.params,";
311 $sql .=
" t.md5params,";
312 $sql .=
" t.module_name,";
313 $sql .=
" t.priority,";
314 $sql .=
" t.datelastrun,";
315 $sql .=
" t.datenextrun,";
316 $sql .=
" t.dateend,";
317 $sql .=
" t.datestart,";
318 $sql .=
" t.lastresult,";
319 $sql .=
" t.datelastresult,";
320 $sql .=
" t.lastoutput,";
321 $sql .=
" t.unitfrequency,";
322 $sql .=
" t.frequency,";
323 $sql .=
" t.status,";
324 $sql .=
" t.processing,";
325 $sql .=
" t.fk_user_author,";
326 $sql .=
" t.fk_user_mod,";
327 $sql .=
" t.note as note_private,";
329 $sql .=
" t.maxrun,";
330 $sql .=
" t.libname,";
332 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
334 $sql .=
" WHERE t.rowid = ".$id;
336 $sql .=
" WHERE t.entity IN(0, ".getEntity(
'cron').
")";
337 $sql .=
" AND t.objectname = '".$this->db->escape($objectname).
"'";
338 $sql .=
" AND t.methodename = '".$this->db->escape($methodname).
"'";
341 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
347 $obj = $this->
db->fetch_object(
$resql);
349 $this->
id = $obj->rowid;
350 $this->ref = $obj->rowid;
351 $this->entity = $obj->entity;
352 $this->tms = $this->
db->jdate($obj->tms);
353 $this->datec = $this->
db->jdate($obj->datec);
354 $this->label = $obj->label;
355 $this->jobtype = $obj->jobtype;
356 $this->command = $obj->command;
357 $this->classesname = $obj->classesname;
358 $this->objectname = $obj->objectname;
359 $this->methodename = $obj->methodename;
360 $this->params = $obj->params;
361 $this->md5params = $obj->md5params;
362 $this->module_name = $obj->module_name;
363 $this->priority = $obj->priority;
364 $this->datelastrun = $this->
db->jdate($obj->datelastrun);
365 $this->datenextrun = $this->
db->jdate($obj->datenextrun);
366 $this->dateend = $this->
db->jdate($obj->dateend);
367 $this->datestart = $this->
db->jdate($obj->datestart);
368 $this->lastresult = $obj->lastresult;
369 $this->lastoutput = $obj->lastoutput;
370 $this->datelastresult = $this->
db->jdate($obj->datelastresult);
371 $this->unitfrequency = $obj->unitfrequency;
372 $this->frequency = $obj->frequency;
373 $this->status = $obj->status;
374 $this->processing = $obj->processing;
375 $this->fk_user_author = $obj->fk_user_author;
376 $this->fk_user_mod = $obj->fk_user_mod;
377 $this->note_private = $obj->note_private;
378 $this->nbrun = $obj->nbrun;
379 $this->maxrun = $obj->maxrun;
380 $this->libname = $obj->libname;
381 $this->test = $obj->test;
387 $this->error =
"Error ".$this->db->lasterror();
405 public function fetch_all($sortorder =
'DESC', $sortfield =
't.rowid', $limit = 0, $offset = 0, $status = 1, $filter =
'', $processing = -1)
410 $this->lines = array();
414 $sql .=
" t.entity,";
417 $sql .=
" t.jobtype,";
419 $sql .=
" t.command,";
420 $sql .=
" t.classesname,";
421 $sql .=
" t.objectname,";
422 $sql .=
" t.methodename,";
423 $sql .=
" t.params,";
424 $sql .=
" t.md5params,";
425 $sql .=
" t.module_name,";
426 $sql .=
" t.priority,";
427 $sql .=
" t.datelastrun,";
428 $sql .=
" t.datenextrun,";
429 $sql .=
" t.dateend,";
430 $sql .=
" t.datestart,";
431 $sql .=
" t.lastresult,";
432 $sql .=
" t.datelastresult,";
433 $sql .=
" t.lastoutput,";
434 $sql .=
" t.unitfrequency,";
435 $sql .=
" t.frequency,";
436 $sql .=
" t.status,";
437 $sql .=
" t.processing,";
438 $sql .=
" t.fk_user_author,";
439 $sql .=
" t.fk_user_mod,";
440 $sql .=
" t.note as note_private,";
442 $sql .=
" t.libname,";
444 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as t";
445 $sql .=
" WHERE 1 = 1";
446 if ($processing >= 0) $sql .=
" AND t.processing = ".(empty($processing) ?
'0' :
'1');
447 if ($status >= 0 && $status < 2) $sql .=
" AND t.status = ".(empty($status) ?
'0' :
'1');
448 elseif ($status == 2) $sql .=
" AND t.status = 2";
450 if (is_array($filter) && count($filter) > 0) {
451 foreach ($filter as $key => $value)
453 if ($key ==
't.rowid') $sql .=
' AND '.$key.
' = '.$this->
db->escape($value);
454 else $sql .=
' AND '.$key.
' LIKE \'%'.$this->
db->escape($value).
'%\'';
458 $sql .= $this->
db->order($sortfield, $sortorder);
459 if (!empty($limit) && !empty($offset)) {
460 $sql .= $this->
db->plimit($limit + 1, $offset);
465 if (count($sqlwhere) > 0) {
466 $sql .=
" WHERE ".implode(
' AND ', $sqlwhere);
469 dol_syslog(get_class($this).
"::fetch_all", LOG_DEBUG);
482 $obj = $this->
db->fetch_object(
$resql);
484 $line->id = $obj->rowid;
485 $line->ref = $obj->rowid;
486 $line->entity = $obj->entity;
487 $line->tms = $this->
db->jdate($obj->tms);
488 $line->datec = $this->
db->jdate($obj->datec);
489 $line->label = $obj->label;
490 $line->jobtype = $obj->jobtype;
491 $line->command = $obj->command;
492 $line->classesname = $obj->classesname;
493 $line->objectname = $obj->objectname;
494 $line->methodename = $obj->methodename;
495 $line->params = $obj->params;
496 $line->md5params = $obj->md5params;
497 $line->module_name = $obj->module_name;
498 $line->priority = $obj->priority;
499 $line->datelastrun = $this->
db->jdate($obj->datelastrun);
500 $line->datenextrun = $this->
db->jdate($obj->datenextrun);
501 $line->dateend = $this->
db->jdate($obj->dateend);
502 $line->datestart = $this->
db->jdate($obj->datestart);
503 $line->lastresult = $obj->lastresult;
504 $line->datelastresult = $this->
db->jdate($obj->datelastresult);
505 $line->lastoutput = $obj->lastoutput;
506 $line->unitfrequency = $obj->unitfrequency;
507 $line->frequency = $obj->frequency;
508 $line->status = $obj->status;
509 $line->processing = $obj->processing;
510 $line->fk_user_author = $obj->fk_user_author;
511 $line->fk_user_mod = $obj->fk_user_mod;
512 $line->note_private = $obj->note_private;
513 $line->nbrun = $obj->nbrun;
514 $line->libname = $obj->libname;
515 $line->test = $obj->test;
516 $this->lines[] = $line;
525 $this->error =
"Error ".$this->db->lasterror();
538 public function update($user = null, $notrigger = 0)
540 global $conf, $langs;
542 $langs->load(
'cron');
547 if (isset($this->label)) $this->label = trim($this->label);
548 if (isset($this->jobtype)) $this->jobtype = trim($this->jobtype);
549 if (isset($this->command)) $this->command = trim($this->command);
550 if (isset($this->classesname)) $this->classesname = trim($this->classesname);
551 if (isset($this->objectname)) $this->objectname = trim($this->objectname);
552 if (isset($this->methodename)) $this->methodename = trim($this->methodename);
553 if (isset($this->params)) $this->params = trim($this->params);
554 if (isset($this->md5params)) $this->md5params = trim($this->md5params);
555 if (isset($this->module_name)) $this->module_name = trim($this->module_name);
556 if (isset($this->priority)) $this->priority = trim($this->priority);
557 if (isset($this->lastoutput)) $this->lastoutput = trim($this->lastoutput);
558 if (isset($this->lastresult)) $this->lastresult = trim($this->lastresult);
559 if (isset($this->unitfrequency)) $this->unitfrequency = trim($this->unitfrequency);
560 if (isset($this->frequency)) $this->frequency = trim($this->frequency);
561 if (isset($this->status)) $this->status = trim($this->status);
562 if (isset($this->note_private)) $this->note_private = trim($this->note_private);
563 if (isset($this->nbrun)) $this->nbrun = trim($this->nbrun);
564 if (isset($this->libname)) $this->libname = trim($this->libname);
565 if (isset($this->test)) $this->test = trim($this->test);
567 if (empty($this->maxrun)) $this->maxrun = 0;
568 if (empty($this->processing)) $this->processing = 0;
573 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronDtStart'));
576 if ((
dol_strlen($this->datestart) != 0) && (
dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
577 $this->errors[] = $langs->trans(
'CronErrEndDateStartDt');
580 if (empty($this->label)) {
581 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLabel'));
584 if (empty($this->unitfrequency)) {
585 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronFrequency'));
588 if (($this->jobtype ==
'command') && (empty($this->command))) {
589 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronCommand'));
592 if (($this->jobtype ==
'method') && (empty($this->classesname))) {
593 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronClass'));
596 if (($this->jobtype ==
'method' || $this->jobtype ==
'function') && (empty($this->methodename))) {
597 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronMethod'));
600 if (($this->jobtype ==
'method') && (empty($this->objectname))) {
601 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronObject'));
605 if (($this->jobtype ==
'function') && (empty($this->libname))) {
606 $this->errors[] = $langs->trans(
'CronFieldMandatory', $langs->transnoentitiesnoconv(
'CronLib'));
612 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"cronjob SET";
613 $sql .=
" entity=".(isset($this->entity) ? $this->
db->escape($this->entity) : $conf->entity).
",";
614 $sql .=
" label=".(isset($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
615 $sql .=
" jobtype=".(isset($this->jobtype) ?
"'".$this->db->escape($this->jobtype).
"'" :
"null").
",";
616 $sql .=
" command=".(isset($this->command) ?
"'".$this->db->escape($this->command).
"'" :
"null").
",";
617 $sql .=
" classesname=".(isset($this->classesname) ?
"'".$this->db->escape($this->classesname).
"'" :
"null").
",";
618 $sql .=
" objectname=".(isset($this->objectname) ?
"'".$this->db->escape($this->objectname).
"'" :
"null").
",";
619 $sql .=
" methodename=".(isset($this->methodename) ?
"'".$this->db->escape($this->methodename).
"'" :
"null").
",";
620 $sql .=
" params=".(isset($this->params) ?
"'".$this->db->escape($this->params).
"'" :
"null").
",";
621 $sql .=
" md5params=".(isset($this->md5params) ?
"'".$this->db->escape($this->md5params).
"'" :
"null").
",";
622 $sql .=
" module_name=".(isset($this->module_name) ?
"'".$this->db->escape($this->module_name).
"'" :
"null").
",";
623 $sql .=
" priority=".(isset($this->priority) ? $this->priority :
"null").
",";
624 $sql .=
" datelastrun=".(dol_strlen($this->datelastrun) != 0 ?
"'".$this->db->idate($this->datelastrun).
"'" :
'null').
",";
625 $sql .=
" datenextrun=".(dol_strlen($this->datenextrun) != 0 ?
"'".$this->db->idate($this->datenextrun).
"'" :
'null').
",";
626 $sql .=
" dateend=".(dol_strlen($this->dateend) != 0 ?
"'".$this->db->idate($this->dateend).
"'" :
'null').
",";
627 $sql .=
" datestart=".(dol_strlen($this->datestart) != 0 ?
"'".$this->db->idate($this->datestart).
"'" :
'null').
",";
628 $sql .=
" datelastresult=".(dol_strlen($this->datelastresult) != 0 ?
"'".$this->db->idate($this->datelastresult).
"'" :
'null').
",";
629 $sql .=
" lastresult=".(isset($this->lastresult) ?
"'".$this->db->escape($this->lastresult).
"'" :
"null").
",";
630 $sql .=
" lastoutput=".(isset($this->lastoutput) ?
"'".$this->db->escape($this->lastoutput).
"'" :
"null").
",";
631 $sql .=
" unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency :
"null").
",";
632 $sql .=
" frequency=".(isset($this->frequency) ? $this->frequency :
"null").
",";
633 $sql .=
" status=".(isset($this->status) ? $this->status :
"null").
",";
634 $sql .=
" processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing :
"0").
",";
635 $sql .=
" fk_user_mod=".$user->id.
",";
636 $sql .=
" note=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
637 $sql .=
" nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun :
"null").
",";
638 $sql .=
" maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun :
"0").
",";
639 $sql .=
" libname=".(isset($this->libname) ?
"'".$this->db->escape($this->libname).
"'" :
"null").
",";
640 $sql .=
" test=".(isset($this->test) ?
"'".$this->db->escape($this->test).
"'" :
"null");
641 $sql .=
" WHERE rowid=".$this->id;
645 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
647 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
652 foreach ($this->errors as $errmsg)
654 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
655 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
657 $this->
db->rollback();
673 public function delete($user, $notrigger = 0)
679 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"cronjob";
680 $sql .=
" WHERE rowid=".$this->id;
682 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
686 $this->errors[] =
"Error ".$this->db->lasterror();
692 foreach ($this->errors as $errmsg)
694 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
695 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
697 $this->
db->rollback();
723 $object->fetch($fromid);
731 $object->context[
'createfromclone'] =
'createfromclone';
732 $result = $object->create($user);
737 $this->error = $object->error;
746 unset($object->context[
'createfromclone']);
754 $this->
db->rollback();
776 $this->classesname =
'';
777 $this->objectname =
'';
778 $this->methodename =
'';
780 $this->md5params =
'';
781 $this->module_name =
'';
782 $this->priority =
'';
783 $this->datelastrun =
'';
784 $this->datenextrun =
'';
786 $this->datestart =
'';
787 $this->datelastresult =
'';
788 $this->lastoutput =
'';
789 $this->lastresult =
'';
790 $this->unitfrequency =
'';
791 $this->frequency =
'';
793 $this->processing = 0;
794 $this->fk_user_author = 0;
795 $this->fk_user_mod = 0;
796 $this->note_private =
'';
813 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
815 global $db, $conf, $langs;
816 global $dolibarr_main_authentication, $dolibarr_main_demo;
819 if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1;
823 $label =
img_picto(
'',
'object_'.$this->picto).
' <u>'.$langs->trans(
"CronTask").
'</u>';
824 if (isset($this->status)) {
825 $label .=
' '.$this->getLibStatut(5);
827 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
828 $label .=
'<br><b>'.$langs->trans(
'Title').
':</b> '.$this->label;
830 $url = DOL_URL_ROOT.
'/cron/card.php?id='.$this->id;
832 if ($option !=
'nolink')
835 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
836 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/',
$_SERVER[
"PHP_SELF"])) $add_save_lastsearch_values = 1;
837 if ($add_save_lastsearch_values) $url .=
'&save_lastsearch_values=1';
841 if (empty($notooltip))
843 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
845 $label = $langs->trans(
"ShowCronJob");
846 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
848 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
849 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
850 }
else $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
852 $linkstart =
'<a href="'.$url.
'"';
853 $linkstart .= $linkclose.
'>';
856 $result .= $linkstart;
857 if ($withpicto) $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);
858 if ($withpicto != 2) $result .= $this->ref;
875 $sql .=
" f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
876 $sql .=
" FROM ".MAIN_DB_PREFIX.
"cronjob as f";
877 $sql .=
" WHERE f.rowid = ".$id;
879 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
885 $obj = $this->
db->fetch_object(
$resql);
886 $this->
id = $obj->rowid;
887 $this->date_creation = $this->
db->jdate($obj->datec);
888 $this->date_modification = $this->
db->jdate($obj->tms);
889 $this->user_modification = $obj->fk_user_mod;
890 $this->user_creation = $obj->fk_user_author;
896 $this->error =
"Error ".$this->db->lasterror();
914 global $langs, $conf, $hookmanager;
916 $hookmanager->initHooks(array(
'cron'));
922 $langs->load(
'cron');
924 if (empty($userlogin))
926 $this->error =
"User login is mandatory";
927 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
933 if ($conf->entity != $this->entity && $this->entity > 0)
935 dol_syslog(
"We try to run a job in entity ".$this->entity.
" when we are in entity ".$conf->entity, LOG_WARNING);
937 $savcurrententity = $conf->entity;
938 $conf->entity = $this->entity;
939 dol_syslog(get_class($this).
"::run_jobs entity for running job is ".$conf->entity);
941 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
942 $user =
new User($this->
db);
943 $result = $user->fetch(
'', $userlogin);
946 $this->error =
"User Error:".$user->error;
947 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
948 $conf->entity = $savcurrententity;
951 if (empty($user->id))
953 $this->error =
" User user login:".$userlogin.
" do not exists";
954 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
955 $conf->entity = $savcurrententity;
960 dol_syslog(get_class($this).
"::run_jobs jobtype=".$this->jobtype.
" userlogin=".$userlogin, LOG_DEBUG);
963 $ExecTimeLimit = 600;
964 if (!empty($ExecTimeLimit))
966 $err = error_reporting();
969 @set_time_limit($ExecTimeLimit);
970 error_reporting($err);
972 if (!empty($MemoryLimit))
974 @ini_set(
'memory_limit', $MemoryLimit);
978 $this->datelastrun = $now;
979 $this->datelastresult = null;
980 $this->lastoutput =
'';
981 $this->lastresult =
'';
982 $this->processing = 1;
983 $this->nbrun = $this->nbrun + 1;
984 $result = $this->
update($user);
986 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
987 $conf->entity = $savcurrententity;
992 if ($this->jobtype ==
'method')
998 if ($ret ===
false || (!class_exists($this->objectname)))
1000 if ($ret ===
false) $this->error = $langs->trans(
'CronCannotLoadClass', $this->classesname, $this->objectname);
1001 else $this->error = $langs->trans(
'CronCannotLoadObject', $this->classesname, $this->objectname);
1002 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1003 $this->lastoutput = $this->error;
1004 $this->lastresult = -1;
1005 $retval = $this->lastresult;
1013 if (!method_exists($this->objectname, $this->methodename))
1015 $this->error = $langs->trans(
'CronMethodDoesNotExists', $this->objectname, $this->methodename);
1016 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1017 $this->lastoutput = $this->error;
1018 $this->lastresult = -1;
1019 $retval = $this->lastresult;
1022 if (in_array(strtolower(trim($this->methodename)), array(
'executecli')))
1024 $this->error = $langs->trans(
'CronMethodNotAllowed', $this->methodename, $this->objectname);
1025 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1026 $this->lastoutput = $this->error;
1027 $this->lastresult = -1;
1028 $retval = $this->lastresult;
1036 $result = $langs->load($this->module_name);
1037 $result = $langs->load($this->module_name.
'@'.$this->module_name, 0, 0,
'', 0, 1);
1041 dol_syslog(get_class($this).
"::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1042 $this->error = $langs->error;
1043 $this->lastoutput = $this->error;
1044 $this->lastresult = -1;
1045 $retval = $this->lastresult;
1052 dol_syslog(get_class($this).
"::run_jobs START ".$this->objectname.
"->".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1055 $object =
new $this->objectname($this->
db);
1056 if ($this->entity > 0) $object->entity = $this->entity;
1058 $params_arr = array();
1059 if (!empty($this->params) || $this->params ===
'0') {
1060 $params_arr = array_map(
'trim', explode(
",", $this->params));
1063 if (!is_array($params_arr))
1065 $result = call_user_func(array($object, $this->methodename), $this->params);
1067 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1070 if ($result ===
false || (!is_bool($result) && $result != 0))
1072 $langs->load(
"errors");
1075 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) $errmsg .= $object->error;
1076 if (is_array($object->errors) && count($object->errors)) $errmsg .= (($errmsg ?
', ' :
'').join(
', ', $object->errors));
1077 if (empty($errmsg)) $errmsg = $langs->trans(
'ErrorUnknown');
1079 dol_syslog(get_class($this).
"::run_jobs END result=".$result.
" error=".$errmsg, LOG_ERR);
1081 $this->error = $errmsg;
1082 $this->lastoutput = ($object->output ? $object->output.
"\n" :
"").$errmsg;
1083 $this->lastresult = is_numeric($result) ? $result : -1;
1084 $retval = $this->lastresult;
1087 dol_syslog(get_class($this).
"::run_jobs END");
1088 $this->lastoutput = $object->output;
1089 $this->lastresult = var_export($result,
true);
1090 $retval = $this->lastresult;
1095 if ($this->jobtype ==
'function') {
1097 $libpath =
'/'.strtolower($this->module_name).
'/lib/'.$this->libname;
1101 $this->error = $langs->trans(
'CronCannotLoadLib').
': '.$libpath;
1102 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1103 $conf->entity = $savcurrententity;
1108 $result = $langs->load($this->module_name);
1109 $result = $langs->load($this->module_name.
'@'.$this->module_name);
1112 dol_syslog(get_class($this).
"::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1113 $conf->entity = $savcurrententity;
1117 dol_syslog(get_class($this).
"::run_jobs ".$this->libname.
"::".$this->methodename.
"(".$this->params.
");", LOG_DEBUG);
1118 $params_arr = explode(
", ", $this->params);
1119 if (!is_array($params_arr))
1121 $result = call_user_func($this->methodename, $this->params);
1123 $result = call_user_func_array($this->methodename, $params_arr);
1126 if ($result ===
false || (!is_bool($result) && $result != 0))
1128 $langs->load(
"errors");
1129 dol_syslog(get_class($this).
"::run_jobs result=".$result, LOG_ERR);
1130 $this->error = $langs->trans(
'ErrorUnknown');
1131 $this->lastoutput = $this->error;
1132 $this->lastresult = is_numeric($result) ? $result : -1;
1133 $retval = $this->lastresult;
1136 $this->lastoutput = var_export($result,
true);
1137 $this->lastresult = var_export($result,
true);
1138 $retval = $this->lastresult;
1143 if ($this->jobtype ==
'command')
1145 global $dolibarr_cron_allow_cli;
1147 if (empty($dolibarr_cron_allow_cli)) {
1148 $langs->load(
"errors");
1149 $this->error = $langs->trans(
"FailedToExecutCommandJob");
1150 $this->lastoutput =
'';
1151 $this->lastresult = $langs->trans(
"ErrorParameterMustBeEnabledToAllwoThisFeature",
'dolibarr_cron_allow_cli');
1153 $outputdir = $conf->cron->dir_temp;
1154 if (empty($outputdir)) $outputdir = $conf->cronjob->dir_temp;
1156 if (!empty($outputdir))
1159 $outputfile = $outputdir.
'/cronjob.'.$userlogin.
'.out';
1162 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
1163 $utils =
new Utils($this->
db);
1164 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1166 $retval = $arrayresult[
'result'];
1167 $this->error = $arrayresult[
'error'];
1168 $this->lastoutput = $arrayresult[
'output'];
1169 $this->lastresult = $arrayresult[
'result'];
1174 dol_syslog(get_class($this).
"::run_jobs now we update job to track it is finished (with success or error)");
1176 $this->datelastresult =
dol_now();
1177 $this->processing = 0;
1178 $result = $this->
update($user);
1181 dol_syslog(get_class($this).
"::run_jobs ".$this->error, LOG_ERR);
1182 $conf->entity = $savcurrententity;
1186 $conf->entity = $savcurrententity;
1187 return $error ?-1 : 1;
1202 dol_syslog(get_class($this).
"::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1204 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
1205 $user =
new User($this->
db);
1206 $result = $user->fetch(
'', $userlogin);
1209 $this->error =
"User Error : ".$user->error;
1210 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1213 if (empty($user->id))
1215 $this->error =
" User user login:".$userlogin.
" do not exists";
1216 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1221 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun=".$this->datenextrun.
" ".
dol_print_date($this->datenextrun,
'dayhourrfc').
" frequency=".$this->frequency.
" unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1223 if (empty($this->datenextrun))
1225 if (empty($this->datestart)) $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1226 else $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1229 if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0)
1232 while ($this->datenextrun < $now)
1234 $this->datenextrun += ($this->frequency * $this->unitfrequency);
1240 dol_syslog(get_class($this).
"::reprogram_jobs datenextrun is already in future, we do not change it");
1245 if ($this->autodelete == 2)
1247 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1248 || ($this->dateend && ($this->datenextrun > $this->dateend)))
1250 $this->status = self::STATUS_ARCHIVED;
1251 dol_syslog(get_class($this).
"::reprogram_jobs Job will be set to archived", LOG_ERR);
1255 $result = $this->
update($user);
1258 dol_syslog(get_class($this).
"::reprogram_jobs ".$this->error, LOG_ERR);
1273 return $this->
LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1286 public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1289 $this->labelStatus = array();
1290 $this->labelStatusShort = array();
1292 if (empty($this->labelStatus) || empty($this->labelStatusShort))
1295 $langs->load(
'users');
1298 if ($processing) $moretext =
' ('.$langs->trans(
"Running").
')';
1299 elseif ($lastresult) $moretext .=
' ('.$langs->trans(
"Error").
')';
1301 $this->labelStatus[self::STATUS_DISABLED] = $langs->trans(
'Disabled').$moretext;
1302 $this->labelStatus[self::STATUS_ENABLED] = $langs->trans(
'Scheduled').$moretext;
1303 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->trans(
'Disabled');
1304 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->trans(
'Scheduled');
1307 $statusType =
'status4';
1308 if ($status == 1 && $processing) $statusType =
'status1';
1309 if ($status == 0) $statusType =
'status5';
1310 if ($this->lastresult) $statusType =
'status8';
1312 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
1343 public $classesname;
1345 public $methodename;
1348 public $module_name;
1350 public $datelastrun =
'';
1351 public $datenextrun =
'';
1352 public $dateend =
'';
1353 public $datestart =
'';
1354 public $lastresult =
'';
1356 public $unitfrequency;
1367 public $fk_user_author;
1372 public $fk_user_mod;
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
Class to manage utility methods.
create($user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
info($id)
Load object information.
__construct()
Constructor.
dol_now($mode= 'auto')
Return date for now.
reprogram_jobs($userlogin, $now)
Reprogram a job.
Class to manage Dolibarr users.
getLibStatut($mode=0)
Return label of status of user (active, inactive)
__construct($db)
Constructor.
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
run_jobs($userlogin)
Run a job.
$conf db
API class for accounts.
update($user=null, $notrigger=0)
Update object into database.
fetch($id, $objectname= '', $methodname= '')
Load object in memory from the database.
LibStatut($status, $mode=0, $processing=0, $lastresult=0)
Renvoi le libelle d'un statut donne.
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
fetch_all($sortorder= 'DESC', $sortfield= 't.rowid', $limit=0, $offset=0, $status=1, $filter= '', $processing=-1)
Load object in memory from the database.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)