dolibarr 21.0.0-alpha
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 * Copyright (C) 2023-2024 William Mead <william.mead@manchenumerique.fr>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27// Put here all includes required by your class file
28require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
29require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
30
31
35class Cronjob extends CommonObject
36{
40 public $element = 'cronjob';
41
45 public $table_element = 'cronjob';
46
50 public $picto = 'cron';
51
55 public $entity;
56
60 public $jobtype;
61
65 public $datec = '';
66
70 public $label;
71
75 public $command;
76 public $classesname;
77 public $objectname;
78 public $methodename;
79 public $params;
80 public $md5params;
81 public $module_name;
82 public $priority;
83
87 public $datelastrun = '';
88
92 public $datenextrun = '';
93
97 public $dateend = '';
98
102 public $datestart = '';
103
107 public $datelastresult = '';
108
112 public $lastresult;
113
117 public $lastoutput;
118
122 public $unitfrequency;
123
127 public $frequency;
128
132 public $status;
133
137 public $processing;
138
142 public $pid;
143
147 public $email_alert;
148
152 public $fk_user_author;
153
157 public $fk_user_mod;
158
162 public $nbrun;
163
167 public $maxrun;
168
172 public $libname;
173
177 public $test;
178
182 public $autodelete;
183
187 public $lines;
188
189
190 const STATUS_DISABLED = 0;
191 const STATUS_ENABLED = 1;
192 const STATUS_ARCHIVED = 2;
193 const MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD = 65535;
194
195
201 public function __construct(DoliDB $db)
202 {
203 $this->db = $db;
204 }
205
206
214 public function create(User $user, int $notrigger = 0)
215 {
216 global $conf, $langs;
217 $error = 0;
218
219 $now = dol_now();
220
221 // Clean parameters
222 if (isset($this->label)) {
223 $this->label = trim($this->label);
224 }
225 if (isset($this->jobtype)) {
226 $this->jobtype = trim($this->jobtype);
227 }
228 if (isset($this->command)) {
229 $this->command = trim($this->command);
230 }
231 if (isset($this->classesname)) {
232 $this->classesname = trim($this->classesname);
233 }
234 if (isset($this->objectname)) {
235 $this->objectname = trim($this->objectname);
236 }
237 if (isset($this->methodename)) {
238 $this->methodename = trim($this->methodename);
239 }
240 if (isset($this->params)) {
241 $this->params = trim($this->params);
242 }
243 if (isset($this->md5params)) {
244 $this->md5params = trim($this->md5params);
245 }
246 if (isset($this->module_name)) {
247 $this->module_name = trim($this->module_name);
248 }
249 if (isset($this->lastoutput)) {
250 $this->lastoutput = trim($this->lastoutput);
251 }
252 if (isset($this->lastresult)) {
253 $this->lastresult = trim($this->lastresult);
254 }
255 if (isset($this->unitfrequency)) {
256 $this->unitfrequency = trim($this->unitfrequency);
257 }
258 if (isset($this->frequency)) {
259 $this->frequency = (int) $this->frequency;
260 }
261 if (isset($this->status)) {
262 $this->status = (int) $this->status;
263 }
264 if (isset($this->note_private)) {
265 $this->note_private = trim($this->note_private);
266 }
267 if (isset($this->nbrun)) {
268 $this->nbrun = (int) $this->nbrun;
269 }
270 if (isset($this->maxrun)) {
271 $this->maxrun = (int) $this->maxrun;
272 }
273 if (isset($this->libname)) {
274 $this->libname = trim($this->libname);
275 }
276 if (isset($this->test)) {
277 $this->test = trim($this->test);
278 }
279
280 // Check parameters
281 // Put here code to add a control on parameters values
282 if (dol_strlen($this->datenextrun) == 0) {
283 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
284 $error++;
285 }
286 if (empty($this->label)) {
287 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
288 $error++;
289 }
290 if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
291 $this->errors[] = $langs->trans('CronErrEndDateStartDt');
292 $error++;
293 }
294 if (empty($this->unitfrequency)) {
295 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
296 $error++;
297 }
298 if (($this->jobtype == 'command') && (empty($this->command))) {
299 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
300 $error++;
301 }
302 if (($this->jobtype == 'method') && (empty($this->classesname))) {
303 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
304 $error++;
305 }
306 if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
307 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
308 $error++;
309 }
310 if (($this->jobtype == 'method') && (empty($this->objectname))) {
311 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
312 $error++;
313 }
314 if (($this->jobtype == 'function') && (empty($this->libname))) {
315 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
316 $error++;
317 }
318
319 // Insert request
320 $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
321 $sql .= "entity,";
322 $sql .= "datec,";
323 $sql .= "jobtype,";
324 $sql .= "label,";
325 $sql .= "command,";
326 $sql .= "classesname,";
327 $sql .= "objectname,";
328 $sql .= "methodename,";
329 $sql .= "params,";
330 $sql .= "md5params,";
331 $sql .= "module_name,";
332 $sql .= "priority,";
333 $sql .= "datelastrun,";
334 $sql .= "datenextrun,";
335 $sql .= "dateend,";
336 $sql .= "datestart,";
337 $sql .= "lastresult,";
338 $sql .= "datelastresult,";
339 $sql .= "lastoutput,";
340 $sql .= "unitfrequency,";
341 $sql .= "frequency,";
342 $sql .= "status,";
343 $sql .= "fk_user_author,";
344 $sql .= "fk_user_mod,";
345 $sql .= "note,";
346 $sql .= "nbrun,";
347 $sql .= "maxrun,";
348 $sql .= "libname,";
349 $sql .= "test";
350 $sql .= ") VALUES (";
351 $sql .= " ".(!isset($this->entity) ? (int) $conf->entity : (int) $this->entity).",";
352 $sql .= " '".$this->db->idate($now)."',";
353 $sql .= " ".(!isset($this->jobtype) ? 'NULL' : "'".$this->db->escape($this->jobtype)."'").",";
354 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
355 $sql .= " ".(!isset($this->command) ? 'NULL' : "'".$this->db->escape($this->command)."'").",";
356 $sql .= " ".(!isset($this->classesname) ? 'NULL' : "'".$this->db->escape($this->classesname)."'").",";
357 $sql .= " ".(!isset($this->objectname) ? 'NULL' : "'".$this->db->escape($this->objectname)."'").",";
358 $sql .= " ".(!isset($this->methodename) ? 'NULL' : "'".$this->db->escape($this->methodename)."'").",";
359 $sql .= " ".(!isset($this->params) ? 'NULL' : "'".$this->db->escape($this->params)."'").",";
360 $sql .= " ".(!isset($this->md5params) ? 'NULL' : "'".$this->db->escape($this->md5params)."'").",";
361 $sql .= " ".(!isset($this->module_name) ? 'NULL' : "'".$this->db->escape($this->module_name)."'").",";
362 $sql .= " ".(is_numeric($this->priority) ? (int) $this->priority : 50).",";
363 $sql .= " ".(!isset($this->datelastrun) || dol_strlen($this->datelastrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastrun)."'").",";
364 $sql .= " ".(!isset($this->datenextrun) || dol_strlen($this->datenextrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datenextrun)."'").",";
365 $sql .= " ".(!isset($this->dateend) || dol_strlen($this->dateend) == 0 ? 'NULL' : "'".$this->db->idate($this->dateend)."'").",";
366 $sql .= " ".(!isset($this->datestart) || dol_strlen($this->datestart) == 0 ? 'NULL' : "'".$this->db->idate($this->datestart)."'").",";
367 $sql .= " ".(!isset($this->lastresult) ? 'NULL' : "'".$this->db->escape($this->lastresult)."'").",";
368 $sql .= " ".(!isset($this->datelastresult) || dol_strlen($this->datelastresult) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastresult)."'").",";
369 $sql .= " ".(!isset($this->lastoutput) ? 'NULL' : "'".$this->db->escape($this->lastoutput)."'").",";
370 $sql .= " ".(!isset($this->unitfrequency) ? 'NULL' : "'".$this->db->escape($this->unitfrequency)."'").",";
371 $sql .= " ".(!isset($this->frequency) ? '0' : ((int) $this->frequency)).",";
372 $sql .= " ".(!isset($this->status) ? '0' : ((int) $this->status)).",";
373 $sql .= " ".($user->id ? (int) $user->id : "NULL").",";
374 $sql .= " ".($user->id ? (int) $user->id : "NULL").",";
375 $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'").",";
376 $sql .= " ".(!isset($this->nbrun) ? '0' : ((int) $this->nbrun)).",";
377 $sql .= " ".(empty($this->maxrun) ? '0' : ((int) $this->maxrun)).",";
378 $sql .= " ".(!isset($this->libname) ? 'NULL' : "'".$this->db->escape($this->libname)."'").",";
379 $sql .= " ".(!isset($this->test) ? 'NULL' : "'".$this->db->escape($this->test)."'");
380 $sql .= ")";
381
382 $this->db->begin();
383
384 dol_syslog(get_class($this)."::create", LOG_DEBUG);
385 $resql = $this->db->query($sql);
386 if (!$resql) {
387 $error++;
388 $this->errors[] = "Error ".$this->db->lasterror();
389 }
390
391 if (!$error) {
392 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
393 }
394
395 // Commit or rollback
396 if ($error) {
397 $this->db->rollback();
398 return -1 * $error;
399 } else {
400 $this->db->commit();
401 return $this->id;
402 }
403 }
404
405
414 public function fetch(int $id, string $objectname = '', string $methodname = '')
415 {
416 $sql = "SELECT";
417 $sql .= " t.rowid,";
418 $sql .= " t.entity,";
419 $sql .= " t.tms,";
420 $sql .= " t.datec,";
421 $sql .= " t.jobtype,";
422 $sql .= " t.label,";
423 $sql .= " t.command,";
424 $sql .= " t.classesname,";
425 $sql .= " t.objectname,";
426 $sql .= " t.methodename,";
427 $sql .= " t.params,";
428 $sql .= " t.md5params,";
429 $sql .= " t.module_name,";
430 $sql .= " t.priority,";
431 $sql .= " t.datelastrun,";
432 $sql .= " t.datenextrun,";
433 $sql .= " t.dateend,";
434 $sql .= " t.datestart,";
435 $sql .= " t.lastresult,";
436 $sql .= " t.datelastresult,";
437 $sql .= " t.lastoutput,";
438 $sql .= " t.unitfrequency,";
439 $sql .= " t.frequency,";
440 $sql .= " t.status,";
441 $sql .= " t.processing,";
442 $sql .= " t.pid,";
443 $sql .= " t.email_alert,";
444 $sql .= " t.fk_user_author,";
445 $sql .= " t.fk_user_mod,";
446 $sql .= " t.note as note_private,";
447 $sql .= " t.nbrun,";
448 $sql .= " t.maxrun,";
449 $sql .= " t.libname,";
450 $sql .= " t.test";
451 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
452 if ($id > 0) {
453 $sql .= " WHERE t.rowid = ".((int) $id);
454 } else {
455 $sql .= " WHERE t.entity IN(0, ".getEntity('cron').")";
456 $sql .= " AND t.objectname = '".$this->db->escape($objectname)."'";
457 $sql .= " AND t.methodename = '".$this->db->escape($methodname)."'";
458 }
459
460 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
461 $resql = $this->db->query($sql);
462 if ($resql) {
463 if ($this->db->num_rows($resql)) {
464 $obj = $this->db->fetch_object($resql);
465
466 $this->id = $obj->rowid;
467 $this->ref = $obj->rowid;
468 $this->entity = $obj->entity;
469 $this->tms = $this->db->jdate($obj->tms);
470 $this->datec = $this->db->jdate($obj->datec);
471 $this->label = $obj->label;
472 $this->jobtype = $obj->jobtype;
473 $this->command = $obj->command;
474 $this->classesname = $obj->classesname;
475 $this->objectname = $obj->objectname;
476 $this->methodename = $obj->methodename;
477 $this->params = $obj->params;
478 $this->md5params = $obj->md5params;
479 $this->module_name = $obj->module_name;
480 $this->priority = $obj->priority;
481 $this->datelastrun = $this->db->jdate($obj->datelastrun);
482 $this->datenextrun = $this->db->jdate($obj->datenextrun);
483 $this->dateend = $this->db->jdate($obj->dateend);
484 $this->datestart = $this->db->jdate($obj->datestart);
485 $this->lastresult = (string) $obj->lastresult;
486 $this->lastoutput = $obj->lastoutput;
487 $this->datelastresult = $this->db->jdate($obj->datelastresult);
488 $this->unitfrequency = $obj->unitfrequency;
489 $this->frequency = $obj->frequency;
490 $this->status = $obj->status;
491 $this->processing = $obj->processing;
492 $this->pid = $obj->pid;
493 $this->email_alert = $obj->email_alert;
494 $this->fk_user_author = $obj->fk_user_author;
495 $this->fk_user_mod = $obj->fk_user_mod;
496 $this->note_private = $obj->note_private;
497 $this->nbrun = $obj->nbrun;
498 $this->maxrun = $obj->maxrun;
499 $this->libname = $obj->libname;
500 $this->test = $obj->test;
501 }
502 $this->db->free($resql);
503
504 return 1;
505 } else {
506 $this->error = "Error ".$this->db->lasterror();
507 return -1;
508 }
509 }
510
523 public function fetchAll(string $sortorder = 'DESC', string $sortfield = 't.rowid', int $limit = 0, int $offset = 0, int $status = 1, $filter = '', int $processing = -1)
524 {
525 $this->lines = array();
526
527 $sql = "SELECT";
528 $sql .= " t.rowid,";
529 $sql .= " t.entity,";
530 $sql .= " t.tms,";
531 $sql .= " t.datec,";
532 $sql .= " t.jobtype,";
533 $sql .= " t.label,";
534 $sql .= " t.command,";
535 $sql .= " t.classesname,";
536 $sql .= " t.objectname,";
537 $sql .= " t.methodename,";
538 $sql .= " t.params,";
539 $sql .= " t.md5params,";
540 $sql .= " t.module_name,";
541 $sql .= " t.priority,";
542 $sql .= " t.datelastrun,";
543 $sql .= " t.datenextrun,";
544 $sql .= " t.dateend,";
545 $sql .= " t.datestart,";
546 $sql .= " t.lastresult,";
547 $sql .= " t.datelastresult,";
548 $sql .= " t.lastoutput,";
549 $sql .= " t.unitfrequency,";
550 $sql .= " t.frequency,";
551 $sql .= " t.status,";
552 $sql .= " t.processing,";
553 $sql .= " t.pid,";
554 $sql .= " t.email_alert,";
555 $sql .= " t.fk_user_author,";
556 $sql .= " t.fk_user_mod,";
557 $sql .= " t.note as note_private,";
558 $sql .= " t.nbrun,";
559 $sql .= " t.maxrun,";
560 $sql .= " t.libname,";
561 $sql .= " t.test";
562 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
563 $sql .= " WHERE 1 = 1";
564 if ($processing >= 0) {
565 $sql .= " AND t.processing = ".(empty($processing) ? '0' : '1');
566 }
567 if ($status >= 0 && $status < 2) {
568 $sql .= " AND t.status = ".(empty($status) ? '0' : '1');
569 } elseif ($status == 2) {
570 $sql .= " AND t.status = 2";
571 }
572
573 // Manage filter
574 if (is_array($filter)) {
575 if (count($filter) > 0) {
576 foreach ($filter as $key => $value) {
577 if ($key == 't.rowid') {
578 $sql .= " AND ".$this->db->sanitize($key)." = ".((int) $value);
579 } else {
580 $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
581 }
582 }
583 }
584
585 $filter = '';
586 }
587
588 // Manage filter
589 $errormessage = '';
590 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
591 if ($errormessage) {
592 $this->errors[] = $errormessage;
593 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
594 return -1;
595 }
596
597 $sql .= $this->db->order($sortfield, $sortorder);
598 if (!empty($limit) && !empty($offset)) {
599 $sql .= $this->db->plimit($limit + 1, $offset);
600 }
601
602 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
603 $resql = $this->db->query($sql);
604 if ($resql) {
605 $num = $this->db->num_rows($resql);
606 $i = 0;
607
608 if ($num) {
609 while ($i < $num) {
610 $obj = $this->db->fetch_object($resql);
611 $cronjob_obj = new Cronjob($this->db);
612
613 $cronjob_obj->id = $obj->rowid;
614 $cronjob_obj->ref = $obj->rowid;
615 $cronjob_obj->entity = $obj->entity;
616 $cronjob_obj->tms = $this->db->jdate($obj->tms);
617 $cronjob_obj->datec = $this->db->jdate($obj->datec);
618 $cronjob_obj->label = $obj->label;
619 $cronjob_obj->jobtype = $obj->jobtype;
620 $cronjob_obj->command = $obj->command;
621 $cronjob_obj->classesname = $obj->classesname;
622 $cronjob_obj->objectname = $obj->objectname;
623 $cronjob_obj->methodename = $obj->methodename;
624 $cronjob_obj->params = $obj->params;
625 $cronjob_obj->md5params = $obj->md5params;
626 $cronjob_obj->module_name = $obj->module_name;
627 $cronjob_obj->priority = $obj->priority;
628 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
629 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
630 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
631 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
632 $cronjob_obj->lastresult = $obj->lastresult;
633 $cronjob_obj->lastoutput = $obj->lastoutput;
634 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
635 $cronjob_obj->unitfrequency = $obj->unitfrequency;
636 $cronjob_obj->frequency = $obj->frequency;
637 $cronjob_obj->status = $obj->status;
638 $cronjob_obj->processing = $obj->processing;
639 $cronjob_obj->pid = $obj->pid;
640 $cronjob_obj->email_alert = $obj->email_alert;
641 $cronjob_obj->fk_user_author = $obj->fk_user_author;
642 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
643 $cronjob_obj->note_private = $obj->note_private;
644 $cronjob_obj->nbrun = $obj->nbrun;
645 $cronjob_obj->maxrun = $obj->maxrun;
646 $cronjob_obj->libname = $obj->libname;
647 $cronjob_obj->test = $obj->test;
648
649 $this->lines[] = $cronjob_obj;
650
651 $i++;
652 }
653 }
654 $this->db->free($resql);
655
656 return 1;
657 } else {
658 $this->error = "Error ".$this->db->lasterror();
659 return -1;
660 }
661 }
662
663
671 public function update($user = null, int $notrigger = 0)
672 {
673 global $conf, $langs;
674
675 $langs->load('cron');
676
677 $error = 0;
678
679 // Clean parameters
680 if (isset($this->label)) {
681 $this->label = trim($this->label);
682 }
683 if (isset($this->jobtype)) {
684 $this->jobtype = trim($this->jobtype);
685 }
686 if (isset($this->command)) {
687 $this->command = trim($this->command);
688 }
689 if (isset($this->classesname)) {
690 $this->classesname = trim($this->classesname);
691 }
692 if (isset($this->objectname)) {
693 $this->objectname = trim($this->objectname);
694 }
695 if (isset($this->methodename)) {
696 $this->methodename = trim($this->methodename);
697 }
698 if (isset($this->params)) {
699 $this->params = trim($this->params);
700 }
701 if (isset($this->md5params)) {
702 $this->md5params = trim($this->md5params);
703 }
704 if (isset($this->module_name)) {
705 $this->module_name = trim($this->module_name);
706 }
707 if (isset($this->priority)) {
708 $this->priority = trim($this->priority);
709 }
710 if (isset($this->lastoutput)) {
711 $this->lastoutput = trim($this->lastoutput);
712 }
713 if (isset($this->lastresult)) {
714 $this->lastresult = trim($this->lastresult);
715 }
716 if (isset($this->unitfrequency)) {
717 $this->unitfrequency = trim($this->unitfrequency);
718 }
719 if (isset($this->frequency)) {
720 $this->frequency = (int) $this->frequency;
721 }
722 if (isset($this->status)) {
723 $this->status = (int) $this->status;
724 }
725 if (isset($this->note_private)) {
726 $this->note_private = trim($this->note_private);
727 }
728 if (isset($this->nbrun)) {
729 $this->nbrun = (is_numeric($this->nbrun)) ? (int) trim((string) $this->nbrun) : 0;
730 }
731 if (isset($this->libname)) {
732 $this->libname = trim($this->libname);
733 }
734 if (isset($this->test)) {
735 $this->test = trim($this->test);
736 }
737
738 if (empty($this->maxrun)) {
739 $this->maxrun = 0;
740 }
741 if (empty($this->processing)) {
742 $this->processing = 0;
743 }
744 if (empty($this->pid)) {
745 $this->pid = null;
746 }
747 if (empty($this->email_alert)) {
748 $this->email_alert = '';
749 }
750 if (empty($this->datenextrun)) {
751 $this->datenextrun = dol_now();
752 }
753
754 // Check parameters
755 // Put here code to add a control on parameters values
756 if (dol_strlen($this->datenextrun) == 0 && $this->status == self::STATUS_ENABLED) {
757 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
758 $error++;
759 }
760 if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
761 $this->errors[] = $langs->trans('CronErrEndDateStartDt');
762 $error++;
763 }
764 if (empty($this->label)) {
765 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
766 $error++;
767 }
768 if (empty($this->unitfrequency)) {
769 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
770 $error++;
771 }
772 if (($this->jobtype == 'command') && (empty($this->command))) {
773 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
774 $error++;
775 }
776 if (($this->jobtype == 'method') && (empty($this->classesname))) {
777 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
778 $error++;
779 }
780 if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
781 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
782 $error++;
783 }
784 if (($this->jobtype == 'method') && (empty($this->objectname))) {
785 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
786 $error++;
787 }
788
789 if (($this->jobtype == 'function') && (empty($this->libname))) {
790 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
791 $error++;
792 }
793
794
795 // Update request
796 $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
797 $sql .= " entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).",";
798 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
799 $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
800 $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
801 $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
802 $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
803 $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
804 $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
805 $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
806 $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
807 $sql .= " priority=".(isset($this->priority) ? ((int) $this->priority) : "null").",";
808 $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
809 $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
810 $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
811 $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
812 $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
813 $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
814 $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
815 $sql .= " unitfrequency=".(isset($this->unitfrequency) ? "'".$this->db->escape($this->unitfrequency)."'" : "null").",";
816 $sql .= " frequency=".(isset($this->frequency) ? ((int) $this->frequency) : "null").",";
817 $sql .= " status=".(isset($this->status) ? ((int) $this->status) : "null").",";
818 $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
819 $sql .= " pid=".(isset($this->pid) ? ((int) $this->pid) : "null").",";
820 $sql .= " email_alert = ".(isset($this->email_alert) ? "'".$this->db->escape($this->email_alert)."'" : "null").",";
821 $sql .= " fk_user_mod = ".((int) $user->id).",";
822 $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
823 $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
824 $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
825 $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
826 $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
827 $sql .= " WHERE rowid=".((int) $this->id);
828
829 $this->db->begin();
830
831 dol_syslog(get_class($this)."::update", LOG_DEBUG);
832 $resql = $this->db->query($sql);
833 if (!$resql) {
834 $error++;
835 $this->errors[] = "Error ".$this->db->lasterror();
836 }
837
838 // Commit or rollback
839 if ($error) {
840 foreach ($this->errors as $errmsg) {
841 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
842 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
843 }
844 $this->db->rollback();
845 return -1 * $error;
846 } else {
847 $this->db->commit();
848 return 1;
849 }
850 }
851
852
860 public function delete(User $user, int $notrigger = 0)
861 {
862 $error = 0;
863
864 $this->db->begin();
865
866 $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
867 $sql .= " WHERE rowid=".((int) $this->id);
868
869 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
870 $resql = $this->db->query($sql);
871 if (!$resql) {
872 $error++;
873 $this->errors[] = "Error ".$this->db->lasterror();
874 }
875
876 // Commit or rollback
877 if ($error) {
878 foreach ($this->errors as $errmsg) {
879 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
880 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
881 }
882 $this->db->rollback();
883 return -1 * $error;
884 } else {
885 $this->db->commit();
886 return 1;
887 }
888 }
889
890
891
899 public function createFromClone(User $user, int $fromid)
900 {
901 global $langs;
902
903 $error = 0;
904
905 $object = new Cronjob($this->db);
906
907 $this->db->begin();
908
909 // Load source object
910 $object->fetch($fromid);
911 $object->id = 0;
912
913 // Clear fields
914 $object->status = self::STATUS_DISABLED;
915 $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
916 $object->datelastrun = null;
917 $object->lastresult = '';
918 $object->datelastresult = null;
919 $object->lastoutput = '';
920 $object->nbrun = 0;
921
922 // Create clone
923 $object->context['createfromclone'] = 'createfromclone';
924 $result = $object->create($user);
925
926 // Other options
927 if ($result < 0) {
928 $this->error = $object->error;
929 $this->errors = $object->errors;
930 $error++;
931 }
932
933 unset($object->context['createfromclone']);
934
935 // End
936 if (!$error) {
937 $this->db->commit();
938 return $object->id;
939 } else {
940 $this->db->rollback();
941 return -1;
942 }
943 }
944
945
952 public function initAsSpecimen()
953 {
954 $this->id = 0;
955 $this->ref = '';
956 $this->entity = 0;
957 $this->date_modification = dol_now();
958 $this->datec = '';
959 $this->label = '';
960 $this->jobtype = '';
961 $this->command = '';
962 $this->classesname = '';
963 $this->objectname = '';
964 $this->methodename = '';
965 $this->params = '';
966 $this->md5params = '';
967 $this->module_name = '';
968 $this->priority = '';
969 $this->datelastrun = '';
970 $this->datenextrun = '';
971 $this->dateend = '';
972 $this->datestart = '';
973 $this->datelastresult = '';
974 $this->lastoutput = '';
975 $this->lastresult = '';
976 $this->unitfrequency = '86400';
977 $this->frequency = 0;
978 $this->status = 0;
979 $this->processing = 0;
980 $this->pid = null;
981 $this->email_alert = '';
982 $this->fk_user_author = 0;
983 $this->fk_user_mod = 0;
984 $this->note_private = '';
985 $this->nbrun = 0;
986 $this->maxrun = 100;
987 $this->libname = '';
988
989 return 1;
990 }
991
992
999 public function getTooltipContentArray($params)
1000 {
1001 global $langs;
1002
1003 $langs->load('cron');
1004 $datas = [];
1005
1006 $datas['picto'] = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
1007 if (isset($this->status)) {
1008 $datas['picto'] .= ' '.$this->getLibStatut(5);
1009 }
1010 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.dol_escape_htmltag($this->ref);
1011 $datas['label'] = '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
1012 if ($this->label != $langs->trans($this->label)) {
1013 $datas['label'] .= ' <span class="opacitymedium">('.$this->label.')</span>';
1014 }
1015 if (!empty($this->params)) {
1016 $datas['params'] = '<br><b>'.$langs->trans('Parameters').':</b> '.dol_escape_htmltag($this->params);
1017 }
1018 $datas['space'] = '<br>';
1019
1020 if (!empty($this->datestart) && $this->datestart >= dol_now()) {
1021 $datas['crondtstart'] = '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
1022 }
1023 if (!empty($this->dateend)) {
1024 $datas['crondtend'] = '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
1025 }
1026 if (!empty($this->datelastrun)) {
1027 $datas['cronlastlaunch'] = '<br><b>'.$langs->trans('CronDtLastLaunch').':</b> '.dol_print_date($this->datelastrun, 'dayhour', 'tzuserrel');
1028 }
1029 if (!empty($this->datenextrun)) {
1030 $datas['crondtnextlaunch'] = '<br><b>'.$langs->trans('CronDtNextLaunch').':</b> '.dol_print_date($this->datenextrun, 'dayhour', 'tzuserrel');
1031 }
1032
1033 return $datas;
1034 }
1035
1046 public function getNomUrl(int $withpicto = 0, string $option = '', int $notooltip = 0, string $morecss = '', int $save_lastsearch_value = -1)
1047 {
1048 global $conf, $langs;
1049
1050 if (!empty($conf->dol_no_mouse_hover)) {
1051 $notooltip = 1; // Force disable tooltips
1052 }
1053
1054 $result = '';
1055
1056 $params = [
1057 'id' => $this->id,
1058 'objecttype' => $this->element,
1059 ];
1060 $classfortooltip = 'classfortooltip';
1061 $dataparams = '';
1062 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1063 $classfortooltip = 'classforajaxtooltip';
1064 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1065 $label = '';
1066 } else {
1067 $label = implode($this->getTooltipContentArray($params));
1068 }
1069
1070 $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
1071
1072 if ($option != 'nolink') {
1073 // Add param to save lastsearch_values or not
1074 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1075 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1076 $add_save_lastsearch_values = 1;
1077 }
1078 if ($add_save_lastsearch_values) {
1079 $url .= '&save_lastsearch_values=1';
1080 }
1081 }
1082
1083 $linkclose = '';
1084 if (empty($notooltip)) {
1085 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1086 $label = $langs->trans("ShowCronJob");
1087 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1088 }
1089 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1090 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1091 } else {
1092 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1093 }
1094
1095 $linkstart = '<a href="'.$url.'"';
1096 $linkstart .= $linkclose.'>';
1097 $linkend = '</a>';
1098
1099 $result .= $linkstart;
1100 if ($withpicto) {
1101 $result .= img_object(($notooltip ? '' : $label), ($this->picto ?: 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
1102 }
1103 if ($withpicto != 2) {
1104 $result .= $this->ref;
1105 }
1106 $result .= $linkend;
1107 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1108
1109 return $result;
1110 }
1111
1112
1119 public function info(int $id)
1120 {
1121 $sql = "SELECT";
1122 $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1123 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
1124 $sql .= " WHERE f.rowid = ".((int) $id);
1125
1126 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1127 $resql = $this->db->query($sql);
1128 if ($resql) {
1129 if ($this->db->num_rows($resql)) {
1130 $obj = $this->db->fetch_object($resql);
1131
1132 $this->id = $obj->rowid;
1133
1134 $this->user_modification_id = $obj->fk_user_mod;
1135 $this->user_creation_id = $obj->fk_user_author;
1136 $this->date_creation = $this->db->jdate($obj->datec);
1137 $this->date_modification = $this->db->jdate($obj->tms);
1138 }
1139 $this->db->free($resql);
1140
1141 return 1;
1142 } else {
1143 $this->error = "Error ".$this->db->lasterror();
1144 return -1;
1145 }
1146 }
1147
1148
1149 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1158 public function run_jobs(string $userlogin)
1159 {
1160 // phpcs:enable
1161 global $langs, $conf, $hookmanager;
1162
1163 $hookmanager->initHooks(array('cron'));
1164
1165 $now = dol_now();
1166 $error = 0;
1167
1168 $langs->load('cron');
1169
1170 if (empty($userlogin)) {
1171 $this->error = "User login is mandatory";
1172 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1173 return -1;
1174 }
1175
1176 // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
1177 // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
1178 if ($conf->entity != $this->entity && $this->entity > 0) {
1179 dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
1180 }
1181 $savcurrententity = $conf->entity;
1182 $conf->setEntityValues($this->db, $this->entity);
1183 dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
1184
1185 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1186 $user = new User($this->db);
1187 $result = $user->fetch(0, $userlogin);
1188 if ($result < 0) {
1189 $this->error = "User Error:".$user->error;
1190 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1191 $conf->setEntityValues($this->db, $savcurrententity);
1192 return -1;
1193 } else {
1194 if (empty($user->id)) {
1195 $this->error = "User login: ".$userlogin." does not exist";
1196 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1197 $conf->setEntityValues($this->db, $savcurrententity);
1198 return -1;
1199 }
1200 }
1201
1202 dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
1203
1204 // Increase limit of time. Works only if we are not in safe mode
1205 $ExecTimeLimit = 600;
1206 if (!empty($ExecTimeLimit)) {
1207 $err = error_reporting();
1208 error_reporting(0); // Disable all errors
1209 //error_reporting(E_ALL);
1210 @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
1211 error_reporting($err);
1212 }
1213 $MemoryLimit = 0;
1214 if (!empty($MemoryLimit)) {
1215 @ini_set('memory_limit', $MemoryLimit);
1216 }
1217
1218 // Update last run date start (to track running jobs)
1219 $this->datelastrun = $now;
1220 $this->datelastresult = null;
1221 $this->lastoutput = '';
1222 $this->lastresult = '';
1223 $this->processing = 1; // To know job was started
1224 $this->pid = function_exists('getmypid') ? getmypid() : null; // Avoid dol_getmypid to get null if the function is not available
1225 $this->nbrun += 1;
1226 $result = $this->update($user); // This include begin/commit
1227 if ($result < 0) {
1228 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1229 $conf->setEntityValues($this->db, $savcurrententity);
1230 return -1;
1231 }
1232
1233 // Run a method
1234 if ($this->jobtype == 'method') {
1235 // Deny to launch a method from a deactivated module
1236 if (!empty($this->entity) && !empty($this->module_name) && !isModEnabled(strtolower($this->module_name))) {
1237 $this->error = $langs->transnoentitiesnoconv('CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1238 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1239 $this->lastoutput = $this->error;
1240 $this->lastresult = '-1';
1241 $error++;
1242 }
1243
1244 // load classes
1245 if (!$error) {
1246 $ret = dol_include_once($this->classesname);
1247 if ($ret === false || (!class_exists($this->objectname))) {
1248 if ($ret === false) {
1249 $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
1250 } else {
1251 $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
1252 }
1253 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1254 $this->lastoutput = $this->error;
1255 $this->lastresult = '-1';
1256 $error++;
1257 }
1258 }
1259
1260 // test if method exists
1261 if (!$error) {
1262 if (!method_exists($this->objectname, $this->methodename)) {
1263 $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
1264 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1265 $this->lastoutput = $this->error;
1266 $this->lastresult = '-1';
1267 $error++;
1268 }
1269 if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
1270 $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
1271 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1272 $this->lastoutput = $this->error;
1273 $this->lastresult = '-1';
1274 $error++;
1275 }
1276 }
1277
1278 // Load langs
1279 if (!$error) {
1280 $result = $langs->load($this->module_name);
1281 $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
1282
1283 if ($result < 0) { // If technical error
1284 dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1285 $this->error = $langs->error;
1286 $this->lastoutput = $this->error;
1287 $this->lastresult = '-1';
1288 $error++;
1289 }
1290 }
1291
1292 if (!$error) {
1293 dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params."); (Note: Log for cron jobs may be into a different log file)", LOG_DEBUG);
1294
1295 // Create Object for the called module
1296 $nameofclass = (string) $this->objectname;
1297 $object = new $nameofclass($this->db);
1298 if ($this->entity > 0) {
1299 $object->entity = $this->entity; // We work on a dedicated entity
1300 }
1301
1302 $params_arr = array();
1303 if (!empty($this->params) || $this->params === '0') {
1304 $params_arr = array_map('trim', explode(",", $this->params));
1305 }
1306
1307 if (!is_array($params_arr)) {
1308 $result = call_user_func(array($object, $this->methodename), $this->params);
1309 } else {
1310 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1311 }
1312 $errmsg = '';
1313 if ($result === false || (!is_bool($result) && $result != 0)) {
1314 $langs->load("errors");
1315
1316 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1317 $errmsg .= $object->error;
1318 }
1319 if (is_array($object->errors) && count($object->errors)) {
1320 $errmsg .= (($errmsg ? ', ' : '').implode(', ', $object->errors));
1321 }
1322 if (empty($errmsg)) {
1323 $errmsg = $langs->trans('ErrorUnknown');
1324 }
1325
1326 dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
1327
1328 $this->error = $errmsg;
1329 $this->lastoutput = dol_substr((empty($object->output) ? "" : $object->output."\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
1330 $this->lastresult = is_numeric($result) ? var_export($result, true) : '-1';
1331 $error++;
1332 } else {
1333 dol_syslog(get_class($this)."::run_jobs END");
1334 $this->lastoutput = dol_substr((empty($object->output) ? "" : $object->output."\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
1335 $this->lastresult = var_export($result, true);
1336 }
1337 }
1338 }
1339
1340 if ($this->jobtype == 'function') {
1341 //load lib
1342 $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
1343 $ret = dol_include_once($libpath);
1344 if ($ret === false) {
1345 $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
1346 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1347 $conf->setEntityValues($this->db, $savcurrententity);
1348 return -1;
1349 }
1350
1351 // Load langs
1352 $result = $langs->load($this->module_name);
1353 $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
1354 if ($result < 0) { // If technical error
1355 dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1356 $conf->setEntityValues($this->db, $savcurrententity);
1357 return -1;
1358 }
1359
1360 dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
1361 $params_arr = explode(", ", $this->params);
1362 if (!is_array($params_arr)) {
1363 $result = call_user_func($this->methodename, $this->params);
1364 } else {
1365 $result = call_user_func_array($this->methodename, $params_arr);
1366 }
1367
1368 if ($result === false || (!is_bool($result) && $result != 0)) {
1369 $langs->load("errors");
1370 dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
1371 $this->error = $langs->trans('ErrorUnknown');
1372 $this->lastoutput = $this->error;
1373 $this->lastresult = is_numeric($result) ? var_export($result, true) : '-1';
1374 $error++;
1375 } else {
1376 $this->lastoutput = var_export($result, true);
1377 $this->lastresult = var_export($result, true); // Return code
1378 }
1379 }
1380
1381 // Run a command line
1382 if ($this->jobtype == 'command') {
1383 global $dolibarr_cron_allow_cli;
1384
1385 if (empty($dolibarr_cron_allow_cli)) {
1386 $langs->load("errors");
1387 $this->error = $langs->trans("FailedToExecutCommandJob");
1388 $this->lastoutput = '';
1389 $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
1390 } else {
1391 $outputdir = $conf->cron->dir_temp;
1392 if (empty($outputdir)) {
1393 $outputdir = $conf->cronjob->dir_temp;
1394 }
1395
1396 if (!empty($outputdir)) {
1397 dol_mkdir($outputdir);
1398 $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
1399
1400 // Execute a CLI
1401 include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1402 $utils = new Utils($this->db);
1403 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1404
1405 $this->error = $arrayresult['error'];
1406 $this->lastoutput = $arrayresult['output'];
1407 $this->lastresult = (string) $arrayresult['result'];
1408 }
1409 }
1410 }
1411
1412 dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
1413
1414 $this->datelastresult = dol_now();
1415 $this->processing = 0;
1416 $this->pid = null;
1417 $result = $this->update($user); // This include begin/commit
1418 if ($result < 0) {
1419 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1420 $conf->setEntityValues($this->db, $savcurrententity);
1421 return -1;
1422 }
1423
1424 $conf->setEntityValues($this->db, $savcurrententity);
1425
1426 if ($error && !empty($this->email_alert)) {
1427 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
1428 $subject = $langs->trans("ErrorInBatch", $this->label);
1429 $msg = $langs->trans("ErrorInBatch", $this->label);
1430 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
1431 $cmailfile = new CMailFile($subject, $this->email_alert, $from, $msg);
1432 $result = $cmailfile->sendfile(); // Do not test result
1433 }
1434
1435 return $error ? -1 : 1;
1436 }
1437
1438
1439 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1447 public function reprogram_jobs(string $userlogin, int $now)
1448 {
1449 // phpcs:enable
1450 dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1451
1452 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1453 $user = new User($this->db);
1454 $result = $user->fetch(0, $userlogin);
1455 if ($result < 0) {
1456 $this->error = "User Error : ".$user->error;
1457 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1458 return -1;
1459 } else {
1460 if (empty($user->id)) {
1461 $this->error = " User user login:".$userlogin." do not exists";
1462 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1463 return -1;
1464 }
1465 }
1466
1467 dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1468
1469 if (empty($this->datenextrun)) {
1470 if (empty($this->datestart)) {
1471 if (!is_numeric($this->frequency) || (int) $this->unitfrequency == 2678400) {
1472 $this->datenextrun = dol_time_plus_duree($now, $this->frequency, 'm');
1473 } else {
1474 $this->datenextrun = $now + ($this->frequency * (int) $this->unitfrequency);
1475 }
1476 } else {
1477 if (!is_numeric($this->frequency) || (int) $this->unitfrequency == 2678400) {
1478 $this->datenextrun = dol_time_plus_duree($this->datestart, $this->frequency, 'm');
1479 } else {
1480 $this->datenextrun = $this->datestart + ($this->frequency * (int) $this->unitfrequency);
1481 }
1482 }
1483 }
1484
1485 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1486 // Loop until date is after future
1487 while ($this->datenextrun < $now) {
1488 if (!is_numeric($this->unitfrequency) || (int) $this->unitfrequency == 2678400 || (int) $this->unitfrequency <= 0) {
1489 $this->datenextrun = dol_time_plus_duree($this->datenextrun, $this->frequency, 'm');
1490 } else {
1491 $this->datenextrun += ($this->frequency * (int) $this->unitfrequency);
1492 }
1493 }
1494 } else {
1495 dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
1496 }
1497
1498
1499 // Archive job
1500 if ($this->autodelete == 2) {
1501 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1502 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1503 $this->status = self::STATUS_ARCHIVED;
1504 dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
1505 }
1506 }
1507
1508 $result = $this->update($user);
1509 if ($result < 0) {
1510 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1511 return -1;
1512 }
1513
1514 return 1;
1515 }
1516
1523 public function getLibStatut(int $mode = 0)
1524 {
1525 return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1526 }
1527
1528 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1538 public function LibStatut(int $status, int $mode = 0, int $processing = 0, string $lastResult = '')
1539 {
1540 // phpcs:enable
1541 $this->labelStatus = array(); // Force reset o array because label depends on other fields
1542 $this->labelStatusShort = array();
1543
1544 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1545 global $langs;
1546 $langs->load('users');
1547
1548 $moreText = '';
1549 if ($processing) {
1550 $moreText = ' ('.$langs->trans("Running").')';
1551 } elseif ($lastResult) {
1552 $moreText .= ' ('.$langs->trans("Error").')';
1553 }
1554
1555 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moreText;
1556 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moreText;
1557 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
1558 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
1559 }
1560
1561 $statusType = 'status4';
1562 if ($status == 1 && $processing) {
1563 $statusType = 'status1';
1564 }
1565 if ($status == 0) {
1566 $statusType = 'status5';
1567 }
1568 if ($this->lastresult) {
1569 $statusType = 'status8';
1570 }
1571
1572 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1573 }
1574}
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
$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.
fetchAll(string $sortorder='DESC', string $sortfield='t.rowid', int $limit=0, int $offset=0, int $status=1, $filter='', int $processing=-1)
Load list of cron jobs in a memory array from the database.
createFromClone(User $user, int $fromid)
Load an object from its id and create a new one in database.
reprogram_jobs(string $userlogin, int $now)
Reprogram a job.
run_jobs(string $userlogin)
Run a job.
LibStatut(int $status, int $mode=0, int $processing=0, string $lastResult='')
Return label of a giver status.
create(User $user, int $notrigger=0)
Create object into database.
getLibStatut(int $mode=0)
Return label of status of user (active, inactive)
__construct(DoliDB $db)
Constructor.
getNomUrl(int $withpicto=0, string $option='', int $notooltip=0, string $morecss='', int $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
info(int $id)
Load object information.
getTooltipContentArray($params)
getTooltipContentArray
update($user=null, int $notrigger=0)
Update object into database.
fetch(int $id, string $objectname='', string $methodname='')
Load object in memory from the database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
Class to manage utility methods.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:162
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=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
getDolGlobalString($key, $default='')
Return a 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...