cronjob.class.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. /**
  19. * \file cron/class/cronjob.class.php
  20. * \ingroup cron
  21. */
  22. // Put here all includes required by your class file
  23. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  24. /**
  25. * Crob Job class
  26. */
  27. class Cronjob extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element='cronjob';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element='cronjob';
  37. public $picto = 'cron';
  38. /**
  39. * @var int Entity
  40. */
  41. public $entity;
  42. public $jobtype;
  43. public $tms='';
  44. public $datec='';
  45. public $label;
  46. public $command;
  47. public $classesname;
  48. public $objectname;
  49. public $methodename;
  50. public $params;
  51. public $md5params;
  52. public $module_name;
  53. public $priority;
  54. public $datelastrun='';
  55. public $datenextrun='';
  56. public $dateend='';
  57. public $datestart='';
  58. public $datelastresult='';
  59. public $lastresult;
  60. public $lastoutput;
  61. public $unitfrequency;
  62. public $frequency;
  63. public $status;
  64. public $processing;
  65. public $fk_user_author;
  66. public $fk_user_mod;
  67. public $nbrun;
  68. public $libname;
  69. public $test; // A test condition to know if job is visible/qualified
  70. const STATUS_DISABLED = 0;
  71. const STATUS_ENABLED = 1;
  72. const STATUS_ARCHIVED = 2;
  73. /**
  74. * Constructor
  75. *
  76. * @param DoliDb $db Database handler
  77. */
  78. function __construct($db)
  79. {
  80. $this->db = $db;
  81. return 1;
  82. }
  83. /**
  84. * Create object into database
  85. *
  86. * @param User $user User that creates
  87. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  88. * @return int <0 if KO, Id of created object if OK
  89. */
  90. function create($user, $notrigger=0)
  91. {
  92. global $conf, $langs;
  93. $error=0;
  94. $now=dol_now();
  95. // Clean parameters
  96. if (isset($this->label)) $this->label=trim($this->label);
  97. if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
  98. if (isset($this->command)) $this->command=trim($this->command);
  99. if (isset($this->classesname)) $this->classesname=trim($this->classesname);
  100. if (isset($this->objectname)) $this->objectname=trim($this->objectname);
  101. if (isset($this->methodename)) $this->methodename=trim($this->methodename);
  102. if (isset($this->params)) $this->params=trim($this->params);
  103. if (isset($this->md5params)) $this->md5params=trim($this->md5params);
  104. if (isset($this->module_name)) $this->module_name=trim($this->module_name);
  105. if (isset($this->priority)) $this->priority=trim($this->priority);
  106. if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
  107. if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
  108. if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
  109. if (isset($this->frequency)) $this->frequency=trim($this->frequency);
  110. if (isset($this->status)) $this->status=trim($this->status);
  111. if (isset($this->note)) $this->note=trim($this->note);
  112. if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
  113. if (isset($this->libname)) $this->libname = trim($this->libname);
  114. if (isset($this->test)) $this->test = trim($this->test);
  115. // Check parameters
  116. // Put here code to add a control on parameters values
  117. if (dol_strlen($this->datestart)==0) {
  118. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart'));
  119. $error++;
  120. }
  121. if (empty($this->label)) {
  122. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel'));
  123. $error++;
  124. }
  125. if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
  126. $this->errors[]=$langs->trans('CronErrEndDateStartDt');
  127. $error++;
  128. }
  129. if (empty($this->unitfrequency)) {
  130. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency'));
  131. $error++;
  132. }
  133. if (($this->jobtype=='command') && (empty($this->command))) {
  134. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand'));
  135. $error++;
  136. }
  137. if (($this->jobtype=='method') && (empty($this->classesname))) {
  138. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass'));
  139. $error++;
  140. }
  141. if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  142. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod'));
  143. $error++;
  144. }
  145. if (($this->jobtype=='method') && (empty($this->objectname))) {
  146. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject'));
  147. $error++;
  148. }
  149. if (($this->jobtype=='function') && (empty($this->libname))) {
  150. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib'));
  151. $error++;
  152. }
  153. // Insert request
  154. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
  155. $sql.= "entity,";
  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.= " ".(! isset($this->entity)?$conf->entity:$this->db->escape($this->entity)).",";
  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. $this->db->begin();
  216. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  217. $resql=$this->db->query($sql);
  218. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  219. if (! $error)
  220. {
  221. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
  222. if (! $notrigger)
  223. {
  224. // Uncomment this and change MYOBJECT to your own tag if you
  225. // want this action calls a trigger.
  226. //// Call triggers
  227. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  228. //$interface=new Interfaces($this->db);
  229. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  230. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  231. //// End call triggers
  232. }
  233. }
  234. // Commit or rollback
  235. if ($error)
  236. {
  237. foreach($this->errors as $errmsg)
  238. {
  239. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  240. $this->error.=($this->error?', '.$errmsg:$errmsg);
  241. }
  242. $this->db->rollback();
  243. return -1*$error;
  244. }
  245. else
  246. {
  247. $this->db->commit();
  248. return $this->id;
  249. }
  250. }
  251. /**
  252. * Load object in memory from the database
  253. *
  254. * @param int $id Id object
  255. * @return int <0 if KO, >0 if OK
  256. */
  257. function fetch($id)
  258. {
  259. $sql = "SELECT";
  260. $sql.= " t.rowid,";
  261. $sql.= " t.entity,";
  262. $sql.= " t.tms,";
  263. $sql.= " t.datec,";
  264. $sql.= " t.jobtype,";
  265. $sql.= " t.label,";
  266. $sql.= " t.command,";
  267. $sql.= " t.classesname,";
  268. $sql.= " t.objectname,";
  269. $sql.= " t.methodename,";
  270. $sql.= " t.params,";
  271. $sql.= " t.md5params,";
  272. $sql.= " t.module_name,";
  273. $sql.= " t.priority,";
  274. $sql.= " t.datelastrun,";
  275. $sql.= " t.datenextrun,";
  276. $sql.= " t.dateend,";
  277. $sql.= " t.datestart,";
  278. $sql.= " t.lastresult,";
  279. $sql.= " t.datelastresult,";
  280. $sql.= " t.lastoutput,";
  281. $sql.= " t.unitfrequency,";
  282. $sql.= " t.frequency,";
  283. $sql.= " t.status,";
  284. $sql.= " t.processing,";
  285. $sql.= " t.fk_user_author,";
  286. $sql.= " t.fk_user_mod,";
  287. $sql.= " t.note,";
  288. $sql.= " t.nbrun,";
  289. $sql.= " t.maxrun,";
  290. $sql.= " t.libname,";
  291. $sql.= " t.test";
  292. $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  293. $sql.= " WHERE t.rowid = ".$id;
  294. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  295. $resql=$this->db->query($sql);
  296. if ($resql)
  297. {
  298. if ($this->db->num_rows($resql))
  299. {
  300. $obj = $this->db->fetch_object($resql);
  301. $this->id = $obj->rowid;
  302. $this->ref = $obj->rowid;
  303. $this->entity = $obj->entity;
  304. $this->tms = $this->db->jdate($obj->tms);
  305. $this->datec = $this->db->jdate($obj->datec);
  306. $this->label = $obj->label;
  307. $this->jobtype = $obj->jobtype;
  308. $this->command = $obj->command;
  309. $this->classesname = $obj->classesname;
  310. $this->objectname = $obj->objectname;
  311. $this->methodename = $obj->methodename;
  312. $this->params = $obj->params;
  313. $this->md5params = $obj->md5params;
  314. $this->module_name = $obj->module_name;
  315. $this->priority = $obj->priority;
  316. $this->datelastrun = $this->db->jdate($obj->datelastrun);
  317. $this->datenextrun = $this->db->jdate($obj->datenextrun);
  318. $this->dateend = $this->db->jdate($obj->dateend);
  319. $this->datestart = $this->db->jdate($obj->datestart);
  320. $this->lastresult = $obj->lastresult;
  321. $this->lastoutput = $obj->lastoutput;
  322. $this->datelastresult = $this->db->jdate($obj->datelastresult);
  323. $this->unitfrequency = $obj->unitfrequency;
  324. $this->frequency = $obj->frequency;
  325. $this->status = $obj->status;
  326. $this->processing = $obj->processing;
  327. $this->fk_user_author = $obj->fk_user_author;
  328. $this->fk_user_mod = $obj->fk_user_mod;
  329. $this->note = $obj->note;
  330. $this->nbrun = $obj->nbrun;
  331. $this->maxrun = $obj->maxrun;
  332. $this->libname = $obj->libname;
  333. $this->test = $obj->test;
  334. }
  335. $this->db->free($resql);
  336. return 1;
  337. }
  338. else
  339. {
  340. $this->error="Error ".$this->db->lasterror();
  341. return -1;
  342. }
  343. }
  344. /**
  345. * Load object in memory from the database
  346. *
  347. * @param string $sortorder sort order
  348. * @param string $sortfield sort field
  349. * @param int $limit limit page
  350. * @param int $offset page
  351. * @param int $status display active or not
  352. * @param array $filter filter output
  353. * @param int $processing Processing or not
  354. * @return int <0 if KO, >0 if OK
  355. */
  356. function fetch_all($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='', $processing=-1)
  357. {
  358. global $langs;
  359. $this->lines=array();
  360. $sql = "SELECT";
  361. $sql.= " t.rowid,";
  362. $sql.= " t.entity,";
  363. $sql.= " t.tms,";
  364. $sql.= " t.datec,";
  365. $sql.= " t.jobtype,";
  366. $sql.= " t.label,";
  367. $sql.= " t.command,";
  368. $sql.= " t.classesname,";
  369. $sql.= " t.objectname,";
  370. $sql.= " t.methodename,";
  371. $sql.= " t.params,";
  372. $sql.= " t.md5params,";
  373. $sql.= " t.module_name,";
  374. $sql.= " t.priority,";
  375. $sql.= " t.datelastrun,";
  376. $sql.= " t.datenextrun,";
  377. $sql.= " t.dateend,";
  378. $sql.= " t.datestart,";
  379. $sql.= " t.lastresult,";
  380. $sql.= " t.datelastresult,";
  381. $sql.= " t.lastoutput,";
  382. $sql.= " t.unitfrequency,";
  383. $sql.= " t.frequency,";
  384. $sql.= " t.status,";
  385. $sql.= " t.processing,";
  386. $sql.= " t.fk_user_author,";
  387. $sql.= " t.fk_user_mod,";
  388. $sql.= " t.note,";
  389. $sql.= " t.nbrun,";
  390. $sql.= " t.libname,";
  391. $sql.= " t.test";
  392. $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  393. $sql.= " WHERE 1 = 1";
  394. if ($processing >= 0) $sql.= " AND t.processing = ".(empty($processing)?'0':'1');
  395. if ($status >= 0 && $status < 2) $sql.= " AND t.status = ".(empty($status)?'0':'1');
  396. if ($status == 2) $sql.= " AND t.status = 2";
  397. //Manage filter
  398. if (is_array($filter) && count($filter)>0) {
  399. foreach($filter as $key => $value)
  400. {
  401. if ($key == 't.rowid') $sql.= ' AND '.$key.' = '.$this->db->escape($value);
  402. else $sql.= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
  403. }
  404. }
  405. $sql.= $this->db->order($sortfield,$sortorder);
  406. if (!empty($limit) && !empty($offset)) {
  407. $sql.= $this->db->plimit($limit + 1,$offset);
  408. }
  409. $sqlwhere = array();
  410. if (count($sqlwhere)>0) {
  411. $sql.= " WHERE ".implode(' AND ',$sqlwhere);
  412. }
  413. dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
  414. $resql=$this->db->query($sql);
  415. if ($resql)
  416. {
  417. $num=$this->db->num_rows($resql);
  418. $i=0;
  419. if ($num)
  420. {
  421. while ($i < $num)
  422. {
  423. $line = new Cronjobline();
  424. $obj = $this->db->fetch_object($resql);
  425. $line->id = $obj->rowid;
  426. $line->ref = $obj->rowid;
  427. $line->entity = $obj->entity;
  428. $line->tms = $this->db->jdate($obj->tms);
  429. $line->datec = $this->db->jdate($obj->datec);
  430. $line->label = $obj->label;
  431. $line->jobtype = $obj->jobtype;
  432. $line->command = $obj->command;
  433. $line->classesname = $obj->classesname;
  434. $line->objectname = $obj->objectname;
  435. $line->methodename = $obj->methodename;
  436. $line->params = $obj->params;
  437. $line->md5params = $obj->md5params;
  438. $line->module_name = $obj->module_name;
  439. $line->priority = $obj->priority;
  440. $line->datelastrun = $this->db->jdate($obj->datelastrun);
  441. $line->datenextrun = $this->db->jdate($obj->datenextrun);
  442. $line->dateend = $this->db->jdate($obj->dateend);
  443. $line->datestart = $this->db->jdate($obj->datestart);
  444. $line->lastresult = $obj->lastresult;
  445. $line->datelastresult = $this->db->jdate($obj->datelastresult);
  446. $line->lastoutput = $obj->lastoutput;
  447. $line->unitfrequency = $obj->unitfrequency;
  448. $line->frequency = $obj->frequency;
  449. $line->status = $obj->status;
  450. $line->processing = $obj->processing;
  451. $line->fk_user_author = $obj->fk_user_author;
  452. $line->fk_user_mod = $obj->fk_user_mod;
  453. $line->note = $obj->note;
  454. $line->nbrun = $obj->nbrun;
  455. $line->libname = $obj->libname;
  456. $line->test = $obj->test;
  457. $this->lines[]=$line;
  458. $i++;
  459. }
  460. }
  461. $this->db->free($resql);
  462. return 1;
  463. }
  464. else
  465. {
  466. $this->error="Error ".$this->db->lasterror();
  467. return -1;
  468. }
  469. }
  470. /**
  471. * Update object into database
  472. *
  473. * @param User $user User that modifies
  474. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  475. * @return int <0 if KO, >0 if OK
  476. */
  477. function update($user=null, $notrigger=0)
  478. {
  479. global $conf, $langs;
  480. $langs->load('cron');
  481. $error=0;
  482. // Clean parameters
  483. if (isset($this->label)) $this->label=trim($this->label);
  484. if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
  485. if (isset($this->command)) $this->command=trim($this->command);
  486. if (isset($this->classesname)) $this->classesname=trim($this->classesname);
  487. if (isset($this->objectname)) $this->objectname=trim($this->objectname);
  488. if (isset($this->methodename)) $this->methodename=trim($this->methodename);
  489. if (isset($this->params)) $this->params=trim($this->params);
  490. if (isset($this->md5params)) $this->md5params=trim($this->md5params);
  491. if (isset($this->module_name)) $this->module_name=trim($this->module_name);
  492. if (isset($this->priority)) $this->priority=trim($this->priority);
  493. if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
  494. if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
  495. if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
  496. if (isset($this->frequency)) $this->frequency=trim($this->frequency);
  497. if (isset($this->status)) $this->status=trim($this->status);
  498. if (isset($this->note)) $this->note=trim($this->note);
  499. if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
  500. if (isset($this->libname)) $this->libname = trim($this->libname);
  501. if (isset($this->test)) $this->test = trim($this->test);
  502. if (empty($this->maxrun)) $this->maxrun=0;
  503. if (empty($this->processing)) $this->processing=0;
  504. // Check parameters
  505. // Put here code to add a control on parameters values
  506. if (dol_strlen($this->datestart)==0) {
  507. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronDtStart'));
  508. $error++;
  509. }
  510. if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
  511. $this->errors[]=$langs->trans('CronErrEndDateStartDt');
  512. $error++;
  513. }
  514. if (empty($this->label)) {
  515. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronLabel'));
  516. $error++;
  517. }
  518. if (empty($this->unitfrequency)) {
  519. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronFrequency'));
  520. $error++;
  521. }
  522. if (($this->jobtype=='command') && (empty($this->command))) {
  523. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronCommand'));
  524. $error++;
  525. }
  526. if (($this->jobtype=='method') && (empty($this->classesname))) {
  527. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronClass'));
  528. $error++;
  529. }
  530. if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  531. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronMethod'));
  532. $error++;
  533. }
  534. if (($this->jobtype=='method') && (empty($this->objectname))) {
  535. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronObject'));
  536. $error++;
  537. }
  538. if (($this->jobtype=='function') && (empty($this->libname))) {
  539. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->transnoentitiesnoconv('CronLib'));
  540. $error++;
  541. }
  542. // Update request
  543. $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
  544. $sql.= " entity=".(isset($this->entity)?$this->db->escape($this->entity):$conf->entity).",";
  545. $sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
  546. $sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").",";
  547. $sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").",";
  548. $sql.= " classesname=".(isset($this->classesname)?"'".$this->db->escape($this->classesname)."'":"null").",";
  549. $sql.= " objectname=".(isset($this->objectname)?"'".$this->db->escape($this->objectname)."'":"null").",";
  550. $sql.= " methodename=".(isset($this->methodename)?"'".$this->db->escape($this->methodename)."'":"null").",";
  551. $sql.= " params=".(isset($this->params)?"'".$this->db->escape($this->params)."'":"null").",";
  552. $sql.= " md5params=".(isset($this->md5params)?"'".$this->db->escape($this->md5params)."'":"null").",";
  553. $sql.= " module_name=".(isset($this->module_name)?"'".$this->db->escape($this->module_name)."'":"null").",";
  554. $sql.= " priority=".(isset($this->priority)?$this->priority:"null").",";
  555. $sql.= " datelastrun=".(dol_strlen($this->datelastrun)!=0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
  556. $sql.= " datenextrun=".(dol_strlen($this->datenextrun)!=0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
  557. $sql.= " dateend=".(dol_strlen($this->dateend)!=0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
  558. $sql.= " datestart=".(dol_strlen($this->datestart)!=0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
  559. $sql.= " datelastresult=".(dol_strlen($this->datelastresult)!=0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
  560. $sql.= " lastresult=".(isset($this->lastresult)?"'".$this->db->escape($this->lastresult)."'":"null").",";
  561. $sql.= " lastoutput=".(isset($this->lastoutput)?"'".$this->db->escape($this->lastoutput)."'":"null").",";
  562. $sql.= " unitfrequency=".(isset($this->unitfrequency)?$this->unitfrequency:"null").",";
  563. $sql.= " frequency=".(isset($this->frequency)?$this->frequency:"null").",";
  564. $sql.= " status=".(isset($this->status)?$this->status:"null").",";
  565. $sql.= " processing=".((isset($this->processing) && $this->processing > 0)?$this->processing:"0").",";
  566. $sql.= " fk_user_mod=".$user->id.",";
  567. $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
  568. $sql.= " nbrun=".((isset($this->nbrun) && $this->nbrun >0)?$this->nbrun:"null").",";
  569. $sql.= " maxrun=".((isset($this->maxrun) && $this->maxrun > 0)?$this->maxrun:"0").",";
  570. $sql.= " libname=".(isset($this->libname)?"'".$this->db->escape($this->libname)."'":"null").",";
  571. $sql.= " test=".(isset($this->test)?"'".$this->db->escape($this->test)."'":"null");
  572. $sql.= " WHERE rowid=".$this->id;
  573. $this->db->begin();
  574. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  575. $resql = $this->db->query($sql);
  576. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  577. if (! $error)
  578. {
  579. if (! $notrigger)
  580. {
  581. // Uncomment this and change MYOBJECT to your own tag if you
  582. // want this action calls a trigger.
  583. //// Call triggers
  584. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  585. //$interface=new Interfaces($this->db);
  586. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  587. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  588. //// End call triggers
  589. }
  590. }
  591. // Commit or rollback
  592. if ($error)
  593. {
  594. foreach($this->errors as $errmsg)
  595. {
  596. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  597. $this->error.=($this->error?', '.$errmsg:$errmsg);
  598. }
  599. $this->db->rollback();
  600. return -1*$error;
  601. }
  602. else
  603. {
  604. $this->db->commit();
  605. return 1;
  606. }
  607. }
  608. /**
  609. * Delete object in database
  610. *
  611. * @param User $user User that deletes
  612. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  613. * @return int <0 if KO, >0 if OK
  614. */
  615. function delete($user, $notrigger=0)
  616. {
  617. $error=0;
  618. $this->db->begin();
  619. // if (! $error)
  620. // {
  621. // if (! $notrigger)
  622. // {
  623. // Uncomment this and change MYOBJECT to your own tag if you
  624. // want this action calls a trigger.
  625. //// Call triggers
  626. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  627. //$interface=new Interfaces($this->db);
  628. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  629. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  630. //// End call triggers
  631. // }
  632. // }
  633. // if (! $error)
  634. // {
  635. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  636. $sql.= " WHERE rowid=".$this->id;
  637. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  638. $resql = $this->db->query($sql);
  639. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  640. // }
  641. // Commit or rollback
  642. if ($error)
  643. {
  644. foreach($this->errors as $errmsg)
  645. {
  646. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  647. $this->error.=($this->error?', '.$errmsg:$errmsg);
  648. }
  649. $this->db->rollback();
  650. return -1*$error;
  651. }
  652. else
  653. {
  654. $this->db->commit();
  655. return 1;
  656. }
  657. }
  658. /**
  659. * Load an object from its id and create a new one in database
  660. *
  661. * @param int $fromid Id of object to clone
  662. * @return int New id of clone
  663. */
  664. function createFromClone($fromid)
  665. {
  666. global $user,$langs;
  667. $error=0;
  668. $object=new Cronjob($this->db);
  669. $object->context['createfromclone'] = 'createfromclone';
  670. $this->db->begin();
  671. // Load source object
  672. $object->fetch($fromid);
  673. $object->id=0;
  674. $object->statut=0;
  675. // Clear fields
  676. // ...
  677. // Create clone
  678. $result=$object->create($user);
  679. // Other options
  680. if ($result < 0)
  681. {
  682. $this->error=$object->error;
  683. $error++;
  684. }
  685. if (! $error)
  686. {
  687. }
  688. unset($this->context['createfromclone']);
  689. // End
  690. if (! $error)
  691. {
  692. $this->db->commit();
  693. return $object->id;
  694. }
  695. else
  696. {
  697. $this->db->rollback();
  698. return -1;
  699. }
  700. }
  701. /**
  702. * Initialise object with example values
  703. * Id must be 0 if object instance is a specimen
  704. *
  705. * @return void
  706. */
  707. function initAsSpecimen()
  708. {
  709. $this->id=0;
  710. $this->ref=0;
  711. $this->entity=0;
  712. $this->tms='';
  713. $this->datec='';
  714. $this->label='';
  715. $this->jobtype='';
  716. $this->command='';
  717. $this->classesname='';
  718. $this->objectname='';
  719. $this->methodename='';
  720. $this->params='';
  721. $this->md5params='';
  722. $this->module_name='';
  723. $this->priority='';
  724. $this->datelastrun='';
  725. $this->datenextrun='';
  726. $this->dateend='';
  727. $this->datestart='';
  728. $this->datelastresult='';
  729. $this->lastoutput='';
  730. $this->lastresult='';
  731. $this->unitfrequency='';
  732. $this->frequency='';
  733. $this->status=0;
  734. $this->processing=0;
  735. $this->fk_user_author='';
  736. $this->fk_user_mod='';
  737. $this->note='';
  738. $this->nbrun='';
  739. $this->maxrun=100;
  740. $this->libname = '';
  741. }
  742. /**
  743. * Return a link to the object card (with optionaly the picto)
  744. *
  745. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  746. * @param string $option On what the link point to ('nolink', ...)
  747. * @param int $notooltip 1=Disable tooltip
  748. * @param string $morecss Add more css on link
  749. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  750. * @return string String with URL
  751. */
  752. function getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
  753. {
  754. global $db, $conf, $langs;
  755. global $dolibarr_main_authentication, $dolibarr_main_demo;
  756. global $menumanager;
  757. if (! empty($conf->dol_no_mouse_hover)) $notooltip=1; // Force disable tooltips
  758. $result = '';
  759. $companylink = '';
  760. $label = '<u>' . $langs->trans("CronJob") . '</u>';
  761. $label.= '<br>';
  762. $label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  763. $url = DOL_URL_ROOT.'/cron/card.php?id='.$this->id;
  764. if ($option != 'nolink')
  765. {
  766. // Add param to save lastsearch_values or not
  767. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  768. if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1;
  769. if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1';
  770. }
  771. $linkclose='';
  772. if (empty($notooltip))
  773. {
  774. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
  775. {
  776. $label=$langs->trans("ShowCronJob");
  777. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  778. }
  779. $linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
  780. $linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
  781. }
  782. else $linkclose = ($morecss?' class="'.$morecss.'"':'');
  783. $linkstart = '<a href="'.$url.'"';
  784. $linkstart.=$linkclose.'>';
  785. $linkend='</a>';
  786. $result .= $linkstart;
  787. if ($withpicto) $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);
  788. if ($withpicto != 2) $result.= $this->ref;
  789. $result .= $linkend;
  790. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  791. return $result;
  792. }
  793. /**
  794. * Load object information
  795. *
  796. * @return int
  797. */
  798. function info()
  799. {
  800. $sql = "SELECT";
  801. $sql.= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
  802. $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as f";
  803. $sql.= " WHERE f.rowid = ".$this->id;
  804. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  805. $resql=$this->db->query($sql);
  806. if ($resql)
  807. {
  808. if ($this->db->num_rows($resql))
  809. {
  810. $obj = $this->db->fetch_object($resql);
  811. $this->id = $obj->rowid;
  812. $this->date_creation = $this->db->jdate($obj->datec);
  813. $this->date_modification = $this->db->jdate($obj->tms);
  814. $this->user_modification = $obj->fk_user_mod;
  815. $this->user_creation = $obj->fk_user_author;
  816. }
  817. $this->db->free($resql);
  818. return 1;
  819. }
  820. else
  821. {
  822. $this->error="Error ".$this->db->lasterror();
  823. return -1;
  824. }
  825. }
  826. /**
  827. * Run a job.
  828. * Once job is finished, status and nb of run is updated.
  829. * This function does not plan the next run. This is done by function ->reprogram_jobs
  830. *
  831. * @param string $userlogin User login
  832. * @return int <0 if KO, >0 if OK
  833. */
  834. function run_jobs($userlogin)
  835. {
  836. global $langs, $conf;
  837. $now=dol_now();
  838. $error = 0;
  839. $retval = '';
  840. $langs->load('cron');
  841. if (empty($userlogin))
  842. {
  843. $this->error="User login is mandatory";
  844. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  845. return -1;
  846. }
  847. // Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
  848. // When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
  849. if ($conf->entity != $this->entity && $this->entity > 0)
  850. {
  851. dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
  852. }
  853. $savcurrententity = $conf->entity;
  854. $conf->entity = $this->entity;
  855. dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
  856. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  857. $user=new User($this->db);
  858. $result=$user->fetch('',$userlogin);
  859. if ($result<0)
  860. {
  861. $this->error="User Error:".$user->error;
  862. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  863. $conf->entity = $savcurrententity;
  864. return -1;
  865. }
  866. else
  867. {
  868. if (empty($user->id))
  869. {
  870. $this->error=" User user login:".$userlogin." do not exists";
  871. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  872. $conf->entity = $savcurrententity;
  873. return -1;
  874. }
  875. }
  876. dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
  877. // Increase limit of time. Works only if we are not in safe mode
  878. $ExecTimeLimit=600;
  879. if (!empty($ExecTimeLimit))
  880. {
  881. $err=error_reporting();
  882. error_reporting(0); // Disable all errors
  883. //error_reporting(E_ALL);
  884. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  885. error_reporting($err);
  886. }
  887. if (!empty($MemoryLimit))
  888. {
  889. @ini_set('memory_limit', $MemoryLimit);
  890. }
  891. // Update last run date start (to track running jobs)
  892. $this->datelastrun=$now;
  893. $this->datelastresult=null;
  894. $this->lastoutput='';
  895. $this->lastresult='';
  896. $this->processing = 1; // To know job was started
  897. $this->nbrun=$this->nbrun + 1;
  898. $result = $this->update($user); // This include begin/commit
  899. if ($result<0) {
  900. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  901. $conf->entity = $savcurrententity;
  902. return -1;
  903. }
  904. // Run a method
  905. if ($this->jobtype=='method')
  906. {
  907. // load classes
  908. if (! $error)
  909. {
  910. $ret=dol_include_once($this->classesname);
  911. if ($ret===false || (! class_exists($this->objectname)))
  912. {
  913. if ($ret===false) $this->error=$langs->trans('CronCannotLoadClass',$this->classesname,$this->objectname);
  914. else $this->error=$langs->trans('CronCannotLoadObject',$this->classesname,$this->objectname);
  915. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  916. $this->lastoutput = $this->error;
  917. $this->lastresult = -1;
  918. $retval = $this->lastresult;
  919. $error++;
  920. }
  921. }
  922. // test if method exists
  923. if (! $error)
  924. {
  925. if (! method_exists($this->objectname, $this->methodename))
  926. {
  927. $this->error=$langs->trans('CronMethodDoesNotExists',$this->objectname,$this->methodename);
  928. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  929. $this->lastoutput = $this->error;
  930. $this->lastresult = -1;
  931. $retval = $this->lastresult;
  932. $error++;
  933. }
  934. }
  935. // Load langs
  936. if (! $error)
  937. {
  938. $result=$langs->load($this->module_name.'@'.$this->module_name);
  939. if ($result < 0)
  940. {
  941. dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
  942. $this->error = $langs->error;
  943. $this->lastoutput = $this->error;
  944. $this->lastresult = -1;
  945. $retval = $this->lastresult;
  946. $error++;
  947. }
  948. }
  949. if (! $error)
  950. {
  951. dol_syslog(get_class($this)."::run_jobs START ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
  952. // Create Object for the called module
  953. $object = new $this->objectname($this->db);
  954. if ($this->entity > 0) $object->entity = $this->entity; // We work on a dedicated entity
  955. $params_arr = array_map('trim', explode(",",$this->params));
  956. if (!is_array($params_arr))
  957. {
  958. $result = call_user_func(array($object, $this->methodename), $this->params);
  959. }
  960. else
  961. {
  962. $result = call_user_func_array(array($object, $this->methodename), $params_arr);
  963. }
  964. if ($result === false || (! is_bool($result) && $result != 0))
  965. {
  966. $langs->load("errors");
  967. $errmsg='';
  968. if (! is_array($object->errors) || ! in_array($object->error, $object->errors)) $errmsg.=$object->error;
  969. if (is_array($object->errors) && count($object->errors)) $errmsg.=($errmsg?', '.$errmsg:'').join(', ',$object->errors);
  970. if (empty($errmsg)) $errmsg=$langs->trans('ErrorUnknown');
  971. dol_syslog(get_class($this)."::run_jobs END result=".$result." error=".$errmsg, LOG_ERR);
  972. $this->error = $errmsg;
  973. $this->lastoutput = ($object->output?$object->output."\n":"").$errmsg;
  974. $this->lastresult = is_numeric($result)?$result:-1;
  975. $retval = $this->lastresult;
  976. $error++;
  977. }
  978. else
  979. {
  980. dol_syslog(get_class($this)."::run_jobs END");
  981. $this->lastoutput=$object->output;
  982. $this->lastresult=var_export($result,true);
  983. $retval = $this->lastresult;
  984. }
  985. }
  986. }
  987. if($this->jobtype == 'function')
  988. {
  989. //load lib
  990. $libpath = '/' . strtolower($this->module_name) . '/lib/' . $this->libname;
  991. $ret = dol_include_once($libpath);
  992. if ($ret === false)
  993. {
  994. $this->error = $langs->trans('CronCannotLoadLib') . ': ' . $libpath;
  995. dol_syslog(get_class($this) . "::run_jobs " . $this->error, LOG_ERR);
  996. $conf->entity = $savcurrententity;
  997. return -1;
  998. }
  999. // Load langs
  1000. $result=$langs->load($this->module_name . '@' . $this->module_name);
  1001. if ($result<0)
  1002. {
  1003. dol_syslog(get_class($this) . "::run_jobs Cannot load module langs" . $langs->error, LOG_ERR);
  1004. $conf->entity = $savcurrententity;
  1005. return -1;
  1006. }
  1007. dol_syslog(get_class($this) . "::run_jobs " . $this->libname . "::" . $this->methodename."(" . $this->params . ");", LOG_DEBUG);
  1008. $params_arr = explode(", ", $this->params);
  1009. if (!is_array($params_arr))
  1010. {
  1011. $result = call_user_func($this->methodename, $this->params);
  1012. }
  1013. else
  1014. {
  1015. $result = call_user_func_array($this->methodename, $params_arr);
  1016. }
  1017. if ($result === false || (! is_bool($result) && $result != 0))
  1018. {
  1019. $langs->load("errors");
  1020. dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
  1021. $this->error = $langs->trans('ErrorUnknown');
  1022. $this->lastoutput = $this->error;
  1023. $this->lastresult = is_numeric($result)?$result:-1;
  1024. $retval = $this->lastresult;
  1025. $error++;
  1026. }
  1027. else
  1028. {
  1029. $this->lastoutput=var_export($result,true);
  1030. $this->lastresult=var_export($result,true); // Return code
  1031. $retval = $this->lastresult;
  1032. }
  1033. }
  1034. // Run a command line
  1035. if ($this->jobtype=='command')
  1036. {
  1037. $outputdir = $conf->cron->dir_temp;
  1038. if (empty($outputdir)) $outputdir = $conf->cronjob->dir_temp;
  1039. if (! empty($outputdir))
  1040. {
  1041. dol_mkdir($outputdir);
  1042. $outputfile=$outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method
  1043. // Execute a CLI
  1044. include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  1045. $utils = new Utils($this->db);
  1046. $arrayresult = $utils->executeCLI($this->command, $outputfile);
  1047. $retval = $arrayresult['result'];
  1048. $this->error = $arrayresult['error'];
  1049. $this->lastoutput = $arrayresult['output'];
  1050. $this->lastresult = $arrayresult['result'];
  1051. }
  1052. }
  1053. dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)");
  1054. $this->datelastresult=dol_now();
  1055. $this->processing=0;
  1056. $result = $this->update($user); // This include begin/commit
  1057. if ($result < 0)
  1058. {
  1059. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  1060. $conf->entity = $savcurrententity;
  1061. return -1;
  1062. }
  1063. $conf->entity = $savcurrententity;
  1064. return $error?-1:1;
  1065. }
  1066. /**
  1067. * Reprogram a job
  1068. *
  1069. * @param string $userlogin User login
  1070. * @param timestamp $now Date returned by dol_now()
  1071. * @return int <0 if KO, >0 if OK
  1072. */
  1073. function reprogram_jobs($userlogin, $now)
  1074. {
  1075. dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
  1076. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1077. $user=new User($this->db);
  1078. $result=$user->fetch('',$userlogin);
  1079. if ($result<0)
  1080. {
  1081. $this->error="User Error:".$user->error;
  1082. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1083. return -1;
  1084. }
  1085. else
  1086. {
  1087. if (empty($user->id))
  1088. {
  1089. $this->error=" User user login:".$userlogin." do not exists";
  1090. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1091. return -1;
  1092. }
  1093. }
  1094. dol_syslog(get_class($this)."::reprogram_jobs datenextrun=".$this->datenextrun." ".dol_print_date($this->datenextrun, 'dayhourrfc')." frequency=".$this->frequency." unitfrequency=".$this->unitfrequency, LOG_DEBUG);
  1095. if (empty($this->datenextrun))
  1096. {
  1097. if (empty($this->datestart)) $this->datenextrun = $now + ($this->frequency * $this->unitfrequency);
  1098. else $this->datenextrun = $this->datestart + ($this->frequency * $this->unitfrequency);
  1099. }
  1100. if ($this->datenextrun < $now && $this->frequency > 0 && $this->unitfrequency > 0)
  1101. {
  1102. // Loop until date is after future
  1103. while ($this->datenextrun < $now)
  1104. {
  1105. $this->datenextrun += ($this->frequency * $this->unitfrequency);
  1106. // TODO For exact frequency (every month, every year, ...), use instead a dol_time_plus_duree($time, $duration_value, $duration_unit)
  1107. }
  1108. }
  1109. else
  1110. {
  1111. //$this->datenextrun=$this->datenextrun + ($this->frequency * $this->unitfrequency);
  1112. dol_syslog(get_class($this)."::reprogram_jobs datenextrun is already in future, we do not change it");
  1113. }
  1114. // Archive job
  1115. if ($this->autodelete == 2)
  1116. {
  1117. if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
  1118. || ($this->dateend && ($this->datenextrun > $this->dateend)))
  1119. {
  1120. $this->status = 2;
  1121. dol_syslog(get_class($this)."::reprogram_jobs Job will be set to archived", LOG_ERR);
  1122. }
  1123. }
  1124. $result = $this->update($user);
  1125. if ($result<0)
  1126. {
  1127. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  1128. return -1;
  1129. }
  1130. return 1;
  1131. }
  1132. /**
  1133. * Return label of status of user (active, inactive)
  1134. *
  1135. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1136. * @return string Label of status
  1137. */
  1138. function getLibStatut($mode=0)
  1139. {
  1140. return $this->LibStatut($this->status,$mode);
  1141. }
  1142. /**
  1143. * Renvoi le libelle d'un statut donne
  1144. *
  1145. * @param int $status Id statut
  1146. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  1147. * @return string Label of status
  1148. */
  1149. function LibStatut($status,$mode=0)
  1150. {
  1151. global $langs;
  1152. $langs->load('users');
  1153. if ($mode == 0)
  1154. {
  1155. $prefix='';
  1156. if ($status == 1) return $langs->trans('Enabled');
  1157. if ($status == 0) return $langs->trans('Disabled');
  1158. }
  1159. if ($mode == 1)
  1160. {
  1161. if ($status == 1) return $langs->trans('Enabled');
  1162. if ($status == 0) return $langs->trans('Disabled');
  1163. }
  1164. if ($mode == 2)
  1165. {
  1166. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"').' '.$langs->trans('Enabled');
  1167. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"').' '.$langs->trans('Disabled');
  1168. }
  1169. if ($mode == 3)
  1170. {
  1171. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"');
  1172. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"');
  1173. }
  1174. if ($mode == 4)
  1175. {
  1176. if ($status == 1) return img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"').' '.$langs->trans('Enabled');
  1177. if ($status == 0) return img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"').' '.$langs->trans('Disabled');
  1178. }
  1179. if ($mode == 5)
  1180. {
  1181. if ($status == 1) return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'),'statut4','class="pictostatus"');
  1182. if ($status == 0) return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'),'statut5','class="pictostatus"');
  1183. }
  1184. }
  1185. }
  1186. /**
  1187. * Crob Job line class
  1188. */
  1189. class Cronjobline
  1190. {
  1191. public $id;
  1192. /**
  1193. * @var string Ref
  1194. */
  1195. public $ref;
  1196. public $tms='';
  1197. public $datec='';
  1198. public $label;
  1199. public $jobtype;
  1200. public $command;
  1201. public $classesname;
  1202. public $objectname;
  1203. public $methodename;
  1204. public $params;
  1205. public $md5params;
  1206. public $module_name;
  1207. public $priority;
  1208. public $datelastrun='';
  1209. public $datenextrun='';
  1210. public $dateend='';
  1211. public $datestart='';
  1212. public $lastresult='';
  1213. public $lastoutput;
  1214. public $unitfrequency;
  1215. public $frequency;
  1216. public $status;
  1217. public $fk_user_author;
  1218. public $fk_user_mod;
  1219. public $note;
  1220. public $nbrun;
  1221. public $libname;
  1222. /**
  1223. * Constructor
  1224. *
  1225. */
  1226. function __construct()
  1227. {
  1228. return 1;
  1229. }
  1230. }