ccountry.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <https://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. public $element = 'ccountry'; //!< Id that identify managed objects
  44. public $table_element = 'c_country'; //!< 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. public $fields = array(
  57. 'label' => array('type'=>'varchar(250)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>15, 'notnull'=>-1, 'showoncombobox'=>'1')
  58. );
  59. /**
  60. * Constructor
  61. *
  62. * @param DoliDb $db Database handler
  63. */
  64. public function __construct($db)
  65. {
  66. $this->db = $db;
  67. }
  68. /**
  69. * Create object into database
  70. *
  71. * @param User $user User that create
  72. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  73. * @return int <0 if KO, Id of created object if OK
  74. */
  75. public function create($user, $notrigger = 0)
  76. {
  77. global $conf, $langs;
  78. $error = 0;
  79. // Clean parameters
  80. if (isset($this->code)) {
  81. $this->code = trim($this->code);
  82. }
  83. if (isset($this->code_iso)) {
  84. $this->code_iso = trim($this->code_iso);
  85. }
  86. if (isset($this->label)) {
  87. $this->label = trim($this->label);
  88. }
  89. if (isset($this->active)) {
  90. $this->active = trim($this->active);
  91. }
  92. // Check parameters
  93. // Put here code to add control on parameters values
  94. // Insert request
  95. $sql = "INSERT INTO ".$this->db->prefix()."c_country(";
  96. $sql .= "rowid,";
  97. $sql .= "code,";
  98. $sql .= "code_iso,";
  99. $sql .= "label,";
  100. $sql .= "active";
  101. $sql .= ") VALUES (";
  102. $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
  103. $sql .= " ".(!isset($this->code) ? 'NULL' : "'".$this->db->escape($this->code)."'").",";
  104. $sql .= " ".(!isset($this->code_iso) ? 'NULL' : "'".$this->db->escape($this->code_iso)."'").",";
  105. $sql .= " ".(!isset($this->label) ? 'NULL' : "'".$this->db->escape($this->label)."'").",";
  106. $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'")."";
  107. $sql .= ")";
  108. $this->db->begin();
  109. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  110. $resql = $this->db->query($sql);
  111. if (!$resql) {
  112. $error++;
  113. $this->errors[] = "Error ".$this->db->lasterror();
  114. }
  115. if (!$error) {
  116. $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
  117. }
  118. // Commit or rollback
  119. if ($error) {
  120. foreach ($this->errors as $errmsg) {
  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. } else {
  127. $this->db->commit();
  128. return $this->id;
  129. }
  130. }
  131. /**
  132. * Load object in memory from database
  133. *
  134. * @param int $id Id object
  135. * @param string $code Code
  136. * @param string $code_iso Code ISO
  137. * @return int >0 if OK, 0 if not found, <0 if KO
  138. */
  139. public function fetch($id, $code = '', $code_iso = '')
  140. {
  141. $sql = "SELECT";
  142. $sql .= " t.rowid,";
  143. $sql .= " t.code,";
  144. $sql .= " t.code_iso,";
  145. $sql .= " t.label,";
  146. $sql .= " t.active";
  147. $sql .= " FROM ".$this->db->prefix()."c_country as t";
  148. if ($id) {
  149. $sql .= " WHERE t.rowid = ".((int) $id);
  150. } elseif ($code) {
  151. $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
  152. } elseif ($code_iso) {
  153. $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
  154. }
  155. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  156. $resql = $this->db->query($sql);
  157. if ($resql) {
  158. if ($this->db->num_rows($resql)) {
  159. $obj = $this->db->fetch_object($resql);
  160. if ($obj) {
  161. $this->id = $obj->rowid;
  162. $this->code = $obj->code;
  163. $this->code_iso = $obj->code_iso;
  164. $this->label = $obj->label;
  165. $this->active = $obj->active;
  166. }
  167. $this->db->free($resql);
  168. return 1;
  169. } else {
  170. return 0;
  171. }
  172. } else {
  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. public function update($user = null, $notrigger = 0)
  185. {
  186. global $conf, $langs;
  187. $error = 0;
  188. // Clean parameters
  189. if (isset($this->code)) {
  190. $this->code = trim($this->code);
  191. }
  192. if (isset($this->code_iso)) {
  193. $this->code_iso = trim($this->code_iso);
  194. }
  195. if (isset($this->label)) {
  196. $this->label = trim($this->label);
  197. }
  198. if (isset($this->active)) {
  199. $this->active = trim($this->active);
  200. }
  201. // Check parameters
  202. // Put here code to add control on parameters values
  203. // Update request
  204. $sql = "UPDATE ".$this->db->prefix()."c_country SET";
  205. $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
  206. $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
  207. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  208. $sql .= " active=".(isset($this->active) ? $this->active : "null")."";
  209. $sql .= " WHERE rowid=".((int) $this->id);
  210. $this->db->begin();
  211. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  212. $resql = $this->db->query($sql);
  213. if (!$resql) {
  214. $error++;
  215. $this->errors[] = "Error ".$this->db->lasterror();
  216. }
  217. // Commit or rollback
  218. if ($error) {
  219. foreach ($this->errors as $errmsg) {
  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. } else {
  226. $this->db->commit();
  227. return 1;
  228. }
  229. }
  230. /**
  231. * Delete object in database
  232. *
  233. * @param User $user User that delete
  234. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  235. * @return int <0 if KO, >0 if OK
  236. */
  237. public function delete($user, $notrigger = 0)
  238. {
  239. global $conf, $langs;
  240. $error = 0;
  241. $sql = "DELETE FROM ".$this->db->prefix()."c_country";
  242. $sql .= " WHERE rowid=".((int) $this->id);
  243. $this->db->begin();
  244. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  245. $resql = $this->db->query($sql);
  246. if (!$resql) {
  247. $error++;
  248. $this->errors[] = "Error ".$this->db->lasterror();
  249. }
  250. // Commit or rollback
  251. if ($error) {
  252. foreach ($this->errors as $errmsg) {
  253. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  254. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  255. }
  256. $this->db->rollback();
  257. return -1 * $error;
  258. } else {
  259. $this->db->commit();
  260. return 1;
  261. }
  262. }
  263. /**
  264. * Return a link to the object card (with optionaly the picto)
  265. *
  266. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  267. * @param string $option On what the link point to ('nolink', ...)
  268. * @param int $notooltip 1=Disable tooltip
  269. * @param string $morecss Add more css on link
  270. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  271. * @return string String with URL
  272. */
  273. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  274. {
  275. global $langs;
  276. return $langs->trans($this->label);
  277. }
  278. }