cstate.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /**
  32. * @var DoliDB Database handler.
  33. */
  34. public $db;
  35. /**
  36. * @var string Error code (or message)
  37. */
  38. public $error='';
  39. /**
  40. * @var string[] Error codes (or messages)
  41. */
  42. public $errors = array();
  43. //var $element='cstate'; //!< Id that identify managed objects
  44. //var $table_element='cstate'; //!< Name of table without prefix where object is stored
  45. /**
  46. * @var int ID
  47. */
  48. public $id;
  49. public $code_departement;
  50. /**
  51. * @var string
  52. * @deprecated
  53. * @see name
  54. */
  55. public $nom='';
  56. /**
  57. * @var string name
  58. */
  59. public $name='';
  60. public $active;
  61. /**
  62. * Constructor
  63. *
  64. * @param DoliDb $db Database handler
  65. */
  66. function __construct($db)
  67. {
  68. $this->db = $db;
  69. }
  70. /**
  71. * Create object into database
  72. *
  73. * @param User $user User that create
  74. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  75. * @return int <0 if KO, Id of created object if OK
  76. */
  77. function create($user, $notrigger=0)
  78. {
  79. global $conf, $langs;
  80. $error=0;
  81. // Clean parameters
  82. if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
  83. if (isset($this->nom)) $this->nom=trim($this->nom);
  84. if (isset($this->active)) $this->active=trim($this->active);
  85. // Check parameters
  86. // Put here code to add control on parameters values
  87. // Insert request
  88. $sql = "INSERT INTO ".MAIN_DB_PREFIX."c_departements(";
  89. $sql.= "rowid,";
  90. $sql.= "code_departement,";
  91. $sql.= "nom,";
  92. $sql.= "active";
  93. $sql.= ") VALUES (";
  94. $sql.= " ".(! isset($this->rowid)?'NULL':"'".$this->db->escape($this->rowid)."'").",";
  95. $sql.= " ".(! isset($this->code_departement)?'NULL':"'".$this->db->escape($this->code_departement)."'").",";
  96. $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
  97. $sql.= " ".(! isset($this->active)?'NULL':"'".$this->db->escape($this->active)."'")."";
  98. $sql.= ")";
  99. $this->db->begin();
  100. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  101. $resql=$this->db->query($sql);
  102. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  103. if (! $error)
  104. {
  105. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."c_departements");
  106. if (! $notrigger)
  107. {
  108. // Uncomment this and change MYOBJECT to your own tag if you
  109. // want this action call a trigger.
  110. //// Call triggers
  111. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  112. //$interface=new Interfaces($this->db);
  113. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  114. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  115. //// End call triggers
  116. }
  117. }
  118. // Commit or rollback
  119. if ($error)
  120. {
  121. foreach($this->errors as $errmsg)
  122. {
  123. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  124. $this->error.=($this->error?', '.$errmsg:$errmsg);
  125. }
  126. $this->db->rollback();
  127. return -1*$error;
  128. }
  129. else
  130. {
  131. $this->db->commit();
  132. return $this->id;
  133. }
  134. }
  135. /**
  136. * Load object in memory from database
  137. *
  138. * @param int $id Id object
  139. * @param string $code Code
  140. * @return int <0 if KO, >0 if OK
  141. */
  142. function fetch($id,$code='')
  143. {
  144. global $langs;
  145. $sql = "SELECT";
  146. $sql.= " t.rowid,";
  147. $sql.= " t.code_departement,";
  148. $sql.= " t.nom,";
  149. $sql.= " t.active";
  150. $sql.= " FROM ".MAIN_DB_PREFIX."c_departements as t";
  151. if ($id) $sql.= " WHERE t.rowid = ".$id;
  152. elseif ($code) $sql.= " WHERE t.code_departement = '".$this->db->escape($code)."'";
  153. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  154. $resql=$this->db->query($sql);
  155. if ($resql)
  156. {
  157. if ($this->db->num_rows($resql))
  158. {
  159. $obj = $this->db->fetch_object($resql);
  160. $this->id = $obj->rowid;
  161. $this->code_departement = $obj->code_departement;
  162. $this->nom = $obj->nom;
  163. $this->active = $obj->active;
  164. }
  165. $this->db->free($resql);
  166. return 1;
  167. }
  168. else
  169. {
  170. $this->error="Error ".$this->db->lasterror();
  171. return -1;
  172. }
  173. }
  174. /**
  175. * Update object into database
  176. *
  177. * @param User $user User that modify
  178. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  179. * @return int <0 if KO, >0 if OK
  180. */
  181. function update($user=null, $notrigger=0)
  182. {
  183. global $conf, $langs;
  184. $error=0;
  185. // Clean parameters
  186. if (isset($this->code_departement)) $this->code_departement=trim($this->code_departement);
  187. if (isset($this->nom)) $this->nom=trim($this->nom);
  188. if (isset($this->active)) $this->active=trim($this->active);
  189. // Check parameters
  190. // Put here code to add control on parameters values
  191. // Update request
  192. $sql = "UPDATE ".MAIN_DB_PREFIX."c_departements SET";
  193. $sql.= " code_departement=".(isset($this->code_departement)?"'".$this->db->escape($this->code_departement)."'":"null").",";
  194. $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
  195. $sql.= " active=".(isset($this->active)?$this->active:"null")."";
  196. $sql.= " WHERE rowid=".$this->id;
  197. $this->db->begin();
  198. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  199. $resql = $this->db->query($sql);
  200. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  201. if (! $error)
  202. {
  203. if (! $notrigger)
  204. {
  205. // Uncomment this and change MYOBJECT to your own tag if you
  206. // want this action call a trigger.
  207. //// Call triggers
  208. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  209. //$interface=new Interfaces($this->db);
  210. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  211. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  212. //// End call triggers
  213. }
  214. }
  215. // Commit or rollback
  216. if ($error)
  217. {
  218. foreach($this->errors as $errmsg)
  219. {
  220. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  221. $this->error.=($this->error?', '.$errmsg:$errmsg);
  222. }
  223. $this->db->rollback();
  224. return -1*$error;
  225. }
  226. else
  227. {
  228. $this->db->commit();
  229. return 1;
  230. }
  231. }
  232. /**
  233. * Delete object in database
  234. *
  235. * @param User $user User that delete
  236. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  237. * @return int <0 if KO, >0 if OK
  238. */
  239. function delete($user, $notrigger=0)
  240. {
  241. global $conf, $langs;
  242. $error=0;
  243. $sql = "DELETE FROM ".MAIN_DB_PREFIX."c_departements";
  244. $sql.= " WHERE rowid=".$this->id;
  245. $this->db->begin();
  246. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  247. $resql = $this->db->query($sql);
  248. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  249. if (! $error)
  250. {
  251. if (! $notrigger)
  252. {
  253. // Uncomment this and change MYOBJECT to your own tag if you
  254. // want this action call a trigger.
  255. //// Call triggers
  256. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  257. //$interface=new Interfaces($this->db);
  258. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  259. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  260. //// End call triggers
  261. }
  262. }
  263. // Commit or rollback
  264. if ($error)
  265. {
  266. foreach($this->errors as $errmsg)
  267. {
  268. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  269. $this->error.=($this->error?', '.$errmsg:$errmsg);
  270. }
  271. $this->db->rollback();
  272. return -1*$error;
  273. }
  274. else
  275. {
  276. $this->db->commit();
  277. return 1;
  278. }
  279. }
  280. }