Completed
Branch develop (aca1c1)
by
unknown
24:57
created

Cronjob::getLibStatut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2007-2012 Laurent Destailleur  <[email protected]>
3
 * Copyright (C) 2013	Florian Henry	<[email protected]>
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
19
/**
20
 *  \file       cron/class/cronjob.class.php
21
 *  \ingroup    cron
22
 */
23
24
// Put here all includes required by your class file
25
require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
26
27
28
/**
29
 *	Crob Job class
30
 */
31
class Cronjob extends CommonObject
32
{
33
	var $element='cronjob';			//!< Id that identify managed objects
34
	var $table_element='cronjob';		//!< Name of table without prefix where object is stored
35
36
    var $jobtype;
37
	var $tms='';
38
	var $datec='';
39
	var $label;
40
	var $command;
41
	var $classesname;
42
	var $objectname;
43
	var $methodename;
44
	var $params;
45
	var $md5params;
46
	var $module_name;
47
	var $priority;
48
	var $datelastrun='';
49
	var $datenextrun='';
50
	var $dateend='';
51
	var $datestart='';
52
	var $datelastresult='';
53
	var $lastresult;
54
	var $lastoutput;
55
	var $unitfrequency;
56
	var $frequency;
57
	var $status;
58
	var $processing;
59
	var $fk_user_author;
60
	var $fk_user_mod;
61
	var $nbrun;
62
	var $libname;
63
64
65
    /**
66
     *  Constructor
67
     *
68
     *  @param	DoliDb		$db      Database handler
69
     */
70
    function __construct($db)
71
    {
72
        $this->db = $db;
73
        return 1;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
74
    }
75
76
77
    /**
78
     *  Create object into database
79
     *
80
     *  @param	User	$user        User that creates
81
     *  @param  int		$notrigger   0=launch triggers after, 1=disable triggers
82
     *  @return int      		   	 <0 if KO, Id of created object if OK
83
     */
84
    function create($user, $notrigger=0)
85
    {
86
    	global $conf, $langs;
87
		$error=0;
88
89
		$now=dol_now();
90
91
		// Clean parameters
92
93
		if (isset($this->label)) $this->label=trim($this->label);
94
		if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
95
		if (isset($this->command)) $this->command=trim($this->command);
96
		if (isset($this->classesname)) $this->classesname=trim($this->classesname);
97
		if (isset($this->objectname)) $this->objectname=trim($this->objectname);
98
		if (isset($this->methodename)) $this->methodename=trim($this->methodename);
99
		if (isset($this->params)) $this->params=trim($this->params);
100
		if (isset($this->md5params)) $this->md5params=trim($this->md5params);
101
		if (isset($this->module_name)) $this->module_name=trim($this->module_name);
102
		if (isset($this->priority)) $this->priority=trim($this->priority);
103
		if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
104
		if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
105
		if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
106
		if (isset($this->frequency)) $this->frequency=trim($this->frequency);
107
		if (isset($this->status)) $this->status=trim($this->status);
108
		if (isset($this->note)) $this->note=trim($this->note);
109
		if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
110
		if (isset($this->libname)) $this->libname = trim($this->libname);
111
		if (isset($this->test)) $this->test = trim($this->test);
112
		
113
		// Check parameters
114
		// Put here code to add a control on parameters values
115
		if (dol_strlen($this->datestart)==0) {
116
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart'));
117
			$error++;
118
		}
119
		if (empty($this->label)) {
120
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel'));
121
			$error++;
122
		}
123
		if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
124
			$this->errors[]=$langs->trans('CronErrEndDateStartDt');
125
			$error++;
126
		}
127
		if (empty($this->unitfrequency)) {
128
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency'));
129
			$error++;
130
		}
131
		if (($this->jobtype=='command') && (empty($this->command))) {
132
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand'));
133
			$error++;
134
		}
135
		if (($this->jobtype=='method') && (empty($this->classesname))) {
136
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass'));
137
			$error++;
138
		}
139
		if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
140
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod'));
141
			$error++;
142
		}
143
		if (($this->jobtype=='method') && (empty($this->objectname))) {
144
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject'));
145
			$error++;
146
		}
147
148
		if (($this->jobtype=='function') && (empty($this->libname))) {
149
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib'));
150
			$error++;
151
		}
152
153
        // Insert request
154
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
155
156
		$sql.= "datec,";
157
		$sql.= "jobtype,";
158
		$sql.= "label,";
159
		$sql.= "command,";
160
		$sql.= "classesname,";
161
		$sql.= "objectname,";
162
		$sql.= "methodename,";
163
		$sql.= "params,";
164
		$sql.= "md5params,";
165
		$sql.= "module_name,";
166
		$sql.= "priority,";
167
		$sql.= "datelastrun,";
168
		$sql.= "datenextrun,";
169
		$sql.= "dateend,";
170
		$sql.= "datestart,";
171
		$sql.= "lastresult,";
172
		$sql.= "datelastresult,";
173
		$sql.= "lastoutput,";
174
		$sql.= "unitfrequency,";
175
		$sql.= "frequency,";
176
		$sql.= "status,";
