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