price_global_variable.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/product/dynamic_price/class/price_global_variable.class.php
  21. * \ingroup product
  22. * \brief Class for accessing price global variables table
  23. */
  24. /**
  25. * Class for accesing price global variables table
  26. */
  27. class PriceGlobalVariable
  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. /**
  42. * @var int ID
  43. */
  44. public $id;
  45. public $code;
  46. /**
  47. * @var string description
  48. */
  49. public $description;
  50. public $value;
  51. /**
  52. * @var string Name of table without prefix where object is stored
  53. */
  54. public $table_element = "c_price_global_variable";
  55. /**
  56. * Constructor
  57. *
  58. * @param DoliDb $db Database handler
  59. */
  60. function __construct($db)
  61. {
  62. $this->db = $db;
  63. }
  64. /**
  65. * Create object into database
  66. *
  67. * @param User $user User that creates
  68. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  69. * @return int <0 if KO, Id of created object if OK
  70. */
  71. function create($user, $notrigger=0)
  72. {
  73. $error=0;
  74. $this->checkParameters();
  75. // Insert request
  76. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
  77. $sql.= "code, description, value";
  78. $sql.= ") VALUES (";
  79. $sql.= " ".(isset($this->code)?"'".$this->db->escape($this->code)."'":"''").",";
  80. $sql.= " ".(isset($this->description)?"'".$this->db->escape($this->description)."'":"''").",";
  81. $sql.= " ".$this->value;
  82. $sql.= ")";
  83. $this->db->begin();
  84. dol_syslog(__METHOD__);
  85. $resql=$this->db->query($sql);
  86. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  87. if (! $error)
  88. {
  89. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  90. if (! $notrigger)
  91. {
  92. // Uncomment this and change MYOBJECT to your own tag if you
  93. // want this action calls a trigger.
  94. //// Call triggers
  95. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  96. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  97. //// End call triggers
  98. }
  99. }
  100. // Commit or rollback
  101. if ($error)
  102. {
  103. foreach($this->errors as $errmsg)
  104. {
  105. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  106. $this->error.=($this->error?', '.$errmsg:$errmsg);
  107. }
  108. $this->db->rollback();
  109. return -1*$error;
  110. }
  111. else
  112. {
  113. $this->db->commit();
  114. return $this->id;
  115. }
  116. }
  117. /**
  118. * Load object in memory from the database
  119. *
  120. * @param int $id Id object
  121. * @return int < 0 if KO, 0 if OK but not found, > 0 if OK
  122. */
  123. function fetch($id)
  124. {
  125. $sql = "SELECT code, description, value";
  126. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  127. $sql.= " WHERE rowid = ".$id;
  128. dol_syslog(__METHOD__);
  129. $resql=$this->db->query($sql);
  130. if ($resql)
  131. {
  132. $obj = $this->db->fetch_object($resql);
  133. if ($obj)
  134. {
  135. $this->id = $id;
  136. $this->code = $obj->code;
  137. $this->description = $obj->description;
  138. $this->value = $obj->value;
  139. $this->checkParameters();
  140. return 1;
  141. }
  142. else
  143. {
  144. return 0;
  145. }
  146. }
  147. else
  148. {
  149. $this->error="Error ".$this->db->lasterror();
  150. return -1;
  151. }
  152. }
  153. /**
  154. * Update object into database
  155. *
  156. * @param User $user User that modifies
  157. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  158. * @return int <0 if KO, >0 if OK
  159. */
  160. function update($user=0, $notrigger=0)
  161. {
  162. $error=0;
  163. $this->checkParameters();
  164. // Update request
  165. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  166. $sql.= " code = ".(isset($this->code)?"'".$this->db->escape($this->code)."'":"''").",";
  167. $sql.= " description = ".(isset($this->description)?"'".$this->db->escape($this->description)."'":"''").",";
  168. $sql.= " value = ".$this->value;
  169. $sql.= " WHERE rowid = ".$this->id;
  170. $this->db->begin();
  171. dol_syslog(__METHOD__);
  172. $resql = $this->db->query($sql);
  173. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  174. if (! $error)
  175. {
  176. if (! $notrigger)
  177. {
  178. // Uncomment this and change MYOBJECT to your own tag if you
  179. // want this action calls a trigger.
  180. //// Call triggers
  181. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  182. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  183. //// End call triggers
  184. }
  185. }
  186. // Commit or rollback
  187. if ($error)
  188. {
  189. foreach($this->errors as $errmsg)
  190. {
  191. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  192. $this->error.=($this->error?', '.$errmsg:$errmsg);
  193. }
  194. $this->db->rollback();
  195. return -1*$error;
  196. }
  197. else
  198. {
  199. $this->db->commit();
  200. return 1;
  201. }
  202. }
  203. /**
  204. * Delete object in database
  205. *
  206. * @param int $rowid Row id of global variable
  207. * @param User $user User that deletes
  208. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  209. * @return int <0 if KO, >0 if OK
  210. */
  211. function delete($rowid, $user, $notrigger=0)
  212. {
  213. $error=0;
  214. $this->db->begin();
  215. if (! $error)
  216. {
  217. if (! $notrigger)
  218. {
  219. // Uncomment this and change MYOBJECT to your own tag if you
  220. // want this action calls a trigger.
  221. //// Call triggers
  222. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  223. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  224. //// End call triggers
  225. }
  226. }
  227. if (! $error)
  228. {
  229. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  230. $sql.= " WHERE rowid = ".$rowid;
  231. dol_syslog(__METHOD__);
  232. $resql = $this->db->query($sql);
  233. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  234. }
  235. // Commit or rollback
  236. if ($error)
  237. {
  238. foreach($this->errors as $errmsg)
  239. {
  240. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  241. $this->error.=($this->error?', '.$errmsg:$errmsg);
  242. }
  243. $this->db->rollback();
  244. return -1*$error;
  245. }
  246. else
  247. {
  248. $this->db->commit();
  249. return 1;
  250. }
  251. }
  252. /**
  253. * Initialise object with example values
  254. * Id must be 0 if object instance is a specimen
  255. *
  256. * @return void
  257. */
  258. function initAsSpecimen()
  259. {
  260. $this->id=0;
  261. $this->code='';
  262. $this->description='';
  263. $this->value='';
  264. }
  265. /**
  266. * Checks if all parameters are in order
  267. *
  268. * @return void
  269. */
  270. function checkParameters()
  271. {
  272. // Clean parameters
  273. if (isset($this->code)) $this->code=trim($this->code);
  274. if (isset($this->description)) $this->description=trim($this->description);
  275. // Check parameters
  276. if (empty($this->value) || !is_numeric($this->value)) $this->value=0;
  277. }
  278. /**
  279. * List all price global variables
  280. *
  281. * @return array Array of price global variables
  282. */
  283. function listGlobalVariables()
  284. {
  285. $sql = "SELECT rowid, code, description, value";
  286. $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
  287. $sql.= " ORDER BY code";
  288. dol_syslog(__METHOD__, LOG_DEBUG);
  289. $resql=$this->db->query($sql);
  290. if ($resql)
  291. {
  292. $retarray = array();
  293. while ($record = $this->db->fetch_array($resql))
  294. {
  295. $variable_obj = new PriceGlobalVariable($this->db);
  296. $variable_obj->id = $record["rowid"];
  297. $variable_obj->code = $record["code"];
  298. $variable_obj->description = $record["description"];
  299. $variable_obj->value = $record["value"];
  300. $variable_obj->checkParameters();
  301. $retarray[]=$variable_obj;
  302. }
  303. $this->db->free($resql);
  304. return $retarray;
  305. }
  306. else
  307. {
  308. $this->error=$this->db->error();
  309. return -1;
  310. }
  311. }
  312. }