177
		$sql.= "fk_user_author,";
178
		$sql.= "fk_user_mod,";
179
		$sql.= "note,";
180
		$sql.= "nbrun,";
181
		$sql.= "maxrun,";
182
		$sql.= "libname,";
183
		$sql.= "test";
184
		$sql.= ") VALUES (";
185
		$sql.= " '".$this->db->idate($now)."',";
186
		$sql.= " ".(! isset($this->jobtype)?'NULL':"'".$this->db->escape($this->jobtype)."'").",";
187
		$sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").",";
188
		$sql.= " ".(! isset($this->command)?'NULL':"'".$this->db->escape($this->command)."'").",";
189
		$sql.= " ".(! isset($this->classesname)?'NULL':"'".$this->db->escape($this->classesname)."'").",";
190
		$sql.= " ".(! isset($this->objectname)?'NULL':"'".$this->db->escape($this->objectname)."'").",";
191
		$sql.= " ".(! isset($this->methodename)?'NULL':"'".$this->db->escape($this->methodename)."'").",";
192
		$sql.= " ".(! isset($this->params)?'NULL':"'".$this->db->escape($this->params)."'").",";
193
		$sql.= " ".(! isset($this->md5params)?'NULL':"'".$this->db->escape($this->md5params)."'").",";
194
		$sql.= " ".(! isset($this->module_name)?'NULL':"'".$this->db->escape($this->module_name)."'").",";
195
		$sql.= " ".(! isset($this->priority)?'0':$this->priority).",";
196
		$sql.= " ".(! isset($this->datelastrun) || dol_strlen($this->datelastrun)==0?'NULL':$this->db->idate($this->datelastrun)).",";
197
		$sql.= " ".(! isset($this->datenextrun) || dol_strlen($this->datenextrun)==0?'NULL':$this->db->idate($this->datenextrun)).",";
198
		$sql.= " ".(! isset($this->dateend) || dol_strlen($this->dateend)==0?'NULL':$this->db->idate($this->dateend)).",";
199
		$sql.= " ".(! isset($this->datestart) || dol_strlen($this->datestart)==0?'NULL':$this->db->idate($this->datestart)).",";
200
		$sql.= " ".(! isset($this->lastresult)?'NULL':"'".$this->db->escape($this->lastresult)."'").",";
201
		$sql.= " ".(! isset($this->datelastresult) || dol_strlen($this->datelastresult)==0?'NULL':$this->db->idate($this->datelastresult)).",";
202
		$sql.= " ".(! isset($this->lastoutput)?'NULL':"'".$this->db->escape($this->lastoutput)."'").",";
203
		$sql.= " ".(! isset($this->unitfrequency)?'NULL':"'".$this->unitfrequency."'").",";
204
		$sql.= " ".(! isset($this->frequency)?'0':$this->frequency).",";
205
		$sql.= " ".(! isset($this->status)?'0':$this->status).",";
206
		$sql.= " ".$user->id.",";
207
		$sql.= " ".$user->id.",";
208
		$sql.= " ".(! isset($this->note)?'NULL':"'".$this->db->escape($this->note)."'").",";
209
		$sql.= " ".(! isset($this->nbrun)?'0':$this->db->escape($this->nbrun)).",";
210
		$sql.= " ".(empty($this->maxrun)?'0':$this->db->escape($this->maxrun)).",";
211
		$sql.= " ".(! isset($this->libname)?'NULL':"'".$this->db->escape($this->libname)."'").",";
212
		$sql.= " ".(! isset($this->test)?'NULL':"'".$this->db->escape($this->test)."'")."";
213
		$sql.= ")";
214
215
		$this->db->begin();
216
217
	   	dol_syslog(get_class($this)."::create", LOG_DEBUG);
218
        $resql=$this->db->query($sql);
219
    	if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
220
221
		if (! $error)
222
        {
223
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
224
225
			if (! $notrigger)
226
			{
227
	            // Uncomment this and change MYOBJECT to your own tag if you
228
	            // want this action calls a trigger.
229
230
	            //// Call triggers
231
	            //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
232
	            //$interface=new Interfaces($this->db);
233
	            //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
234
	            //if ($result < 0) { $error++; $this->errors=$interface->errors; }
235
	            //// End call triggers
236
			}
237
        }
238
239
        // Commit or rollback
240
        if ($error)
241
		{
242
			foreach($this->errors as $errmsg)
243
			{
244
	            dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
245
	            $this->error.=($this->error?', '.$errmsg:$errmsg);
246
			}
247
			$this->db->rollback();
248
			return -1*$error;
249
		}
250
		else
251
		{
252
			$this->db->commit();
253
            return $this->id;
254
		}
255
    }
256
257
258
    /**
259
     *  Load object in memory from the database
260
     *
261
     *  @param	int		$id    Id object
262
     *  @return int          	<0 if KO, >0 if OK
263
     */
264
    function fetch($id)
