cronjob.class.php 44 KB

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