cronjob.class.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. var $db; //!< To store db handler
  30. var $error; //!< To return error code (or message)
  31. var $errors=array(); //!< To return several error codes (or messages)
  32. var $element='cronjob'; //!< Id that identify managed objects
  33. var $table_element='cronjob'; //!< Name of table without prefix where object is stored
  34. var $id;
  35. var $ref; //Use for prevnext_ref
  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 $fk_user_author;
  59. var $fk_user_mod;
  60. var $note;
  61. var $nbrun;
  62. var $libname;
  63. var $lines;
  64. /**
  65. * Constructor
  66. *
  67. * @param DoliDb $db Database handler
  68. */
  69. function __construct($db)
  70. {
  71. $this->db = $db;
  72. return 1;
  73. }
  74. /**
  75. * Create object into database
  76. *
  77. * @param User $user User that creates
  78. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  79. * @return int <0 if KO, Id of created object if OK
  80. */
  81. function create($user, $notrigger=0)
  82. {
  83. global $conf, $langs;
  84. $error=0;
  85. $now=dol_now();
  86. // Clean parameters
  87. if (isset($this->label)) $this->label=trim($this->label);
  88. if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
  89. if (isset($this->command)) $this->command=trim($this->command);
  90. if (isset($this->classesname)) $this->classesname=trim($this->classesname);
  91. if (isset($this->objectname)) $this->objectname=trim($this->objectname);
  92. if (isset($this->methodename)) $this->methodename=trim($this->methodename);
  93. if (isset($this->params)) $this->params=trim($this->params);
  94. if (isset($this->md5params)) $this->md5params=trim($this->md5params);
  95. if (isset($this->module_name)) $this->module_name=trim($this->module_name);
  96. if (isset($this->priority)) $this->priority=trim($this->priority);
  97. if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
  98. if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
  99. if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
  100. if (isset($this->frequency)) $this->frequency=trim($this->frequency);
  101. if (isset($this->status)) $this->status=trim($this->status);
  102. if (isset($this->note)) $this->note=trim($this->note);
  103. if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
  104. if (isset($this->libname)) $this->libname = trim($this->libname);
  105. // Check parameters
  106. // Put here code to add a control on parameters values
  107. if (dol_strlen($this->datestart)==0) {
  108. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart'));
  109. $error++;
  110. }
  111. if (empty($this->label)) {
  112. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel'));
  113. $error++;
  114. }
  115. if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
  116. $this->errors[]=$langs->trans('CronErrEndDateStartDt');
  117. $error++;
  118. }
  119. if (empty($this->unitfrequency)) {
  120. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency'));
  121. $error++;
  122. }
  123. if (($this->jobtype=='command') && (empty($this->command))) {
  124. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand'));
  125. $error++;
  126. }
  127. if (($this->jobtype=='method') && (empty($this->classesname))) {
  128. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass'));
  129. $error++;
  130. }
  131. if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  132. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod'));
  133. $error++;
  134. }
  135. if (($this->jobtype=='method') && (empty($this->objectname))) {
  136. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject'));
  137. $error++;
  138. }
  139. if (($this->jobtype=='function') && (empty($this->libname))) {
  140. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib'));
  141. $error++;
  142. }
  143. // Insert request
  144. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
  145. $sql.= "datec,";
  146. $sql.= "jobtype,";
  147. $sql.= "label,";
  148. $sql.= "command,";
  149. $sql.= "classesname,";
  150. $sql.= "objectname,";
  151. $sql.= "methodename,";
  152. $sql.= "params,";
  153. $sql.= "md5params,";
  154. $sql.= "module_name,";
  155. $sql.= "priority,";
  156. $sql.= "datelastrun,";
  157. $sql.= "datenextrun,";
  158. $sql.= "dateend,";
  159. $sql.= "datestart,";
  160. $sql.= "lastresult,";
  161. $sql.= "datelastresult,";
  162. $sql.= "lastoutput,";
  163. $sql.= "unitfrequency,";
  164. $sql.= "frequency,";
  165. $sql.= "status,";
  166. $sql.= "fk_user_author,";
  167. $sql.= "fk_user_mod,";
  168. $sql.= "note,";
  169. $sql.= "nbrun";
  170. $sql .= ",libname";
  171. $sql.= ") VALUES (";
  172. $sql.= " '".$this->db->idate($now)."',";
  173. $sql.= " ".(! isset($this->jobtype)?'NULL':"'".$this->db->escape($this->jobtype)."'").",";
  174. $sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").",";
  175. $sql.= " ".(! isset($this->command)?'NULL':"'".$this->db->escape($this->command)."'").",";
  176. $sql.= " ".(! isset($this->classesname)?'NULL':"'".$this->db->escape($this->classesname)."'").",";
  177. $sql.= " ".(! isset($this->objectname)?'NULL':"'".$this->db->escape($this->objectname)."'").",";
  178. $sql.= " ".(! isset($this->methodename)?'NULL':"'".$this->db->escape($this->methodename)."'").",";
  179. $sql.= " ".(! isset($this->params)?'NULL':"'".$this->db->escape($this->params)."'").",";
  180. $sql.= " ".(! isset($this->md5params)?'NULL':"'".$this->db->escape($this->md5params)."'").",";
  181. $sql.= " ".(! isset($this->module_name)?'NULL':"'".$this->db->escape($this->module_name)."'").",";
  182. $sql.= " ".(! isset($this->priority)?'NULL':"'".$this->priority."'").",";
  183. $sql.= " ".(! isset($this->datelastrun) || dol_strlen($this->datelastrun)==0?'NULL':$this->db->idate($this->datelastrun)).",";
  184. $sql.= " ".(! isset($this->datenextrun) || dol_strlen($this->datenextrun)==0?'NULL':$this->db->idate($this->datenextrun)).",";
  185. $sql.= " ".(! isset($this->dateend) || dol_strlen($this->dateend)==0?'NULL':$this->db->idate($this->dateend)).",";
  186. $sql.= " ".(! isset($this->datestart) || dol_strlen($this->datestart)==0?'NULL':$this->db->idate($this->datestart)).",";
  187. $sql.= " ".(! isset($this->lastresult)?'NULL':"'".$this->db->escape($this->lastresult)."'").",";
  188. $sql.= " ".(! isset($this->datelastresult) || dol_strlen($this->datelastresult)==0?'NULL':$this->db->idate($this->datelastresult)).",";
  189. $sql.= " ".(! isset($this->lastoutput)?'NULL':"'".$this->db->escape($this->lastoutput)."'").",";
  190. $sql.= " ".(! isset($this->unitfrequency)?'NULL':"'".$this->unitfrequency."'").",";
  191. $sql.= " ".(! isset($this->frequency)?'NULL':"'".$this->frequency."'").",";
  192. $sql.= " ".(! isset($this->status)?'0':"'".$this->status."'").",";
  193. $sql.= " ".$user->id.",";
  194. $sql.= " ".$user->id.",";
  195. $sql.= " ".(! isset($this->note)?'NULL':"'".$this->db->escape($this->note)."'").",";
  196. $sql.= " ".(! isset($this->nbrun)?'0':"'".$this->db->escape($this->nbrun)."'").",";
  197. $sql.= " ".(! isset($this->libname)?'NULL':"'".$this->db->escape($this->libname)."'")."";
  198. $sql.= ")";
  199. $this->db->begin();
  200. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  201. $resql=$this->db->query($sql);
  202. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  203. if (! $error)
  204. {
  205. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob");
  206. if (! $notrigger)
  207. {
  208. // Uncomment this and change MYOBJECT to your own tag if you
  209. // want this action calls a trigger.
  210. //// Call triggers
  211. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  212. //$interface=new Interfaces($this->db);
  213. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  214. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  215. //// End call triggers
  216. }
  217. }
  218. // Commit or rollback
  219. if ($error)
  220. {
  221. foreach($this->errors as $errmsg)
  222. {
  223. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  224. $this->error.=($this->error?', '.$errmsg:$errmsg);
  225. }
  226. $this->db->rollback();
  227. return -1*$error;
  228. }
  229. else
  230. {
  231. $this->db->commit();
  232. return $this->id;
  233. }
  234. }
  235. /**
  236. * Load object in memory from the database
  237. *
  238. * @param int $id Id object
  239. * @return int <0 if KO, >0 if OK
  240. */
  241. function fetch($id)
  242. {
  243. $sql = "SELECT";
  244. $sql.= " t.rowid,";
  245. $sql.= " t.tms,";
  246. $sql.= " t.datec,";
  247. $sql.= " t.jobtype,";
  248. $sql.= " t.label,";
  249. $sql.= " t.command,";
  250. $sql.= " t.classesname,";
  251. $sql.= " t.objectname,";
  252. $sql.= " t.methodename,";
  253. $sql.= " t.params,";
  254. $sql.= " t.md5params,";
  255. $sql.= " t.module_name,";
  256. $sql.= " t.priority,";
  257. $sql.= " t.datelastrun,";
  258. $sql.= " t.datenextrun,";
  259. $sql.= " t.dateend,";
  260. $sql.= " t.datestart,";
  261. $sql.= " t.lastresult,";
  262. $sql.= " t.datelastresult,";
  263. $sql.= " t.lastoutput,";
  264. $sql.= " t.unitfrequency,";
  265. $sql.= " t.frequency,";
  266. $sql.= " t.status,";
  267. $sql.= " t.fk_user_author,";
  268. $sql.= " t.fk_user_mod,";
  269. $sql.= " t.note,";
  270. $sql.= " t.nbrun";
  271. $sql .= ", t.libname";
  272. $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  273. $sql.= " WHERE t.rowid = ".$id;
  274. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  275. $resql=$this->db->query($sql);
  276. if ($resql)
  277. {
  278. if ($this->db->num_rows($resql))
  279. {
  280. $obj = $this->db->fetch_object($resql);
  281. $this->id = $obj->rowid;
  282. $this->ref = $obj->rowid;
  283. $this->tms = $this->db->jdate($obj->tms);
  284. $this->datec = $this->db->jdate($obj->datec);
  285. $this->label = $obj->label;
  286. $this->jobtype = $obj->jobtype;
  287. $this->command = $obj->command;
  288. $this->classesname = $obj->classesname;
  289. $this->objectname = $obj->objectname;
  290. $this->methodename = $obj->methodename;
  291. $this->params = $obj->params;
  292. $this->md5params = $obj->md5params;
  293. $this->module_name = $obj->module_name;
  294. $this->priority = $obj->priority;
  295. $this->datelastrun = $this->db->jdate($obj->datelastrun);
  296. $this->datenextrun = $this->db->jdate($obj->datenextrun);
  297. $this->dateend = $this->db->jdate($obj->dateend);
  298. $this->datestart = $this->db->jdate($obj->datestart);
  299. $this->lastresult = $obj->lastresult;
  300. $this->lastoutput = $obj->lastoutput;
  301. $this->datelastresult = $this->db->jdate($obj->datelastresult);
  302. $this->unitfrequency = $obj->unitfrequency;
  303. $this->frequency = $obj->frequency;
  304. $this->status = $obj->status;
  305. $this->fk_user_author = $obj->fk_user_author;
  306. $this->fk_user_mod = $obj->fk_user_mod;
  307. $this->note = $obj->note;
  308. $this->nbrun = $obj->nbrun;
  309. $this->libname = $obj->libname;
  310. }
  311. $this->db->free($resql);
  312. return 1;
  313. }
  314. else
  315. {
  316. $this->error="Error ".$this->db->lasterror();
  317. return -1;
  318. }
  319. }
  320. /**
  321. * Load object in memory from the database
  322. *
  323. * @param string $sortorder sort order
  324. * @param string $sortfield sort field
  325. * @param int $limit limit page
  326. * @param int $offset page
  327. * @param int $status display active or not
  328. * @param array $filter filter output
  329. * @return int <0 if KO, >0 if OK
  330. */
  331. function fetch_all($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='')
  332. {
  333. global $langs;
  334. $sql = "SELECT";
  335. $sql.= " t.rowid,";
  336. $sql.= " t.tms,";
  337. $sql.= " t.datec,";
  338. $sql.= " t.jobtype,";
  339. $sql.= " t.label,";
  340. $sql.= " t.command,";
  341. $sql.= " t.classesname,";
  342. $sql.= " t.objectname,";
  343. $sql.= " t.methodename,";
  344. $sql.= " t.params,";
  345. $sql.= " t.md5params,";
  346. $sql.= " t.module_name,";
  347. $sql.= " t.priority,";
  348. $sql.= " t.datelastrun,";
  349. $sql.= " t.datenextrun,";
  350. $sql.= " t.dateend,";
  351. $sql.= " t.datestart,";
  352. $sql.= " t.lastresult,";
  353. $sql.= " t.datelastresult,";
  354. $sql.= " t.lastoutput,";
  355. $sql.= " t.unitfrequency,";
  356. $sql.= " t.frequency,";
  357. $sql.= " t.status,";
  358. $sql.= " t.fk_user_author,";
  359. $sql.= " t.fk_user_mod,";
  360. $sql.= " t.note,";
  361. $sql.= " t.nbrun";
  362. $sql .= ", t.libname";
  363. $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
  364. $sql.= " WHERE 1 = 1";
  365. if ($status >= 0) $sql.= " AND t.status = ".(empty($status)?'0':'1');
  366. //Manage filter
  367. if (is_array($filter) && count($filter)>0) {
  368. foreach($filter as $key => $value) {
  369. $sql.= ' AND '.$key.' LIKE \'%'.$value.'%\'';
  370. }
  371. }
  372. $sql.= " ORDER BY $sortfield $sortorder ";
  373. if (!empty($limit) && !empty($offset)) {
  374. $sql.= $this->db->plimit($limit + 1,$offset);
  375. }
  376. $sqlwhere = array();
  377. if (!empty($module_name)) {
  378. $sqlwhere[]='(t.module_name='.$module_name.')';
  379. }
  380. if (count($sqlwhere)>0) {
  381. $sql.= " WHERE ".implode(' AND ',$sqlwhere);
  382. }
  383. dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
  384. $resql=$this->db->query($sql);
  385. if ($resql)
  386. {
  387. $num=$this->db->num_rows($resql);
  388. $i=0;
  389. if ($num)
  390. {
  391. $this->lines=array();
  392. while ($i < $num)
  393. {
  394. $line = new Cronjobline();
  395. $obj = $this->db->fetch_object($resql);
  396. $line->id = $obj->rowid;
  397. $line->ref = $obj->rowid;
  398. $line->tms = $this->db->jdate($obj->tms);
  399. $line->datec = $this->db->jdate($obj->datec);
  400. $line->label = $obj->label;
  401. $line->jobtype = $obj->jobtype;
  402. $line->command = $obj->command;
  403. $line->classesname = $obj->classesname;
  404. $line->objectname = $obj->objectname;
  405. $line->methodename = $obj->methodename;
  406. $line->params = $obj->params;
  407. $line->md5params = $obj->md5params;
  408. $line->module_name = $obj->module_name;
  409. $line->priority = $obj->priority;
  410. $line->datelastrun = $this->db->jdate($obj->datelastrun);
  411. $line->datenextrun = $this->db->jdate($obj->datenextrun);
  412. $line->dateend = $this->db->jdate($obj->dateend);
  413. $line->datestart = $this->db->jdate($obj->datestart);
  414. $line->lastresult = $obj->lastresult;
  415. $line->datelastresult = $this->db->jdate($obj->datelastresult);
  416. $line->lastoutput = $obj->lastoutput;
  417. $line->unitfrequency = $obj->unitfrequency;
  418. $line->frequency = $obj->frequency;
  419. $line->status = $obj->status;
  420. $line->fk_user_author = $obj->fk_user_author;
  421. $line->fk_user_mod = $obj->fk_user_mod;
  422. $line->note = $obj->note;
  423. $line->nbrun = $obj->nbrun;
  424. $line->libname = $obj->libname;
  425. $this->lines[]=$line;
  426. $i++;
  427. }
  428. }
  429. $this->db->free($resql);
  430. return 1;
  431. }
  432. else
  433. {
  434. $this->error="Error ".$this->db->lasterror();
  435. return -1;
  436. }
  437. }
  438. /**
  439. * Update object into database
  440. *
  441. * @param User $user User that modifies
  442. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  443. * @return int <0 if KO, >0 if OK
  444. */
  445. function update($user=null, $notrigger=0)
  446. {
  447. global $conf, $langs;
  448. $langs->load('cron');
  449. $error=0;
  450. // Clean parameters
  451. if (isset($this->label)) $this->label=trim($this->label);
  452. if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype);
  453. if (isset($this->command)) $this->command=trim($this->command);
  454. if (isset($this->classesname)) $this->classesname=trim($this->classesname);
  455. if (isset($this->objectname)) $this->objectname=trim($this->objectname);
  456. if (isset($this->methodename)) $this->methodename=trim($this->methodename);
  457. if (isset($this->params)) $this->params=trim($this->params);
  458. if (isset($this->md5params)) $this->md5params=trim($this->md5params);
  459. if (isset($this->module_name)) $this->module_name=trim($this->module_name);
  460. if (isset($this->priority)) $this->priority=trim($this->priority);
  461. if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput);
  462. if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult);
  463. if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency);
  464. if (isset($this->frequency)) $this->frequency=trim($this->frequency);
  465. if (isset($this->status)) $this->status=trim($this->status);
  466. if (isset($this->note)) $this->note=trim($this->note);
  467. if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun);
  468. if (isset($this->libname)) $this->libname = trim($this->libname);
  469. // Check parameters
  470. // Put here code to add a control on parameters values
  471. if (empty($this->status)) {
  472. $this->dateend=dol_now();
  473. }
  474. if (dol_strlen($this->datestart)==0) {
  475. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart'));
  476. $error++;
  477. }
  478. if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) {
  479. $this->errors[]=$langs->trans('CronErrEndDateStartDt');
  480. $error++;
  481. }
  482. if (empty($this->label)) {
  483. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel'));
  484. $error++;
  485. }
  486. if (empty($this->unitfrequency)) {
  487. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency'));
  488. $error++;
  489. }
  490. if (($this->jobtype=='command') && (empty($this->command))) {
  491. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand'));
  492. $error++;
  493. }
  494. if (($this->jobtype=='method') && (empty($this->classesname))) {
  495. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass'));
  496. $error++;
  497. }
  498. if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) {
  499. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod'));
  500. $error++;
  501. }
  502. if (($this->jobtype=='method') && (empty($this->objectname))) {
  503. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject'));
  504. $error++;
  505. }
  506. if (($this->jobtype=='function') && (empty($this->libname))) {
  507. $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib'));
  508. $error++;
  509. }
  510. // Update request
  511. $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
  512. $sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
  513. $sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").",";
  514. $sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").",";
  515. $sql.= " classesname=".(isset($this->classesname)?"'".$this->db->escape($this->classesname)."'":"null").",";
  516. $sql.= " objectname=".(isset($this->objectname)?"'".$this->db->escape($this->objectname)."'":"null").",";
  517. $sql.= " methodename=".(isset($this->methodename)?"'".$this->db->escape($this->methodename)."'":"null").",";
  518. $sql.= " params=".(isset($this->params)?"'".$this->db->escape($this->params)."'":"null").",";
  519. $sql.= " md5params=".(isset($this->md5params)?"'".$this->db->escape($this->md5params)."'":"null").",";
  520. $sql.= " module_name=".(isset($this->module_name)?"'".$this->db->escape($this->module_name)."'":"null").",";
  521. $sql.= " priority=".(isset($this->priority)?$this->priority:"null").",";
  522. $sql.= " datelastrun=".(dol_strlen($this->datelastrun)!=0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').",";
  523. $sql.= " datenextrun=".(dol_strlen($this->datenextrun)!=0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').",";
  524. $sql.= " dateend=".(dol_strlen($this->dateend)!=0 ? "'".$this->db->idate($this->dateend)."'" : 'null').",";
  525. $sql.= " datestart=".(dol_strlen($this->datestart)!=0 ? "'".$this->db->idate($this->datestart)."'" : 'null').",";
  526. $sql.= " datelastresult=".(dol_strlen($this->datelastresult)!=0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').",";
  527. $sql.= " lastresult=".(isset($this->lastresult)?"'".$this->db->escape($this->lastresult)."'":"null").",";
  528. $sql.= " lastoutput=".(isset($this->lastoutput)?"'".$this->db->escape($this->lastoutput)."'":"null").",";
  529. $sql.= " unitfrequency=".(isset($this->unitfrequency)?$this->unitfrequency:"null").",";
  530. $sql.= " frequency=".(isset($this->frequency)?$this->frequency:"null").",";
  531. $sql.= " status=".(isset($this->status)?$this->status:"null").",";
  532. $sql.= " fk_user_mod=".$user->id.",";
  533. $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
  534. $sql.= " nbrun=".(isset($this->nbrun)?$this->nbrun:"null");
  535. $sql.= ", libname=".(isset($this->libname)?"'".$this->db->escape($this->libname)."'":"null");
  536. $sql.= " WHERE rowid=".$this->id;
  537. $this->db->begin();
  538. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  539. $resql = $this->db->query($sql);
  540. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  541. if (! $error)
  542. {
  543. if (! $notrigger)
  544. {
  545. // Uncomment this and change MYOBJECT to your own tag if you
  546. // want this action calls a trigger.
  547. //// Call triggers
  548. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  549. //$interface=new Interfaces($this->db);
  550. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  551. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  552. //// End call triggers
  553. }
  554. }
  555. // Commit or rollback
  556. if ($error)
  557. {
  558. foreach($this->errors as $errmsg)
  559. {
  560. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  561. $this->error.=($this->error?', '.$errmsg:$errmsg);
  562. }
  563. $this->db->rollback();
  564. return -1*$error;
  565. }
  566. else
  567. {
  568. $this->db->commit();
  569. return 1;
  570. }
  571. }
  572. /**
  573. * Delete object in database
  574. *
  575. * @param User $user User that deletes
  576. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  577. * @return int <0 if KO, >0 if OK
  578. */
  579. function delete($user, $notrigger=0)
  580. {
  581. $error=0;
  582. $this->db->begin();
  583. // if (! $error)
  584. // {
  585. // if (! $notrigger)
  586. // {
  587. // Uncomment this and change MYOBJECT to your own tag if you
  588. // want this action calls a trigger.
  589. //// Call triggers
  590. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  591. //$interface=new Interfaces($this->db);
  592. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  593. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  594. //// End call triggers
  595. // }
  596. // }
  597. // if (! $error)
  598. // {
  599. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  600. $sql.= " WHERE rowid=".$this->id;
  601. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  602. $resql = $this->db->query($sql);
  603. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  604. // }
  605. // Commit or rollback
  606. if ($error)
  607. {
  608. foreach($this->errors as $errmsg)
  609. {
  610. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  611. $this->error.=($this->error?', '.$errmsg:$errmsg);
  612. }
  613. $this->db->rollback();
  614. return -1*$error;
  615. }
  616. else
  617. {
  618. $this->db->commit();
  619. return 1;
  620. }
  621. }
  622. /**
  623. * Load an object from its id and create a new one in database
  624. *
  625. * @param int $fromid Id of object to clone
  626. * @return int New id of clone
  627. */
  628. function createFromClone($fromid)
  629. {
  630. global $user,$langs;
  631. $error=0;
  632. $object=new Cronjob($this->db);
  633. $object->context['createfromclone'] = 'createfromclone';
  634. $this->db->begin();
  635. // Load source object
  636. $object->fetch($fromid);
  637. $object->id=0;
  638. $object->statut=0;
  639. // Clear fields
  640. // ...
  641. // Create clone
  642. $result=$object->create($user);
  643. // Other options
  644. if ($result < 0)
  645. {
  646. $this->error=$object->error;
  647. $error++;
  648. }
  649. if (! $error)
  650. {
  651. }
  652. unset($this->context['createfromclone']);
  653. // End
  654. if (! $error)
  655. {
  656. $this->db->commit();
  657. return $object->id;
  658. }
  659. else
  660. {
  661. $this->db->rollback();
  662. return -1;
  663. }
  664. }
  665. /**
  666. * Initialise object with example values
  667. * Id must be 0 if object instance is a specimen
  668. *
  669. * @return void
  670. */
  671. function initAsSpecimen()
  672. {
  673. $this->id=0;
  674. $this->ref=0;
  675. $this->tms='';
  676. $this->datec='';
  677. $this->label='';
  678. $this->jobtype='';
  679. $this->command='';
  680. $this->classesname='';
  681. $this->objectname='';
  682. $this->methodename='';
  683. $this->params='';
  684. $this->md5params='';
  685. $this->module_name='';
  686. $this->priority='';
  687. $this->datelastrun='';
  688. $this->datenextrun='';
  689. $this->dateend='';
  690. $this->datestart='';
  691. $this->datelastresult='';
  692. $this->lastoutput='';
  693. $this->lastresult='';
  694. $this->unitfrequency='';
  695. $this->frequency='';
  696. $this->status='';
  697. $this->fk_user_author='';
  698. $this->fk_user_mod='';
  699. $this->note='';
  700. $this->nbrun='';
  701. $this->libname = '';
  702. }
  703. /**
  704. * Load object information
  705. *
  706. * @return int
  707. */
  708. function info()
  709. {
  710. $sql = "SELECT";
  711. $sql.= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author";
  712. $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as f";
  713. $sql.= " WHERE f.rowid = ".$this->id;
  714. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  715. $resql=$this->db->query($sql);
  716. if ($resql)
  717. {
  718. if ($this->db->num_rows($resql))
  719. {
  720. $obj = $this->db->fetch_object($resql);
  721. $this->id = $obj->rowid;
  722. $this->date_creation = $this->db->jdate($obj->datec);
  723. $this->date_modification = $this->db->jdate($obj->tms);
  724. $this->user_modification = $obj->fk_user_mod;
  725. $this->user_creation = $obj->fk_user_author;
  726. }
  727. $this->db->free($resql);
  728. return 1;
  729. }
  730. else
  731. {
  732. $this->error="Error ".$this->db->lasterror();
  733. return -1;
  734. }
  735. }
  736. /**
  737. * Run a job
  738. *
  739. * @param string $userlogin User login
  740. * @return int <0 if KO, >0 if OK
  741. */
  742. function run_jobs($userlogin)
  743. {
  744. global $langs, $conf;
  745. $now=dol_now();
  746. $langs->load('cron');
  747. if (empty($userlogin)) {
  748. $this->error="User login is mandatory";
  749. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  750. return -1;
  751. }
  752. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  753. $user=new User($this->db);
  754. $result=$user->fetch('',$userlogin);
  755. if ($result<0)
  756. {
  757. $this->error="User Error:".$user->error;
  758. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  759. return -1;
  760. }
  761. else
  762. {
  763. if (empty($user->id))
  764. {
  765. $this->error=" User user login:".$userlogin." do not exists";
  766. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  767. return -1;
  768. }
  769. }
  770. dol_syslog(get_class($this)."::run_jobs jobtype=".$this->jobtype." userlogin=".$userlogin, LOG_DEBUG);
  771. // Increase limit of time. Works only if we are not in safe mode
  772. $ExecTimeLimit=600;
  773. if (!empty($ExecTimeLimit))
  774. {
  775. $err=error_reporting();
  776. error_reporting(0); // Disable all errors
  777. //error_reporting(E_ALL);
  778. @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
  779. error_reporting($err);
  780. }
  781. if (!empty($MemoryLimit))
  782. {
  783. @ini_set('memory_limit', $MemoryLimit);
  784. }
  785. // Update last run date (to track launch)
  786. $this->datelastrun=$now;
  787. $this->lastoutput='';
  788. $this->lastresult='';
  789. $this->nbrun=$this->nbrun+1;
  790. $result = $this->update($user);
  791. if ($result<0) {
  792. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  793. return -1;
  794. }
  795. // Run a method
  796. if ($this->jobtype=='method')
  797. {
  798. // load classes
  799. $file = "/".$this->module_name."/class/".$this->classesname;
  800. $ret=dol_include_once($file,$this->objectname);
  801. if ($ret===false)
  802. {
  803. $this->error=$langs->trans('CronCannotLoadClass',$file,$this->objectname);
  804. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  805. return -1;
  806. }
  807. // Load langs
  808. $result=$langs->load($this->module_name.'@'.$this->module_name);
  809. if ($result<0)
  810. {
  811. dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR);
  812. return -1;
  813. }
  814. dol_syslog(get_class($this)."::run_jobs ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG);
  815. // Create Object for the call module
  816. $object = new $this->objectname($this->db);
  817. $params_arr = explode(", ",$this->params);
  818. if (!is_array($params_arr))
  819. {
  820. $result = call_user_func(array($object, $this->methodename), $this->params);
  821. }
  822. else
  823. {
  824. $result = call_user_func_array(array($object, $this->methodename), $params_arr);
  825. }
  826. if ($result===false)
  827. {
  828. dol_syslog(get_class($this)."::run_jobs ".$object->error, LOG_ERR);
  829. return -1;
  830. }
  831. else
  832. {
  833. $this->lastoutput=var_export($result,true);
  834. $this->lastresult=var_export($result,true);
  835. }
  836. }
  837. if($this->jobtype == 'function')
  838. {
  839. //load lib
  840. $libpath = '/' . strtolower($this->module_name) . '/lib/' . $this->libname;
  841. $ret = dol_include_once($libpath);
  842. if ($ret === false)
  843. {
  844. $this->error = $langs->trans('CronCannotLoadLib') . ': ' . $libpath;
  845. dol_syslog(get_class($this) . "::run_jobs " . $this->error, LOG_ERR);
  846. return -1;
  847. }
  848. // Load langs
  849. $result=$langs->load($this->module_name . '@' . $this->module_name);
  850. if ($result<0)
  851. {
  852. dol_syslog(get_class($this) . "::run_jobs Cannot load module langs" . $langs->error, LOG_ERR);
  853. return -1;
  854. }
  855. dol_syslog(get_class($this) . "::run_jobs " . $this->libname . "::" . $this->methodename."(" . $this->params . ");", LOG_DEBUG);
  856. $params_arr = explode(", ", $this->params);
  857. if (!is_array($params_arr))
  858. {
  859. $result = call_user_func($this->methodename, $this->params);
  860. }
  861. else
  862. {
  863. $result = call_user_func_array($this->methodename, $params_arr);
  864. }
  865. if ($result === false)
  866. {
  867. dol_syslog(get_class($this) . "::run_jobs " . $object->error, LOG_ERR);
  868. return -1;
  869. }
  870. else
  871. {
  872. $this->lastoutput=var_export($result,true);
  873. $this->lastresult=var_export($result,true);
  874. }
  875. }
  876. // Run a command line
  877. if ($this->jobtype=='command')
  878. {
  879. $command=escapeshellcmd($this->command);
  880. $command.=" 2>&1";
  881. dol_mkdir($conf->cronjob->dir_temp);
  882. $outputfile=$conf->cronjob->dir_temp.'/cronjob.'.$userlogin.'.out';
  883. dol_syslog(get_class($this)."::run_jobs system:".$command, LOG_DEBUG);
  884. $output_arr=array();
  885. $execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2
  886. if ($execmethod == 1)
  887. {
  888. exec($command, $output_arr, $retval);
  889. }
  890. if ($execmethod == 2)
  891. {
  892. $ok=0;
  893. $handle = fopen($outputfile, 'w');
  894. if ($handle)
  895. {
  896. dol_syslog("Run command ".$command);
  897. $handlein = popen($command, 'r');
  898. while (!feof($handlein))
  899. {
  900. $read = fgets($handlein);
  901. fwrite($handle,$read);
  902. $output_arr[]=$read;
  903. }
  904. pclose($handlein);
  905. fclose($handle);
  906. }
  907. if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  908. }
  909. }
  910. dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true), LOG_DEBUG);
  911. // Update with result
  912. $this->lastoutput='';
  913. if (is_array($output_arr) && count($output_arr)>0)
  914. {
  915. foreach($output_arr as $val)
  916. {
  917. $this->lastoutput.=$val."\n";
  918. }
  919. }
  920. $this->lastresult=$retval;
  921. $this->datelastresult=dol_now();
  922. $result = $this->update($user);
  923. if ($result < 0)
  924. {
  925. dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
  926. return -1;
  927. }
  928. else
  929. {
  930. return 1;
  931. }
  932. }
  933. /**
  934. * Reprogram a job
  935. *
  936. * @param string $userlogin User login
  937. * @return int <0 if KO, >0 if OK
  938. *
  939. */
  940. function reprogram_jobs($userlogin)
  941. {
  942. dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
  943. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  944. $user=new User($this->db);
  945. $result=$user->fetch('',$userlogin);
  946. if ($result<0) {
  947. $this->error="User Error:".$user->error;
  948. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  949. return -1;
  950. }else {
  951. if (empty($user->id)) {
  952. $this->error=" User user login:".$userlogin." do not exists";
  953. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  954. return -1;
  955. }
  956. }
  957. dol_syslog(get_class($this)."::reprogram_jobs ", LOG_DEBUG);
  958. if (empty($this->datenextrun)) {
  959. $this->datenextrun=dol_now()+$this->frequency;
  960. } else {
  961. if ($this->datenextrun<dol_now()) {
  962. $this->datenextrun=dol_now()+$this->frequency;
  963. } else {
  964. $this->datenextrun=$this->datenextrun+$this->frequency;
  965. }
  966. }
  967. $result = $this->update($user);
  968. if ($result<0) {
  969. dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
  970. return -1;
  971. }
  972. return 1;
  973. }
  974. }
  975. /**
  976. * Crob Job line class
  977. */
  978. class Cronjobline
  979. {
  980. var $id;
  981. var $ref;
  982. var $tms='';
  983. var $datec='';
  984. var $label;
  985. var $jobtype;
  986. var $command;
  987. var $classesname;
  988. var $objectname;
  989. var $methodename;
  990. var $params;
  991. var $md5params;
  992. var $module_name;
  993. var $priority;
  994. var $datelastrun='';
  995. var $datenextrun='';
  996. var $dateend='';
  997. var $datestart='';
  998. var $lastresult='';
  999. var $lastoutput;
  1000. var $unitfrequency;
  1001. var $frequency;
  1002. var $status;
  1003. var $fk_user_author;
  1004. var $fk_user_mod;
  1005. var $note;
  1006. var $nbrun;
  1007. var $libname;
  1008. /**
  1009. * Constructor
  1010. *
  1011. */
  1012. function __construct()
  1013. {
  1014. return 1;
  1015. }
  1016. }