265
    {
266
        $sql = "SELECT";
267
		$sql.= " t.rowid,";
268
269
		$sql.= " t.tms,";
270
		$sql.= " t.datec,";
271
		$sql.= " t.jobtype,";
272
		$sql.= " t.label,";
273
		$sql.= " t.command,";
274
		$sql.= " t.classesname,";
275
		$sql.= " t.objectname,";
276
		$sql.= " t.methodename,";
277
		$sql.= " t.params,";
278
		$sql.= " t.md5params,";
279
		$sql.= " t.module_name,";
280
		$sql.= " t.priority,";
281
		$sql.= " t.datelastrun,";
282
		$sql.= " t.datenextrun,";
283
		$sql.= " t.dateend,";
284
		$sql.= " t.datestart,";
285
		$sql.= " t.lastresult,";
286
		$sql.= " t.datelastresult,";
287
		$sql.= " t.lastoutput,";
288
		$sql.= " t.unitfrequency,";
289
		$sql.= " t.frequency,";
290
		$sql.= " t.status,";
291
		$sql.= " t.processing,";
292
		$sql.= " t.fk_user_author,";
293
		$sql.= " t.fk_user_mod,";
294
		$sql.= " t.note,";
295
		$sql.= " t.nbrun,";
296
		$sql.= " t.maxrun,";
297
		$sql.= " t.libname,";
298
		$sql.= " t.test";
299
        $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
300
        $sql.= " WHERE t.rowid = ".$id;
301
302
    	dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
303
        $resql=$this->db->query($sql);
304
        if ($resql)
305
        {
306
            if ($this->db->num_rows($resql))
307
            {
308
                $obj = $this->db->fetch_object($resql);
309
310
                $this->id    = $obj->rowid;
311
                $this->ref = $obj->rowid;
312
313
				$this->tms = $this->db->jdate($obj->tms);
314
				$this->datec = $this->db->jdate($obj->datec);
315
				$this->label = $obj->label;
316
				$this->jobtype = $obj->jobtype;
317
				$this->command = $obj->command;
318
				$this->classesname = $obj->classesname;
319
				$this->objectname = $obj->objectname;
320
				$this->methodename = $obj->methodename;
321
				$this->params = $obj->params;
322
				$this->md5params = $obj->md5params;
323
				$this->module_name = $obj->module_name;
324
				$this->priority = $obj->priority;
325
				$this->datelastrun = $this->db->jdate($obj->datelastrun);
326
				$this->datenextrun = $this->db->jdate($obj->datenextrun);
327
				$this->dateend = $this->db->jdate($obj->dateend);
328
				$this->datestart = $this->db->jdate($obj->datestart);
329
				$this->lastresult = $obj->lastresult;
330
				$this->lastoutput = $obj->lastoutput;
331
				$this->datelastresult = $this->db->jdate($obj->datelastresult);
332
				$this->unitfrequency = $obj->unitfrequency;
333
				$this->frequency = $obj->frequency;
334
				$this->status = $obj->status;
335
				$this->processing = $obj->processing;
336
				$this->fk_user_author = $obj->fk_user_author;
337
				$this->fk_user_mod = $obj->fk_user_mod;
338
				$this->note = $obj->note;
339
				$this->nbrun = $obj->nbrun;
340
				$this->maxrun = $obj->maxrun;
341
				$this->libname = $obj->libname;
342
				$this->test = $obj->test;
343
            }
344
            $this->db->free($resql);
345
346
            return 1;
347
        }
348
        else
349
        {
350
      	    $this->error="Error ".$this->db->lasterror();
351
            return -1;
352
        }
353
    }
354
355
    /**
356
     *  Load object in memory from the database
357
     *
358
	 *  @param	string		$sortorder      sort order
359
	 *  @param	string		$sortfield      sort field
360
	 *  @param	int			$limit		    limit page
361
	 *  @param	int			$offset    	    page
362
	 *  @param	int			$status    	    display active or not
363
	 *  @param	array		$filter    	    filter output
364
	 *  @param  int         $processing     Processing or not 
365
     *  @return int          			    <0 if KO, >0 if OK
366
     */
367
    function fetch_all($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='', $processing=-1)
