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