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