dolibarr  16.0.5
cronjob.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2019 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 
27 
31 class Cronjob extends CommonObject
32 {
36  public $element = 'cronjob';
37 
41  public $table_element = 'cronjob';
42 
46  public $picto = 'cron';
47 
51  public $entity;
52 
56  public $jobtype;
57 
61  public $tms = '';
62 
66  public $datec = '';
67 
71  public $label;
72 
76  public $command;
77  public $classesname;
78  public $objectname;
79  public $methodename;
80  public $params;
81  public $md5params;
82  public $module_name;
83  public $priority;
84 
88  public $datelastrun = '';
89 
93  public $datenextrun = '';
94 
98  public $dateend = '';
99 
103  public $datestart = '';
104 
108  public $datelastresult = '';
109 
113  public $lastresult;
114 
118  public $lastoutput;
119 
123  public $unitfrequency;
124 
128  public $frequency;
129 
133  public $status;
134 
138  public $processing;
139 
143  public $fk_user_author;
144 
148  public $fk_user_mod;
149 
153  public $nbrun;
154 
158  public $maxrun;
159 
163  public $libname;
164 
168  public $test;
169 
173  public $autodelete;
174 
175 
176  const STATUS_DISABLED = 0;
177  const STATUS_ENABLED = 1;
178  const STATUS_ARCHIVED = 2;
179 
180 
186  public function __construct($db)
187  {
188  $this->db = $db;
189  }
190 
191 
199  public function create($user, $notrigger = 0)
200  {
201  global $conf, $langs;
202  $error = 0;
203 
204  $now = dol_now();
205 
206  // Clean parameters
207 
208  if (isset($this->label)) {
209  $this->label = trim($this->label);
210  }
211  if (isset($this->jobtype)) {
212  $this->jobtype = trim($this->jobtype);
213  }
214  if (isset($this->command)) {
215  $this->command = trim($this->command);
216  }
217  if (isset($this->classesname)) {
218  $this->classesname = trim($this->classesname);
219  }
220  if (isset($this->objectname)) {
221  $this->objectname = trim($this->objectname);
222  }
223  if (isset($this->methodename)) {
224  $this->methodename = trim($this->methodename);
225  }
226  if (isset($this->params)) {
227  $this->params = trim($this->params);
228  }
229  if (isset($this->md5params)) {
230  $this->md5params = trim($this->md5params);
231  }
232  if (isset($this->module_name)) {
233  $this->module_name = trim($this->module_name);
234  }
235  if (isset($this->priority)) {
236  $this->priority = trim($this->priority);
237  }
238  if (isset($this->lastoutput)) {
239  $this->lastoutput = trim($this->lastoutput);
240  }
241  if (isset($this->lastresult)) {
242  $this->lastresult = trim($this->lastresult);
243  }
244  if (isset($this->unitfrequency)) {
245  $this->unitfrequency = trim($this->unitfrequency);
246  }
247  if (isset($this->frequency)) {
248  $this->frequency = trim($this->frequency);
249  }
250  if (isset($this->status)) {
251  $this->status = trim($this->status);
252  }
253  if (isset($this->note_private)) {
254  $this->note_private = trim($this->note_private);
255  }
256  if (isset($this->nbrun)) {
257  $this->nbrun = (int) $this->nbrun;
258  }
259  if (isset($this->maxrun)) {
260  $this->maxrun = (int) $this->maxrun;
261  }
262  if (isset($this->libname)) {
263  $this->libname = trim($this->libname);
264  }
265  if (isset($this->test)) {
266  $this->test = trim($this->test);
267  }
268 
269  // Check parameters
270  // Put here code to add a control on parameters values
271  if (dol_strlen($this->datenextrun) == 0) {
272  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
273  $error++;
274  }
275  if (empty($this->label)) {
276  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
277  $error++;
278  }
279  if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
280  $this->errors[] = $langs->trans('CronErrEndDateStartDt');
281  $error++;
282  }
283  if (empty($this->unitfrequency)) {
284  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
285  $error++;
286  }
287  if (($this->jobtype == 'command') && (empty($this->command))) {
288  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
289  $error++;
290  }
291  if (($this->jobtype == 'method') && (empty($this->classesname))) {
292  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
293  $error++;
294  }
295  if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
296  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
297  $error++;
298  }
299  if (($this->jobtype == 'method') && (empty($this->objectname))) {
300  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
301  $error++;
302  }
303  if (($this->jobtype == 'function') && (empty($this->libname))) {
304  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
305  $error++;
306  }
307 
308  // Insert request
309  $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
310  $sql .= "entity,";
311  $sql .= "datec,";
312  $sql .= "jobtype,";
313  $sql .= "label,";
314  $sql .= "command,";
315  $sql .= "classesname,";
316  $sql .= "objectname,";
317  $sql .= "methodename,";
318  $sql .= "params,";
319  $sql .= "md5params,";
320  $sql .= "module_name,";
321  $sql .= "priority,";
322  $sql .= "datelastrun,";
323  $sql .= "datenextrun,";
324  $sql .= "dateend,";
325  $sql .= "datestart,";
326  $sql .= "lastresult,";
327  $sql .= "datelastresult,";
328  $sql .= "lastoutput,";
329  $sql .= "unitfrequency,";
330  $sql .= "frequency,";
331  $sql .= "status,";
332  $sql .= "fk_user_author,";
333  $sql .= "fk_user_mod,";
334  $sql .= "note,";
335  $sql .= "nbrun,";
336  $sql .= "maxrun,";
337  $sql .= "libname,";
338  $sql .= "test";
339  $sql .= ") VALUES (";
340  $sql .= " ".(!isset($this->entity) ? $conf->entity : $this->db->escape($this->entity)).",";
341  $sql .= " '".$this->db->idate($now)."',";
342  $sql .= " ".(!isset($this->jobtype) ? 'NULL' : "'".$this->db->escape($this->jobtype)."'").",";
343  $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
344  $sql .= " ".(!isset($this->command) ? 'NULL' : "'".$this->db->escape($this->command)."'").",";
345  $sql .= " ".(!isset($this->classesname) ? 'NULL' : "'".$this->db->escape($this->classesname)."'").",";
346  $sql .= " ".(!isset($this->objectname) ? 'NULL' : "'".$this->db->escape($this->objectname)."'").",";
347  $sql .= " ".(!isset($this->methodename) ? 'NULL' : "'".$this->db->escape($this->methodename)."'").",";
348  $sql .= " ".(!isset($this->params) ? 'NULL' : "'".$this->db->escape($this->params)."'").",";
349  $sql .= " ".(!isset($this->md5params) ? 'NULL' : "'".$this->db->escape($this->md5params)."'").",";
350  $sql .= " ".(!isset($this->module_name) ? 'NULL' : "'".$this->db->escape($this->module_name)."'").",";
351  $sql .= " ".(!isset($this->priority) ? '0' : $this->priority).",";
352  $sql .= " ".(!isset($this->datelastrun) || dol_strlen($this->datelastrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastrun)."'").",";
353  $sql .= " ".(!isset($this->datenextrun) || dol_strlen($this->datenextrun) == 0 ? 'NULL' : "'".$this->db->idate($this->datenextrun)."'").",";
354  $sql .= " ".(!isset($this->dateend) || dol_strlen($this->dateend) == 0 ? 'NULL' : "'".$this->db->idate($this->dateend)."'").",";
355  $sql .= " ".(!isset($this->datestart) || dol_strlen($this->datestart) == 0 ? 'NULL' : "'".$this->db->idate($this->datestart)."'").",";
356  $sql .= " ".(!isset($this->lastresult) ? 'NULL' : "'".$this->db->escape($this->lastresult)."'").",";
357  $sql .= " ".(!isset($this->datelastresult) || dol_strlen($this->datelastresult) == 0 ? 'NULL' : "'".$this->db->idate($this->datelastresult)."'").",";
358  $sql .= " ".(!isset($this->lastoutput) ? 'NULL' : "'".$this->db->escape($this->lastoutput)."'").",";
359  $sql .= " ".(!isset($this->unitfrequency) ? 'NULL' : "'".$this->db->escape($this->unitfrequency)."'").",";
360  $sql .= " ".(!isset($this->frequency) ? '0' : $this->frequency).",";
361  $sql .= " ".(!isset($this->status) ? '0' : $this->status).",";
362  $sql .= " ".$user->id.",";
363  $sql .= " ".$user->id.",";
364  $sql .= " ".(!isset($this->note_private) ? 'NULL' : "'".$this->db->escape($this->note_private)."'").",";
365  $sql .= " ".(!isset($this->nbrun) ? '0' : $this->db->escape($this->nbrun)).",";
366  $sql .= " ".(empty($this->maxrun) ? '0' : $this->db->escape($this->maxrun)).",";
367  $sql .= " ".(!isset($this->libname) ? 'NULL' : "'".$this->db->escape($this->libname)."'").",";
368  $sql .= " ".(!isset($this->test) ? 'NULL' : "'".$this->db->escape($this->test)."'")."";
369  $sql .= ")";
370 
371  $this->db->begin();
372 
373  dol_syslog(get_class($this)."::create", LOG_DEBUG);
374  $resql = $this->db->query($sql);
375  if (!$resql) {
376  $error++;
377  $this->errors[] = "Error ".$this->db->lasterror();
378  }
379 
380  if (!$error) {
381  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
382  }
383 
384  // Commit or rollback
385  if ($error) {
386  $this->db->rollback();
387  return -1 * $error;
388  } else {
389  $this->db->commit();
390  return $this->id;
391  }
392  }
393 
394 
403  public function fetch($id, $objectname = '', $methodname = '')
404  {
405  $sql = "SELECT";
406  $sql .= " t.rowid,";
407  $sql .= " t.entity,";
408  $sql .= " t.tms,";
409  $sql .= " t.datec,";
410  $sql .= " t.jobtype,";
411  $sql .= " t.label,";
412  $sql .= " t.command,";
413  $sql .= " t.classesname,";
414  $sql .= " t.objectname,";
415  $sql .= " t.methodename,";
416  $sql .= " t.params,";
417  $sql .= " t.md5params,";
418  $sql .= " t.module_name,";
419  $sql .= " t.priority,";
420  $sql .= " t.datelastrun,";
421  $sql .= " t.datenextrun,";
422  $sql .= " t.dateend,";
423  $sql .= " t.datestart,";
424  $sql .= " t.lastresult,";
425  $sql .= " t.datelastresult,";
426  $sql .= " t.lastoutput,";
427  $sql .= " t.unitfrequency,";
428  $sql .= " t.frequency,";
429  $sql .= " t.status,";
430  $sql .= " t.processing,";
431  $sql .= " t.fk_user_author,";
432  $sql .= " t.fk_user_mod,";
433  $sql .= " t.note as note_private,";
434  $sql .= " t.nbrun,";
435  $sql .= " t.maxrun,";
436  $sql .= " t.libname,";
437  $sql .= " t.test";
438  $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
439  if ($id > 0) {
440  $sql .= " WHERE t.rowid = ".((int) $id);
441  } else {
442  $sql .= " WHERE t.entity IN(0, ".getEntity('cron').")";
443  $sql .= " AND t.objectname = '".$this->db->escape($objectname)."'";
444  $sql .= " AND t.methodename = '".$this->db->escape($methodname)."'";
445  }
446 
447  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
448  $resql = $this->db->query($sql);
449  if ($resql) {
450  if ($this->db->num_rows($resql)) {
451  $obj = $this->db->fetch_object($resql);
452 
453  $this->id = $obj->rowid;
454  $this->ref = $obj->rowid;
455  $this->entity = $obj->entity;
456  $this->tms = $this->db->jdate($obj->tms);
457  $this->datec = $this->db->jdate($obj->datec);
458  $this->label = $obj->label;
459  $this->jobtype = $obj->jobtype;
460  $this->command = $obj->command;
461  $this->classesname = $obj->classesname;
462  $this->objectname = $obj->objectname;
463  $this->methodename = $obj->methodename;
464  $this->params = $obj->params;
465  $this->md5params = $obj->md5params;
466  $this->module_name = $obj->module_name;
467  $this->priority = $obj->priority;
468  $this->datelastrun = $this->db->jdate($obj->datelastrun);
469  $this->datenextrun = $this->db->jdate($obj->datenextrun);
470  $this->dateend = $this->db->jdate($obj->dateend);
471  $this->datestart = $this->db->jdate($obj->datestart);
472  $this->lastresult = $obj->lastresult;
473  $this->lastoutput = $obj->lastoutput;
474  $this->datelastresult = $this->db->jdate($obj->datelastresult);
475  $this->unitfrequency = $obj->unitfrequency;
476  $this->frequency = $obj->frequency;
477  $this->status = $obj->status;
478  $this->processing = $obj->processing;
479  $this->fk_user_author = $obj->fk_user_author;
480  $this->fk_user_mod = $obj->fk_user_mod;
481  $this->note_private = $obj->note_private;
482  $this->nbrun = $obj->nbrun;
483  $this->maxrun = $obj->maxrun;
484  $this->libname = $obj->libname;
485  $this->test = $obj->test;
486  }
487  $this->db->free($resql);
488 
489  return 1;
490  } else {
491  $this->error = "Error ".$this->db->lasterror();
492  return -1;
493  }
494  }
495 
509  public function fetchAll($sortorder = 'DESC', $sortfield = 't.rowid', $limit = 0, $offset = 0, $status = 1, $filter = '', $processing = -1)
510  {
511  $this->lines = array();
512 
513  $sql = "SELECT";
514  $sql .= " t.rowid,";
515  $sql .= " t.entity,";
516  $sql .= " t.tms,";
517  $sql .= " t.datec,";
518  $sql .= " t.jobtype,";
519  $sql .= " t.label,";
520  $sql .= " t.command,";
521  $sql .= " t.classesname,";
522  $sql .= " t.objectname,";
523  $sql .= " t.methodename,";
524  $sql .= " t.params,";
525  $sql .= " t.md5params,";
526  $sql .= " t.module_name,";
527  $sql .= " t.priority,";
528  $sql .= " t.datelastrun,";
529  $sql .= " t.datenextrun,";
530  $sql .= " t.dateend,";
531  $sql .= " t.datestart,";
532  $sql .= " t.lastresult,";
533  $sql .= " t.datelastresult,";
534  $sql .= " t.lastoutput,";
535  $sql .= " t.unitfrequency,";
536  $sql .= " t.frequency,";
537  $sql .= " t.status,";
538  $sql .= " t.processing,";
539  $sql .= " t.fk_user_author,";
540  $sql .= " t.fk_user_mod,";
541  $sql .= " t.note as note_private,";
542  $sql .= " t.nbrun,";
543  $sql .= " t.libname,";
544  $sql .= " t.test";
545  $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as t";
546  $sql .= " WHERE 1 = 1";
547  if ($processing >= 0) {
548  $sql .= " AND t.processing = ".(empty($processing) ? '0' : '1');
549  }
550  if ($status >= 0 && $status < 2) {
551  $sql .= " AND t.status = ".(empty($status) ? '0' : '1');
552  } elseif ($status == 2) {
553  $sql .= " AND t.status = 2";
554  }
555  // Manage filter
556  if (is_array($filter) && count($filter) > 0) {
557  foreach ($filter as $key => $value) {
558  if ($key == 't.rowid') {
559  $sql .= " AND ".$key." = ".((int) $value);
560  } else {
561  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
562  }
563  }
564  }
565 
566  $sql .= $this->db->order($sortfield, $sortorder);
567  if (!empty($limit) && !empty($offset)) {
568  $sql .= $this->db->plimit($limit + 1, $offset);
569  }
570 
571  $sqlwhere = array();
572 
573  if (count($sqlwhere) > 0) {
574  $sql .= " WHERE ".implode(' AND ', $sqlwhere);
575  }
576 
577  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
578  $resql = $this->db->query($sql);
579  if ($resql) {
580  $num = $this->db->num_rows($resql);
581  $i = 0;
582 
583  if ($num) {
584  while ($i < $num) {
585  $line = new Cronjobline();
586 
587  $obj = $this->db->fetch_object($resql);
588 
589  $line->id = $obj->rowid;
590  $line->ref = $obj->rowid;
591  $line->entity = $obj->entity;
592  $line->tms = $this->db->jdate($obj->tms);
593  $line->datec = $this->db->jdate($obj->datec);
594  $line->label = $obj->label;
595  $line->jobtype = $obj->jobtype;
596  $line->command = $obj->command;
597  $line->classesname = $obj->classesname;
598  $line->objectname = $obj->objectname;
599  $line->methodename = $obj->methodename;
600  $line->params = $obj->params;
601  $line->md5params = $obj->md5params;
602  $line->module_name = $obj->module_name;
603  $line->priority = $obj->priority;
604  $line->datelastrun = $this->db->jdate($obj->datelastrun);
605  $line->datenextrun = $this->db->jdate($obj->datenextrun);
606  $line->dateend = $this->db->jdate($obj->dateend);
607  $line->datestart = $this->db->jdate($obj->datestart);
608  $line->lastresult = $obj->lastresult;
609  $line->datelastresult = $this->db->jdate($obj->datelastresult);
610  $line->lastoutput = $obj->lastoutput;
611  $line->unitfrequency = $obj->unitfrequency;
612  $line->frequency = $obj->frequency;
613  $line->status = $obj->status;
614  $line->processing = $obj->processing;
615  $line->fk_user_author = $obj->fk_user_author;
616  $line->fk_user_mod = $obj->fk_user_mod;
617  $line->note_private = $obj->note_private;
618  $line->nbrun = $obj->nbrun;
619  $line->libname = $obj->libname;
620  $line->test = $obj->test;
621  $this->lines[] = $line;
622 
623  $i++;
624  }
625  }
626  $this->db->free($resql);
627 
628  return 1;
629  } else {
630  $this->error = "Error ".$this->db->lasterror();
631  return -1;
632  }
633  }
634 
635 
643  public function update($user = null, $notrigger = 0)
644  {
645  global $conf, $langs;
646 
647  $langs->load('cron');
648 
649  $error = 0;
650 
651  // Clean parameters
652  if (isset($this->label)) {
653  $this->label = trim($this->label);
654  }
655  if (isset($this->jobtype)) {
656  $this->jobtype = trim($this->jobtype);
657  }
658  if (isset($this->command)) {
659  $this->command = trim($this->command);
660  }
661  if (isset($this->classesname)) {
662  $this->classesname = trim($this->classesname);
663  }
664  if (isset($this->objectname)) {
665  $this->objectname = trim($this->objectname);
666  }
667  if (isset($this->methodename)) {
668  $this->methodename = trim($this->methodename);
669  }
670  if (isset($this->params)) {
671  $this->params = trim($this->params);
672  }
673  if (isset($this->md5params)) {
674  $this->md5params = trim($this->md5params);
675  }
676  if (isset($this->module_name)) {
677  $this->module_name = trim($this->module_name);
678  }
679  if (isset($this->priority)) {
680  $this->priority = trim($this->priority);
681  }
682  if (isset($this->lastoutput)) {
683  $this->lastoutput = trim($this->lastoutput);
684  }
685  if (isset($this->lastresult)) {
686  $this->lastresult = trim($this->lastresult);
687  }
688  if (isset($this->unitfrequency)) {
689  $this->unitfrequency = trim($this->unitfrequency);
690  }
691  if (isset($this->frequency)) {
692  $this->frequency = trim($this->frequency);
693  }
694  if (isset($this->status)) {
695  $this->status = trim($this->status);
696  }
697  if (isset($this->note_private)) {
698  $this->note_private = trim($this->note_private);
699  }
700  if (isset($this->nbrun)) {
701  $this->nbrun = trim($this->nbrun);
702  }
703  if (isset($this->libname)) {
704  $this->libname = trim($this->libname);
705  }
706  if (isset($this->test)) {
707  $this->test = trim($this->test);
708  }
709 
710  if (empty($this->maxrun)) {
711  $this->maxrun = 0;
712  }
713  if (empty($this->processing)) {
714  $this->processing = 0;
715  }
716 
717  // Check parameters
718  // Put here code to add a control on parameters values
719  if (dol_strlen($this->datenextrun) == 0) {
720  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronDtNextLaunch'));
721  $error++;
722  }
723  if ((dol_strlen($this->datestart) != 0) && (dol_strlen($this->dateend) != 0) && ($this->dateend < $this->datestart)) {
724  $this->errors[] = $langs->trans('CronErrEndDateStartDt');
725  $error++;
726  }
727  if (empty($this->label)) {
728  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLabel'));
729  $error++;
730  }
731  if (empty($this->unitfrequency)) {
732  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronFrequency'));
733  $error++;
734  }
735  if (($this->jobtype == 'command') && (empty($this->command))) {
736  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronCommand'));
737  $error++;
738  }
739  if (($this->jobtype == 'method') && (empty($this->classesname))) {
740  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronClass'));
741  $error++;
742  }
743  if (($this->jobtype == 'method' || $this->jobtype == 'function') && (empty($this->methodename))) {
744  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronMethod'));
745  $error++;
746  }
747  if (($this->jobtype == 'method') && (empty($this->objectname))) {
748  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronObject'));
749  $error++;
750  }
751 
752  if (($this->jobtype == 'function') && (empty($this->libname))) {
753  $this->errors[] = $langs->trans('CronFieldMandatory', $langs->transnoentitiesnoconv('CronLib'));
754  $error++;
755  }
756 
757 
758  // Update request
759  $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
760  $sql .= " entity=".(isset($this->entity) ? $this->db->escape($this->entity) : $conf->entity).",";
761  $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
762  $sql .= " jobtype=".(isset($this->jobtype) ? "'".$this->db->escape($this->jobtype)."'" : "null").",";
763  $sql .= " command=".(isset($this->command) ? "'".$this->db->escape($this->command)."'" : "null").",";
764  $sql .= " classesname=".(isset($this->classesname) ? "'".$this->db->escape($this->classesname)."'" : "null").",";
765  $sql .= " objectname=".(isset($this->objectname) ? "'".$this->db->escape($this->objectname)."'" : "null").",";
766  $sql .= " methodename=".(isset($this->methodename) ? "'".$this->db->escape($this->methodename)."'" : "null").",";
767  $sql .= " params=".(isset($this->params) ? "'".$this->db->escape($this->params)."'" : "null").",";
768  $sql .= " md5params=".(isset($this->md5params) ? "'".$this->db->escape($this->md5params)."'" : "null").",";
769  $sql .= " module_name=".(isset($this->module_name) ? "'".$this->db->escape($this->module_name)."'" : "null").",";
770  $sql .= " priority=".(isset($this->priority) ? $this->priority : "null").",";
771  $sql .= " datelastrun=".(dol_strlen($this->datelastrun) != 0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
772  $sql .= " datenextrun=".(dol_strlen($this->datenextrun) != 0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
773  $sql .= " dateend=".(dol_strlen($this->dateend) != 0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
774  $sql .= " datestart=".(dol_strlen($this->datestart) != 0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
775  $sql .= " datelastresult=".(dol_strlen($this->datelastresult) != 0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
776  $sql .= " lastresult=".(isset($this->lastresult) ? "'".$this->db->escape($this->lastresult)."'" : "null").",";
777  $sql .= " lastoutput=".(isset($this->lastoutput) ? "'".$this->db->escape($this->lastoutput)."'" : "null").",";
778  $sql .= " unitfrequency=".(isset($this->unitfrequency) ? $this->unitfrequency : "null").",";
779  $sql .= " frequency=".(isset($this->frequency) ? $this->frequency : "null").",";
780  $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
781  $sql .= " processing=".((isset($this->processing) && $this->processing > 0) ? $this->processing : "0").",";
782  $sql .= " fk_user_mod=".$user->id.",";
783  $sql .= " note=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
784  $sql .= " nbrun=".((isset($this->nbrun) && $this->nbrun > 0) ? $this->nbrun : "null").",";
785  $sql .= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0) ? $this->maxrun : "0").",";
786  $sql .= " libname=".(isset($this->libname) ? "'".$this->db->escape($this->libname)."'" : "null").",";
787  $sql .= " test=".(isset($this->test) ? "'".$this->db->escape($this->test)."'" : "null");
788  $sql .= " WHERE rowid=".((int) $this->id);
789 
790  $this->db->begin();
791 
792  dol_syslog(get_class($this)."::update", LOG_DEBUG);
793  $resql = $this->db->query($sql);
794  if (!$resql) {
795  $error++; $this->errors[] = "Error ".$this->db->lasterror();
796  }
797 
798  // Commit or rollback
799  if ($error) {
800  foreach ($this->errors as $errmsg) {
801  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
802  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
803  }
804  $this->db->rollback();
805  return -1 * $error;
806  } else {
807  $this->db->commit();
808  return 1;
809  }
810  }
811 
812 
820  public function delete($user, $notrigger = 0)
821  {
822  $error = 0;
823 
824  $this->db->begin();
825 
826  $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
827  $sql .= " WHERE rowid=".((int) $this->id);
828 
829  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
830  $resql = $this->db->query($sql);
831  if (!$resql) {
832  $error++;
833  $this->errors[] = "Error ".$this->db->lasterror();
834  }
835 
836  // Commit or rollback
837  if ($error) {
838  foreach ($this->errors as $errmsg) {
839  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
840  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
841  }
842  $this->db->rollback();
843  return -1 * $error;
844  } else {
845  $this->db->commit();
846  return 1;
847  }
848  }
849 
850 
851 
859  public function createFromClone(User $user, $fromid)
860  {
861  global $langs;
862 
863  $error = 0;
864 
865  $object = new Cronjob($this->db);
866 
867  $this->db->begin();
868 
869  // Load source object
870  $object->fetch($fromid);
871  $object->id = 0;
872 
873  // Clear fields
874  $object->status = self::STATUS_DISABLED;
875  $object->label = $langs->trans("CopyOf").' '.$langs->trans($object->label);
876 
877  // Create clone
878  $object->context['createfromclone'] = 'createfromclone';
879  $result = $object->create($user);
880 
881  // Other options
882  if ($result < 0) {
883  $this->error = $object->error;
884  $error++;
885  }
886 
887  unset($object->context['createfromclone']);
888 
889  // End
890  if (!$error) {
891  $this->db->commit();
892  return $object->id;
893  } else {
894  $this->db->rollback();
895  return -1;
896  }
897  }
898 
899 
906  public function initAsSpecimen()
907  {
908  $this->id = 0;
909  $this->ref = 0;
910  $this->entity = 0;
911  $this->tms = '';
912  $this->datec = '';
913  $this->label = '';
914  $this->jobtype = '';
915  $this->command = '';
916  $this->classesname = '';
917  $this->objectname = '';
918  $this->methodename = '';
919  $this->params = '';
920  $this->md5params = '';
921  $this->module_name = '';
922  $this->priority = '';
923  $this->datelastrun = '';
924  $this->datenextrun = '';
925  $this->dateend = '';
926  $this->datestart = '';
927  $this->datelastresult = '';
928  $this->lastoutput = '';
929  $this->lastresult = '';
930  $this->unitfrequency = '';
931  $this->frequency = '';
932  $this->status = 0;
933  $this->processing = 0;
934  $this->fk_user_author = 0;
935  $this->fk_user_mod = 0;
936  $this->note_private = '';
937  $this->nbrun = '';
938  $this->maxrun = 100;
939  $this->libname = '';
940  }
941 
942 
953  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
954  {
955  global $db, $conf, $langs;
956  global $dolibarr_main_authentication, $dolibarr_main_demo;
957  global $menumanager;
958 
959  if (!empty($conf->dol_no_mouse_hover)) {
960  $notooltip = 1; // Force disable tooltips
961  }
962 
963  $result = '';
964 
965  $label = img_picto('', 'object_'.$this->picto).' <u>'.$langs->trans("CronTask").'</u>';
966  if (isset($this->status)) {
967  $label .= ' '.$this->getLibStatut(5);
968  }
969  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
970  $label .= '<br><b>'.$langs->trans('Title').':</b> '.$langs->trans($this->label);
971  if ($this->label != $langs->trans($this->label)) {
972  $label .= ' <span class="opacitymedium">('.$this->label.')</span>';
973  }
974  if (!empty($this->datestart)) {
975  $label .= '<br><b>'.$langs->trans('CronDtStart').':</b> '.dol_print_date($this->datestart, 'dayhour', 'tzuserrel');
976  }
977  if (!empty($this->dateend)) {
978  $label .= '<br><b>'.$langs->trans('CronDtEnd').':</b> '.dol_print_date($this->dateend, 'dayhour', 'tzuserrel');
979  }
980 
981  $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
982 
983  if ($option != 'nolink') {
984  // Add param to save lastsearch_values or not
985  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
986  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
987  $add_save_lastsearch_values = 1;
988  }
989  if ($add_save_lastsearch_values) {
990  $url .= '&save_lastsearch_values=1';
991  }
992  }
993 
994  $linkclose = '';
995  if (empty($notooltip)) {
996  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
997  $label = $langs->trans("ShowCronJob");
998  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
999  }
1000  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
1001  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
1002  } else {
1003  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1004  }
1005 
1006  $linkstart = '<a href="'.$url.'"';
1007  $linkstart .= $linkclose.'>';
1008  $linkend = '</a>';
1009 
1010  $result .= $linkstart;
1011  if ($withpicto) {
1012  $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);
1013  }
1014  if ($withpicto != 2) {
1015  $result .= $this->ref;
1016  }
1017  $result .= $linkend;
1018  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
1019 
1020  return $result;
1021  }
1022 
1023 
1030  public function info($id)
1031  {
1032  $sql = "SELECT";
1033  $sql .= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
1034  $sql .= " FROM ".MAIN_DB_PREFIX."cronjob as f";
1035  $sql .= " WHERE f.rowid = ".((int) $id);
1036 
1037  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
1038  $resql = $this->db->query($sql);
1039  if ($resql) {
1040  if ($this->db->num_rows($resql)) {
1041  $obj = $this->db->fetch_object($resql);
1042  $this->id = $obj->rowid;
1043 
1044  $this->user_modification_id = $obj->fk_user_mod;
1045  $this->user_creation_id = $obj->fk_user_author;
1046  $this->date_creation = $this->db->jdate($obj->datec);
1047  $this->date_modification = $this->db->jdate($obj->tms);
1048  }
1049  $this->db->free($resql);
1050 
1051  return 1;
1052  } else {
1053  $this->error = "Error ".$this->db->lasterror();
1054  return -1;
1055  }
1056  }
1057 
1058 
1059  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1068  public function run_jobs($userlogin)
1069  {
1070  // phpcs:enable
1071  global $langs, $conf, $hookmanager;
1072 
1073  $hookmanager->initHooks(array('cron'));
1074 
1075  $now = dol_now();
1076  $error = 0;
1077  $retval = '';
1078 
1079  $langs->load('cron');
1080 
1081  if (empty($userlogin)) {
1082  $this->error = "User login is mandatory";
1083  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1084  return -1;
1085  }
1086 
1087  // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
1088  // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
1089  if ($conf->entity != $this->entity && $this->entity > 0) {
1090  dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
1091  }
1092  $savcurrententity = $conf->entity;
1093  $conf->setEntityValues($this->db, $this->entity);
1094  dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
1095 
1096  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1097  $user = new User($this->db);
1098  $result = $user->fetch('', $userlogin);
1099  if ($result < 0) {
1100  $this->error = "User Error:".$user->error;
1101  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1102  $conf->setEntityValues($this->db, $savcurrententity);
1103  return -1;
1104  } else {
1105  if (empty($user->id)) {
1106  $this->error = " User user login:".$userlogin." do not exists";
1107  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1108  $conf->setEntityValues($this->db, $savcurrententity);
1109  return -1;
1110  }
1111  }
1112 
1113  dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
1114 
1115  // Increase limit of time. Works only if we are not in safe mode
1116  $ExecTimeLimit = 600;
1117  if (!empty($ExecTimeLimit)) {
1118  $err = error_reporting();
1119  error_reporting(0); // Disable all errors
1120  //error_reporting(E_ALL);
1121  @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
1122  error_reporting($err);
1123  }
1124  if (!empty($MemoryLimit)) {
1125  @ini_set('memory_limit', $MemoryLimit);
1126  }
1127 
1128  // Update last run date start (to track running jobs)
1129  $this->datelastrun = $now;
1130  $this->datelastresult = null;
1131  $this->lastoutput = '';
1132  $this->lastresult = '';
1133  $this->processing = 1; // To know job was started
1134  $this->nbrun = $this->nbrun + 1;
1135  $result = $this->update($user); // This include begin/commit
1136  if ($result < 0) {
1137  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1138  $conf->setEntityValues($this->db, $savcurrententity);
1139  return -1;
1140  }
1141 
1142  // Run a method
1143  if ($this->jobtype == 'method') {
1144  // load classes
1145  if (!$error) {
1146  $ret = dol_include_once($this->classesname);
1147  if ($ret === false || (!class_exists($this->objectname))) {
1148  if ($ret === false) {
1149  $this->error = $langs->transnoentitiesnoconv('CronCannotLoadClass', $this->classesname, $this->objectname);
1150  } else {
1151  $this->error = $langs->transnoentitiesnoconv('CronCannotLoadObject', $this->classesname, $this->objectname);
1152  }
1153  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1154  $this->lastoutput = $this->error;
1155  $this->lastresult = -1;
1156  $retval = $this->lastresult;
1157  $error++;
1158  }
1159  }
1160 
1161  // test if method exists
1162  if (!$error) {
1163  if (!method_exists($this->objectname, $this->methodename)) {
1164  $this->error = $langs->transnoentitiesnoconv('CronMethodDoesNotExists', $this->objectname, $this->methodename);
1165  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1166  $this->lastoutput = $this->error;
1167  $this->lastresult = -1;
1168  $retval = $this->lastresult;
1169  $error++;
1170  }
1171  if (in_array(strtolower(trim($this->methodename)), array('executecli'))) {
1172  $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname);
1173  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1174  $this->lastoutput = $this->error;
1175  $this->lastresult = -1;
1176  $retval = $this->lastresult;
1177  $error++;
1178  }
1179  }
1180 
1181  // Load langs
1182  if (!$error) {
1183  $result = $langs->load($this->module_name);
1184  $result = $langs->load($this->module_name.'@'.$this->module_name, 0, 0, '', 0, 1);
1185 
1186  if ($result < 0) { // If technical error
1187  dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
1188  $this->error = $langs->error;
1189  $this->lastoutput = $this->error;
1190  $this->lastresult = -1;
1191  $retval = $this->lastresult;
1192  $error++;
1193  }
1194  }
1195 
1196  if (!$error) {
1197  dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
1198 
1199  // Create Object for the called module
1200  $nameofclass = $this->objectname;
1201  $object = new $nameofclass($this->db);
1202  if ($this->entity > 0) {
1203  $object->entity = $this->entity; // We work on a dedicated entity
1204  }
1205 
1206  $params_arr = array();
1207  if (!empty($this->params) || $this->params === '0') {
1208  $params_arr = array_map('trim', explode(",", $this->params));
1209  }
1210 
1211  if (!is_array($params_arr)) {
1212  $result = call_user_func(array($object, $this->methodename), $this->params);
1213  } else {
1214  $result = call_user_func_array(array($object, $this->methodename), $params_arr);
1215  }
1216 
1217  if ($result === false || (!is_bool($result) && $result != 0)) {
1218  $langs->load("errors");
1219 
1220  $errmsg = '';
1221  if (!is_array($object->errors) || !in_array($object->error, $object->errors)) {
1222  $errmsg .= $object->error;
1223  }
1224  if (is_array($object->errors) && count($object->errors)) {
1225  $errmsg .= (($errmsg ? ', ' : '').join(', ', $object->errors));
1226  }
1227  if (empty($errmsg)) {
1228  $errmsg = $langs->trans('ErrorUnknown');
1229  }
1230 
1231  dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
1232 
1233  $this->error = $errmsg;
1234  $this->lastoutput = ($object->output ? $object->output."\n" : "").$errmsg;
1235  $this->lastresult = is_numeric($result) ? $result : -1;
1236  $retval = $this->lastresult;
1237  $error++;
1238  } else {
1239  dol_syslog(get_class($this)."::run_jobs END");
1240  $this->lastoutput = $object->output;
1241  $this->lastresult = var_export($result, true);
1242  $retval = $this->lastresult;
1243  }
1244  }
1245  }
1246 
1247  if ($this->jobtype == 'function') {
1248  //load lib
1249  $libpath = '/'.strtolower($this->module_name).'/lib/'.$this->libname;
1250  $ret = dol_include_once($libpath);
1251  if ($ret === false) {
1252  $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath;
1253  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1254  $conf->setEntityValues($this->db, $savcurrententity);
1255  return -1;
1256  }
1257 
1258  // Load langs
1259  $result = $langs->load($this->module_name);
1260  $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing
1261  if ($result < 0) { // If technical error
1262  dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
1263  $conf->setEntityValues($this->db, $savcurrententity);
1264  return -1;
1265  }
1266 
1267  dol_syslog(get_class($this)."::run_jobs ".$this->libname."::".$this->methodename."(".$this->params.");", LOG_DEBUG);
1268  $params_arr = explode(", ", $this->params);
1269  if (!is_array($params_arr)) {
1270  $result = call_user_func($this->methodename, $this->params);
1271  } else {
1272  $result = call_user_func_array($this->methodename, $params_arr);
1273  }
1274 
1275  if ($result === false || (!is_bool($result) && $result != 0)) {
1276  $langs->load("errors");
1277  dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
1278  $this->error = $langs->trans('ErrorUnknown');
1279  $this->lastoutput = $this->error;
1280  $this->lastresult = is_numeric($result) ? $result : -1;
1281  $retval = $this->lastresult;
1282  $error++;
1283  } else {
1284  $this->lastoutput = var_export($result, true);
1285  $this->lastresult = var_export($result, true); // Return code
1286  $retval = $this->lastresult;
1287  }
1288  }
1289 
1290  // Run a command line
1291  if ($this->jobtype == 'command') {
1292  global $dolibarr_cron_allow_cli;
1293 
1294  if (empty($dolibarr_cron_allow_cli)) {
1295  $langs->load("errors");
1296  $this->error = $langs->trans("FailedToExecutCommandJob");
1297  $this->lastoutput = '';
1298  $this->lastresult = $langs->trans("ErrorParameterMustBeEnabledToAllwoThisFeature", 'dolibarr_cron_allow_cli');
1299  } else {
1300  $outputdir = $conf->cron->dir_temp;
1301  if (empty($outputdir)) {
1302  $outputdir = $conf->cronjob->dir_temp;
1303  }
1304 
1305  if (!empty($outputdir)) {
1306  dol_mkdir($outputdir);
1307  $outputfile = $outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
1308 
1309  // Execute a CLI
1310  include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1311  $utils = new Utils($this->db);
1312  $arrayresult = $utils->executeCLI($this->command, $outputfile);
1313 
1314  $retval = $arrayresult['result'];
1315  $this->error = $arrayresult['error'];
1316  $this->lastoutput = $arrayresult['output'];
1317  $this->lastresult = $arrayresult['result'];
1318  }
1319  }
1320  }
1321 
1322  dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
1323 
1324  $this->datelastresult = dol_now();
1325  $this->processing = 0;
1326  $result = $this->update($user); // This include begin/commit
1327  if ($result < 0) {
1328  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1329  $conf->setEntityValues($this->db, $savcurrententity);
1330  return -1;
1331  }
1332 
1333  $conf->setEntityValues($this->db, $savcurrententity);
1334  return $error ?-1 : 1;
1335  }
1336 
1337 
1338  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1346  public function reprogram_jobs($userlogin, $now)
1347  {
1348  // phpcs:enable
1349  dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1350 
1351  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1352  $user = new User($this->db);
1353  $result = $user->fetch('', $userlogin);
1354  if ($result < 0) {
1355  $this->error = "User Error : ".$user->error;
1356  dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1357  return -1;
1358  } else {
1359  if (empty($user->id)) {
1360  $this->error = " User user login:".$userlogin." do not exists";
1361  dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1362  return -1;
1363  }
1364  }
1365 
1366  dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1367 
1368  if (empty($this->datenextrun)) {
1369  if (empty($this->datestart)) {
1370  $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1371  } else {
1372  $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1373  }
1374  }
1375 
1376  if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) {
1377  // Loop until date is after future
1378  while ($this->datenextrun < $now) {
1379  $this->datenextrun += ($this->frequency * $this->unitfrequency);
1380 
1381  // TODO For exact frequency (every month, every year, ...), use instead a dol_time_plus_duree($time, $duration_value, $duration_unit)
1382  }
1383  } else {
1384  //$this->datenextrun=$this->datenextrun + ($this->frequency * $this->unitfrequency);
1385  dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
1386  }
1387 
1388 
1389  // Archive job
1390  if ($this->autodelete == 2) {
1391  if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1392  || ($this->dateend && ($this->datenextrun > $this->dateend))) {
1393  $this->status = self::STATUS_ARCHIVED;
1394  dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
1395  }
1396  }
1397 
1398  $result = $this->update($user);
1399  if ($result < 0) {
1400  dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1401  return -1;
1402  }
1403 
1404  return 1;
1405  }
1406 
1413  public function getLibStatut($mode = 0)
1414  {
1415  return $this->LibStatut($this->status, $mode, $this->processing, $this->lastresult);
1416  }
1417 
1418  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1428  public function LibStatut($status, $mode = 0, $processing = 0, $lastresult = 0)
1429  {
1430  // phpcs:enable
1431  $this->labelStatus = array(); // Force reset o array because label depends on other fields
1432  $this->labelStatusShort = array();
1433 
1434  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
1435  global $langs;
1436  $langs->load('users');
1437 
1438  $moretext = '';
1439  if ($processing) {
1440  $moretext = ' ('.$langs->trans("Running").')';
1441  } elseif ($lastresult) {
1442  $moretext .= ' ('.$langs->trans("Error").')';
1443  }
1444 
1445  $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled').$moretext;
1446  $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled').$moretext;
1447  $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv('Disabled');
1448  $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv('Scheduled');
1449  }
1450 
1451  $statusType = 'status4';
1452  if ($status == 1 && $processing) {
1453  $statusType = 'status1';
1454  }
1455  if ($status == 0) {
1456  $statusType = 'status5';
1457  }
1458  if ($this->lastresult) {
1459  $statusType = 'status8';
1460  }
1461 
1462  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
1463  }
1464 }
1465 
1466 
1471 {
1472 
1476  public $id;
1477 
1478  public $entity;
1479 
1483  public $ref;
1484 
1485  public $tms = '';
1486  public $datec = '';
1487 
1491  public $label;
1492 
1493  public $jobtype;
1494  public $command;
1495  public $classesname;
1496  public $objectname;
1497  public $methodename;
1498  public $params;
1499  public $md5params;
1500  public $module_name;
1501  public $priority;
1502  public $datelastrun = '';
1503  public $datenextrun = '';
1504  public $dateend = '';
1505  public $datestart = '';
1506  public $lastresult = '';
1507  public $lastoutput;
1508  public $unitfrequency;
1509  public $frequency;
1510 
1514  public $status;
1515 
1519  public $fk_user_author;
1520 
1524  public $fk_user_mod;
1525 
1526  public $note;
1527  public $nbrun;
1528  public $libname;
1529 
1534  public function __construct()
1535  {
1536  return 1;
1537  }
1538 }
db
$conf db
API class for accounts.
Definition: inc.php:41
Cronjob
Cron Job class.
Definition: cronjob.class.php:31
Cronjobline\__construct
__construct()
Constructor.
Definition: cronjob.class.php:1534
Utils
Class to manage utility methods.
Definition: utils.class.php:30
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
ref
$object ref
Definition: info.php:77
Cronjob\reprogram_jobs
reprogram_jobs($userlogin, $now)
Reprogram a job.
Definition: cronjob.class.php:1346
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Cronjob\update
update($user=null, $notrigger=0)
Update object into database.
Definition: cronjob.class.php:643
Cronjob\info
info($id)
Load object information.
Definition: cronjob.class.php:1030
Cronjob\fetch
fetch($id, $objectname='', $methodname='')
Load object in memory from the database.
Definition: cronjob.class.php:403
Cronjobline
Crob Job line class.
Definition: cronjob.class.php:1470
Cronjob\getLibStatut
getLibStatut($mode=0)
Return label of status of user (active, inactive)
Definition: cronjob.class.php:1413
Cronjob\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
Definition: cronjob.class.php:953
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Cronjob\create
create($user, $notrigger=0)
Create object into database.
Definition: cronjob.class.php:199
Cronjob\__construct
__construct($db)
Constructor.
Definition: cronjob.class.php:186
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
User
Class to manage Dolibarr users.
Definition: user.class.php:44
Cronjob\LibStatut
LibStatut($status, $mode=0, $processing=0, $lastresult=0)
Renvoi le libelle d'un statut donne.
Definition: cronjob.class.php:1428
Cronjob\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: cronjob.class.php:906
Cronjob\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: cronjob.class.php:859
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
Cronjob\fetchAll
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...
Definition: cronjob.class.php:509
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6603
Cronjob\run_jobs
run_jobs($userlogin)
Run a job.
Definition: cronjob.class.php:1068