cronjob.class.php 44 KB

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