368
    {
369
    	global $langs;
370
    	
371
    	$this->lines=array();
372
    	
373
    	$sql = "SELECT";
374
    	$sql.= " t.rowid,";
375
    	$sql.= " t.entity,";
376
    	$sql.= " t.tms,";
377
    	$sql.= " t.datec,";
378
    	$sql.= " t.jobtype,";
379
    	$sql.= " t.label,";
380
    	$sql.= " t.command,";
381
    	$sql.= " t.classesname,";
382
    	$sql.= " t.objectname,";
383
    	$sql.= " t.methodename,";
384
    	$sql.= " t.params,";
385
    	$sql.= " t.md5params,";
386
    	$sql.= " t.module_name,";
387
    	$sql.= " t.priority,";
388
    	$sql.= " t.datelastrun,";
389
    	$sql.= " t.datenextrun,";
390
    	$sql.= " t.dateend,";
391
    	$sql.= " t.datestart,";
392
    	$sql.= " t.lastresult,";
393
    	$sql.= " t.datelastresult,";
394
    	$sql.= " t.lastoutput,";
395
    	$sql.= " t.unitfrequency,";
396
    	$sql.= " t.frequency,";
397
    	$sql.= " t.status,";
398
    	$sql.= " t.processing,";
399
    	$sql.= " t.fk_user_author,";
400
    	$sql.= " t.fk_user_mod,";
401
    	$sql.= " t.note,";
402
    	$sql.= " t.nbrun,";
403
    	$sql.= " t.libname,";
404
    	$sql.= " t.test";
405
    	$sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
406
    	$sql.= " WHERE 1 = 1";
407
    	if ($processing >= 0) $sql.= " AND t.processing = ".(empty($processing)?'0':'1');
408
    	if ($status >= 0 && $status < 2) $sql.= " AND t.status = ".(empty($status)?'0':'1');
409
    	if ($status == 2) $sql.= " AND t.status = 2";
410
    	//Manage filter
411
    	if (is_array($filter) && count($filter)>0) {
412
    		foreach($filter as $key => $value) 
413
    		{
414
    		    if ($key == 't.rowid') $sql.= ' AND '.$key.' = '.$this->db->escape($value);
415
   				else $sql.= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
416
    		}
417
    	}
418
419
    	$sql.= " ORDER BY $sortfield $sortorder ";
420
    	if (!empty($limit) && !empty($offset)) {
421
    		$sql.= $this->db->plimit($limit + 1,$offset);
422
    	}
423
424
    	$sqlwhere = array();
425
426
    	if (count($sqlwhere)>0) {
427
    		$sql.= " WHERE ".implode(' AND ',$sqlwhere);
428
    	}
429
430
    	dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
431
    	$resql=$this->db->query($sql);
432
    	if ($resql)
433
    	{
434
    		$num=$this->db->num_rows($resql);
435
    		$i=0;
436
437
    		if ($num)
438
    		{
439
	    		while ($i < $num)
440
	    		{
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
    		}
485
    		$this->db->free($resql);
486
487
    		return 1;
488
    	}
489
    	else
490
    	{
491
    		$this->error="Error ".$this->db->lasterror();
492
    		return -1;
493
    	}
494
    }
495
496
497
    /**
498
     *  Update object into database
499
     *
500
     *  @param	User	$user        User that modifies
501
     *  @param  int		$notrigger	 0=launch triggers after, 1=disable triggers
502
     *  @return int     		   	 <0 if KO, >0 if OK
503
     */
504
    function update($user=null, $notrigger=0)
505
    {
506
    	global $conf, $langs;
507
508
    	$langs->load('cron');
509
510
		$error=0;
511
512
		// Clean parameters
513
		if (isset($this->label)) $this->label=trim($this->label);
514
		if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
515
		if (isset($this->command)) $this->command=trim($this->command);
516
		if (isset($this->classesname)) $this->classesname=trim($this->classesname);
517
		if (isset($this->objectname)) $this->objectname=trim($this->objectname);
518
		if (isset($this->methodename)) $this->methodename=trim($this->methodename);
519
		if (isset($this->params)) $this->params=trim($this->params);
520
		if (isset($this->md5params)) $this->md5params=trim($this->md5params);
521
		if (isset($this->module_name)) $this->module_name=trim($this->module_name);
522
		if (isset($this->priority)) $this->priority=trim($this->priority);
523
		if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
524
		if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
525
		if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
526
		if (isset($this->frequency)) $this->frequency=trim($this->frequency);
527
		if (isset($this->status)) $this->status=trim($this->status);
528
		if (isset($this->note)) $this->note=trim($this->note);
529
		if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
530
        if (isset($this->libname)) $this->libname = trim($this->libname);
531
        if (isset($this->test)) $this->test = trim($this->test);
532
533
		if (empty($this->maxrun)) $this->maxrun=0;
534
        if (empty($this->processing)) $this->processing=0;
535
        
536
		// Check parameters
537
		// Put here code to add a control on parameters values
538
		if (dol_strlen($this->datestart)==0) {
539
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronDtStart'));
540
			$error++;
541
		}
542
		if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
543
			$this->errors[]=$langs->trans('CronErrEndDateStartDt');
544
			$error++;
545
		}
546
		if (empty($this->label)) {
547
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronLabel'));
548
			$error++;
549
		}
550
		if (empty($this->unitfrequency)) {
551
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronFrequency'));
552
			$error++;
553
		}
554
		if (($this->jobtype=='command') && (empty($this->command))) {
555
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronCommand'));
556
			$error++;
557
		}
558
		if (($this->jobtype=='method') && (empty($this->classesname))) {
559
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronClass'));
560
			$error++;
561
		}
562
		if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
563
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronMethod'));
564
			$error++;
565
		}
566
		if (($this->jobtype=='method') && (empty($this->objectname))) {
567
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronObject'));
568
			$error++;
569
		}
570
571
		if (($this->jobtype=='function') && (empty($this->libname))) {
572
			$this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronLib'));
573
			$error++;
574
		}
575
576
577
        // Update request
578
        $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
579
580
		$sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
581
		$sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").",";
582
		$sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").",";
583
		$sql.= " classesname=".(isset($this->classesname)?"'".$this->db->escape($this->classesname)."'":"null").",";
