cstate.class.php 9.0 KB

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