dolibarr  7.0.0-beta
cronjob.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 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 <http://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 {
33  public $element='cronjob';
34  public $table_element='cronjob';
35  public $picto = 'cron';
36 
37  public $jobtype;
38  public $tms='';
39  public $datec='';
40  public $label;
41  public $command;
42  public $classesname;
43  public $objectname;
44  public $methodename;
45  public $params;
46  public $md5params;
47  public $module_name;
48  public $priority;
49  public $datelastrun='';
50  public $datenextrun='';
51  public $dateend='';
52  public $datestart='';
53  public $datelastresult='';
54  public $lastresult;
55  public $lastoutput;
56  public $unitfrequency;
57  public $frequency;
58  public $status;
59  public $processing;
60  public $fk_user_author;
61  public $fk_user_mod;
62  public $nbrun;
63  public $libname;
64  public $test; // A test condition to know if job is visible/qualified
65 
71  function __construct($db)
72  {
73  $this->db = $db;
74  return 1;
75  }
76 
77 
85  function create($user, $notrigger=0)
86  {
87  global $conf, $langs;
88  $error=0;
89 
90  $now=dol_now();
91 
92  // Clean parameters
93 
94  if (isset($this->label)) $this->label=trim($this->label);
95  if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
96  if (isset($this->command)) $this->command=trim($this->command);
97  if (isset($this->classesname)) $this->classesname=trim($this->classesname);
98  if (isset($this->objectname)) $this->objectname=trim($this->objectname);
99  if (isset($this->methodename)) $this->methodename=trim($this->methodename);
100  if (isset($this->params)) $this->params=trim($this->params);
101  if (isset($this->md5params)) $this->md5params=trim($this->md5params);
102  if (isset($this->module_name)) $this->module_name=trim($this->module_name);
103  if (isset($this->priority)) $this->priority=trim($this->priority);
104  if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
105  if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
106  if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
107  if (isset($this->frequency)) $this->frequency=trim($this->frequency);
108  if (isset($this->status)) $this->status=trim($this->status);
109  if (isset($this->note)) $this->note=trim($this->note);
110  if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
111  if (isset($this->libname)) $this->libname = trim($this->libname);
112  if (isset($this->test)) $this->test = trim($this->test);
113 
114  // Check parameters
115  // Put here code to add a control on parameters values
116  if (dol_strlen($this->datestart)==0) {
117  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart'));
118  $error++;
119  }
120  if (empty($this->label)) {
121  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel'));
122  $error++;
123  }
124  if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
125  $this->errors[]=$langs->trans('CronErrEndDateStartDt');
126  $error++;
127  }
128  if (empty($this->unitfrequency)) {
129  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency'));
130  $error++;
131  }
132  if (($this->jobtype=='command') && (empty($this->command))) {
133  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand'));
134  $error++;
135  }
136  if (($this->jobtype=='method') && (empty($this->classesname))) {
137  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass'));
138  $error++;
139  }
140  if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
141  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod'));
142  $error++;
143  }
144  if (($this->jobtype=='method') && (empty($this->objectname))) {
145  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject'));
146  $error++;
147  }
148 
149  if (($this->jobtype=='function') && (empty($this->libname))) {
150  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib'));
151  $error++;
152  }
153 
154  // Insert request
155  $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
156 
157  $sql.= "datec,";
158  $sql.= "jobtype,";
159  $sql.= "label,";
160  $sql.= "command,";
161  $sql.= "classesname,";
162  $sql.= "objectname,";
163  $sql.= "methodename,";
164  $sql.= "params,";
165  $sql.= "md5params,";
166  $sql.= "module_name,";
167  $sql.= "priority,";
168  $sql.= "datelastrun,";
169  $sql.= "datenextrun,";
170  $sql.= "dateend,";
171  $sql.= "datestart,";
172  $sql.= "lastresult,";
173  $sql.= "datelastresult,";
174  $sql.= "lastoutput,";
175  $sql.= "unitfrequency,";
176  $sql.= "frequency,";
177  $sql.= "status,";
178  $sql.= "fk_user_author,";
179  $sql.= "fk_user_mod,";
180  $sql.= "note,";
181  $sql.= "nbrun,";
182  $sql.= "maxrun,";
183  $sql.= "libname,";
184  $sql.= "test";
185  $sql.= ") VALUES (";
186  $sql.= " '".$this->db->idate($now)."',";
187  $sql.= " ".(! isset($this->jobtype)?'NULL':"'".$this->db->escape($this->jobtype)."'").",";
188  $sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").",";
189  $sql.= " ".(! isset($this->command)?'NULL':"'".$this->db->escape($this->command)."'").",";
190  $sql.= " ".(! isset($this->classesname)?'NULL':"'".$this->db->escape($this->classesname)."'").",";
191  $sql.= " ".(! isset($this->objectname)?'NULL':"'".$this->db->escape($this->objectname)."'").",";
192  $sql.= " ".(! isset($this->methodename)?'NULL':"'".$this->db->escape($this->methodename)."'").",";
193  $sql.= " ".(! isset($this->params)?'NULL':"'".$this->db->escape($this->params)."'").",";
194  $sql.= " ".(! isset($this->md5params)?'NULL':"'".$this->db->escape($this->md5params)."'").",";
195  $sql.= " ".(! isset($this->module_name)?'NULL':"'".$this->db->escape($this->module_name)."'").",";
196  $sql.= " ".(! isset($this->priority)?'0':$this->priority).",";
197  $sql.= " ".(! isset($this->datelastrun) || dol_strlen($this->datelastrun)==0?'NULL':"'".$this->db->idate($this->datelastrun)."'").",";
198  $sql.= " ".(! isset($this->datenextrun) || dol_strlen($this->datenextrun)==0?'NULL':"'".$this->db->idate($this->datenextrun)."'").",";
199  $sql.= " ".(! isset($this->dateend) || dol_strlen($this->dateend)==0?'NULL':"'".$this->db->idate($this->dateend)."'").",";
200  $sql.= " ".(! isset($this->datestart) || dol_strlen($this->datestart)==0?'NULL':"'".$this->db->idate($this->datestart)."'").",";
201  $sql.= " ".(! isset($this->lastresult)?'NULL':"'".$this->db->escape($this->lastresult)."'").",";
202  $sql.= " ".(! isset($this->datelastresult) || dol_strlen($this->datelastresult)==0?'NULL':"'".$this->db->idate($this->datelastresult)."'").",";
203  $sql.= " ".(! isset($this->lastoutput)?'NULL':"'".$this->db->escape($this->lastoutput)."'").",";
204  $sql.= " ".(! isset($this->unitfrequency)?'NULL':"'".$this->db->escape($this->unitfrequency)."'").",";
205  $sql.= " ".(! isset($this->frequency)?'0':$this->frequency).",";
206  $sql.= " ".(! isset($this->status)?'0':$this->status).",";
207  $sql.= " ".$user->id.",";
208  $sql.= " ".$user->id.",";
209  $sql.= " ".(! isset($this->note)?'NULL':"'".$this->db->escape($this->note)."'").",";
210  $sql.= " ".(! isset($this->nbrun)?'0':$this->db->escape($this->nbrun)).",";
211  $sql.= " ".(empty($this->maxrun)?'0':$this->db->escape($this->maxrun)).",";
212  $sql.= " ".(! isset($this->libname)?'NULL':"'".$this->db->escape($this->libname)."'").",";
213  $sql.= " ".(! isset($this->test)?'NULL':"'".$this->db->escape($this->test)."'")."";
214  $sql.= ")";
215 
216  $this->db->begin();
217 
218  dol_syslog(get_class($this)."::create", LOG_DEBUG);
219  $resql=$this->db->query($sql);
220  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
221 
222  if (! $error)
223  {
224  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
225 
226  if (! $notrigger)
227  {
228  // Uncomment this and change MYOBJECT to your own tag if you
229  // want this action calls a trigger.
230 
232  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
233  //$interface=new Interfaces($this->db);
234  //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
235  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
237  }
238  }
239 
240  // Commit or rollback
241  if ($error)
242  {
243  foreach($this->errors as $errmsg)
244  {
245  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
246  $this->error.=($this->error?', '.$errmsg:$errmsg);
247  }
248  $this->db->rollback();
249  return -1*$error;
250  }
251  else
252  {
253  $this->db->commit();
254  return $this->id;
255  }
256  }
257 
258 
265  function fetch($id)
266  {
267  $sql = "SELECT";
268  $sql.= " t.rowid,";
269 
270  $sql.= " t.tms,";
271  $sql.= " t.datec,";
272  $sql.= " t.jobtype,";
273  $sql.= " t.label,";
274  $sql.= " t.command,";
275  $sql.= " t.classesname,";
276  $sql.= " t.objectname,";
277  $sql.= " t.methodename,";
278  $sql.= " t.params,";
279  $sql.= " t.md5params,";
280  $sql.= " t.module_name,";
281  $sql.= " t.priority,";
282  $sql.= " t.datelastrun,";
283  $sql.= " t.datenextrun,";
284  $sql.= " t.dateend,";
285  $sql.= " t.datestart,";
286  $sql.= " t.lastresult,";
287  $sql.= " t.datelastresult,";
288  $sql.= " t.lastoutput,";
289  $sql.= " t.unitfrequency,";
290  $sql.= " t.frequency,";
291  $sql.= " t.status,";
292  $sql.= " t.processing,";
293  $sql.= " t.fk_user_author,";
294  $sql.= " t.fk_user_mod,";
295  $sql.= " t.note,";
296  $sql.= " t.nbrun,";
297  $sql.= " t.maxrun,";
298  $sql.= " t.libname,";
299  $sql.= " t.test";
300  $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
301  $sql.= " WHERE t.rowid = ".$id;
302 
303  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
304  $resql=$this->db->query($sql);
305  if ($resql)
306  {
307  if ($this->db->num_rows($resql))
308  {
309  $obj = $this->db->fetch_object($resql);
310 
311  $this->id = $obj->rowid;
312  $this->ref = $obj->rowid;
313 
314  $this->tms = $this->db->jdate($obj->tms);
315  $this->datec = $this->db->jdate($obj->datec);
316  $this->label = $obj->label;
317  $this->jobtype = $obj->jobtype;
318  $this->command = $obj->command;
319  $this->classesname = $obj->classesname;
320  $this->objectname = $obj->objectname;
321  $this->methodename = $obj->methodename;
322  $this->params = $obj->params;
323  $this->md5params = $obj->md5params;
324  $this->module_name = $obj->module_name;
325  $this->priority = $obj->priority;
326  $this->datelastrun = $this->db->jdate($obj->datelastrun);
327  $this->datenextrun = $this->db->jdate($obj->datenextrun);
328  $this->dateend = $this->db->jdate($obj->dateend);
329  $this->datestart = $this->db->jdate($obj->datestart);
330  $this->lastresult = $obj->lastresult;
331  $this->lastoutput = $obj->lastoutput;
332  $this->datelastresult = $this->db->jdate($obj->datelastresult);
333  $this->unitfrequency = $obj->unitfrequency;
334  $this->frequency = $obj->frequency;
335  $this->status = $obj->status;
336  $this->processing = $obj->processing;
337  $this->fk_user_author = $obj->fk_user_author;
338  $this->fk_user_mod = $obj->fk_user_mod;
339  $this->note = $obj->note;
340  $this->nbrun = $obj->nbrun;
341  $this->maxrun = $obj->maxrun;
342  $this->libname = $obj->libname;
343  $this->test = $obj->test;
344  }
345  $this->db->free($resql);
346 
347  return 1;
348  }
349  else
350  {
351  $this->error="Error ".$this->db->lasterror();
352  return -1;
353  }
354  }
355 
368  function fetch_all($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='', $processing=-1)
369  {
370  global $langs;
371 
372  $this->lines=array();
373 
374  $sql = "SELECT";
375  $sql.= " t.rowid,";
376  $sql.= " t.entity,";
377  $sql.= " t.tms,";
378  $sql.= " t.datec,";
379  $sql.= " t.jobtype,";
380  $sql.= " t.label,";
381  $sql.= " t.command,";
382  $sql.= " t.classesname,";
383  $sql.= " t.objectname,";
384  $sql.= " t.methodename,";
385  $sql.= " t.params,";
386  $sql.= " t.md5params,";
387  $sql.= " t.module_name,";
388  $sql.= " t.priority,";
389  $sql.= " t.datelastrun,";
390  $sql.= " t.datenextrun,";
391  $sql.= " t.dateend,";
392  $sql.= " t.datestart,";
393  $sql.= " t.lastresult,";
394  $sql.= " t.datelastresult,";
395  $sql.= " t.lastoutput,";
396  $sql.= " t.unitfrequency,";
397  $sql.= " t.frequency,";
398  $sql.= " t.status,";
399  $sql.= " t.processing,";
400  $sql.= " t.fk_user_author,";
401  $sql.= " t.fk_user_mod,";
402  $sql.= " t.note,";
403  $sql.= " t.nbrun,";
404  $sql.= " t.libname,";
405  $sql.= " t.test";
406  $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
407  $sql.= " WHERE 1 = 1";
408  if ($processing >= 0) $sql.= " AND t.processing = ".(empty($processing)?'0':'1');
409  if ($status >= 0 && $status < 2) $sql.= " AND t.status = ".(empty($status)?'0':'1');
410  if ($status == 2) $sql.= " AND t.status = 2";
411  //Manage filter
412  if (is_array($filter) && count($filter)>0) {
413  foreach($filter as $key => $value)
414  {
415  if ($key == 't.rowid') $sql.= ' AND '.$key.' = '.$this->db->escape($value);
416  else $sql.= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
417  }
418  }
419 
420  $sql.= " ORDER BY $sortfield $sortorder ";
421  if (!empty($limit) && !empty($offset)) {
422  $sql.= $this->db->plimit($limit + 1,$offset);
423  }
424 
425  $sqlwhere = array();
426 
427  if (count($sqlwhere)>0) {
428  $sql.= " WHERE ".implode(' AND ',$sqlwhere);
429  }
430 
431  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
432  $resql=$this->db->query($sql);
433  if ($resql)
434  {
435  $num=$this->db->num_rows($resql);
436  $i=0;
437 
438  if ($num)
439  {
440  while ($i < $num)
441  {
442  $line = new Cronjobline();
443 
444  $obj = $this->db->fetch_object($resql);
445 
446  $line->id = $obj->rowid;
447  $line->ref = $obj->rowid;
448 
449  $line->entity = $obj->entity;
450  $line->tms = $this->db->jdate($obj->tms);
451  $line->datec = $this->db->jdate($obj->datec);
452  $line->label = $obj->label;
453  $line->jobtype = $obj->jobtype;
454  $line->command = $obj->command;
455  $line->classesname = $obj->classesname;
456  $line->objectname = $obj->objectname;
457  $line->methodename = $obj->methodename;
458  $line->params = $obj->params;
459  $line->md5params = $obj->md5params;
460  $line->module_name = $obj->module_name;
461  $line->priority = $obj->priority;
462  $line->datelastrun = $this->db->jdate($obj->datelastrun);
463  $line->datenextrun = $this->db->jdate($obj->datenextrun);
464  $line->dateend = $this->db->jdate($obj->dateend);
465  $line->datestart = $this->db->jdate($obj->datestart);
466  $line->lastresult = $obj->lastresult;
467  $line->datelastresult = $this->db->jdate($obj->datelastresult);
468  $line->lastoutput = $obj->lastoutput;
469  $line->unitfrequency = $obj->unitfrequency;
470  $line->frequency = $obj->frequency;
471  $line->status = $obj->status;
472  $line->processing = $obj->processing;
473  $line->fk_user_author = $obj->fk_user_author;
474  $line->fk_user_mod = $obj->fk_user_mod;
475  $line->note = $obj->note;
476  $line->nbrun = $obj->nbrun;
477  $line->libname = $obj->libname;
478  $line->test = $obj->test;
479  $this->lines[]=$line;
480 
481  $i++;
482  }
483  }
484  $this->db->free($resql);
485 
486  return 1;
487  }
488  else
489  {
490  $this->error="Error ".$this->db->lasterror();
491  return -1;
492  }
493  }
494 
495 
503  function update($user=null, $notrigger=0)
504  {
505  global $conf, $langs;
506 
507  $langs->load('cron');
508 
509  $error=0;
510 
511  // Clean parameters
512  if (isset($this->label)) $this->label=trim($this->label);
513  if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
514  if (isset($this->command)) $this->command=trim($this->command);
515  if (isset($this->classesname)) $this->classesname=trim($this->classesname);
516  if (isset($this->objectname)) $this->objectname=trim($this->objectname);
517  if (isset($this->methodename)) $this->methodename=trim($this->methodename);
518  if (isset($this->params)) $this->params=trim($this->params);
519  if (isset($this->md5params)) $this->md5params=trim($this->md5params);
520  if (isset($this->module_name)) $this->module_name=trim($this->module_name);
521  if (isset($this->priority)) $this->priority=trim($this->priority);
522  if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
523  if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
524  if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
525  if (isset($this->frequency)) $this->frequency=trim($this->frequency);
526  if (isset($this->status)) $this->status=trim($this->status);
527  if (isset($this->note)) $this->note=trim($this->note);
528  if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
529  if (isset($this->libname)) $this->libname = trim($this->libname);
530  if (isset($this->test)) $this->test = trim($this->test);
531 
532  if (empty($this->maxrun)) $this->maxrun=0;
533  if (empty($this->processing)) $this->processing=0;
534 
535  // Check parameters
536  // Put here code to add a control on parameters values
537  if (dol_strlen($this->datestart)==0) {
538  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronDtStart'));
539  $error++;
540  }
541  if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
542  $this->errors[]=$langs->trans('CronErrEndDateStartDt');
543  $error++;
544  }
545  if (empty($this->label)) {
546  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronLabel'));
547  $error++;
548  }
549  if (empty($this->unitfrequency)) {
550  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronFrequency'));
551  $error++;
552  }
553  if (($this->jobtype=='command') && (empty($this->command))) {
554  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronCommand'));
555  $error++;
556  }
557  if (($this->jobtype=='method') && (empty($this->classesname))) {
558  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronClass'));
559  $error++;
560  }
561  if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
562  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronMethod'));
563  $error++;
564  }
565  if (($this->jobtype=='method') && (empty($this->objectname))) {
566  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronObject'));
567  $error++;
568  }
569 
570  if (($this->jobtype=='function') && (empty($this->libname))) {
571  $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronLib'));
572  $error++;
573  }
574 
575 
576  // Update request
577  $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
578 
579  $sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
580  $sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").",";
581  $sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").",";
582  $sql.= " classesname=".(isset($this->classesname)?"'".$this->db->escape($this->classesname)."'":"null").",";
583  $sql.= " objectname=".(isset($this->objectname)?"'".$this->db->escape($this->objectname)."'":"null").",";
584  $sql.= " methodename=".(isset($this->methodename)?"'".$this->db->escape($this->methodename)."'":"null").",";
585  $sql.= " params=".(isset($this->params)?"'".$this->db->escape($this->params)."'":"null").",";
586  $sql.= " md5params=".(isset($this->md5params)?"'".$this->db->escape($this->md5params)."'":"null").",";
587  $sql.= " module_name=".(isset($this->module_name)?"'".$this->db->escape($this->module_name)."'":"null").",";
588  $sql.= " priority=".(isset($this->priority)?$this->priority:"null").",";
589  $sql.= " datelastrun=".(dol_strlen($this->datelastrun)!=0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
590  $sql.= " datenextrun=".(dol_strlen($this->datenextrun)!=0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
591  $sql.= " dateend=".(dol_strlen($this->dateend)!=0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
592  $sql.= " datestart=".(dol_strlen($this->datestart)!=0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
593  $sql.= " datelastresult=".(dol_strlen($this->datelastresult)!=0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
594  $sql.= " lastresult=".(isset($this->lastresult)?"'".$this->db->escape($this->lastresult)."'":"null").",";
595  $sql.= " lastoutput=".(isset($this->lastoutput)?"'".$this->db->escape($this->lastoutput)."'":"null").",";
596  $sql.= " unitfrequency=".(isset($this->unitfrequency)?$this->unitfrequency:"null").",";
597  $sql.= " frequency=".(isset($this->frequency)?$this->frequency:"null").",";
598  $sql.= " status=".(isset($this->status)?$this->status:"null").",";
599  $sql.= " processing=".((isset($this->processing) && $this->processing > 0)?$this->processing:"0").",";
600  $sql.= " fk_user_mod=".$user->id.",";
601  $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
602  $sql.= " nbrun=".((isset($this->nbrun) && $this->nbrun >0)?$this->nbrun:"null").",";
603  $sql.= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0)?$this->maxrun:"0").",";
604  $sql.= " libname=".(isset($this->libname)?"'".$this->db->escape($this->libname)."'":"null").",";
605  $sql.= " test=".(isset($this->test)?"'".$this->db->escape($this->test)."'":"null");
606  $sql.= " WHERE rowid=".$this->id;
607 
608  $this->db->begin();
609 
610  dol_syslog(get_class($this)."::update", LOG_DEBUG);
611  $resql = $this->db->query($sql);
612  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
613 
614  if (! $error)
615  {
616  if (! $notrigger)
617  {
618  // Uncomment this and change MYOBJECT to your own tag if you
619  // want this action calls a trigger.
620 
622  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
623  //$interface=new Interfaces($this->db);
624  //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
625  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
627  }
628  }
629 
630  // Commit or rollback
631  if ($error)
632  {
633  foreach($this->errors as $errmsg)
634  {
635  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
636  $this->error.=($this->error?', '.$errmsg:$errmsg);
637  }
638  $this->db->rollback();
639  return -1*$error;
640  }
641  else
642  {
643  $this->db->commit();
644  return 1;
645  }
646  }
647 
648 
656  function delete($user, $notrigger=0)
657  {
658  $error=0;
659 
660  $this->db->begin();
661 
662 // if (! $error)
663 // {
664 // if (! $notrigger)
665 // {
666  // Uncomment this and change MYOBJECT to your own tag if you
667  // want this action calls a trigger.
668 
670  //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
671  //$interface=new Interfaces($this->db);
672  //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
673  //if ($result < 0) { $error++; $this->errors=$interface->errors; }
675 // }
676 // }
677 
678 // if (! $error)
679 // {
680  $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
681  $sql.= " WHERE rowid=".$this->id;
682 
683  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
684  $resql = $this->db->query($sql);
685  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
686 // }
687 
688  // Commit or rollback
689  if ($error)
690  {
691  foreach($this->errors as $errmsg)
692  {
693  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
694  $this->error.=($this->error?', '.$errmsg:$errmsg);
695  }
696  $this->db->rollback();
697  return -1*$error;
698  }
699  else
700  {
701  $this->db->commit();
702  return 1;
703  }
704  }
705 
706 
707 
714  function createFromClone($fromid)
715  {
716  global $user,$langs;
717 
718  $error=0;
719 
720  $object=new Cronjob($this->db);
721 
722  $object->context['createfromclone'] = 'createfromclone';
723 
724  $this->db->begin();
725 
726  // Load source object
727  $object->fetch($fromid);
728  $object->id=0;
729  $object->statut=0;
730 
731  // Clear fields
732  // ...
733 
734  // Create clone
735  $result=$object->create($user);
736 
737  // Other options
738  if ($result < 0)
739  {
740  $this->error=$object->error;
741  $error++;
742  }
743 
744  if (! $error)
745  {
746 
747 
748  }
749 
750  unset($this->context['createfromclone']);
751 
752  // End
753  if (! $error)
754  {
755  $this->db->commit();
756  return $object->id;
757  }
758  else
759  {
760  $this->db->rollback();
761  return -1;
762  }
763  }
764 
765 
772  function initAsSpecimen()
773  {
774  $this->id=0;
775  $this->ref=0;
776 
777  $this->tms='';
778  $this->datec='';
779  $this->label='';
780  $this->jobtype='';
781  $this->command='';
782  $this->classesname='';
783  $this->objectname='';
784  $this->methodename='';
785  $this->params='';
786  $this->md5params='';
787  $this->module_name='';
788  $this->priority='';
789  $this->datelastrun='';
790  $this->datenextrun='';
791  $this->dateend='';
792  $this->datestart='';
793  $this->datelastresult='';
794  $this->lastoutput='';
795  $this->lastresult='';
796  $this->unitfrequency='';
797  $this->frequency='';
798  $this->status=0;
799  $this->processing=0;
800  $this->fk_user_author='';
801  $this->fk_user_mod='';
802  $this->note='';
803  $this->nbrun='';
804  $this->maxrun=100;
805  $this->libname = '';
806  }
807 
813  function info()
814  {
815  $sql = "SELECT";
816  $sql.= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
817  $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as f";
818  $sql.= " WHERE f.rowid = ".$this->id;
819 
820  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
821  $resql=$this->db->query($sql);
822  if ($resql)
823  {
824  if ($this->db->num_rows($resql))
825  {
826  $obj = $this->db->fetch_object($resql);
827  $this->id = $obj->rowid;
828  $this->date_creation = $this->db->jdate($obj->datec);
829  $this->date_modification = $this->db->jdate($obj->tms);
830  $this->user_modification = $obj->fk_user_mod;
831  $this->user_creation = $obj->fk_user_author;
832  }
833  $this->db->free($resql);
834 
835  return 1;
836  }
837  else
838  {
839  $this->error="Error ".$this->db->lasterror();
840  return -1;
841  }
842  }
843 
844 
853  function run_jobs($userlogin)
854  {
855  global $langs, $conf;
856 
857  $now=dol_now();
858  $error = 0;
859  $retval = '';
860 
861  $langs->load('cron');
862 
863  if (empty($userlogin))
864  {
865  $this->error="User login is mandatory";
866  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
867  return -1;
868  }
869 
870  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
871  $user=new User($this->db);
872  $result=$user->fetch('',$userlogin);
873  if ($result<0)
874  {
875  $this->error="User Error:".$user->error;
876  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
877  return -1;
878  }
879  else
880  {
881  if (empty($user->id))
882  {
883  $this->error=" User user login:".$userlogin." do not exists";
884  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
885  return -1;
886  }
887  }
888 
889  dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
890 
891  // Increase limit of time. Works only if we are not in safe mode
892  $ExecTimeLimit=600;
893  if (!empty($ExecTimeLimit))
894  {
895  $err=error_reporting();
896  error_reporting(0); // Disable all errors
897  //error_reporting(E_ALL);
898  @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
899  error_reporting($err);
900  }
901  if (!empty($MemoryLimit))
902  {
903  @ini_set('memory_limit', $MemoryLimit);
904  }
905 
906  // Update last run date start (to track running jobs)
907  $this->datelastrun=$now;
908  $this->datelastresult=null;
909  $this->lastoutput='';
910  $this->lastresult='';
911  $this->processing = 1; // To know job was started
912  $this->nbrun=$this->nbrun + 1;
913  $result = $this->update($user); // This include begin/commit
914  if ($result<0) {
915  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
916  return -1;
917  }
918 
919  // Run a method
920  if ($this->jobtype=='method')
921  {
922  // load classes
923  if (! $error)
924  {
925  $ret=dol_include_once($this->classesname);
926  if ($ret===false || (! class_exists($this->objectname)))
927  {
928  $this->error=$langs->trans('CronCannotLoadClass',$this->classesname,$this->objectname);
929  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
930  $this->lastoutput = $this->error;
931  $this->lastresult = -1;
932  $retval = $this->lastresult;
933  $error++;
934  }
935  }
936 
937  // test if method exists
938  if (! $error)
939  {
940  if (! method_exists($this->objectname, $this->methodename))
941  {
942  $this->error=$langs->trans('CronMethodDoesNotExists',$this->objectname,$this->methodename);
943  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
944  $this->lastoutput = $this->error;
945  $this->lastresult = -1;
946  $retval = $this->lastresult;
947  $error++;
948  }
949  }
950 
951  // Load langs
952  if (! $error)
953  {
954  $result=$langs->load($this->module_name.'@'.$this->module_name);
955  if ($result < 0)
956  {
957  dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
958  $this->error = $langs->error;
959  $this->lastoutput = $this->error;
960  $this->lastresult = -1;
961  $retval = $this->lastresult;
962  $error++;
963  }
964  }
965 
966  if (! $error)
967  {
968  dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
969 
970  // Create Object for the call module
971  $object = new $this->objectname($this->db);
972 
973  $params_arr = array_map('trim', explode(",",$this->params));
974 
975  if (!is_array($params_arr))
976  {
977  $result = call_user_func(array($object, $this->methodename), $this->params);
978  }
979  else
980  {
981  $result = call_user_func_array(array($object, $this->methodename), $params_arr);
982  }
983 
984  if ($result === false || (! is_bool($result) && $result != 0))
985  {
986  $langs->load("errors");
987  dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$object->error, LOG_ERR);
988  $this->error = $object->error?$object->error:$langs->trans('ErrorUnknown');
989  $this->lastoutput = ($object->output?$object->output."\n":"").$this->error;
990  $this->lastresult = is_numeric($result)?$result:-1;
991  $retval = $this->lastresult;
992  $error++;
993  }
994  else
995  {
996  dol_syslog(get_class($this)."::run_jobs END");
997  $this->lastoutput=$object->output;
998  $this->lastresult=var_export($result,true);
999  $retval = $this->lastresult;
1000  }
1001  }
1002  }
1003 
1004  if($this->jobtype == 'function')
1005  {
1006  //load lib
1007  $libpath = '/' . strtolower($this->module_name) . '/lib/' . $this->libname;
1008  $ret = dol_include_once($libpath);
1009  if ($ret === false)
1010  {
1011  $this->error = $langs->trans('CronCannotLoadLib') . ': ' . $libpath;
1012  dol_syslog(get_class($this) . "::run_jobs " . $this->error, LOG_ERR);
1013  return -1;
1014  }
1015  // Load langs
1016  $result=$langs->load($this->module_name . '@' . $this->module_name);
1017  if ($result<0)
1018  {
1019  dol_syslog(get_class($this) . "::run_jobs Cannot load module langs" . $langs->error, LOG_ERR);
1020  return -1;
1021  }
1022  dol_syslog(get_class($this) . "::run_jobs " . $this->libname . "::" . $this->methodename."(" . $this->params . ");", LOG_DEBUG);
1023  $params_arr = explode(", ", $this->params);
1024  if (!is_array($params_arr))
1025  {
1026  $result = call_user_func($this->methodename, $this->params);
1027  }
1028  else
1029  {
1030  $result = call_user_func_array($this->methodename, $params_arr);
1031  }
1032 
1033  if ($result === false || (! is_bool($result) && $result != 0))
1034  {
1035  $langs->load("errors");
1036  dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
1037  $this->error = $langs->trans('ErrorUnknown');
1038  $this->lastoutput = $this->error;
1039  $this->lastresult = is_numeric($result)?$result:-1;
1040  $retval = $this->lastresult;
1041  $error++;
1042  }
1043  else
1044  {
1045  $this->lastoutput=var_export($result,true);
1046  $this->lastresult=var_export($result,true); // Return code
1047  $retval = $this->lastresult;
1048  }
1049  }
1050 
1051  // Run a command line
1052  if ($this->jobtype=='command')
1053  {
1054  $outputdir = $conf->cron->dir_temp;
1055  if (empty($outputdir)) $outputdir = $conf->cronjob->dir_temp;
1056 
1057  if (! empty($outputdir))
1058  {
1059  dol_mkdir($outputdir);
1060  $outputfile=$outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
1061 
1062  // Execute a CLI
1063  include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1064  $utils = new Utils($this->db);
1065  $arrayresult = $utils->executeCLI($this->command, $outputfile);
1066 
1067  $retval = $arrayresult['result'];
1068  $this->error = $arrayresult['error'];
1069  $this->lastoutput = $arrayresult['output'];
1070  $this->lastresult = $arrayresult['result'];
1071  }
1072  }
1073 
1074  dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
1075 
1076  $this->datelastresult=dol_now();
1077  $this->processing=0;
1078  $result = $this->update($user); // This include begin/commit
1079  if ($result < 0)
1080  {
1081  dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1082  return -1;
1083  }
1084  else
1085  {
1086  return $error?-1:1;
1087  }
1088 
1089  }
1090 
1091 
1099  function reprogram_jobs($userlogin, $now)
1100  {
1101  dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1102 
1103  require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1104  $user=new User($this->db);
1105  $result=$user->fetch('',$userlogin);
1106  if ($result<0)
1107  {
1108  $this->error="User Error:".$user->error;
1109  dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1110  return -1;
1111  }
1112  else
1113  {
1114  if (empty($user->id))
1115  {
1116  $this->error=" User user login:".$userlogin." do not exists";
1117  dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1118  return -1;
1119  }
1120  }
1121 
1122  dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
1123 
1124  if (empty($this->datenextrun))
1125  {
1126  if (empty($this->datestart)) $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1127  else $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1128  }
1129 
1130  if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0)
1131  {
1132  // Loop until date is after future
1133  while ($this->datenextrun < $now)
1134  {
1135  $this->datenextrun += ($this->frequency * $this->unitfrequency);
1136 
1137  // TODO For exact frequency (every month, every year, ...), use instead a dol_time_plus_duree($time, $duration_value, $duration_unit)
1138  }
1139  }
1140  else
1141  {
1142  //$this->datenextrun=$this->datenextrun + ($this->frequency * $this->unitfrequency);
1143  dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
1144  }
1145 
1146 
1147  // Archive job
1148  if ($this->autodelete == 2)
1149  {
1150  if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1151  || ($this->dateend && ($this->datenextrun > $this->dateend)))
1152  {
1153  $this->status = 2;
1154  dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
1155  }
1156  }
1157 
1158  $result = $this->update($user);
1159  if ($result<0)
1160  {
1161  dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1162  return -1;
1163  }
1164 
1165  return 1;
1166  }
1167 
1174  function getLibStatut($mode=0)
1175  {
1176  return $this->LibStatut($this->status,$mode);
1177  }
1178 
1186  function LibStatut($status,$mode=0)
1187  {
1188  global $langs;
1189  $langs->load('users');
1190 
1191  if ($mode == 0)
1192  {
1193  $prefix='';
1194  if ($status == 1) return $langs->trans('Enabled');
1195  if ($status == 0) return $langs->trans('Disabled');
1196  }
1197  if ($mode == 1)
1198  {
1199  if ($status == 1) return $langs->trans('Enabled');
1200  if ($status == 0) return $langs->trans('Disabled');
1201  }
1202  if ($mode == 2)
1203  {
1204  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"').' '.$langs->trans('Enabled');
1205  if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"').' '.$langs->trans('Disabled');
1206  }
1207  if ($mode == 3)
1208  {
1209  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"');
1210  if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"');
1211  }
1212  if ($mode == 4)
1213  {
1214  if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"').' '.$langs->trans('Enabled');
1215  if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"').' '.$langs->trans('Disabled');
1216  }
1217  if ($mode == 5)
1218  {
1219  if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"');
1220  if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"');
1221  }
1222  }
1223 }
1224 
1225 
1230 {
1231 
1232  public $id;
1233  public $ref;
1234 
1235  public $tms='';
1236  public $datec='';
1237  public $label;
1238  public $jobtype;
1239  public $command;
1240  public $classesname;
1241  public $objectname;
1242  public $methodename;
1243  public $params;
1244  public $md5params;
1245  public $module_name;
1246  public $priority;
1247  public $datelastrun='';
1248  public $datenextrun='';
1249  public $dateend='';
1250  public $datestart='';
1251  public $lastresult='';
1252  public $lastoutput;
1253  public $unitfrequency;
1254  public $frequency;
1255  public $status;
1256  public $fk_user_author;
1257  public $fk_user_mod;
1258  public $note;
1259  public $nbrun;
1260  public $libname;
1261 
1266  function __construct()
1267  {
1268  return 1;
1269  }
1270 }
Crob Job line class.
Class to manage utility methods.
Definition: utils.class.php:28
create($user, $notrigger=0)
Create object into database.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
fetch_all($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='', $processing=-1)
Load object in memory from the database.
createFromClone($fromid)
Load an object from its id and create a new one in database.
__construct()
Constructor.
reprogram_jobs($userlogin, $now)
Reprogram a job.
Class to manage Dolibarr users.
Definition: user.class.php:39
getLibStatut($mode=0)
Return label of status of user (active, inactive)
__construct($db)
Constructor.
run_jobs($userlogin)
Run a job.
$element
Id that identify managed objects.
update($user=null, $notrigger=0)
Update object into database.
info()
Load object information.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
$table_element
Name of table without prefix where object is stored.
dol_now($mode='gmt')
Return date for now.
fetch($id)
Load object in memory from the database.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
LibStatut($status, $mode=0)
Renvoi le libelle d'un statut donne.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Return a prefix to use for this Dolibarr instance, for session/cookie names or email id...
Crob Job class.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)