cstate.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/cstate.class.php
  19. * \ingroup core
  20. * \brief This file is a CRUD class file (Create/Read/Update/Delete) for c_departements dictionary
  21. */
  22. // Put here all includes required by your class file
  23. //require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  24. //require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  25. //require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  26. /**
  27. * Class to manage dictionary States (used by imports)
  28. */
  29. class Cstate // extends CommonObject
  30. {
  31. var $db; //!< To store db handler
  32. var $error; //!< To return error code (or message)
  33. var $errors=array(); //!< To return several error codes (or messages)
  34. //var $element='cstate'; //!< Id that identify managed objects
  35. //var $table_element='cstate'; //!< Name of table without prefix where object is stored
  36. var $id;
  37. var $code_departement;
  38. var $nom;
  39. var $active;
  40. /**
  41. * Constructor
  42. *
  43. * @param DoliDb $db Database handler
  44. */
  45. function __construct($db)
  46. {
  47. $this->db = $db;
  48. return 1;
  49. }
  50. /**
  51. * Create object into database
  52. *
  53. * @param User $user User that create
  54. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  55. * @return int <0 if KO, Id of created object if OK
  56. */
  57. function create($user, $notrigger=0)
  58. {
  59. global $conf, $langs;
  60. $error=0;
  61. // Clean parameters
  62. if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
  63. if (isset($this->nom)) $this->nom=trim($this->nom);
  64. if (isset($this->active)) $this->active=trim($this->active);
  65. // Check parameters
  66. // Put here code to add control on parameters values
  67. // Insert request
  68. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_departements(";
  69. $sql.= "rowid,";
  70. $sql.= "code_departement,";
  71. $sql.= "nom,";
  72. $sql.= "active";
  73. $sql.= ") VALUES (";
  74. $sql.= " ".(! isset($this->rowid)?'NULL':"'".$this->rowid."'").",";
  75. $sql.= " ".(! isset($this->code_departement)?'NULL':"'".$this->db->escape($this->code_departement)."'").",";
  76. $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
  77. $sql.= " ".(! isset($this->active)?'NULL':"'".$this->active."'")."";
  78. $sql.= ")";
  79. $this->db->begin();
  80. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  81. $resql=$this->db->query($sql);
  82. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  83. if (! $error)
  84. {
  85. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_departements");
  86. if (! $notrigger)
  87. {
  88. // Uncomment this and change MYOBJECT to your own tag if you
  89. // want this action call a trigger.
  90. //// Call triggers
  91. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  92. //$interface=new Interfaces($this->db);
  93. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  94. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  95. //// End call triggers
  96. }
  97. }
  98. // Commit or rollback
  99. if ($error)
  100. {
  101. foreach($this->errors as $errmsg)
  102. {
  103. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  104. $this->error.=($this->error?', '.$errmsg:$errmsg);
  105. }
  106. $this->db->rollback();
  107. return -1*$error;
  108. }
  109. else
  110. {
  111. $this->db->commit();
  112. return $this->id;
  113. }
  114. }
  115. /**
  116. * Load object in memory from database
  117. *
  118. * @param int $id Id object
  119. * @param string $code Code
  120. * @return int <0 if KO, >0 if OK
  121. */
  122. function fetch($id,$code='')
  123. {
  124. global $langs;
  125. $sql = "SELECT";
  126. $sql.= " t.rowid,";
  127. $sql.= " t.code_departement,";
  128. $sql.= " t.nom,";
  129. $sql.= " t.active";
  130. $sql.= " FROM ".MAIN_DB_PREFIX."c_departements as t";
  131. if ($id) $sql.= " WHERE t.rowid = ".$id;
  132. elseif ($code) $sql.= " WHERE t.code_departement = '".$this->db->escape($code)."'";
  133. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  134. $resql=$this->db->query($sql);
  135. if ($resql)
  136. {
  137. if ($this->db->num_rows($resql))
  138. {
  139. $obj = $this->db->fetch_object($resql);
  140. $this->id = $obj->rowid;
  141. $this->code_departement = $obj->code_departement;
  142. $this->nom = $obj->nom;
  143. $this->active = $obj->active;
  144. }
  145. $this->db->free($resql);
  146. return 1;
  147. }
  148. else
  149. {
  150. $this->error="Error ".$this->db->lasterror();
  151. return -1;
  152. }
  153. }
  154. /**
  155. * Update object into database
  156. *
  157. * @param User $user User that modify
  158. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  159. * @return int <0 if KO, >0 if OK
  160. */
  161. function update($user=null, $notrigger=0)
  162. {
  163. global $conf, $langs;
  164. $error=0;
  165. // Clean parameters
  166. if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
  167. if (isset($this->nom)) $this->nom=trim($this->nom);
  168. if (isset($this->active)) $this->active=trim($this->active);
  169. // Check parameters
  170. // Put here code to add control on parameters values
  171. // Update request
  172. $sql = "UPDATE ".MAIN_DB_PREFIX."c_departements SET";
  173. $sql.= " code_departement=".(isset($this->code_departement)?"'".$this->db->escape($this->code_departement)."'":"null").",";
  174. $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
  175. $sql.= " active=".(isset($this->active)?$this->active:"null")."";
  176. $sql.= " WHERE rowid=".$this->id;
  177. $this->db->begin();
  178. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  179. $resql = $this->db->query($sql);
  180. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  181. if (! $error)
  182. {
  183. if (! $notrigger)
  184. {
  185. // Uncomment this and change MYOBJECT to your own tag if you
  186. // want this action call a trigger.
  187. //// Call triggers
  188. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  189. //$interface=new Interfaces($this->db);
  190. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  191. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  192. //// End call triggers
  193. }
  194. }
  195. // Commit or rollback
  196. if ($error)
  197. {
  198. foreach($this->errors as $errmsg)
  199. {
  200. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  201. $this->error.=($this->error?', '.$errmsg:$errmsg);
  202. }
  203. $this->db->rollback();
  204. return -1*$error;
  205. }
  206. else
  207. {
  208. $this->db->commit();
  209. return 1;
  210. }
  211. }
  212. /**
  213. * Delete object in database
  214. *
  215. * @param User $user User that delete
  216. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  217. * @return int <0 if KO, >0 if OK
  218. */
  219. function delete($user, $notrigger=0)
  220. {
  221. global $conf, $langs;
  222. $error=0;
  223. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_departements";
  224. $sql.= " WHERE rowid=".$this->id;
  225. $this->db->begin();
  226. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  227. $resql = $this->db->query($sql);
  228. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  229. if (! $error)
  230. {
  231. if (! $notrigger)
  232. {
  233. // Uncomment this and change MYOBJECT to your own tag if you
  234. // want this action call a trigger.
  235. //// Call triggers
  236. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  237. //$interface=new Interfaces($this->db);
  238. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  239. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  240. //// End call triggers
  241. }
  242. }
  243. // Commit or rollback
  244. if ($error)
  245. {
  246. foreach($this->errors as $errmsg)
  247. {
  248. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  249. $this->error.=($this->error?', '.$errmsg:$errmsg);
  250. }
  251. $this->db->rollback();
  252. return -1*$error;
  253. }
  254. else
  255. {
  256. $this->db->commit();
  257. return 1;
  258. }
  259. }
  260. }