584
		$sql.= " objectname=".(isset($this->objectname)?"'".$this->db->escape($this->objectname)."'":"null").",";
585
		$sql.= " methodename=".(isset($this->methodename)?"'".$this->db->escape($this->methodename)."'":"null").",";
586
		$sql.= " params=".(isset($this->params)?"'".$this->db->escape($this->params)."'":"null").",";
587
		$sql.= " md5params=".(isset($this->md5params)?"'".$this->db->escape($this->md5params)."'":"null").",";
588
		$sql.= " module_name=".(isset($this->module_name)?"'".$this->db->escape($this->module_name)."'":"null").",";
589
		$sql.= " priority=".(isset($this->priority)?$this->priority:"null").",";
590
		$sql.= " datelastrun=".(dol_strlen($this->datelastrun)!=0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
591
		$sql.= " datenextrun=".(dol_strlen($this->datenextrun)!=0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
592
		$sql.= " dateend=".(dol_strlen($this->dateend)!=0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
593
		$sql.= " datestart=".(dol_strlen($this->datestart)!=0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
594
		$sql.= " datelastresult=".(dol_strlen($this->datelastresult)!=0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
595
		$sql.= " lastresult=".(isset($this->lastresult)?"'".$this->db->escape($this->lastresult)."'":"null").",";
596
		$sql.= " lastoutput=".(isset($this->lastoutput)?"'".$this->db->escape($this->lastoutput)."'":"null").",";
597
		$sql.= " unitfrequency=".(isset($this->unitfrequency)?$this->unitfrequency:"null").",";
598
		$sql.= " frequency=".(isset($this->frequency)?$this->frequency:"null").",";
599
		$sql.= " status=".(isset($this->status)?$this->status:"null").",";
600
		$sql.= " processing=".((isset($this->processing) && $this->processing > 0)?$this->processing:"0").",";
601
		$sql.= " fk_user_mod=".$user->id.",";
602
		$sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
603
		$sql.= " nbrun=".((isset($this->nbrun) && $this->nbrun >0)?$this->nbrun:"null").",";
604
		$sql.= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0)?$this->maxrun:"0").",";
605
		$sql.= " libname=".(isset($this->libname)?"'".$this->db->escape($this->libname)."'":"null").",";
606
		$sql.= " test=".(isset($this->test)?"'".$this->db->escape($this->test)."'":"null");
607
		$sql.= " WHERE rowid=".$this->id;
608
609
        $this->db->begin();
610
611
		dol_syslog(get_class($this)."::update", LOG_DEBUG);
612
        $resql = $this->db->query($sql);
613
    	if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
614
615
		if (! $error)
616
		{
617
			if (! $notrigger)
618
			{
619
	            // Uncomment this and change MYOBJECT to your own tag if you
620
	            // want this action calls a trigger.
621
622
	            //// Call triggers
623
	            //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
624
	            //$interface=new Interfaces($this->db);
625
	            //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
626
	            //if ($result < 0) { $error++; $this->errors=$interface->errors; }
627
	            //// End call triggers
628
	    	}
629
		}
630
631
        // Commit or rollback
632
		if ($error)
633
		{
634
			foreach($this->errors as $errmsg)
635
			{
636
	            dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
637
	            $this->error.=($this->error?', '.$errmsg:$errmsg);
638
			}
639
			$this->db->rollback();
640
			return -1*$error;
641
		}
642
		else
643
		{
644
			$this->db->commit();
645
			return 1;
646
		}
647
    }
648
649
650
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
651
	 *  Delete object in database
652
	 *
653
     *	@param  User	$user        User that deletes
654
     *  @param  int		$notrigger	 0=launch triggers after, 1=disable triggers
655
	 *  @return	int					 <0 if KO, >0 if OK
656
	 */
657
	function delete($user, $notrigger=0)
658
	{
659
		$error=0;
660
661
		$this->db->begin();
662
663
//		if (! $error)
664
//		{
665
//			if (! $notrigger)
666
//			{
667
				// Uncomment this and change MYOBJECT to your own tag if you
668
		        // want this action calls a trigger.
669
670
		        //// Call triggers
671
		        //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
672
		        //$interface=new Interfaces($this->db);
673
		        //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
674
		        //if ($result < 0) { $error++; $this->errors=$interface->errors; }
675
		        //// End call triggers
676
//			}
677
//		}
678
679
//		if (! $error)
680
//		{
681
    		$sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
682
    		$sql.= " WHERE rowid=".$this->id;
683
684
    		dol_syslog(get_class($this)."::delete", LOG_DEBUG);
685
    		$resql = $this->db->query($sql);
686
        	if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
687
//		}
688
689
        // Commit or rollback
690
		if ($error)
691
		{
692
			foreach($this->errors as $errmsg)
693
			{
694
	            dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
695
	            $this->error.=($this->error?', '.$errmsg:$errmsg);
696
			}
697
			$this->db->rollback();
698
			return -1*$error;
699
		}
700
		else
701
		{
702
			$this->db->commit();
703
			return 1;
704
		}
705
	}
706
707
708
709
	/**
710
	 *	Load an object from its id and create a new one in database
711
	 *
712
	 *	@param	int		$fromid     Id of object to clone
713
	 * 	@return	int					New id of clone
714
	 */
715
	function createFromClone($fromid)
716
	{
717
		global $user,$langs;
718
719
		$error=0;
720
721
		$object=new Cronjob($this->db);
722
723
		$object->context['createfromclone'] = 'createfromclone';
724
725
		$this->db->begin();
726
727
		// Load source object
728
		$object->fetch($fromid);
729
		$object->id=0;
730
		$object->statut=0;
731
732
		// Clear fields
733
		// ...
734
735
		// Create clone
736
		$result=$object->create($user);
737
738
		// Other options
739
		if ($result < 0)
740
		{
741
			$this->error=$object->error;
742
			$error++;
743
		}
744
745
		if (! $error)
746
		{
747
748
749
		}
750
751
		unset($this->context['createfromclone']);
752
753
		// End
754
		if (! $error)
755
		{
756
			$this->db->commit();
757
			return $object->id;
758
		}
759
		else
760
		{
761
			$this->db->rollback();
762
			return -1;
763
		}
764
	}
765
766
767
	/**
768
	 *	Initialise object with example values
769
	 *	Id must be 0 if object instance is a specimen
770
	 *
771
	 *	@return	void
772
	 */
773
	function initAsSpecimen()
774
	{
775
		$this->id=0;
776
		$this->ref=0;
0 ignored issues
show
Documentation Bug introduced by
The property $ref was declared of type string, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
777
778
		$this->tms='';
779
		$this->datec='';
780
		$this->label='';
781
		$this->jobtype='';
782
		$this->command='';
783
		$this->classesname='';
784
		$this->objectname='';
785
		$this->methodename='';
786
		$this->params='';
787
		$this->md5params='';
788
		$this->module_name='';
789
		$this->priority='';
790
		$this->datelastrun='';
791
		$this->datenextrun='';
792
		$this->dateend='';
793
		$this->datestart='';
794
		$this->datelastresult='';
795
		$this->lastoutput='';
796
		$this->lastresult='';
797
		$this->unitfrequency='';
798
		$this->frequency='';
799
		$this->status=0;
800
		$this->processing=0;
801
		$this->fk_user_author='';
802
		$this->fk_user_mod='';
803
		$this->note='';
804
		$this->nbrun='';
805
		$this->maxrun=100;
806
        $this->libname = '';
807
	}
808
809
	/**
810
	 *	Load object information
811
	 *
812
	 *	@return	int
813
	 */
814
	function info()
815
	{
816
		$sql = "SELECT";
817
		$sql.= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
818
		$sql.= " FROM ".MAIN_DB_PREFIX."cronjob as f";
819
		$sql.= " WHERE f.rowid = ".$this->id;
820
821
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
822
		$resql=$this->db->query($sql);
823
		if ($resql)
824
		{
825
			if ($this->db->num_rows($resql))
826
			{
827
				$obj = $this->db->fetch_object($resql);
828
				$this->id = $obj->rowid;
829
				$this->date_creation = $this->db->jdate($obj->datec);
830
				$this->date_modification = $this->db->jdate($obj->tms);
831
				$this->user_modification = $obj->fk_user_mod;
832
				$this->user_creation = $obj->fk_user_author;
833
			}
834
			$this->db->free($resql);
835
836
			return 1;
837
		}
838
		else
839
		{
840
			$this->error="Error ".$this->db->lasterror();
841
			return -1;
842
		}
843
	}
844
845
846
	/**
847
	 * Run a job.
848
	 * Once job is finished, status and nb of run is updated. 
849
	 * This function does not plan the next run. This is done by function ->reprogram_jobs 
850
	 *
851
	 * @param   string		$userlogin    	User login
852
	 * @return	int					 		<0 if KO, >0 if OK
853
	 */
854
	function run_jobs($userlogin)
855
	{
856
		global $langs, $conf;
857
858
		$now=dol_now();
859
		$error = 0;
860
		$retval = '';
861
862
		$langs->load('cron');
863
864
		if (empty($userlogin)) 
865
		{
866
			$this->error="User login is mandatory";
867
			dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
868
			return -1;
869
		}
870
871
		require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
872
		$user=new User($this->db);
873
		$result=$user->fetch('',$userlogin);
874
		if ($result<0)
875
		{
876
			$this->error="User Error:".$user->error;
877
			dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
878
			return -1;
879
		}
880
		else
881
		{
882
			if (empty($user->id))
883
			{
884
				$this->error=" User user login:".$userlogin." do not exists";
885
				dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
886
				return -1;
887
			}
888
		}
889
890
		dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
891
892
		// Increase limit of time. Works only if we are not in safe mode
893
		$ExecTimeLimit=600;
894
		if (!empty($ExecTimeLimit))
895
		{
896
			$err=error_reporting();
897
			error_reporting(0);     // Disable all errors
898
			//error_reporting(E_ALL);
899
			@set_time_limit($ExecTimeLimit);   // Need more than 240 on Windows 7/64
900
			error_reporting($err);
901
		}
902
		if (!empty($MemoryLimit))
0 ignored issues
show
Bug introduced by
The variable $MemoryLimit seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
903
		{
904
			@ini_set('memory_limit', $MemoryLimit);
905
		}
906
907
		// Update last run date start (to track running jobs)
908
		$this->datelastrun=$now;
909
		$this->datelastresult=null;
910
		$this->lastoutput='';
911
		$this->lastresult='';
912
		$this->processing = 1;                // To know job was started
913
		$this->nbrun=$this->nbrun + 1;
914
		$result = $this->update($user);       // This include begin/commit
915
		if ($result<0) {
916
			dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
917
			return -1;
918
		}
919
920
		// Run a method
921
		if ($this->jobtype=='method')
922
		{
923
			// load classes
924
			if (! $error)
925
			{
926
    			$ret=dol_include_once($this->classesname);
927
    			if ($ret===false || (! class_exists($this->objectname)))
928
    			{
929
    			    $this->error=$langs->trans('CronCannotLoadClass',$this->classesname,$this->objectname);
930
    				dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
931
    				$this->lastoutput = $this->error;
932
    				$this->lastresult = -1;
933
                    $retval = $this->lastresult;
934
                    $error++;
935
    			}
936
			}
937
938
			// test if method exists
939
			if (! $error)
940
			{
941
			    if (! method_exists($this->objectname, $this->methodename))
942
			    {
943
			        $this->error=$langs->trans('CronMethodDoesNotExists',$this->objectname,$this->methodename);
944
    				dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
945
    				$this->lastoutput = $this->error;
946
    				$this->lastresult = -1;
947
    				$retval = $this->lastresult;
948
    				$error++;
949
			    }
950
			}
951
			
952
			// Load langs
953
			if (! $error)
954
			{
955
				$result=$langs->load($this->module_name.'@'.$this->module_name);
956
				if ($result < 0)
957
				{
958
					dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
959
					$this->error = $langs->error;
960
					$this->lastoutput = $this->error;
961
					$this->lastresult = -1;
962
	                $retval = $this->lastresult;
963
	                $error++;
964
				}
965
			}
966
			
967
			if (! $error)
968
			{
969
				dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
970
	
971
				// Create Object for the call module
972
				$object = new $this->objectname($this->db);
973
	
974
				$params_arr = array_map('trim', explode(",",$this->params));
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 || $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 || $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
			$command=escapeshellcmd($this->command);
1055
			$command.=" 2>&1";
1056
			dol_mkdir($conf->cronjob->dir_temp);
1057
			$outputfile=$conf->cronjob->dir_temp.'/cronjob.'.$userlogin.'.out';
1058
1059
			dol_syslog(get_class($this)."::run_jobs system:".$command, LOG_DEBUG);
1060
			$output_arr=array();
1061
1062
			$execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2);	// 1 or 2
1063
			if ($execmethod == 1)
1064
			{
1065
				exec($command, $output_arr, $retval);
1066
				if ($retval != 0)
1 ignored issue
show
Bug introduced by
It seems like you are loosely comparing $retval of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
1067
				{
1068
				    $langs->load("errors");
1069
				    dol_syslog(get_class($this)."::run_jobs retval=".$retval, LOG_ERR);
1070
				    $this->error = 'Error '.$retval;
1071
				    $this->lastoutput = '';     // Will be filled later
1072
				    $this->lastresult = $retval;
1073
				    $retval = $this->lastresult;
1074
				    $error++;				    
1075
				}
1076
			}
1077
			if ($execmethod == 2)
1078
			{
1079
				$ok=0;
1080
				$handle = fopen($outputfile, 'w');
1081
				if ($handle)
1082
				{
1083
					dol_syslog("Run command ".$command);
1084
					$handlein = popen($command, 'r');
1085
					while (!feof($handlein))
1086
					{
1087
						$read = fgets($handlein);
1088
						fwrite($handle,$read);
1089
						$output_arr[]=$read;
1090
					}
1091
					pclose($handlein);
1092
					fclose($handle);
1093
				}
1094
				if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
1095
			}
1096
1097
			// Update with result
1098
    		if (is_array($output_arr) && count($output_arr)>0)
1099
    		{
1100
    			foreach($output_arr as $val)
1101
    			{
1102
    				$this->lastoutput.=$val."\n";
1103
    			}
1104
    		}
1105
    		
1106
    		$this->lastresult=$retval;
1107
		
1108
    		dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true)." lastoutput=".$this->lastoutput." lastresult=".$this->lastresult, LOG_DEBUG);
1109
		}
1110
		
1111
		dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
1112
		
1113
		$this->datelastresult=dol_now();
1114
		$this->processing=0;
1115
		$result = $this->update($user);       // This include begin/commit
1116
		if ($result < 0)
1117
		{
1118
			dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
1119
			return -1;
1120
		}
1121
		else
1122
		{
1123
			return $error?-1:1;
1124
		}
1125
1126
	}
