ccountry.class.php 9.2 KB

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