ccountry.class.php 9.4 KB

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