price_global_variable_updater.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Ion Agorria <ion@agorria.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/product/dynamic_price/class/price_global_variable_updater.class.php
  21. * \ingroup product
  22. * \brief Class for price global variable updaters table
  23. */
  24. /**
  25. * Class for price global variable updaters table
  26. */
  27. class PriceGlobalVariableUpdater
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error = '';
  37. /**
  38. * @var string[] Error codes (or messages)
  39. */
  40. public $errors = array();
  41. public $types = array(0, 1); //!< Updater types
  42. public $update_min = 5; //!< Minimal update rate
  43. /**
  44. * @var int ID
  45. */
  46. public $id;
  47. public $type;
  48. /**
  49. * @var string description
  50. */
  51. public $description;
  52. public $parameters;
  53. /**
  54. * @var int ID
  55. */
  56. public $fk_variable;
  57. public $update_interval; //!< Interval in mins
  58. public $next_update; //!< Next update timestamp
  59. public $last_status;
  60. /**
  61. * @var string Name of table without prefix where object is stored
  62. */
  63. public $table_element = "c_price_global_variable_updater";
  64. /**
  65. * Constructor
  66. *
  67. * @param DoliDb $db Database handler
  68. */
  69. public function __construct($db)
  70. {
  71. $this->db = $db;
  72. }
  73. /**
  74. * Create object into database
  75. *
  76. * @param User $user User that creates
  77. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  78. * @return int Return integer <0 if KO, Id of created object if OK
  79. */
  80. public function create($user, $notrigger = 0)
  81. {
  82. $error = 0;
  83. $this->checkParameters();
  84. // Insert request
  85. $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
  86. $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status";
  87. $sql .= ") VALUES (";
  88. $sql .= " ".((int) $this->type).",";
  89. $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
  90. $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
  91. $sql .= " ".((int) $this->fk_variable).",";
  92. $sql .= " ".((int) $this->update_interval).",";
  93. $sql .= " ".((int) $this->next_update).",";
  94. $sql .= " ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
  95. $sql .= ")";
  96. $this->db->begin();
  97. dol_syslog(__METHOD__, LOG_DEBUG);
  98. $resql = $this->db->query($sql);
  99. if (!$resql) {
  100. $error++;
  101. $this->errors[] = "Error ".$this->db->lasterror();
  102. }
  103. if (!$error) {
  104. $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
  105. if (!$notrigger) {
  106. // Uncomment this and change MYOBJECT to your own tag if you
  107. // want this action calls a trigger.
  108. //// Call triggers
  109. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  110. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  111. //// End call triggers
  112. }
  113. }
  114. // Commit or rollback
  115. if ($error) {
  116. foreach ($this->errors as $errmsg) {
  117. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  118. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  119. }
  120. $this->db->rollback();
  121. return -1 * $error;
  122. } else {
  123. $this->db->commit();
  124. return $this->id;
  125. }
  126. }
  127. /**
  128. * Load object in memory from the database
  129. *
  130. * @param int $id Id object
  131. * @return int Return integer < 0 if KO, 0 if OK but not found, > 0 if OK
  132. */
  133. public function fetch($id)
  134. {
  135. $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status";
  136. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  137. $sql .= " WHERE rowid = ".((int) $id);
  138. dol_syslog(__METHOD__);
  139. $resql = $this->db->query($sql);
  140. if ($resql) {
  141. $obj = $this->db->fetch_object($resql);
  142. if ($obj) {
  143. $this->id = $id;
  144. $this->type = $obj->type;
  145. $this->description = $obj->description;
  146. $this->parameters = $obj->parameters;
  147. $this->fk_variable = $obj->fk_variable;
  148. $this->update_interval = $obj->update_interval;
  149. $this->next_update = $obj->next_update;
  150. $this->last_status = $obj->last_status;
  151. $this->checkParameters();
  152. return 1;
  153. } else {
  154. return 0;
  155. }
  156. } else {
  157. $this->error = "Error ".$this->db->lasterror();
  158. return -1;
  159. }
  160. }
  161. /**
  162. * Update object into database
  163. *
  164. * @param User $user User that modifies
  165. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  166. * @return int Return integer <0 if KO, >0 if OK
  167. */
  168. public function update($user = 0, $notrigger = 0)
  169. {
  170. $error = 0;
  171. $this->checkParameters();
  172. // Update request
  173. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  174. $sql .= " type = ".((int) $this->type).",";
  175. $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
  176. $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
  177. $sql .= " fk_variable = ".((int) $this->fk_variable).",";
  178. $sql .= " update_interval = ".((int) $this->update_interval).",";
  179. $sql .= " next_update = ".((int) $this->next_update).",";
  180. $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
  181. $sql .= " WHERE rowid = ".((int) $this->id);
  182. $this->db->begin();
  183. dol_syslog(__METHOD__);
  184. $resql = $this->db->query($sql);
  185. if (!$resql) {
  186. $error++;
  187. $this->errors[] = "Error ".$this->db->lasterror();
  188. }
  189. // if (! $error)
  190. // {
  191. // if (! $notrigger)
  192. // {
  193. // // Uncomment this and change MYOBJECT to your own tag if you
  194. // // want this action calls a trigger.
  195. // //// Call triggers
  196. // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  197. // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  198. // //// End call triggers
  199. // }
  200. // }
  201. // Commit or rollback
  202. if ($error) {
  203. foreach ($this->errors as $errmsg) {
  204. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  205. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  206. }
  207. $this->db->rollback();
  208. return -1 * $error;
  209. } else {
  210. $this->db->commit();
  211. return 1;
  212. }
  213. }
  214. /**
  215. * Delete object in database
  216. *
  217. * @param int $rowid Row id of global variable
  218. * @param User $user User that deletes
  219. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  220. * @return int Return integer <0 if KO, >0 if OK
  221. */
  222. public function delete($rowid, $user, $notrigger = 0)
  223. {
  224. $error = 0;
  225. $this->db->begin();
  226. //if (! $error)
  227. //{
  228. // if (! $notrigger)
  229. // {
  230. // Uncomment this and change MYOBJECT to your own tag if you
  231. // want this action calls a trigger.
  232. //// Call triggers
  233. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  234. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  235. //// End call triggers
  236. // }
  237. //}
  238. if (!$error) {
  239. $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
  240. $sql .= " WHERE rowid = ".((int) $rowid);
  241. dol_syslog(__METHOD__);
  242. $resql = $this->db->query($sql);
  243. if (!$resql) {
  244. $error++;
  245. $this->errors[] = "Error ".$this->db->lasterror();
  246. }
  247. }
  248. // Commit or rollback
  249. if ($error) {
  250. foreach ($this->errors as $errmsg) {
  251. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  252. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  253. }
  254. $this->db->rollback();
  255. return -1 * $error;
  256. } else {
  257. $this->db->commit();
  258. return 1;
  259. }
  260. }
  261. /**
  262. * Initialise object with example values
  263. * Id must be 0 if object instance is a specimen
  264. *
  265. * @return void
  266. */
  267. public function initAsSpecimen()
  268. {
  269. $this->id = 0;
  270. $this->type = 0;
  271. $this->description = '';
  272. $this->parameters = '';
  273. $this->fk_variable = 0;
  274. $this->update_interval = 0;
  275. $this->next_update = 0;
  276. $this->last_status = '';
  277. }
  278. /**
  279. * Returns the last updated time in string html format, returns "never" if its less than 1
  280. *
  281. * @return string
  282. */
  283. public function getLastUpdated()
  284. {
  285. global $langs;
  286. $last = $this->next_update - ($this->update_interval * 60);
  287. if ($last < 1) {
  288. return $langs->trans("Never");
  289. }
  290. $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status;
  291. return $status.'<br>'.dol_print_date($last, '%d/%m/%Y %H:%M:%S');
  292. }
  293. /**
  294. * Checks if all parameters are in order
  295. *
  296. * @return void
  297. */
  298. public function checkParameters()
  299. {
  300. // Clean parameters
  301. if (isset($this->description)) {
  302. $this->description = trim($this->description);
  303. }
  304. if (isset($this->parameters)) {
  305. $this->parameters = trim($this->parameters);
  306. } else {
  307. $this->parameters = "";
  308. }
  309. if (isset($this->last_status)) {
  310. $this->last_status = trim($this->last_status);
  311. }
  312. // Check parameters
  313. if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) {
  314. $this->type = 0;
  315. }
  316. if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
  317. $this->update_interval = $this->update_min;
  318. }
  319. if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
  320. $this->next_update = 0;
  321. }
  322. }
  323. /**
  324. * List all price global variables
  325. *
  326. * @return array|int Array of price global variable updaters
  327. */
  328. public function listUpdaters()
  329. {
  330. $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
  331. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  332. dol_syslog(__METHOD__, LOG_DEBUG);
  333. $resql = $this->db->query($sql);
  334. if ($resql) {
  335. $retarray = array();
  336. while ($record = $this->db->fetch_array($resql)) {
  337. $updater_obj = new PriceGlobalVariableUpdater($this->db);
  338. $updater_obj->id = $record["rowid"];
  339. $updater_obj->type = $record["type"];
  340. $updater_obj->description = $record["description"];
  341. $updater_obj->parameters = $record["parameters"];
  342. $updater_obj->fk_variable = $record["fk_variable"];
  343. $updater_obj->update_interval = $record["update_interval"];
  344. $updater_obj->next_update = $record["next_update"];
  345. $updater_obj->last_status = $record["last_status"];
  346. $updater_obj->checkParameters();
  347. $retarray[] = $updater_obj;
  348. }
  349. $this->db->free($resql);
  350. return $retarray;
  351. } else {
  352. $this->error = $this->db->error();
  353. return -1;
  354. }
  355. }
  356. /**
  357. * List all updaters which need to be processed
  358. *
  359. * @return array|int Array of price global variable updaters
  360. */
  361. public function listPendingUpdaters()
  362. {
  363. $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
  364. $sql .= " FROM ".$this->db->prefix().$this->table_element;
  365. $sql .= " WHERE next_update < ".dol_now();
  366. dol_syslog(__METHOD__, LOG_DEBUG);
  367. $resql = $this->db->query($sql);
  368. if ($resql) {
  369. $retarray = array();
  370. while ($record = $this->db->fetch_array($resql)) {
  371. $updater_obj = new PriceGlobalVariableUpdater($this->db);
  372. $updater_obj->id = $record["rowid"];
  373. $updater_obj->type = $record["type"];
  374. $updater_obj->description = $record["description"];
  375. $updater_obj->parameters = $record["parameters"];
  376. $updater_obj->fk_variable = $record["fk_variable"];
  377. $updater_obj->update_interval = $record["update_interval"];
  378. $updater_obj->next_update = $record["next_update"];
  379. $updater_obj->last_status = $record["last_status"];
  380. $updater_obj->checkParameters();
  381. $retarray[] = $updater_obj;
  382. }
  383. $this->db->free($resql);
  384. return $retarray;
  385. } else {
  386. $this->error = $this->db->error();
  387. return -1;
  388. }
  389. }
  390. /**
  391. * Handles the processing of this updater
  392. *
  393. * @return int Return integer <0 if KO, 0 if OK but no global variable found, >0 if OK
  394. */
  395. public function process()
  396. {
  397. global $langs, $user;
  398. $langs->load("errors");
  399. dol_syslog(__METHOD__, LOG_DEBUG);
  400. $this->error = null;
  401. $this->checkParameters();
  402. //Try to load the target global variable and abort if fails
  403. if ($this->fk_variable < 1) {
  404. $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
  405. return 0;
  406. }
  407. $price_globals = new PriceGlobalVariable($this->db);
  408. $res = $price_globals->fetch($this->fk_variable);
  409. if ($res < 1) {
  410. $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
  411. return 0;
  412. }
  413. //Process depending of type
  414. if ($this->type == 0 || $this->type == 1) {
  415. //Get and check if required parameters are present
  416. $parameters = json_decode($this->parameters, true);
  417. if (!isset($parameters)) {
  418. $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters);
  419. return -1;
  420. }
  421. $url = $parameters['URL'];
  422. if (!isset($url)) {
  423. $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL');
  424. return -1;
  425. }
  426. $value = $parameters['VALUE'];
  427. if (!isset($value)) {
  428. $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE');
  429. return -1;
  430. }
  431. $result = "";
  432. if ($this->type == 0) {
  433. // Call JSON request
  434. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  435. $tmpresult = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 0);
  436. $code = $tmpresult['http_code'];
  437. $result = $tmpresult['content'];
  438. if (!isset($result)) {
  439. $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response");
  440. return -1;
  441. }
  442. if ($code !== 200) {
  443. $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']);
  444. return -1;
  445. }
  446. //Decode returned response
  447. $result = json_decode($result, true);
  448. } elseif ($this->type == 1) {
  449. $ns = $parameters['NS'];
  450. if (!isset($ns)) {
  451. $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS');
  452. return -1;
  453. }
  454. $method = $parameters['METHOD'];
  455. if (!isset($method)) {
  456. $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD');
  457. return -1;
  458. }
  459. $data = $parameters['DATA'];
  460. if (!isset($data)) {
  461. $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA');
  462. return -1;
  463. }
  464. //SOAP client
  465. require_once NUSOAP_PATH.'/nusoap.php';
  466. $soap_client = new nusoap_client($url);
  467. $soap_client->soap_defencoding = 'UTF-8';
  468. $soap_client->decodeUTF8(false);
  469. $result = $soap_client->call($method, $data, $ns, '');
  470. //Check if result is a error
  471. if ($result === false) {
  472. $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str);
  473. return -1;
  474. }
  475. }
  476. //Explode value and walk for each key in value array to get the relevant key
  477. $value = explode(',', $value);
  478. foreach ($value as $key) {
  479. $result = $result[$key];
  480. }
  481. if (!isset($result)) {
  482. $this->error = $langs->trans("ErrorGlobalVariableUpdater3");
  483. return -1;
  484. }
  485. //Save data to global and update it
  486. $price_globals->value = $result;
  487. $price_globals->update($user);
  488. }
  489. return 1;
  490. }
  491. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  492. /**
  493. * Update next_update into database
  494. *
  495. * @param string $next_update Next update to write
  496. * @param User $user User that modifies
  497. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  498. * @return int Return integer <0 if KO, >0 if OK
  499. */
  500. public function update_next_update($next_update, $user = 0, $notrigger = 0)
  501. {
  502. // phpcs:enable
  503. $error = 0;
  504. $this->next_update = $next_update;
  505. $this->checkParameters();
  506. // Update request
  507. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  508. $sql .= " next_update = ".$this->next_update;
  509. $sql .= " WHERE rowid = ".((int) $this->id);
  510. $this->db->begin();
  511. dol_syslog(__METHOD__);
  512. $resql = $this->db->query($sql);
  513. if (!$resql) {
  514. $error++;
  515. $this->errors[] = "Error ".$this->db->lasterror();
  516. }
  517. // Commit or rollback
  518. if ($error) {
  519. foreach ($this->errors as $errmsg) {
  520. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  521. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  522. }
  523. $this->db->rollback();
  524. return -1 * $error;
  525. } else {
  526. $this->db->commit();
  527. return 1;
  528. }
  529. }
  530. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  531. /**
  532. * Update last_status into database
  533. *
  534. * @param string $last_status Status to write
  535. * @param User $user User that modifies
  536. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  537. * @return int Return integer <0 if KO, >0 if OK
  538. */
  539. public function update_status($last_status, $user = 0, $notrigger = 0)
  540. {
  541. // phpcs:enable
  542. $error = 0;
  543. $this->last_status = $last_status;
  544. $this->checkParameters();
  545. // Update request
  546. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  547. $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
  548. $sql .= " WHERE rowid = ".((int) $this->id);
  549. $this->db->begin();
  550. dol_syslog(__METHOD__);
  551. $resql = $this->db->query($sql);
  552. if (!$resql) {
  553. $error++;
  554. $this->errors[] = "Error ".$this->db->lasterror();
  555. }
  556. // Commit or rollback
  557. if ($error) {
  558. foreach ($this->errors as $errmsg) {
  559. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  560. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  561. }
  562. $this->db->rollback();
  563. return -1 * $error;
  564. } else {
  565. $this->db->commit();
  566. return 1;
  567. }
  568. }
  569. }