1127
1128
	/**
1129
	 * Reprogram a job
1130
	 *
1131
	 * @param  string		$userlogin      User login
1132
	 * @param  timestamp    $now            Date returned by dol_now()
1133
	 * @return int					        <0 if KO, >0 if OK
1134
	 */
1135
	function reprogram_jobs($userlogin, $now)
1136
	{
1137
		dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
1138
        
1139
		require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
1140
		$user=new User($this->db);
1141
		$result=$user->fetch('',$userlogin);
1142
		if ($result<0) 
1143
		{
1144
			$this->error="User Error:".$user->error;
1145
			dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1146
			return -1;
1147
		}
1148
		else 
1149
		{
1150
			if (empty($user->id)) 
1151
			{
1152
				$this->error=" User user login:".$userlogin." do not exists";
1153
				dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1154
				return -1;
1155
			}
1156
		}
1157
1158
		dol_syslog(get_class($this)."::reprogram_jobs  ", LOG_DEBUG);
1159
1160
		
1161
		if (empty($this->datenextrun)) 
1162
		{
1163
			if (empty($this->datestart)) $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
1164
			else $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
1165
		}
1166
1167
		if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0) 
1168
		{
1169
		    // Loop until date is after future
1170
		    while ($this->datenextrun < $now)
1171
		    {
1172
		        $this->datenextrun += ($this->frequency * $this->unitfrequency);
1173
		        
1174
		        // TODO For exact frequency (every month, every year, ...), use instead a dol_time_plus_duree($time, $duration_value, $duration_unit)
1175
		    }
1176
		}
