ccountry.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/commondict.class.php';
  24. /**
  25. * Class to manage dictionary Countries (used by imports)
  26. */
  27. class Ccountry extends CommonDict
  28. {
  29. public $element = 'ccountry'; //!< Id that identify managed objects
  30. public $table_element = 'c_country'; //!< Name of table without prefix where object is stored
  31. public $code_iso;
  32. public $fields = array(
  33. 'label' => array('type'=>'varchar(250)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>15, 'notnull'=>-1, 'showoncombobox'=>'1')
  34. );
  35. /**
  36. * Constructor
  37. *
  38. * @param DoliDb $db Database handler
  39. */
  40. public function __construct($db)
  41. {
  42. $this->db = $db;
  43. }
  44. /**
  45. * Create object into database
  46. *
  47. * @param User $user User that create
  48. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  49. * @return int Return integer <0 if KO, Id of created object if OK
  50. */
  51. public function create($user, $notrigger = 0)
  52. {
  53. $error = 0;
  54. // Clean parameters
  55. if (isset($this->code)) {
  56. $this->code = trim($this->code);
  57. }
  58. if (isset($this->code_iso)) {
  59. $this->code_iso = trim($this->code_iso);
  60. }
  61. if (isset($this->label)) {
  62. $this->label = trim($this->label);
  63. }
  64. if (isset($this->active)) {
  65. $this->active = trim($this->active);
  66. }
  67. // Check parameters
  68. // Put here code to add control on parameters values
  69. // Insert request
  70. $sql = "INSERT INTO ".$this->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->db->escape($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->db->escape($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) {
  87. $error++;
  88. $this->errors[] = "Error ".$this->db->lasterror();
  89. }
  90. if (!$error) {
  91. $this->id = $this->db->last_insert_id($this->db->prefix()."c_country");
  92. }
  93. // Commit or rollback
  94. if ($error) {
  95. foreach ($this->errors as $errmsg) {
  96. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  97. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  98. }
  99. $this->db->rollback();
  100. return -1 * $error;
  101. } else {
  102. $this->db->commit();
  103. return $this->id;
  104. }
  105. }
  106. /**
  107. * Load object in memory from database
  108. *
  109. * @param int $id Id object
  110. * @param string $code Code
  111. * @param string $code_iso Code ISO
  112. * @return int >0 if OK, 0 if not found, <0 if KO
  113. */
  114. public function fetch($id, $code = '', $code_iso = '')
  115. {
  116. $sql = "SELECT";
  117. $sql .= " t.rowid,";
  118. $sql .= " t.code,";
  119. $sql .= " t.code_iso,";
  120. $sql .= " t.label,";
  121. $sql .= " t.active";
  122. $sql .= " FROM ".$this->db->prefix()."c_country as t";
  123. if ($id) {
  124. $sql .= " WHERE t.rowid = ".((int) $id);
  125. } elseif ($code) {
  126. $sql .= " WHERE t.code = '".$this->db->escape(strtoupper($code))."'";
  127. } elseif ($code_iso) {
  128. $sql .= " WHERE t.code_iso = '".$this->db->escape(strtoupper($code_iso))."'";
  129. }
  130. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  131. $resql = $this->db->query($sql);
  132. if ($resql) {
  133. if ($this->db->num_rows($resql)) {
  134. $obj = $this->db->fetch_object($resql);
  135. if ($obj) {
  136. $this->id = $obj->rowid;
  137. $this->code = $obj->code;
  138. $this->code_iso = $obj->code_iso;
  139. $this->label = $obj->label;
  140. $this->active = $obj->active;
  141. }
  142. $this->db->free($resql);
  143. return 1;
  144. } else {
  145. return 0;
  146. }
  147. } else {
  148. $this->error = "Error ".$this->db->lasterror();
  149. return -1;
  150. }
  151. }
  152. /**
  153. * Update object into database
  154. *
  155. * @param User $user User that modify
  156. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  157. * @return int Return integer <0 if KO, >0 if OK
  158. */
  159. public function update($user = null, $notrigger = 0)
  160. {
  161. $error = 0;
  162. // Clean parameters
  163. if (isset($this->code)) {
  164. $this->code = trim($this->code);
  165. }
  166. if (isset($this->code_iso)) {
  167. $this->code_iso = trim($this->code_iso);
  168. }
  169. if (isset($this->label)) {
  170. $this->label = trim($this->label);
  171. }
  172. if (isset($this->active)) {
  173. $this->active = trim($this->active);
  174. }
  175. // Check parameters
  176. // Put here code to add control on parameters values
  177. // Update request
  178. $sql = "UPDATE ".$this->db->prefix()."c_country SET";
  179. $sql .= " code=".(isset($this->code) ? "'".$this->db->escape($this->code)."'" : "null").",";
  180. $sql .= " code_iso=".(isset($this->code_iso) ? "'".$this->db->escape($this->code_iso)."'" : "null").",";
  181. $sql .= " label=".(isset($this->label) ? "'".$this->db->escape($this->label)."'" : "null").",";
  182. $sql .= " active=".(isset($this->active) ? $this->active : "null");
  183. $sql .= " WHERE rowid=".((int) $this->id);
  184. $this->db->begin();
  185. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  186. $resql = $this->db->query($sql);
  187. if (!$resql) {
  188. $error++;
  189. $this->errors[] = "Error ".$this->db->lasterror();
  190. }
  191. // Commit or rollback
  192. if ($error) {
  193. foreach ($this->errors as $errmsg) {
  194. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  195. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  196. }
  197. $this->db->rollback();
  198. return -1 * $error;
  199. } else {
  200. $this->db->commit();
  201. return 1;
  202. }
  203. }
  204. /**
  205. * Delete object in database
  206. *
  207. * @param User $user User that delete
  208. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  209. * @return int Return integer <0 if KO, >0 if OK
  210. */
  211. public function delete($user, $notrigger = 0)
  212. {
  213. $error = 0;
  214. $sql = "DELETE FROM ".$this->db->prefix()."c_country";
  215. $sql .= " WHERE rowid=".((int) $this->id);
  216. $this->db->begin();
  217. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  218. $resql = $this->db->query($sql);
  219. if (!$resql) {
  220. $error++;
  221. $this->errors[] = "Error ".$this->db->lasterror();
  222. }
  223. // Commit or rollback
  224. if ($error) {
  225. foreach ($this->errors as $errmsg) {
  226. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  227. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  228. }
  229. $this->db->rollback();
  230. return -1 * $error;
  231. } else {
  232. $this->db->commit();
  233. return 1;
  234. }
  235. }
  236. /**
  237. * Return a link to the object card (with optionaly the picto)
  238. *
  239. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  240. * @param string $option On what the link point to ('nolink', ...)
  241. * @param int $notooltip 1=Disable tooltip
  242. * @param string $morecss Add more css on link
  243. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  244. * @return string String with URL
  245. */
  246. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  247. {
  248. global $langs;
  249. return $langs->trans($this->label);
  250. }
  251. }