dolibarr 24.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-2025 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024-2026 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 $jobtype;
56
60 public $datec = '';
61
65 public $label;
66
70 public $command;
74 public $classesname;
78 public $objectname;
82 public $methodename;
86 public $params;
90 public $md5params;
94 public $module_name;
98 public $priority;
99
103 public $datelastrun = '';
104
108 public $datenextrun = '';
109
113 public $dateend = '';
114
118 public $datestart = '';
119
123 public $datelastresult = '';
124
128 public $lastresult;
129
133 public $lastoutput;
134
138 public $unitfrequency;
139
143 public $frequency;
144
148 public $status;
149
153 public $processing;
154
158 public $pid;
159
163 public $email_alert;
164
168 public $fk_user_author;
169
173 public $fk_user_mod;
174
178 public $nbrun;
179
183 public $maxrun;
184
188 public $libname;
189
193 public $test;
194
198 public $autodelete;
199
203 public $lines;
204
205
206 const STATUS_DISABLED = 0;
207 const STATUS_ENABLED = 1;
208 const STATUS_ARCHIVED = 2;
209 const MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD = 65535;
210
211
255 // BEGIN MODULEBUILDER PROPERTIES
260 public $fields = array(
261 "rowid" => array("type" => "integer", "label" => "Ref", "enabled" => "1", 'position' => 10, 'notnull' => 1, "visible" => "1",),
262 "label" => array("type" => "varchar(255)", "label" => "Label", "enabled" => "1", 'position' => 30, 'notnull' => 1, "visible" => "1", "alwayseditable" => "1", "css" => "minwidth300", "cssview" => "wordbreak", "csslist" => "tdoverflowmax150",),
263 "params" => array("type" => "text", "label" => "Params", "enabled" => "1", 'position' => 55, 'notnull' => 0, "visible" => "0",),
264 "md5params" => array("type" => "varchar(32)", "label" => "Md5params", "enabled" => "1", 'position' => 60, 'notnull' => 0, "visible" => "0",),
265 "module_name" => array("type" => "varchar(255)", "label" => "Module", "enabled" => "1", 'position' => 65, 'notnull' => 0, "visible" => "1",),
266 "jobtype" => array("type" => "varchar(10)", "label" => "Type", "enabled" => "1", 'position' => 68, 'notnull' => 1, "visible" => "1",),
267 //"command" => array("type" => "varchar(255)", "label" => "Command", "enabled" => "1", 'position' => 35, 'notnull' => 0, "visible" => "-1",),
268 //"classesname" => array("type" => "varchar(255)", "label" => "Classesname", "enabled" => "1", 'position' => 40, 'notnull' => 0, "visible" => "-1",),
269 //"objectname" => array("type" => "varchar(255)", "label" => "Objectname", "enabled" => "1", 'position' => 45, 'notnull' => 0, "visible" => "-1",),
270 //"methodename" => array("type" => "varchar(255)", "label" => "Methodename", "enabled" => "1", 'position' => 50, 'notnull' => 0, "visible" => "-1",),
271 "priority" => array("type" => "integer", "label" => "Priority", "enabled" => "1", 'position' => 70, 'notnull' => 0, "visible" => "0",),
272 "datelastrun" => array("type" => "datetime", "label" => "Datelastrun", "enabled" => "1", 'position' => 75, 'notnull' => 0, "visible" => "-1",),
273 "datenextrun" => array("type" => "datetime", "label" => "Datenextrun", "enabled" => "1", 'position' => 80, 'notnull' => 0, "visible" => "1",),
274 //"datestart" => array("type" => "datetime", "label" => "Datestart", "enabled" => "1", 'position' => 85, 'notnull' => 0, "visible" => "0",),
275 //"dateend" => array("type" => "datetime", "label" => "Dateend", "enabled" => "1", 'position' => 90, 'notnull' => 0, "visible" => "0",),
276 "datelastresult" => array("type" => "datetime", "label" => "Datelastresult", "enabled" => "1", 'position' => 95, 'notnull' => 0, "visible" => "0",),
277 "lastresult" => array("type" => "text", "label" => "Lastresult", "enabled" => "1", 'position' => 100, 'notnull' => 0, "visible" => "1",),
278 "lastoutput" => array("type" => "text", "label" => "Lastoutput", "enabled" => "1", 'position' => 105, 'notnull' => 0, "visible" => "-1",),
279 //"frequency" => array("type" => "integer", "label" => "Frequency", "enabled" => "1", 'position' => 115, 'notnull' => 1, "visible" => "1",),
280 //"unitfrequency" => array("type" => "varchar(255)", "label" => "Unitfrequency", "enabled" => "1", 'position' => 116, 'notnull' => 1, "visible" => "1",),
281 "nbrun" => array("type" => "integer", "label" => "Nbrun", "enabled" => "1", 'position' => 120, 'notnull' => 0, "visible" => "1",),
282 "fk_user_author" => array("type" => "integer", "label" => "UserCreation", "picto" => "user", "enabled" => "1", 'position' => 130, 'notnull' => 0, "visible" => "-1", "css" => "maxwidth500 widthcentpercentminusxx",),
283 "fk_user_mod" => array("type" => "integer", "label" => "UserModification", "picto" => "user", "enabled" => "1", 'position' => 135, 'notnull' => 0, "visible" => "-1", "css" => "maxwidth500 widthcentpercentminusxx",),
284 "note" => array("type" => "text", "label" => "Note", "enabled" => "1", 'position' => 140, 'notnull' => 0, "visible" => "0",),
285 "libname" => array("type" => "varchar(255)", "label" => "Libname", "enabled" => "1", 'position' => 145, 'notnull' => 0, "visible" => "0",),
286 "maxrun" => array("type" => "integer", "label" => "Maxrun", "enabled" => "1", 'position' => 155, 'notnull' => 1, "visible" => "0",),
287 "autodelete" => array("type" => "integer", "label" => "Autodelete", "enabled" => "1", 'position' => 160, 'notnull' => 0, "visible" => "0",),
288 "fk_mailing" => array("type" => "integer", "label" => "Fkmailing", "enabled" => "1", 'position' => 165, 'notnull' => 0, "visible" => "0", "css" => "maxwidth500 widthcentpercentminusxx",),
289 "test" => array("type" => "varchar(255)", "label" => "Test", "enabled" => "1", 'position' => 170, 'notnull' => 0, "visible" => "0",),
290 "processing" => array("type" => "integer", "label" => "Processing", "enabled" => "1", 'position' => 175, 'notnull' => 1, "visible" => "0",),
291 "email_alert" => array("type" => "varchar(128)", "label" => "Emailalert", "enabled" => "1", 'position' => 180, 'notnull' => 0, "visible" => "0",),
292 "pid" => array("type" => "integer", "label" => "Pid", "enabled" => "1", 'position' => 185, 'notnull' => 0, "visible" => "0",),
293 "tms" => array("type" => "timestamp", "label" => "DateModification", "enabled" => "1", 'position' => 300, 'notnull' => 1, "visible" => "-1",),
294 "datec" => array("type" => "datetime", "label" => "DateCreation", "enabled" => "1", 'position' => 301, 'notnull' => 0, "visible" => "-1",),
295 "status" => array("type" => "integer", "label" => "Status", "enabled" => "1", 'position' => 500, 'notnull' => 1, "visible" => "1",),
296 );
297 // END MODULEBUILDER PROPERTIES
298
299
305 public function __construct(DoliDB $db)
306 {
307 $this->db = $db;
308 }
309
310
318 public function create(User $user, int $notrigger = 0)
319 {
320 global $conf, $langs;
321 $error = 0;
322
323 $now = dol_now();
324
325 // Clean parameters
326 if (isset($this->label)) {
327 $this->label = trim($this->label);
328 }
329 if (isset($this->jobtype)) {
330 $this->jobtype = trim($this->jobtype);
331 }
332 if (isset($this->command)) {
333 $this->command = trim($this->command);
334 }
335 if (isset($this->classesname)) {
336 $this->classesname = trim($this->classesname);
337 }
338 if (isset($this->objectname)) {
339 $this->objectname = trim($this->objectname);
340 }
341 if (isset($this->methodename)) {
342 $this->methodename = trim($this->methodename);
343 }
344 if (isset($this->params)) {
345 $this->params = trim($this->params);
346 }
347 if (isset($this->md5params)) {
348 $this->md5params = trim($this->md5params);
349 }
350 if (isset($this->module_name)) {
351 $this->module_name = trim($this->module_name);
352 }
353 if (isset($this->lastoutput)) {
354 $this->lastoutput = trim($this->lastoutput);
355 $this->lastoutput = dol_substr($this->lastoutput, 0, self::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
356 }
357 if (isset($this->lastresult)) {
358 $this->lastresult = trim($this->lastresult);
359 }
360 if (isset($this->unitfrequency)) {
361 $this->unitfrequency = trim($this->unitfrequency);
362 }
363 if (isset($this->frequency)) {
364 $this->frequency = (int) $this->frequency;
365 }
366 if (isset($this->status)) {
367 $this->status = (int) $this->status;
368 }
369 if (isset($this->note_private)) {
370 $this->note_private = trim($this->note_private);
371 }
372 if (isset($this->nbrun)) {
373 $this->nbrun = (int) $this->nbrun;
374 }
375 if (isset($this->maxrun)) {
376 $this->maxrun = (int) $this->maxrun;
377 }
378 if (isset($this->libname)) {
379 $this->libname = trim($this->libname);
380 }
381 if (isset($this->test)) {
382 $this->test = trim($this->test);
383 }
384
385 // Check parameters
386 // Put here code to add a control on parameters values
387 if (dol_strlen($this->datenextrun) == 0) {
388 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
389 $error++;
390 }
391 if (empty($this->label)) {
392 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
393 $error++;
394 }
395 if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
396 $this->errors[] = $langs->trans('CronErrEndDateStartDt');
397 $error++;
398 }
399 if (empty($this->unitfrequency)) {
400 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
401 $error++;
402 }
403 if (($this->jobtype == 'command') && (empty($this->command))) {
404 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
405 $error++;
406 }
407 if (($this->jobtype == 'method') && (empty($this->classesname))) {
408 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
409 $error++;
410 }
411 if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
412 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
413 $error++;
414 }
415 if (($this->jobtype == 'method') && (empty($this->objectname))) {
416 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
417 $error++;
418 }
419 if (($this->jobtype == 'function') && (empty($this->libname))) {
420 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
421 $error++;
422 }
423
424 // Insert request
425 $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
426 $sql .= "entity,";
427 $sql .= "datec,";
428 $sql .= "jobtype,";
429 $sql .= "label,";
430 $sql .= "command,";
431 $sql .= "classesname,";
432 $sql .= "objectname,";
433 $sql .= "methodename,";
434 $sql .= "params,";
435 $sql .= "md5params,";
436 $sql .= "module_name,";
437 $sql .= "priority,";
438 $sql .= "datelastrun,";
439 $sql .= "datenextrun,";
440 $sql .= "dateend,";
441 $sql .= "datestart,";
442 $sql .= "lastresult,";
443 $sql .= "datelastresult,";
444 $sql .= "lastoutput,";
445 $sql .= "unitfrequency,";
446 $sql .= "frequency,";
447 $sql .= "status,";
448 $sql .= "fk_user_author,";
449 $sql .= "fk_user_mod,";
450 $sql .= "note,";
451 $sql .= "nbrun,";
452 $sql .= "maxrun,";
453 $sql .= "libname,";
454 $sql .= "test";
455 $sql .= ") VALUES (";
456 $sql .= " ".(!isset($this->entity) ? (int) $conf->entity : (int) $this->entity).",";
457 $sql .= " '".$this->db->idate($now)."',";
458 $sql .= " ".(!isset($this->jobtype) ? 'NULL' : "'".$this->db->escape($this->jobtype)."'").",";
459 $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
460 $sql .= " ".(!isset($this->command) ? 'NULL' : "'".$this->db->escape($this->command)."'").",";
461 $sql .= " ".(!isset($this->classesname) ? 'NULL' : "'".$this->db->escape($this->classesname)."'").",";
462 $sql .= " ".(!isset($this->objectname) ? 'NULL' : "'".$this->db->escape($this->objectname)."'").",";
463 $sql .= " ".(!isset($this->methodename) ? 'NULL' : "'".$this->db->escape($this->methodename)."'").",";
464 $sql .= " ".(!isset($this->params) ? 'NULL' : "'".$this->db->escape($this->params)."'").",";
465 $sql .= " ".(!isset($this->md5params) ? 'NULL' : "'".$this->db->escape($this->md5params)."'").",";
466 $sql .= " ".(!isset($this->module_name) ? 'NULL' : "'".$this->db->escape($this->module_name)."'").",";
467 $sql .= " ".(is_numeric($this->priority) ? (int) $this->priority : 50).",";
468 $sql .= " ".(!isset($this->datelastrun) || dol_strlen($this->datelastrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastrun)."'").",";
469 $sql .= " ".(!isset($this->datenextrun) || dol_strlen($this->datenextrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datenextrun)."'").",";
470 $sql .= " ".(!isset($this->dateend) || dol_strlen($this->dateend) == 0 ? 'NULL' : "'".$this->db->idate($this->dateend)."'").",";
471 $sql .= " ".(!isset($this->datestart) || dol_strlen($this->datestart) == 0 ? 'NULL' : "'".$this->db->idate($this->datestart)."'").",";
472 $sql .= " ".(!isset($this->lastresult) ? 'NULL' : "'".$this->db->escape($this->lastresult)."'").",";
473 $sql .= " ".(!isset($this->datelastresult) || dol_strlen($this->datelastresult) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastresult)."'").",";
474 $sql .= " ".(!isset($this->lastoutput) ? 'NULL' : "'".$this->db->escape($this->lastoutput)."'").",";
475 $sql .= " ".(!isset($this->unitfrequency) ? 'NULL' : "'".$this->db->escape($this->unitfrequency)."'").",";
476 $sql .= " ".(!isset($this->frequency) ? '0' : ((int) $this->frequency)).",";
477 $sql .= " ".(!isset($this->status) ? '0' : ((int) $this->status)).",";
478 $sql .= " ".($user->id ? (int) $user->id : "NULL").",";
479 $sql .= " ".($user->id ? (int) $user->id : "NULL").",";
480 $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'").",";
481 $sql .= " ".(!isset($this->nbrun) ? '0' : ((int) $this->nbrun)).",";
482 $sql .= " ".(empty($this->maxrun) ? '0' : ((int) $this->maxrun)).",";
483 $sql .= " ".(!isset($this->libname) ? 'NULL' : "'".$this->db->escape($this->libname)."'").",";
484 $sql .= " ".(!isset($this->test) ? 'NULL' : "'".$this->db->escape($this->test)."'");
485 $sql .= ")";
486
487 $this->db->begin();
488
489 dol_syslog(get_class($this)."::create", LOG_DEBUG);
490 $resql = $this->db->query($sql);
491 if (!$resql) {
492 $error++;
493 $this->errors[] = "Error ".$this->db->lasterror();
494 }
495
496 if (!$error) {
497 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
498 }
499
500 // Commit or rollback
501 if ($error) {
502 $this->db->rollback();
503 return -1 * $error;
504 } else {
505 $this->db->commit();
506 return $this->id;
507 }
508 }
509
510
520 public function fetch(int $id, string $objectname = '', string $methodname = '', string $label = '')
521 {
522 $sql = "SELECT";
523 $sql .= " t.rowid,";
524 $sql .= " t.entity,";
525 $sql .= " t.tms,";
526 $sql .= " t.datec,";
527 $sql .= " t.jobtype,";
528 $sql .= " t.label,";
529 $sql .= " t.command,";
530 $sql .= " t.classesname,";
531 $sql .= " t.objectname,";
532 $sql .= " t.methodename,";
533 $sql .= " t.params,";
534 $sql .= " t.md5params,";
535 $sql .= " t.module_name,";
536 $sql .= " t.priority,";
537 $sql .= " t.datelastrun,";
538 $sql .= " t.datenextrun,";
539 $sql .= " t.dateend,";
540 $sql .= " t.datestart,";
541 $sql .= " t.lastresult,";
542 $sql .= " t.datelastresult,";
543 $sql .= " t.lastoutput,";
544 $sql .= " t.unitfrequency,";
545 $sql .= " t.frequency,";
546 $sql .= " t.status,";
547 $sql .= " t.processing,";
548 $sql .= " t.pid,";
549 $sql .= " t.email_alert,";
550 $sql .= " t.fk_user_author,";
551 $sql .= " t.fk_user_mod,";
552 $sql .= " t.note as note_private,";
553 $sql .= " t.nbrun,";
554 $sql .= " t.maxrun,";
555 $sql .= " t.libname,";
556 $sql .= " t.test";
557 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
558 if ($id > 0) {
559 $sql .= " WHERE t.rowid = ".((int) $id);
560 } elseif ($label) {
561 $sql .= " WHERE t.entity IN(0, ".getEntity('cron').")";
562 $sql .= " AND t.label = '".$this->db->escape($label)."'";
563 } else {
564 $sql .= " WHERE t.entity IN(0, ".getEntity('cron').")";
565 $sql .= " AND t.objectname = '".$this->db->escape($objectname)."'";
566 $sql .= " AND t.methodename = '".$this->db->escape($methodname)."'";
567 }
568
569 $resql = $this->db->query($sql);
570 if ($resql) {
571 if ($this->db->num_rows($resql)) {
572 $obj = $this->db->fetch_object($resql);
573
574 $this->id = $obj->rowid;
575 $this->ref = $obj->rowid;
576 $this->entity = $obj->entity;
577 $this->tms = $this->db->jdate($obj->tms);
578 $this->date_modification = $this->db->jdate($obj->tms);
579 $this->datec = $this->db->jdate($obj->datec);
580 $this->label = $obj->label;
581 $this->jobtype = $obj->jobtype;
582 $this->command = $obj->command;
583 $this->classesname = $obj->classesname;
584 $this->objectname = $obj->objectname;
585 $this->methodename = $obj->methodename;
586 $this->params = $obj->params;
587 $this->md5params = $obj->md5params;
588 $this->module_name = $obj->module_name;
589 $this->priority = $obj->priority;
590 $this->datelastrun = $this->db->jdate($obj->datelastrun);
591 $this->datenextrun = $this->db->jdate($obj->datenextrun);
592 $this->dateend = $this->db->jdate($obj->dateend);
593 $this->datestart = $this->db->jdate($obj->datestart);
594 $this->lastresult = (string) $obj->lastresult;
595 $this->lastoutput = $obj->lastoutput;
596 $this->datelastresult = $this->db->jdate($obj->datelastresult);
597 $this->unitfrequency = $obj->unitfrequency;
598 $this->frequency = $obj->frequency;
599 $this->status = $obj->status;
600 $this->processing = $obj->processing;
601 $this->pid = $obj->pid;
602 $this->email_alert = $obj->email_alert;
603 $this->fk_user_author = $obj->fk_user_author;
604 $this->fk_user_mod = $obj->fk_user_mod;
605 $this->note_private = $obj->note_private;
606 $this->nbrun = $obj->nbrun;
607 $this->maxrun = $obj->maxrun;
608 $this->libname = $obj->libname;
609 $this->test = $obj->test;
610 }
611 $this->db->free($resql);
612
613 return 1;
614 } else {
615 $this->error = "Error ".$this->db->lasterror();
616 return -1;
617 }
618 }
619
632 public function fetchAll(string $sortorder = 'DESC', string $sortfield = 't.rowid', int $limit = 0, int $offset = 0, int $status = 1, $filter = '', int $processing = -1)
633 {
634 $this->lines = array();
635
636 $sql = "SELECT";
637 $sql .= " t.rowid,";
638 $sql .= " t.entity,";
639 $sql .= " t.tms,";
640 $sql .= " t.datec,";
641 $sql .= " t.jobtype,";
642 $sql .= " t.label,";
643 $sql .= " t.command,";
644 $sql .= " t.classesname,";
645 $sql .= " t.objectname,";
646 $sql .= " t.methodename,";
647 $sql .= " t.params,";
648 $sql .= " t.md5params,";
649 $sql .= " t.module_name,";
650 $sql .= " t.priority,";
651 $sql .= " t.datelastrun,";
652 $sql .= " t.datenextrun,";
653 $sql .= " t.dateend,";
654 $sql .= " t.datestart,";
655 $sql .= " t.lastresult,";
656 $sql .= " t.datelastresult,";
657 $sql .= " t.lastoutput,";
658 $sql .= " t.unitfrequency,";
659 $sql .= " t.frequency,";
660 $sql .= " t.status,";
661 $sql .= " t.processing,";
662 $sql .= " t.pid,";
663 $sql .= " t.email_alert,";
664 $sql .= " t.fk_user_author,";
665 $sql .= " t.fk_user_mod,";
666 $sql .= " t.note as note_private,";
667 $sql .= " t.nbrun,";
668 $sql .= " t.maxrun,";
669 $sql .= " t.libname,";
670 $sql .= " t.test";
671 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
672 $sql .= " WHERE 1 = 1";
673 if ($processing >= 0) {
674 $sql .= " AND t.processing = ".(empty($processing) ? '0' : '1');
675 }
676 if ($status >= 0 && $status < 2) {
677 $sql .= " AND t.status = ".(empty($status) ? '0' : '1');
678 } elseif ($status == 2) {
679 $sql .= " AND t.status = 2";
680 }
681
682 // Manage filter
683 if (is_array($filter)) {
684 if (count($filter) > 0) {
685 foreach ($filter as $key => $value) {
686 if ($key == 't.rowid') {
687 $sql .= " AND ".$this->db->sanitize($key)." = ".((int) $value);
688 } else {
689 $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
690 }
691 }
692 }
693
694 $filter = '';
695 }
696
697 // Manage filter
698 $errormessage = '';
699 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
700 if ($errormessage) {
701 $this->errors[] = $errormessage;
702 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
703 return -1;
704 }
705
706 $sql .= $this->db->order($sortfield, $sortorder);
707 if (!empty($limit) && !empty($offset)) {
708 $sql .= $this->db->plimit($limit + 1, $offset);
709 }
710
711 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
712 $resql = $this->db->query($sql);
713 if ($resql) {
714 $num = $this->db->num_rows($resql);
715 $i = 0;
716
717 if ($num) {
718 while ($i < $num) {
719 $obj = $this->db->fetch_object($resql);
720 $cronjob_obj = new Cronjob($this->db);
721
722 $cronjob_obj->id = $obj->rowid;
723 $cronjob_obj->ref = $obj->rowid;
724 $cronjob_obj->entity = $obj->entity;
725 $cronjob_obj->tms = $this->db->jdate($obj->tms);
726 $cronjob_obj->datec = $this->db->jdate($obj->datec);
727 $cronjob_obj->label = $obj->label;
728 $cronjob_obj->jobtype = $obj->jobtype;
729 $cronjob_obj->command = $obj->command;
730 $cronjob_obj->classesname = $obj->classesname;
731 $cronjob_obj->objectname = $obj->objectname;
732 $cronjob_obj->methodename = $obj->methodename;
733 $cronjob_obj->params = $obj->params;
734 $cronjob_obj->md5params = $obj->md5params;
735 $cronjob_obj->module_name = $obj->module_name;
736 $cronjob_obj->priority = $obj->priority;
737 $cronjob_obj->datelastrun = $this->db->jdate($obj->datelastrun);
738 $cronjob_obj->datenextrun = $this->db->jdate($obj->datenextrun);
739 $cronjob_obj->dateend = $this->db->jdate($obj->dateend);
740 $cronjob_obj->datestart = $this->db->jdate($obj->datestart);
741 $cronjob_obj->lastresult = $obj->lastresult;
742 $cronjob_obj->lastoutput = $obj->lastoutput;
743 $cronjob_obj->datelastresult = $this->db->jdate($obj->datelastresult);
744 $cronjob_obj->unitfrequency = $obj->unitfrequency;
745 $cronjob_obj->frequency = $obj->frequency;
746 $cronjob_obj->status = $obj->status;
747 $cronjob_obj->processing = $obj->processing;
748 $cronjob_obj->pid = $obj->pid;
749 $cronjob_obj->email_alert = $obj->email_alert;
750 $cronjob_obj->fk_user_author = $obj->fk_user_author;
751 $cronjob_obj->fk_user_mod = $obj->fk_user_mod;
752 $cronjob_obj->note_private = $obj->note_private;
753 $cronjob_obj->nbrun = $obj->nbrun;
754 $cronjob_obj->maxrun = $obj->maxrun;
755 $cronjob_obj->libname = $obj->libname;
756 $cronjob_obj->test = $obj->test;
757
758 $this->lines[] = $cronjob_obj;
759
760 $i++;
761 }
762 }
763 $this->db->free($resql);
764
765 return 1;
766 } else {
767 $this->error = "Error ".$this->db->lasterror();
768 return -1;
769 }
770 }
771
772
780 public function update($user = null, int $notrigger = 0)
781 {
782 global $conf, $langs;
783
784 $langs->load('cron');
785
786 $error = 0;
787
788 // Clean parameters
789 if (isset($this->label)) {
790 $this->label = trim($this->label);
791 }
792 if (isset($this->jobtype)) {
793 $this->jobtype = trim($this->jobtype);
794 }
795 if (isset($this->command)) {
796 $this->command = trim($this->command);
797 }
798 if (isset($this->classesname)) {
799 $this->classesname = trim($this->classesname);
800 }
801 if (isset($this->objectname)) {
802 $this->objectname = trim($this->objectname);
803 }
804 if (isset($this->methodename)) {
805 $this->methodename = trim($this->methodename);
806 }
807 if (isset($this->params)) {
808 $this->params = trim($this->params);
809 }
810 if (isset($this->md5params)) {
811 $this->md5params = trim($this->md5params);
812 }
813 if (isset($this->module_name)) {
814 $this->module_name = trim($this->module_name);
815 }
816 if (isset($this->priority)) {
817 $this->priority = trim($this->priority);
818 }
819 if (isset($this->lastoutput)) {
820 $this->lastoutput = trim($this->lastoutput);
821 $this->lastoutput = dol_substr($this->lastoutput, 0, self::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
822 }
823 if (isset($this->lastresult)) {
824 $this->lastresult = trim($this->lastresult);
825 }
826 if (isset($this->unitfrequency)) {
827 $this->unitfrequency = trim($this->unitfrequency);
828 }
829 if (isset($this->frequency)) {
830 $this->frequency = (int) $this->frequency;
831 }
832 if (isset($this->status)) {
833 $this->status = (int) $this->status;
834 }
835 if (isset($this->note_private)) {
836 $this->note_private = trim($this->note_private);
837 }
838 if (isset($this->nbrun)) {
839 $this->nbrun = (is_numeric($this->nbrun)) ? (int) trim((string) $this->nbrun) : 0;
840 }
841 if (isset($this->libname)) {
842 $this->libname = trim($this->libname);
843 }
844 if (isset($this->test)) {
845 $this->test = trim($this->test);
846 }
847
848 if (empty($this->maxrun)) {
849 $this->maxrun = 0;
850 }
851 if (empty($this->processing)) {
852 $this->processing = 0;
853 }
854 if (empty($this->pid)) {
855 $this->pid = null;
856 }
857 if (empty($this->email_alert)) {
858 $this->email_alert = '';
859 }
860 if (empty($this->datenextrun)) {
861 $this->datenextrun = dol_now();
862 }
863
864 // Check parameters
865 // Put here code to add a control on parameters values
866 if (dol_strlen($this->datenextrun) == 0 && $this->status == self::STATUS_ENABLED) {
867 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
868 $error++;
869 }
870 if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
871 $this->errors[] = $langs->trans('CronErrEndDateStartDt');
872 $error++;
873 }
874 if (empty($this->label)) {
875 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
876 $error++;
877 }
878 if (empty($this->unitfrequency)) {
879 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
880 $error++;
881 }
882 if (($this->jobtype == 'command') && (empty($this->command))) {
883 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
884 $error++;
885 }
886 if (($this->jobtype == 'method') && (empty($this->classesname))) {
887 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
888 $error++;
889 }
890 if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
891 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
892 $error++;
893 }
894 if (($this->jobtype == 'method') && (empty($this->objectname))) {
895 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
896 $error++;
897 }
898
899 if (($this->jobtype == 'function') && (empty($this->libname))) {
900 $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
901 $error++;
902 }
903
904
905 // Update request
906 $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
907 $sql .= " entity=".(isset($this->entity) ? ((int) $this->entity) : $conf->entity).",";
908 $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
909 $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
910 $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
911 $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
912 $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
913 $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
914 $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
915 $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
916 $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
917 $sql .= " priority=".(isset($this->priority) ? ((int) $this->priority) : "null").",";
918 $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
919 $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
920 $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
921 $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
922 $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
923 $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
924 $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
925 $sql .= " unitfrequency=".(isset($this->unitfrequency) ? "'".$this->db->escape($this->unitfrequency)."'" : "null").",";
926 $sql .= " frequency=".(isset($this->frequency) ? ((int) $this->frequency) : "null").",";
927 $sql .= " status=".(isset($this->status) ? ((int) $this->status) : "null").",";
928 $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
929 $sql .= " pid=".(isset($this->pid) ? ((int) $this->pid) : "null").",";
930 $sql .= " email_alert = ".(isset($this->email_alert) ? "'".$this->db->escape($this->email_alert)."'" : "null").",";
931 $sql .= " fk_user_mod = ".((int) $user->id).",";
932 $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
933 $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
934 $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
935 $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
936 $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
937 $sql .= " WHERE rowid=".((int) $this->id);
938
939 $this->db->begin();
940
941 dol_syslog(get_class($this)."::update", LOG_DEBUG);
942 $resql = $this->db->query($sql);
943 if (!$resql) {
944 $error++;
945 $this->errors[] = "Error ".$this->db->lasterror();
946 }
947
948 // Commit or rollback
949 if ($error) {
950 foreach ($this->errors as $errmsg) {
951 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
952 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
953 }
954 $this->db->rollback();
955 return -1 * $error;
956 } else {
957 $this->db->commit();
958 return 1;
959 }
960 }
961
962
970 public function delete(User $user, int $notrigger = 0)
971 {
972 $error = 0;
973
974 $this->db->begin();
975
976 $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
977 $sql .= " WHERE rowid=".((int) $this->id);
978
979 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
980 $resql = $this->db->query($sql);
981 if (!$resql) {
982 $error++;
983 $this->errors[] = "Error ".$this->db->lasterror();
984 }
985
986 // Commit or rollback
987 if ($error) {
988 foreach ($this->errors as $errmsg) {
989 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
990 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
991 }
992 $this->db->rollback();
993 return -1 * $error;
994 } else {
995 $this->db->commit();
996 return 1;
997 }
998 }
999
1000
1001
1009 public function createFromClone(User $user, int $fromid)
1010 {
1011 global $langs;
1012
1013 $error = 0;
1014
1015 $object = new Cronjob($this->db);
1016
1017 $this->db->begin();
1018
1019 // Load source object
1020 $object->fetch($fromid);
1021 $object->id = 0;
1022
1023 // Clear fields
1024 $object->status = self::STATUS_DISABLED;
1025 $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
1026 $object->datelastrun = null;
1027 $object->lastresult = '';
1028 $object->datelastresult = null;
1029 $object->datenextrun = dol_now();
1030 $object->lastoutput = '';
1031 $object->nbrun = 0;
1032
1033 // Create clone
1034 $object->context['createfromclone'] = 'createfromclone';
1035 $result = $object->create($user);
1036
1037 // Other options
1038 if ($result < 0) {
1039 $this->error = $object->error;
1040 $this->errors = $object->errors;
1041 $error++;
1042 }
1043
1044 unset($object->context['createfromclone']);
1045
1046 // End
1047 if (!$error) {
1048 $this->db->commit();
1049 return $object->id;
1050 } else {
1051 $this->db->rollback();
1052 return -1;
1053 }
1054 }
1055
1056
1063 public function initAsSpecimen()
1064 {
1065 $this->id = 0;
1066 $this->ref = '';
1067 $this->entity = 0;
1068 $this->date_modification = dol_now();
1069 $this->datec = '';
1070 $this->label = '';
1071 $this->jobtype = '';
1072 $this->command = '';
1073 $this->classesname = '';
1074 $this->objectname = '';
1075 $this->methodename = '';
1076 $this->params = '';
1077 $this->md5params = '';
1078 $this->module_name = '';
1079 $this->priority = '';
1080 $this->datelastrun = '';
1081 $this->datenextrun = '';
1082 $this->dateend = '';
1083 $this->datestart = '';
1084 $this->datelastresult = '';
1085 $this->lastoutput = '';
1086 $this->lastresult = '';
1087 $this->unitfrequency = '86400';
1088 $this->frequency = 0;
1089 $this->status = 0;
1090 $this->processing = 0;
1091 $this->pid = null;
1092 $this->email_alert = '';
1093 $this->fk_user_author = 0;
1094 $this->fk_user_mod = 0;
1095 $this->note_private = '';
1096 $this->nbrun = 0;
1097 $this->maxrun = 100;
1098 $this->libname = '';
1099
1100 return 1;
1101 }
1102
1103
1110 public function getTooltipContentArray($params)
1111 {
1112 global $langs;
1113
1114 $langs->load('cron');
1115 $datas = [];
1116
1117 $datas['picto'] = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
1118 if (isset($this->status)) {
1119 $datas['picto'] .= ' '.$this->getLibStatut(5);
1120 }
1121 $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.dol_escape_htmltag($this->ref);
1122 $datas['label'] = '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
1123 if ($this->label != $langs->trans($this->label)) {
1124 $datas['label'] .= ' <span class="opacitymedium">('.$this->label.')</span>';
1125 }
1126 if (!empty($this->params)) {
1127 $datas['params'] = '<br><b>'.$langs->trans('Parameters').':</b> '.dol_escape_htmltag($this->params);
1128 }
1129 $datas['space'] = '<br>';
1130
1131 if (!empty($this->datestart) && $this->datestart >= dol_now()) {
1132 $datas['crondtstart'] = '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
1133 }
1134 if (!empty($this->dateend)) {
1135 $datas['crondtend'] = '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
1136 }
1137 if (!empty($this->datelastrun)) {
1138 $datas['cronlastlaunch'] = '<br><b>'.$langs->trans('CronDtLastLaunch').':</b> '.dol_print_date($this->datelastrun, 'dayhour', 'tzuserrel');
1139 }
1140 if (!empty($this->datenextrun)) {
1141 $datas['crondtnextlaunch'] = '<br><b>'.$langs->trans('CronDtNextLaunch').':</b> '.dol_print_date($this->datenextrun, 'dayhour', 'tzuserrel');
1142 }
1143
1144 return $datas;
1145 }
1146
1157 public function getNomUrl($withpicto = 0, string $option = '', int $notooltip = 0, string $morecss = '', int $save_lastsearch_value = -1)
1158 {
1159 global $conf, $langs;
1160
1161 if (!empty($conf->dol_no_mouse_hover)) {
1162 $notooltip = 1; // Force disable tooltips
1163 }
1164
1165 $result = '';
1166
1167 $params = [
1168 'id' => $this->id,
1169 'objecttype' => $this->element,
1170 ];
1171 $classfortooltip = 'classfortooltip';
1172 $dataparams = '';
1173 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1174 $classfortooltip = 'classforajaxtooltip';
1175 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1176 $label = '';
1177 } else {
1178 $label = implode($this->getTooltipContentArray($params));
1179 }
1180
1181 $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
1182
1183 if ($option != 'nolink') {
1184 // Add param to save lastsearch_values or not
1185 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1186 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1187 $add_save_lastsearch_values = 1;
1188 }
1189 if ($add_save_lastsearch_values) {
1190 $url .= '&save_lastsearch_values=1';
1191 }
1192 }
1193
1194 $linkclose = '';
1195 if (empty($notooltip)) {
1196 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1197 $label = $langs->trans("ShowCronJob");
1198 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1199 }
1200 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1201 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1202 } else {
1203 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1204 }
1205
1206 $linkstart = '<a href="'.$url.'"';
1207 $linkstart .= $linkclose.'>';
1208 $linkend = '</a>';
1209
1210 $result .= $linkstart;
1211 if ($withpicto) {
1212 $result .= img_object(($notooltip ? '' : $label), ($this->picto ?: 'generic'), (($withpicto != 2) ? 'class="paddingright"' : ''), 0, 0, $notooltip ? 0 : 1);
1213 }
1214 if ($withpicto != 2) {
1215 $result .= $this->ref;
1216 }
1217 $result .= $linkend;
1218 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1219
1220 return $result;
1221 }
1222
1223
1230 public function info(int $id)
1231 {
1232 $sql = "SELECT";
1233 $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1234 $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
1235 $sql .= " WHERE f.rowid = ".((int) $id);
1236
1237 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1238 $resql = $this->db->query($sql);
1239 if ($resql) {
1240 if ($this->db->num_rows($resql)) {
1241 $obj = $this->db->fetch_object($resql);
1242
1243 $this->id = $obj->rowid;
1244
1245 $this->user_modification_id = $obj->fk_user_mod;
1246 $this->user_creation_id = $obj->fk_user_author;
1247 $this->date_creation = $this->db->jdate($obj->datec);
1248 $this->date_modification = $this->db->jdate($obj->tms);
1249 }
1250 $this->db->free($resql);
1251
1252 return 1;
1253 } else {
1254 $this->error = "Error ".$this->db->lasterror();
1255 return -1;
1256 }
1257 }
1258
1259
1260 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1269 public function run_jobs(string $userlogin)
1270 {
1271 // phpcs:enable
1272 global $langs, $conf, $hookmanager;
1273
1274 $hookmanager->initHooks(array('cron'));
1275
1276 $now = dol_now();
1277 $error = 0;
1278
1279 $langs->load('cron');
1280
1281 if (empty($userlogin)) {
1282 $this->error = "User login is mandatory";
1283 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1284 return -1;
1285 }
1286
1287 // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
1288 // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
1289 if ($conf->entity != $this->entity && $this->entity > 0) {
1290 dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
1291 }
1292 $savcurrententity = $conf->entity;
1293 $conf->setEntityValues($this->db, $this->entity);
1294 dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
1295
1296 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1297 $user = new User($this->db);
1298 $result = $user->fetch(0, $userlogin);
1299 if ($result < 0) {
1300 $this->error = "User Error:".$user->error;
1301 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1302 $conf->setEntityValues($this->db, $savcurrententity);
1303 return -1;
1304 } else {
1305 if (empty($user->id)) {
1306 $this->error = "User login: ".$userlogin." does not exist";
1307 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1308 $conf->setEntityValues($this->db, $savcurrententity);
1309 return -1;
1310 }
1311 }
1312
1313 dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
1314
1315 // Increase limit of time. Works only if we are not in safe mode
1316 $ExecTimeLimit = getDolGlobalInt('MAIN_CRON_EXEC_TIME_LIMIT', 600);
1317 if (!empty($ExecTimeLimit)) {
1318 $err = error_reporting();
1319 error_reporting(0); // Disable all errors
1320 //error_reporting(E_ALL);
1321 @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
1322 error_reporting($err);
1323 }
1324 $MemoryLimit = getDolGlobalString('MAIN_CRON_MEMORY_LIMIT');
1325 if (!empty($MemoryLimit)) {
1326 @ini_set('memory_limit', $MemoryLimit);
1327 }
1328
1329 // Update last run date start (to track running jobs)
1330 $this->datelastrun = $now;
1331 $this->datelastresult = null;
1332 $this->lastoutput = '';
1333 $this->lastresult = '';
1334 $this->processing = 1; // To know job was started
1335 $this->pid = function_exists('getmypid') ? getmypid() : null; // Avoid dol_getmypid to get null if the function is not available
1336 $this->nbrun += 1;
1337 $result = $this->update($user); // This include begin/commit
1338 if ($result < 0) {
1339 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1340 $conf->setEntityValues($this->db, $savcurrententity);
1341 return -1;
1342 }
1343
1344 // Safety net: If the job execution ends unexpectedly (fatal error, timeout, explicit exit, ...),
1345 // the row may remain stuck with processing=1 and will never be selected again by runners
1346 // (they fetch jobs with processing=0 by default). We register a shutdown handler to unlock it.
1347 $cronjobid = (int) $this->id;
1348 $cronjobpid = (int) $this->pid;
1349 $dbs = $this->db;
1350 register_shutdown_function(static function () use ($cronjobid, $cronjobpid, $dbs) {
1351 if (empty($cronjobid) || empty($dbs)) {
1352 return;
1353 }
1354
1355 try {
1356 // Ensure we are not trapped into a transaction left open by the job.
1357 $dbs->rollback();
1358 } catch (Throwable $e) {
1359 // Ignore
1360 }
1361
1362 // If job is already closed, do nothing.
1363 $sql = "SELECT processing, pid, datelastresult FROM ".MAIN_DB_PREFIX."cronjob WHERE rowid = ".((int) $cronjobid);
1364 $resql = $dbs->query($sql);
1365 if (!$resql) {
1366 return;
1367 }
1368 $obj = $dbs->fetch_object($resql);
1369 $dbs->free($resql);
1370
1371 if (!$obj || (int) $obj->processing !== 1 || !empty($obj->datelastresult)) {
1372 return;
1373 }
1374
1375 // Protect against unlocking a job started by another PID (concurrent runner).
1376 if (!empty($obj->pid) && (int) $obj->pid !== (int) $cronjobpid) {
1377 return;
1378 }
1379
1380 $now = dol_now();
1381 $lastoutput = 'Cron job aborted unexpectedly (shutdown).';
1382
1383 $lastError = error_get_last();
1384 if (is_array($lastError) && !empty($lastError['message'])) {
1385 $lastoutput .= ' Last error: '.dol_trunc($lastError['message'], 2000, 'right', 'UTF-8', 1);
1386 }
1387
1388 $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob";
1389 $sql .= " SET processing = 0, pid = NULL, datelastresult = '".$dbs->idate($now)."', lastresult = '-1', lastoutput = '".$dbs->escape($lastoutput)."'";
1390 $sql .= " WHERE rowid = ".((int) $cronjobid)." AND processing = 1 AND datelastresult IS NULL";
1391 $dbs->query($sql);
1392 });
1393
1394 // Run a method
1395 if ($this->jobtype == 'method') {
1396 // Deny to launch a method from a deactivated module
1397 if (!empty($this->entity) && !empty($this->module_name) && !isModEnabled(strtolower($this->module_name))) {
1398 $this->error = $langs->transnoentitiesnoconv('CronModuleNotEnabledInThisEntity', $this->methodename, $this->objectname);
1399 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1400 $this->lastoutput = $this->error;
1401 $this->lastresult = '-1';
1402 $error++;
1403 }
1404
1405 // load classes
1406 if (!$error) {
1407 $ret = dol_include_once($this->classesname);
1408 if ($ret === false || (!class_exists($this->objectname))) {
1409 if ($ret === false) {
1410 $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
1411 } else {
1412 $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
1413 }
1414 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1415 $this->lastoutput = $this->error;
1416 $this->lastresult = '-1';
1417 $error++;
1418 }
1419 }
1420
1421 // test if method exists
1422 if (!$error) {
1423 if (!method_exists($this->objectname, $this->methodename)) {
1424 $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
1425 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1426 $this->lastoutput = $this->error;
1427 $this->lastresult = '-1';
1428 $error++;
1429 }
1430 if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
1431 $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
1432 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1433 $this->lastoutput = $this->error;
1434 $this->lastresult = '-1';
1435 $error++;
1436 }
1437 }
1438
1439 // Load langs
1440 if (!$error) {
1441 $result = $langs->load($this->module_name);
1442 $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
1443
1444 if ($result < 0) { // If technical error
1445 dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1446 $this->error = $langs->error;
1447 $this->lastoutput = $this->error;
1448 $this->lastresult = '-1';
1449 $error++;
1450 }
1451 }
1452
1453 if (!$error) {
1454 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);
1455
1456 // Create Object for the called module
1457 $nameofclass = (string) $this->objectname;
1458 $object = new $nameofclass($this->db);
1459 if ($this->entity > 0) {
1460 $object->entity = $this->entity; // We work on a dedicated entity
1461 }
1462
1463 $params_arr = array();
1464 if (!empty($this->params) || $this->params === '0') {
1465 $params_arr = array_map('trim', explode(",", $this->params));
1466 }
1467
1468 if (!is_array($params_arr)) {
1469 $result = call_user_func(array($object, $this->methodename), $this->params);
1470 } else {
1471 $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1472 }
1473 $errmsg = '';
1474 if ($result === false || (!is_bool($result) && $result != 0)) {
1475 $langs->load("errors");
1476
1477 if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1478 $errmsg .= $object->error;
1479 }
1480 if (is_array($object->errors) && count($object->errors)) {
1481 $errmsg .= (($errmsg ? ', ' : '').implode(', ', $object->errors));
1482 }
1483 if (empty($errmsg)) {
1484 $errmsg = $langs->trans('ErrorUnknown');
1485 }
1486
1487 dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
1488
1489 $this->error = $errmsg;
1490 $this->lastoutput = dol_substr((empty($object->output) ? "" : $object->output."\n").$errmsg, 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
1491 $this->lastresult = is_numeric($result) ? var_export($result, true) : '-1';
1492 $error++;
1493 } else {
1494 dol_syslog(get_class($this)."::run_jobs END");
1495 $this->lastoutput = dol_substr((empty($object->output) ? "" : $object->output."\n"), 0, $this::MAXIMUM_LENGTH_FOR_LASTOUTPUT_FIELD, 'UTF-8', 1);
1496 $this->lastresult = var_export($result, true);
1497 }
1498 }
1499 }
1500
1501 if ($this->jobtype == 'function') {
1502 //load lib
1503 $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
1504 $ret = dol_include_once($libpath);
1505 if ($ret === false) {
1506 $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
1507 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1508 $this->lastoutput = $this->error;
1509 $this->lastresult = '-1';
1510 $error++;
1511 }
1512
1513 // Load langs
1514 if (!$error) {
1515 $result = $langs->load($this->module_name);
1516 $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
1517 if ($result < 0) { // If technical error
1518 dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1519 $this->error = $langs->error;
1520 $this->lastoutput = $this->error;
1521 $this->lastresult = '-1';
1522 $error++;
1523 }
1524 }
1525
1526 if (!$error) {
1527 dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
1528
1529 $params_arr = array();
1530 if (!empty($this->params) || $this->params === '0') {
1531 $params_arr = array_map('trim', explode(",", $this->params));
1532 }
1533
1534 $result = call_user_func_array($this->methodename, $params_arr);
1535
1536 if ($result === false || (!is_bool($result) && $result != 0)) {
1537 $langs->load("errors");
1538 dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
1539 $this->error = $langs->trans('ErrorUnknown');
1540 $this->lastoutput = $this->error;
1541 $this->lastresult = is_numeric($result) ? var_export($result, true) : '-1';
1542 $error++;
1543 } else {
1544 $this->lastoutput = var_export($result, true);
1545 $this->lastresult = var_export($result, true); // Return code
1546 }
1547 }
1548 }
1549
1550 // Run a command line
1551 if ($this->jobtype == 'command') {
1552 global $dolibarr_cron_allow_cli;
1553
1554 if (empty($dolibarr_cron_allow_cli)) {
1555 $langs->load("errors");
1556 $this->error = $langs->trans("FailedToExecutCommandJob");
1557 $this->lastoutput = '';
1558 $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
1559 $error++;
1560 } else {
1561 $outputdir = $conf->cron->dir_temp;
1562 if (empty($outputdir)) {
1563 $outputdir = $conf->cronjob->dir_temp;
1564 }
1565 dol_mkdir($outputdir);
1566
1567 if (!empty($outputdir)) {
1568 $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
1569
1570 // Execute a CLI
1571 include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1572 $utils = new Utils($this->db);
1573 $arrayresult = $utils->executeCLI($this->command, $outputfile);
1574
1575 $this->error = $arrayresult['error'];
1576 $this->lastoutput = $arrayresult['output'];
1577 $this->lastresult = (string) $arrayresult['result'];
1578 } else {
1579 $langs->load("errors");
1580 $this->error = $langs->trans("ErrorNoTmpDir", (string) $outputdir);
1581 $this->lastoutput = '';
1582 $this->lastresult = '-1';
1583 $error++;
1584 }
1585 }
1586 }
1587
1588 dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
1589
1590 $this->datelastresult = dol_now();
1591 $this->processing = 0;
1592 $this->pid = null;
1593 $result = $this->update($user); // This include begin/commit
1594 if ($result < 0) {
1595 dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1596 $conf->setEntityValues($this->db, $savcurrententity);
1597 return -1;
1598 }
1599
1600 $conf->setEntityValues($this->db, $savcurrententity);
1601
1602 if ($error && !empty($this->email_alert)) {
1603 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
1604 $subject = $langs->transnoentitiesnoconv("ErrorInBatch", $this->label);
1605 $msg = $langs->transnoentitiesnoconv("ErrorInBatch", $this->label);
1606 $from = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
1607 $cmailfile = new CMailFile($subject, $this->email_alert, $from, $msg);
1608 $result = $cmailfile->sendfile(); // Do not test result
1609 }
1610
1611 return $error ? -1 : 1;
1612 }
1613
1614
1615 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1623 public function reprogram_jobs(string $userlogin, int $now)
1624 {
1625 // phpcs:enable
1626 dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1627
1628 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1629 $user = new User($this->db);
1630 $result = $user->fetch(0, $userlogin);
1631 if ($result < 0) {
1632 $this->error = "User Error : ".$user->error;
1633 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1634 return -1;
1635 } else {
1636 if (empty($user->id)) {
1637 $this->error = " User user login:".$userlogin." do not exists";
1638 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1639 return -1;
1640 }
1641 }
1642
1643 dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1644
1645 if (empty($this->datenextrun)) {
1646 if (empty($this->datestart)) {
1647 if (!is_numeric($this->frequency) || (int) $this->unitfrequency == 2678400) {
1648 $this->datenextrun = dol_time_plus_duree($now, $this->frequency, 'm');
1649 } else {
1650 $this->datenextrun = $now + ((int) $this->frequency * (int) $this->unitfrequency);
1651 }
1652 } else {
1653 if (!is_numeric($this->frequency) || (int) $this->unitfrequency == 2678400) {
1654 $this->datenextrun = dol_time_plus_duree($this->datestart, $this->frequency, 'm');
1655 } else {
1656 $this->datenextrun = $this->datestart + ((int) $this->frequency * (int) $this->unitfrequency);
1657 }
1658 }
1659 }
1660
1661 if ($this->datenextrun < $now && $this->frequency > 0 && !empty($this->unitfrequency)) {
1662 // Loop until date is after future
1663 while ($this->datenextrun < $now) {
1664 if (!is_numeric($this->unitfrequency) || (int) $this->unitfrequency == 2678400 || (int) $this->unitfrequency <= 0) {
1665 $this->datenextrun = dol_time_plus_duree($this->datenextrun, $this->frequency, 'm');
1666 } else {
1667 $this->datenextrun += ((int) $this->frequency * (int) $this->unitfrequency);
1668 }
1669 }
1670 } else {
1671 dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
1672 }
1673
1674
1675 // Archive job
1676 if ($this->autodelete == 2) {
1677 if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1678 || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1679 $this->status = self::STATUS_ARCHIVED;
1680 dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
1681 }
1682 }
1683
1684 $result = $this->update($user);
1685 if ($result < 0) {
1686 dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1687 return -1;
1688 }
1689
1690 return 1;
1691 }
1692
1699 public function getLibStatut(int $mode = 0)
1700 {
1701 return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1702 }
1703
1704 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1714 public function LibStatut(int $status, int $mode = 0, int $processing = 0, string $lastResult = '')
1715 {
1716 // phpcs:enable
1717 $this->labelStatus = array(); // Force reset o array because label depends on other fields
1718 $this->labelStatusShort = array();
1719
1720 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1721 global $langs;
1722 $langs->load('users');
1723
1724 $moreText = '';
1725 if ($processing) {
1726 $moreText = ' ('.$langs->trans("Running").')';
1727 } elseif ($lastResult) {
1728 $moreText .= ' ('.$langs->trans("Error").')';
1729 }
1730
1731 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moreText;
1732 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moreText;
1733 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
1734 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
1735 }
1736
1737 $statusType = 'status4';
1738 if ($status == self::STATUS_ENABLED && $processing) {
1739 $statusType = 'status1';
1740 }
1741 if ($status == self::STATUS_DISABLED) {
1742 $statusType = 'status5';
1743 }
1744 if ($status == self::STATUS_ENABLED && $this->lastresult) {
1745 $statusType = 'status8';
1746 }
1747
1748 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1749 }
1750}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
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.
fetch(int $id, string $objectname='', string $methodname='', string $label='')
Load object in memory from the database.
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)
$fields
'type' field format: 'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortf...
info(int $id)
Load object information.
getTooltipContentArray($params)
getTooltipContentArray
update($user=null, int $notrigger=0)
Update object into 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:168
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:126
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php