1177
		else 
1178
		{
1179
			//$this->datenextrun=$this->datenextrun + ($this->frequency * $this->unitfrequency);
1180
		}
1181
1182
1183
		// Archive job
1184
		if ($this->autodelete == 2)
1185
		{
1186
		    if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
1187
		        || ($this->dateend && ($this->datenextrun > $this->dateend)))
1188
		    {
1189
		        $this->status = 2;
1190
		        dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
1191
		    }
1192
		}
1193
		
1194
		$result = $this->update($user);
1195
		if ($result<0) 
1196
		{
1197
			dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
1198
			return -1;
1199
		}
1200
1201
		return 1;
1202
	}
1203
	
1204
	/**
1205
	 *  Return label of status of user (active, inactive)
1206
	 *
1207
	 *  @param	int		$mode          0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
1208
	 *  @return	string 			       Label of status
1209
	 */
1210
	function getLibStatut($mode=0)
1211
	{
1212
	    return $this->LibStatut($this->status,$mode);
1213
	}
1214
	
1215
	/**
1216
	 *  Renvoi le libelle d'un statut donne
1217
	 *
1218
	 *  @param	int		$status        	Id statut
1219
	 *  @param  int		$mode          	0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
1220
	 *  @return string 			       	Label of status
1221
	 */
