cronjob.class.php 45 KB

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