dolibarr 19.0.3
cronjob.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2022 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24// Put here all includes required by your class file
25require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
26require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
27
28
32class Cronjob extends CommonObject
33{
37 public $element = 'cronjob';
38
42 public $table_element = 'cronjob';
43
47 public $picto = 'cron';
48
52 public $entity;
53
57 public $jobtype;
58
62 public $tms = '';
63
67 public $datec = '';
68
72 public $label;
73
77 public $command;
78 public $classesname;
79 public $objectname;
80 public $methodename;
81 public $params;
82 public $md5params;
83 public $module_name;
84 public $priority;
85
89 public $datelastrun = '';
90
94 public $datenextrun = '';
95
99 public $dateend = '';
100
104 public $datestart = '';
105
109 public $datelastresult = '';
110
114 public $lastresult;
115
119 public $lastoutput;
120
124 public $unitfrequency;
125
129 public $frequency;
130
134 public $status;
135
139 public $processing;
140
144 public $pid;
145
149 public $email_alert;
150
154 public $fk_user_author;
155
159 public $fk_user_mod;
160
164 public $nbrun;
165
169 public $maxrun;
170
174 public $libname;
175
179 public $test;
180
184 public $autodelete;
185
186
187 const STATUS_DISABLED = 0;
188 const STATUS_ENABLED = 1;
189 const STATUS_ARCHIVED = 2;
190 const MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD = 65535;
191
192
198 public function __construct($db)
199 {
200 $this->db = $db;
201 }
202
203
211 public function create($user, $notrigger = 0)
212 {
213 global $conf, $langs;
214 $error = 0;
215
216 $now = dol_now();
217
218 // Clean parameters
219
220 if (isset($this->label)) {
221 $this->label = trim($this->label);
222 }
223 if (isset($this->jobtype)) {
224 $this->jobtype = trim($this->jobtype);
225 }
226 if (isset($this->command)) {
227 $this->command = trim($this->command);
228 }
229 if (isset($this->classesname)) {
230 $this->classesname = trim($this->classesname);
231 }
232 if (isset($this->objectname)) {
233 $this->objectname = trim($this->objectname);
234 }
235 if (isset($this->methodename)) {
236 $this->methodename = trim($this->methodename);
237 }
238 if (isset($this->params)) {
239 $this->params = trim($this->params);
240 }
241 if (isset($this->md5params)) {
242 $this->md5params = trim($this->md5params);
243 }
244 if (isset($this->module_name)) {
245 $this->module_name = trim($this->module_name);
246 }
247 if (isset($this->priority)) {
248 $this->priority = trim($this->priority);
249 }
250 if (isset($this->lastoutput)) {
251 $this->lastoutput = trim($this->lastoutput);
252 }
253 if (isset($this->lastresult)) {
254 $this->lastresult = trim($this->lastresult);
255 }
256 if (isset($this->unitfrequency)) {
257 $this->unitfrequency = trim($this->unitfrequency);
258 }
259 if (isset($this->frequency)) {
260 $this->frequency = trim($this->frequency);
261 }
262 if (isset($this->status)) {
263 $this->status = trim($this->status);
264 }
265 if (isset($this->note_private)) {
266 $this->note_private = trim($this->note_private);
267 }
268 if (isset($this->nbrun)) {
269 $this->nbrun = (int) $this->nbrun;
270 }
271 if (isset($this->maxrun)) {
272 $this->maxrun = (int) $this->maxrun;
273 }
274 if (isset($this->libname)) {
275 $this->libname = trim($this->libname);
276 }
277 if (isset($this->test)) {
278 $this->test = trim($this->test);
279 }
280
281 // Check parameters
282 // Put here code to add a control on parameters values
283 if (dol_strlen($this->datenextrun) == 0) {
284 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
285 $error++;
286 }
287 if (empty($this->label)) {
288 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
289 $error++;
290 }
291 if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
292 $this->errors[] = $langs->trans('CronErrEndDateStartDt');
293 $error++;
294 }
295 if (empty($this->unitfrequency)) {
296 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
297 $error++;
298 }
299 if (($this->jobtype == 'command') && (empty($this->command))) {
300 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
301 $error++;
302 }
303 if (($this->jobtype == 'method') && (empty($this->classesname))) {
304 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
305 $error++;
306 }
307 if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
308 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
309 $error++;
310 }
311 if (($this->jobtype == 'method') && (empty($this->objectname))) {
312 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
313 $error++;
314 }
315 if (($this->jobtype == 'function') && (empty($this->libname))) {
316 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
317 $error++;
318 }
319
320 // Insert request
321 $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
322 $sql .= "entity,";
323 $sql .= "datec,";
324 $sql .= "jobtype,";
325 $sql .= "label,";
326 $sql .= "command,";
327 $sql .= "classesname,";
328 $sql .= "objectname,";
329 $sql .= "methodename,";
330 $sql .= "params,";
331 $sql .= "md5params,";
332 $sql .= "module_name,";
333 $sql .= "priority,";
334 $sql .= "datelastrun,";
335 $sql .= "datenextrun,";
336 $sql .= "dateend,";
337 $sql .= "datestart,";
338 $sql .= "lastresult,";
339 $sql .= "datelastresult,";
340 $sql .= "lastoutput,";
341 $sql .= "unitfrequency,";
342 $sql .= "frequency,";
343 $sql .= "status,";
344 $sql .= "fk_user_author,";
345 $sql .= "fk_user_mod,";
346 $sql .= "note,";
347 $sql .= "nbrun,";
348 $sql .= "maxrun,";
349 $sql .= "libname,";
350 $sql .= "test";
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)."'");
381 $sql .= ")";
382
383 $this->db->begin();
384
385 dol_syslog(get_class($this)."::create", LOG_DEBUG);
386 $resql = $this->db->query($sql);
387 if (!$resql) {
388 $error++;
389 $this->errors[] = "Error ".$this->db->lasterror();
390 }
391
392 if (!$error) {
393 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
394 }
395
396 // Commit or rollback
397 if ($error) {
398 $this->db->rollback();
399 return -1 * $error;
400 } else {
401 $this->db->commit();
402 return $this->id;
403 }
404 }
405
406
415 public function fetch($id, $objectname = '', $methodname = '')
416 {
417 $sql = "SELECT";
418 $sql .= " t.rowid,";
419 $sql .= " t.entity,";
420 $sql .= " t.tms,";
421 $sql .= " t.datec,";
422 $sql .= " t.jobtype,";
423 $sql .= " t.label,";
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,";
443 $sql .= " t.pid,";
444 $sql .= " t.email_alert,";
445 $sql .= " t.fk_user_author,";
446 $sql .= " t.fk_user_mod,";
447 $sql .= " t.note as note_private,";
448 $sql .= " t.nbrun,";
449 $sql .= " t.maxrun,";
450 $sql .= " t.libname,";
451 $sql .= " t.test";
452 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
453 if ($id > 0) {
454 $sql .= " WHERE t.rowid = ".((int) $id);
455 } else {
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)."'";
459 }
460
461 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
462 $resql = $this->db->query($sql);
463 if ($resql) {
464 if ($this->db->num_rows($resql)) {
465 $obj = $this->db->fetch_object($resql);
466
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;
502 }
503 $this->db->free($resql);
504
505 return 1;
506 } else {
507 $this->error = "Error ".$this->db->lasterror();
508 return -1;
509 }
510 }
511
525 public function fetchAll($sortorder = 'DESC', $sortfield = 't.rowid', $limit = 0, $offset = 0, $status = 1, $filter = '', $processing = -1)
526 {
527 $this->lines = array();
528
529 $sql = "SELECT";
530 $sql .= " t.rowid,";
531 $sql .= " t.entity,";
532 $sql .= " t.tms,";
533 $sql .= " t.datec,";
534 $sql .= " t.jobtype,";
535 $sql .= " t.label,";
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,";
555 $sql .= " t.pid,";
556 $sql .= " t.email_alert,";
557 $sql .= " t.fk_user_author,";
558 $sql .= " t.fk_user_mod,";
559 $sql .= " t.note as note_private,";
560 $sql .= " t.nbrun,";
561 $sql .= " t.libname,";
562 $sql .= " t.test";
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');
567 }
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";
572 }
573 // Manage filter
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);
578 } else {
579 $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
580 }
581 }
582 }
583
584 $sql .= $this->db->order($sortfield, $sortorder);
585 if (!empty($limit) && !empty($offset)) {
586 $sql .= $this->db->plimit($limit + 1, $offset);
587 }
588
589 $sqlwhere = array();
590
591 if (count($sqlwhere) > 0) {
592 $sql .= " WHERE ".implode(' AND ', $sqlwhere);
593 }
594
595 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
596 $resql = $this->db->query($sql);
597 if ($resql) {
598 $num = $this->db->num_rows($resql);
599 $i = 0;
600
601 if ($num) {
602 while ($i < $num) {
603 $line = new Cronjobline();
604
605 $obj = $this->db->fetch_object($resql);
606
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;
642
643 $i++;
644 }
645 }
646 $this->db->free($resql);
647
648 return 1;
649 } else {
650 $this->error = "Error ".$this->db->lasterror();
651 return -1;
652 }
653 }
654
655
663 public function update($user = null, $notrigger = 0)
664 {
665 global $conf, $langs;
666
667 $langs->load('cron');
668
669 $error = 0;
670
671 // Clean parameters
672 if (isset($this->label)) {
673 $this->label = trim($this->label);
674 }
675 if (isset($this->jobtype)) {
676 $this->jobtype = trim($this->jobtype);
677 }
678 if (isset($this->command)) {
679 $this->command = trim($this->command);
680 }
681 if (isset($this->classesname)) {
682 $this->classesname = trim($this->classesname);
683 }
684 if (isset($this->objectname)) {
685 $this->objectname = trim($this->objectname);
686 }
687 if (isset($this->methodename)) {
688 $this->methodename = trim($this->methodename);
689 }
690 if (isset($this->params)) {
691 $this->params = trim($this->params);
692 }
693 if (isset($this->md5params)) {
694 $this->md5params = trim($this->md5params);
695 }
696 if (isset($this->module_name)) {
697 $this->module_name = trim($this->module_name);
698 }
699 if (isset($this->priority)) {
700 $this->priority = trim($this->priority);
701 }
702 if (isset($this->lastoutput)) {
703 $this->lastoutput = trim($this->lastoutput);
704 }
705 if (isset($this->lastresult)) {
706 $this->lastresult = trim($this->lastresult);
707 }
708 if (isset($this->unitfrequency)) {
709 $this->unitfrequency = trim($this->unitfrequency);
710 }
711 if (isset($this->frequency)) {
712 $this->frequency = trim($this->frequency);
713 }
714 if (isset($this->status)) {
715 $this->status = trim($this->status);
716 }
717 if (isset($this->note_private)) {
718 $this->note_private = trim($this->note_private);
719 }
720 if (isset($this->nbrun)) {
721 $this->nbrun = trim($this->nbrun);
722 }
723 if (isset($this->libname)) {
724 $this->libname = trim($this->libname);
725 }
726 if (isset($this->test)) {
727 $this->test = trim($this->test);
728 }
729
730 if (empty($this->maxrun)) {
731 $this->maxrun = 0;
732 }
733 if (empty($this->processing)) {
734 $this->processing = 0;
735 }
736 if (empty($this->pid)) {
737 $this->pid = null;
738 }
739 if (empty($this->email_alert)) {
740 $this->email_alert = '';
741 }
742 if (empty($this->datenextrun)) {
743 $this->datenextrun = dol_now();
744 }
745
746 // Check parameters
747 // Put here code to add a control on parameters values
748 if (dol_strlen($this->datenextrun) == 0 && $this->status == self::STATUS_ENABLED) {
749 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
750 $error++;
751 }
752 if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
753 $this->errors[] = $langs->trans('CronErrEndDateStartDt');
754 $error++;
755 }
756 if (empty($this->label)) {
757 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
758 $error++;
759 }
760 if (empty($this->unitfrequency)) {
761 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
762 $error++;
763 }
764 if (($this->jobtype == 'command') && (empty($this->command))) {
765 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
766 $error++;
767 }
768 if (($this->jobtype == 'method') && (empty($this->classesname))) {
769 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
770 $error++;
771 }
772 if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
773 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
774 $error++;
775 }
776 if (($this->jobtype == 'method') && (empty($this->objectname))) {
777 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
778 $error++;
779 }
780
781 if (($this->jobtype == 'function') && (empty($this->libname))) {
782 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
783 $error++;
784 }
785
786
787 // Update request
788 $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
789 $sql .= " entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).",";
790 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
791 $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
792 $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
793 $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
794 $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
795 $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
796 $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
797 $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
798 $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
799 $sql .= " priority=".(isset($this->priority) ? ((int) $this->priority) : "null").",";
800 $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
801 $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
802 $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
803 $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
804 $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
805 $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
806 $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
807 $sql .= " unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency : "null").",";
808 $sql .= " frequency=".(isset($this->frequency) ? $this->frequency : "null").",";
809 $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
810 $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
811 $sql .= " pid=".(isset($this->pid) ? ((int) $this->pid) : "null").",";
812 $sql .= " email_alert = ".(isset($this->email_alert) ? "'".$this->db->escape($this->email_alert)."'" : "null").",";
813 $sql .= " fk_user_mod = ".((int) $user->id).",";
814 $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
815 $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
816 $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
817 $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
818 $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
819 $sql .= " WHERE rowid=".((int) $this->id);
820
821 $this->db->begin();
822
823 dol_syslog(get_class($this)."::update", LOG_DEBUG);
824 $resql = $this->db->query($sql);
825 if (!$resql) {
826 $error++;
827 $this->errors[] = "Error ".$this->db->lasterror();
828 }
829
830 // Commit or rollback
831 if ($error) {
832 foreach ($this->errors as $errmsg) {
833 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
834 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
835 }
836 $this->db->rollback();
837 return -1 * $error;
838 } else {
839 $this->db->commit();
840 return 1;
841 }
842 }
843
844
852 public function delete($user, $notrigger = 0)
853 {
854 $error = 0;
855
856 $this->db->begin();
857
858 $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
859 $sql .= " WHERE rowid=".((int) $this->id);
860
861 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
862 $resql = $this->db->query($sql);
863 if (!$resql) {
864 $error++;
865 $this->errors[] = "Error ".$this->db->lasterror();
866 }
867
868 // Commit or rollback
869 if ($error) {
870 foreach ($this->errors as $errmsg) {
871 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
872 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
873 }
874 $this->db->rollback();
875 return -1 * $error;
876 } else {
877 $this->db->commit();
878 return 1;
879 }
880 }
881
882
883
891 public function createFromClone(User $user, $fromid)
892 {
893 global $langs;
894
895 $error = 0;
896
897 $object = new Cronjob($this->db);
898
899 $this->db->begin();
900
901 // Load source object
902 $object->fetch($fromid);
903 $object->id = 0;
904
905 // Clear fields
906 $object->status = self::STATUS_DISABLED;
907 $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
908 $object->datelastrun = null;
909 $object->lastresult = '';
910 $object->datelastresult = null;
911 $object->lastoutput = '';
912 $object->nbrun = 0;
913
914 // Create clone
915 $object->context['createfromclone'] = 'createfromclone';
916 $result = $object->create($user);
917
918 // Other options
919 if ($result < 0) {
920 $this->error = $object->error;
921 $this->errors = $object->errors;
922 $error++;
923 }
924
925 unset($object->context['createfromclone']);
926
927 // End
928 if (!$error) {
929 $this->db->commit();
930 return $object->id;
931 } else {
932 $this->db->rollback();
933 return -1;
934 }
935 }
936
937
944 public function initAsSpecimen()
945 {
946 $this->id = 0;
947 $this->ref = 0;
948 $this->entity = 0;
949 $this->tms = '';
950 $this->datec = '';
951 $this->label = '';
952 $this->jobtype = '';
953 $this->command = '';
954 $this->classesname = '';
955 $this->objectname = '';
956 $this->methodename = '';
957 $this->params = '';
958 $this->md5params = '';
959 $this->module_name = '';
960 $this->priority = '';
961 $this->datelastrun = '';
962 $this->datenextrun = '';
963 $this->dateend = '';
964 $this->datestart = '';
965 $this->datelastresult = '';
966 $this->lastoutput = '';
967 $this->lastresult = '';
968 $this->unitfrequency = '';
969 $this->frequency = '';
970 $this->status = 0;
971 $this->processing = 0;
972 $this->pid = null;
973 $this->email_alert = '';
974 $this->fk_user_author = 0;
975 $this->fk_user_mod = 0;
976 $this->note_private = '';
977 $this->nbrun = '';
978 $this->maxrun = 100;
979 $this->libname = '';
980 }
981
982
989 public function getTooltipContentArray($params)
990 {
991 global $langs;
992
993 $langs->load('cron');
994 $datas = [];
995
996 $datas['picto'] = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
997 if (isset($this->status)) {
998 $datas['picto'] .= ' '.$this->getLibStatut(5);
999 }
1000 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.dol_escape_htmltag($this->ref);
1001 $datas['label'] = '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
1002 if ($this->label != $langs->trans($this->label)) {
1003 $datas['label'] .= ' <span class="opacitymedium">('.$this->label.')</span>';
1004 }
1005 if (!empty($this->params)) {
1006 $datas['params'] = '<br><b>'.$langs->trans('Parameters').':</b> '.dol_escape_htmltag($this->params);
1007 }
1008 $datas['space'] = '<br>';
1009
1010 if (!empty($this->datestart) && $this->datestart >= dol_now()) {
1011 $datas['crondtstart'] = '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
1012 }
1013 if (!empty($this->dateend)) {
1014 $datas['crondtend'] = '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
1015 }
1016 if (!empty($this->datelastrun)) {
1017 $datas['cronlastlaunch'] = '<br><b>'.$langs->trans('CronDtLastLaunch').':</b> '.dol_print_date($this->datelastrun, 'dayhour', 'tzuserrel');
1018 }
1019 if (!empty($this->datenextrun)) {
1020 $datas['crondtnextlaunch'] = '<br><b>'.$langs->trans('CronDtNextLaunch').':</b> '.dol_print_date($this->datenextrun, 'dayhour', 'tzuserrel');
1021 }
1022
1023 return $datas;
1024 }
1025
1036 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
1037 {
1038 global $db, $conf, $langs;
1039
1040 if (!empty($conf->dol_no_mouse_hover)) {
1041 $notooltip = 1; // Force disable tooltips
1042 }
1043
1044 $result = '';
1045
1046 $params = [
1047 'id' => $this->id,
1048 'objecttype' => $this->element,
1049 ];
1050 $classfortooltip = 'classfortooltip';
1051 $dataparams = '';
1052 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1053 $classfortooltip = 'classforajaxtooltip';
1054 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1055 $label = '';
1056 } else {
1057 $label = implode($this->getTooltipContentArray($params));
1058 }
1059
1060 $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
1061
1062 if ($option != 'nolink') {
1063 // Add param to save lastsearch_values or not
1064 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1065 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1066 $add_save_lastsearch_values = 1;
1067 }
1068 if ($add_save_lastsearch_values) {
1069 $url .= '&save_lastsearch_values=1';
1070 }
1071 }
1072
1073 $linkclose = '';
1074 if (empty($notooltip)) {
1075 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1076 $label = $langs->trans("ShowCronJob");
1077 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1078 }
1079 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1080 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1081 } else {
1082 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1083 }
1084
1085 $linkstart = '<a href="'.$url.'"';
1086 $linkstart .= $linkclose.'>';
1087 $linkend = '</a>';
1088
1089 $result .= $linkstart;
1090 if ($withpicto) {
1091 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
1092 }
1093 if ($withpicto != 2) {
1094 $result .= $this->ref;
1095 }
1096 $result .= $linkend;
1097 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1098
1099 return $result;
1100 }
1101
1102
1109 public function info($id)
1110 {
1111 $sql = "SELECT";
1112 $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1113 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
1114 $sql .= " WHERE f.rowid = ".((int) $id);
1115
1116 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1117 $resql = $this->db->query($sql);
1118 if ($resql) {
1119 if ($this->db->num_rows($resql)) {
1120 $obj = $this->db->fetch_object($resql);
1121
1122 $this->id = $obj->rowid;
1123
1124 $this->user_modification_id = $obj->fk_user_mod;
1125 $this->user_creation_id = $obj->fk_user_author;
1126 $this->date_creation = $this->db->jdate($obj->datec);
1127 $this->date_modification = $this->db->jdate($obj->tms);
1128 }
1129 $this->db->free($resql);
1130
1131 return 1;
1132 } else {
1133 $this->error = "Error ".$this->db->lasterror();
1134 return -1;
1135 }
1136 }
1137
1138
1139 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1148 public function run_jobs($userlogin)
1149 {
1150 // phpcs:enable
1151 global $langs, $conf, $hookmanager;
1152
1153 $hookmanager->initHooks(array('cron'));
1154
1155 $now = dol_now();
1156 $error = 0;
1157 $retval = '';
1158
1159 $langs->load('cron');
1160
1161 if (empty($userlogin)) {
1162 $this->error = "User login is mandatory";
1163 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1164 return -1;
1165 }
1166
1167 // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
1168 // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
1169 if ($conf->entity != $this->entity && $this->entity > 0) {
1170 dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
1171 }
1172 $savcurrententity = $conf->entity;
1173 $conf->setEntityValues($this->db, $this->entity);
1174 dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
1175
1176 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1177 $user = new User($this->db);
1178 $result = $user->fetch('', $userlogin);
1179 if ($result < 0) {
1180 $this->error = "User Error:".$user->error;
1181 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1182 $conf->setEntityValues($this->db, $savcurrententity);
1183 return -1;
1184 } else {
1185 if (empty($user->id)) {
1186 $this->error = "User login: ".$userlogin." does not exist";
1187 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1188 $conf->setEntityValues($this->db, $savcurrententity);
1189 return -1;
1190 }
1191 }
1192
1193 dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
1194
1195 // Increase limit of time. Works only if we are not in safe mode
1196 $ExecTimeLimit = 600;
1197 if (!empty($ExecTimeLimit)) {
1198 $err = error_reporting();
1199 error_reporting(0); // Disable all errors
1200 //error_reporting(E_ALL);
1201 @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
1202 error_reporting($err);
1203 }
1204 $MemoryLimit = 0;
1205 if (!empty($MemoryLimit)) {
1206 @ini_set('memory_limit', $MemoryLimit);
1207 }
1208
1209 // Update last run date start (to track running jobs)
1210 $this->datelastrun = $now;
1211 $this->datelastresult = null;
1212 $this->lastoutput = '';
1213 $this->lastresult = '';
1214 $this->processing = 1; // To know job was started
1215 $this->pid = function_exists('getmypid') ? getmypid() : null; // Avoid dol_getmypid to get null if the function is not available
1216 $this->nbrun = $this->nbrun + 1;
1217 $result = $this->update($user); // This include begin/commit
1218 if ($result < 0) {
1219 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1220 $conf->setEntityValues($this->db, $savcurrententity);
1221 return -1;
1222 }
1223
1224 // Run a method
1225 if ($this->jobtype == 'method') {
1226 // load classes
1227 if (!$error) {
1228 $ret = dol_include_once($this->classesname);
1229 if ($ret === false || (!class_exists($this->objectname))) {
1230 if ($ret === false) {
1231 $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
1232 } else {
1233 $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
1234 }
1235 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1236 $this->lastoutput = $this->error;
1237 $this->lastresult = -1;
1238 $retval = $this->lastresult;
1239 $error++;
1240 }
1241 }
1242
1243 // test if method exists
1244 if (!$error) {
1245 if (!method_exists($this->objectname, $this->methodename)) {
1246 $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
1247 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1248 $this->lastoutput = $this->error;
1249 $this->lastresult = -1;
1250 $retval = $this->lastresult;
1251 $error++;
1252 }
1253 if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
1254 $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
1255 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1256 $this->lastoutput = $this->error;
1257 $this->lastresult = -1;
1258 $retval = $this->lastresult;
1259 $error++;
1260 }
1261 }
1262
1263 // Load langs
1264 if (!$error) {
1265 $result = $langs->load($this->module_name);
1266 $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
1267
1268 if ($result < 0) { // If technical error
1269 dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1270 $this->error = $langs->error;
1271 $this->lastoutput = $this->error;
1272 $this->lastresult = -1;
1273 $retval = $this->lastresult;
1274 $error++;
1275 }
1276 }
1277
1278 if (!$error) {
1279 dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params."); !!! Log for job may be into a different log file...", LOG_DEBUG);
1280
1281 // Create Object for the called module
1282 $nameofclass = $this->objectname;
1283 $object = new $nameofclass($this->db);
1284 if ($this->entity > 0) {
1285 $object->entity = $this->entity; // We work on a dedicated entity
1286 }
1287
1288 $params_arr = array();
1289 if (!empty($this->params) || $this->params === '0') {
1290 $params_arr = array_map('trim', explode(",", $this->params));
1291 }
1292
1293 if (!is_array($params_arr)) {
1294 $result = call_user_func(array($object, $this->methodename), $this->params);
1295 } else {
1296 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1297 }
1298 $errmsg = '';
1299 if ($result === false || (!is_bool($result) && $result != 0)) {
1300 $langs->load("errors");
1301
1302 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1303 $errmsg .= $object->error;
1304 }
1305 if (is_array($object->errors) && count($object->errors)) {
1306 $errmsg .= (($errmsg ? ', ' : '').join(', ', $object->errors));
1307 }
1308 if (empty($errmsg)) {
1309 $errmsg = $langs->trans('ErrorUnknown');
1310 }
1311
1312 dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
1313
1314 $this->error = $errmsg;
1315 $this->lastoutput = dol_substr((empty($object->output) ? "" : $object->output."\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
1316 $this->lastresult = is_numeric($result) ? $result : -1;
1317 $retval = $this->lastresult;
1318 $error++;
1319 } else {
1320 dol_syslog(get_class($this)."::run_jobs END");
1321 $this->lastoutput = dol_substr((empty($object->output) ? "" : $object->output."\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
1322 $this->lastresult = var_export($result, true);
1323 $retval = $this->lastresult;
1324 }
1325 }
1326 }
1327
1328 if ($this->jobtype == 'function') {
1329 //load lib
1330 $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
1331 $ret = dol_include_once($libpath);
1332 if ($ret === false) {
1333 $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
1334 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1335 $conf->setEntityValues($this->db, $savcurrententity);
1336 return -1;
1337 }
1338
1339 // Load langs
1340 $result = $langs->load($this->module_name);
1341 $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
1342 if ($result < 0) { // If technical error
1343 dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1344 $conf->setEntityValues($this->db, $savcurrententity);
1345 return -1;
1346 }
1347
1348 dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
1349 $params_arr = explode(", ", $this->params);
1350 if (!is_array($params_arr)) {
1351 $result = call_user_func($this->methodename, $this->params);
1352 } else {
1353 $result = call_user_func_array($this->methodename, $params_arr);
1354 }
1355
1356 if ($result === false || (!is_bool($result) && $result != 0)) {
1357 $langs->load("errors");
1358 dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
1359 $this->error = $langs->trans('ErrorUnknown');
1360 $this->lastoutput = $this->error;
1361 $this->lastresult = is_numeric($result) ? $result : -1;
1362 $retval = $this->lastresult;
1363 $error++;
1364 } else {
1365 $this->lastoutput = var_export($result, true);
1366 $this->lastresult = var_export($result, true); // Return code
1367 $retval = $this->lastresult;
1368 }
1369 }
1370
1371 // Run a command line
1372 if ($this->jobtype == 'command') {
1373 global $dolibarr_cron_allow_cli;
1374
1375 if (empty($dolibarr_cron_allow_cli)) {
1376 $langs->load("errors");
1377 $this->error = $langs->trans("FailedToExecutCommandJob");
1378 $this->lastoutput = '';
1379 $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
1380 } else {
1381 $outputdir = $conf->cron->dir_temp;
1382 if (empty($outputdir)) {
1383 $outputdir = $conf->cronjob->dir_temp;
1384 }
1385
1386 if (!empty($outputdir)) {
1387 dol_mkdir($outputdir);
1388 $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
1389
1390 // Execute a CLI
1391 include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1392 $utils = new Utils($this->db);
1393 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1394
1395 $retval = $arrayresult['result'];
1396 $this->error = $arrayresult['error'];
1397 $this->lastoutput = $arrayresult['output'];
1398 $this->lastresult = $arrayresult['result'];
1399 }
1400 }
1401 }
1402
1403 dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
1404
1405 $this->datelastresult = dol_now();
1406 $this->processing = 0;
1407 $this->pid = null;
1408 $result = $this->update($user); // This include begin/commit
1409 if ($result < 0) {
1410 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1411 $conf->setEntityValues($this->db, $savcurrententity);
1412 return -1;
1413 }
1414
1415 $conf->setEntityValues($this->db, $savcurrententity);
1416
1417 if ($error && !empty($this->email_alert)) {
1418 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
1419 $subject = $langs->trans("ErrorInBatch", $this->label);
1420 $msg = $langs->trans("ErrorInBatch", $this->label);
1421 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
1422 $cmailfile = new CMailFile($subject, $this->email_alert, $from, $msg);
1423 $result = $cmailfile->sendfile(); // Do not test result
1424 }
1425
1426 return $error ? -1 : 1;
1427 }
1428
1429
1430 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1438 public function reprogram_jobs($userlogin, $now)
1439 {
1440 // phpcs:enable
1441 dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1442
1443 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1444 $user = new User($this->db);
1445 $result = $user->fetch('', $userlogin);
1446 if ($result < 0) {
1447 $this->error = "User Error : ".$user->error;
1448 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1449 return -1;
1450 } else {
1451 if (empty($user->id)) {
1452 $this->error = " User user login:".$userlogin." do not exists";
1453 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1454 return -1;
1455 }
1456 }
1457
1458 dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1459
1460 if (empty($this->datenextrun)) {
1461 if (empty($this->datestart)) {
1462 if ($this->unitfrequency == 2678400) {
1463 $this->datenextrun = dol_time_plus_duree($now, $this->frequency, 'm');
1464 } else {
1465 $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1466 }
1467 } else {
1468 if ($this->unitfrequency == 2678400) {
1469 $this->datenextrun = dol_time_plus_duree($this->datestart, $this->frequency, 'm');
1470 } else {
1471 $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1472 }
1473 }
1474 }
1475
1476 if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
1477 // Loop until date is after future
1478 while ($this->datenextrun < $now) {
1479 if ($this->unitfrequency == 2678400) {
1480 $this->datenextrun = dol_time_plus_duree($this->datenextrun, $this->frequency, 'm');
1481 } else {
1482 $this->datenextrun += ($this->frequency * $this->unitfrequency);
1483 }
1484 }
1485 } else {
1486 dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
1487 }
1488
1489
1490 // Archive job
1491 if ($this->autodelete == 2) {
1492 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1493 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1494 $this->status = self::STATUS_ARCHIVED;
1495 dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
1496 }
1497 }
1498
1499 $result = $this->update($user);
1500 if ($result < 0) {
1501 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1502 return -1;
1503 }
1504
1505 return 1;
1506 }
1507
1514 public function getLibStatut($mode = 0)
1515 {
1516 return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1517 }
1518
1519 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1529 public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1530 {
1531 // phpcs:enable
1532 $this->labelStatus = array(); // Force reset o array because label depends on other fields
1533 $this->labelStatusShort = array();
1534
1535 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1536 global $langs;
1537 $langs->load('users');
1538
1539 $moretext = '';
1540 if ($processing) {
1541 $moretext = ' ('.$langs->trans("Running").')';
1542 } elseif ($lastresult) {
1543 $moretext .= ' ('.$langs->trans("Error").')';
1544 }
1545
1546 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moretext;
1547 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moretext;
1548 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
1549 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
1550 }
1551
1552 $statusType = 'status4';
1553 if ($status == 1 && $processing) {
1554 $statusType = 'status1';
1555 }
1556 if ($status == 0) {
1557 $statusType = 'status5';
1558 }
1559 if ($this->lastresult) {
1560 $statusType = 'status8';
1561 }
1562
1563 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1564 }
1565}
1566
1567
1572{
1576 public $id;
1577
1578 public $entity;
1579
1583 public $ref;
1584
1585 public $tms = '';
1586 public $datec = '';
1587
1591 public $label;
1592
1593 public $jobtype;
1594 public $command;
1595 public $classesname;
1596 public $objectname;
1597 public $methodename;
1598 public $params;
1599 public $md5params;
1600 public $module_name;
1601 public $priority;
1602 public $datelastrun = '';
1603 public $datenextrun = '';
1604 public $dateend = '';
1605 public $datestart = '';
1606 public $datelastresult = '';
1607 public $lastresult = '';
1608 public $lastoutput;
1609 public $unitfrequency;
1610 public $frequency;
1611 public $processing;
1612
1616 public $status;
1617
1621 public $fk_user_author;
1622
1626 public $fk_user_mod;
1627
1628 public $note;
1629 public $note_private;
1630 public $nbrun;
1631 public $libname;
1632 public $test;
1633
1638 public function __construct()
1639 {
1640 }
1641}
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Definition security.php:604
$object ref
Definition info.php:79
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Cron Job class.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
fetch($id, $objectname='', $methodname='')
Load object in memory from the database.
info($id)
Load object information.
getLibStatut($mode=0)
Return label of status of user (active, inactive)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetchAll($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='', $processing=-1)
Load list of cron jobs in a memory array from the database @TODO Use object CronJob and not CronJobLi...
update($user=null, $notrigger=0)
Update object into database.
LibStatut($status, $mode=0, $processing=0, $lastresult=0)
Return label of a giver status.
run_jobs($userlogin)
Run a job.
reprogram_jobs($userlogin, $now)
Reprogram a job.
create($user, $notrigger=0)
Create object into database.
getTooltipContentArray($params)
getTooltipContentArray
__construct($db)
Constructor.
Crob Job line class.
__construct()
Constructor.
Class to manage Dolibarr users.
Class to manage utility methods.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...