1222
	function LibStatut($status,$mode=0)
1223
	{
1224
	    global $langs;
1225
	    $langs->load('users');
1226
	
1227
	    if ($mode == 0)
1228
	    {
1229
	        $prefix='';
1230
	        if ($status == 1) return $langs->trans('Enabled');
1231
	        if ($status == 0) return $langs->trans('Disabled');
1232
	    }
1233
	    if ($mode == 1)
1234
	    {
1235
	        if ($status == 1) return $langs->trans('Enabled');
1236
	        if ($status == 0) return $langs->trans('Disabled');
1237
	    }
1238
	    if ($mode == 2)
1239
	    {
1240
	        if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"').' '.$langs->trans('Enabled');
1241
	        if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"').' '.$langs->trans('Disabled');
1242
	    }
1243
	    if ($mode == 3)
1244
	    {
1245
	        if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"');
1246
	        if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"');
1247
	    }
1248
	    if ($mode == 4)
1249
	    {
1250
	        if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"').' '.$langs->trans('Enabled');
1251
	        if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"').' '.$langs->trans('Disabled');
1252
	    }
1253
	    if ($mode == 5)
1254
	    {
1255
	        if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"');
1256
	        if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"');
1257
	    }
1258
	}	
1259
}
1260
1261
1262
/**
1263
 *	Crob Job line class
1264
 */
1265
class Cronjobline
1266
{
1267
1268
	var $id;
1269
	var $ref;
1270
1271
	var $tms='';
1272
	var $datec='';
1273
	var $label;
1274
	var $jobtype;
1275
	var $command;
1276
	var $classesname;
1277
	var $objectname;
1278
	var $methodename;
1279
	var $params;
1280
	var $md5params;
1281
	var $module_name;
1282
	var $priority;
1283
	var $datelastrun='';
1284
	var $datenextrun='';
1285
	var $dateend='';
1286
	var $datestart='';
1287
	var $lastresult='';
1288
	var $lastoutput;
1289
	var $unitfrequency;
1290
	var $frequency;
1291
	var $status;
1292
	var $fk_user_author;
1293
	var $fk_user_mod;
1294
	var $note;
1295
	var $nbrun;
1296
	var $libname;
1297
1298
	/**
1299
	 *  Constructor
1300
	 *
1301
	 */
1302
	function __construct()
1303
	{
1304
		return 1;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
1305
	}
1306
}
1307