cregion.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* Copyright (C) Richard Rondu <rondu.richard@lainwir3d.net>
  3. * Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/cregion.class.php
  20. * \ingroup core
  21. * \brief This file is a CRUD class file (Create/Read/Update/Delete) for c_regions dictionary
  22. */
  23. /**
  24. * Class to manage dictionary Regions
  25. */
  26. class Cregion
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error = '';
  36. /**
  37. * @var string[] Error codes (or messages)
  38. */
  39. public $errors = array();
  40. //public $element = 'cregion'; //!< Id that identify managed objects
  41. //public $table_element = 'c_regions'; //!< Name of table without prefix where object is stored
  42. /**
  43. * @var int ID
  44. */
  45. public $id;
  46. public $code_region;
  47. public $fk_pays;
  48. /**
  49. * @var string Region name
  50. */
  51. public $name;
  52. /**
  53. * @var string Region chef lieu
  54. */
  55. public $cheflieu;
  56. public $active;
  57. /**
  58. * Constructor
  59. *
  60. * @param DoliDb $db Database handler
  61. */
  62. public function __construct($db)
  63. {
  64. $this->db = $db;
  65. }
  66. /**
  67. * Create object into database
  68. *
  69. * @param User $user User that create
  70. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  71. * @return int <0 if KO, Id of created object if OK
  72. */
  73. public function create($user, $notrigger = 0)
  74. {
  75. global $conf, $langs;
  76. $error = 0;
  77. // Clean parameters
  78. if (isset($this->code_region)) {
  79. $this->code_region = trim($this->code_region);
  80. }
  81. if (isset($this->fk_pays)) {
  82. $this->fk_pays = trim($this->fk_pays);
  83. }
  84. if (isset($this->nom)) {
  85. $this->nom = trim($this->nom);
  86. }
  87. if (isset($this->cheflieu)) {
  88. $this->cheflieu = trim($this->cheflieu);
  89. }
  90. if (isset($this->active)) {
  91. $this->active = trim($this->active);
  92. }
  93. // Check parameters
  94. // Put here code to add control on parameters values
  95. // Insert request
  96. $sql = "INSERT INTO ".$this->db->prefix()."c_regions(";
  97. $sql .= "rowid,";
  98. $sql .= "code_region,";
  99. $sql .= "fk_pays,";
  100. $sql .= "nom,";
  101. $sql .= "cheflieu,";
  102. $sql .= "active";
  103. $sql .= ") VALUES (";
  104. $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
  105. $sql .= " ".(!isset($this->code_region) ? 'NULL' : "'".$this->db->escape($this->code_region)."'").",";
  106. $sql .= " ".(!isset($this->fk_pays) ? 'NULL' : "'".$this->db->escape($this->fk_pays)."'").",";
  107. $sql .= " ".(!isset($this->name) ? 'NULL' : "'".$this->db->escape($this->name)."'").",";
  108. $sql .= " ".(!isset($this->cheflieu) ? 'NULL' : "'".$this->db->escape($this->cheflieu)."'").",";
  109. $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'");
  110. $sql .= ")";
  111. $this->db->begin();
  112. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  113. $resql = $this->db->query($sql);
  114. if (!$resql) {
  115. $error++;
  116. $this->errors[] = "Error ".$this->db->lasterror();
  117. }
  118. if (!$error) {
  119. $this->id = $this->db->last_insert_id($this->db->prefix()."c_regions");
  120. }
  121. // Commit or rollback
  122. if ($error) {
  123. foreach ($this->errors as $errmsg) {
  124. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  125. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  126. }
  127. $this->db->rollback();
  128. return -1 * $error;
  129. } else {
  130. $this->db->commit();
  131. return $this->id;
  132. }
  133. }
  134. /**
  135. * Load object in memory from database
  136. *
  137. * @param int $id Id object
  138. * @param string $code_region Code
  139. * @param string $fk_pays Country Id
  140. * @return int >0 if OK, 0 if not found, <0 if KO
  141. */
  142. public function fetch($id, $code_region = '', $fk_pays = '')
  143. {
  144. $sql = "SELECT";
  145. $sql .= " t.rowid,";
  146. $sql .= " t.code_region,";
  147. $sql .= " t.fk_pays,";
  148. $sql .= " t.nom,";
  149. $sql .= " t.cheflieu,";
  150. $sql .= " t.active";
  151. $sql .= " FROM ".$this->db->prefix()."c_regions as t";
  152. if ($id) {
  153. $sql .= " WHERE t.rowid = ".((int) $id);
  154. } elseif ($code_region) {
  155. $sql .= " WHERE t.code_region = '".$this->db->escape(strtoupper($code_region))."'";
  156. } elseif ($fk_pays) {
  157. $sql .= " WHERE t.fk_pays = '".$this->db->escape(strtoupper($fk_pays))."'";
  158. }
  159. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  160. $resql = $this->db->query($sql);
  161. if ($resql) {
  162. if ($this->db->num_rows($resql)) {
  163. $obj = $this->db->fetch_object($resql);
  164. if ($obj) {
  165. $this->id = $obj->rowid;
  166. $this->code_region = $obj->code_region;
  167. $this->fk_pays = $obj->fk_pays;
  168. $this->name = $obj->nom;
  169. $this->cheflieu = $obj->cheflieu;
  170. $this->active = $obj->active;
  171. }
  172. $this->db->free($resql);
  173. return 1;
  174. } else {
  175. return 0;
  176. }
  177. } else {
  178. $this->error = "Error ".$this->db->lasterror();
  179. return -1;
  180. }
  181. }
  182. /**
  183. * Update object into database
  184. *
  185. * @param User $user User that modify
  186. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  187. * @return int <0 if KO, >0 if OK
  188. */
  189. public function update($user = null, $notrigger = 0)
  190. {
  191. global $conf, $langs;
  192. $error = 0;
  193. // Clean parameters
  194. if (isset($this->code_region)) {
  195. $this->code_region = trim($this->code_region);
  196. }
  197. if (isset($this->fk_pays)) {
  198. $this->fk_pays = trim($this->fk_pays);
  199. }
  200. if (isset($this->name)) {
  201. $this->name = trim($this->name);
  202. }
  203. if (isset($this->cheflieu)) {
  204. $this->cheflieu = trim($this->cheflieu);
  205. }
  206. if (isset($this->active)) {
  207. $this->active = trim($this->active);
  208. }
  209. // Check parameters
  210. // Put here code to add control on parameters values
  211. // Update request
  212. $sql = "UPDATE ".$this->db->prefix()."c_regions SET";
  213. $sql .= " code_region=".(isset($this->code_region) ? "'".$this->db->escape($this->code_region)."'" : "null").",";
  214. $sql .= " fk_pays=".(isset($this->fk_pays) ? "'".$this->db->escape($this->fk_pays)."'" : "null").",";
  215. $sql .= " nom=".(isset($this->name) ? "'".$this->db->escape($this->name)."'" : "null").",";
  216. $sql .= " cheflieu=".(isset($this->cheflieu) ? "'".$this->db->escape($this->cheflieu)."'" : "null").",";
  217. $sql .= " active=".(isset($this->active) ? $this->active : "null");
  218. $sql .= " WHERE rowid=".((int) $this->id);
  219. $this->db->begin();
  220. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  221. $resql = $this->db->query($sql);
  222. if (!$resql) {
  223. $error++;
  224. $this->errors[] = "Error ".$this->db->lasterror();
  225. }
  226. // Commit or rollback
  227. if ($error) {
  228. foreach ($this->errors as $errmsg) {
  229. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  230. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  231. }
  232. $this->db->rollback();
  233. return -1 * $error;
  234. } else {
  235. $this->db->commit();
  236. return 1;
  237. }
  238. }
  239. /**
  240. * Delete object in database
  241. *
  242. * @param User $user User that delete
  243. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  244. * @return int <0 if KO, >0 if OK
  245. */
  246. public function delete($user, $notrigger = 0)
  247. {
  248. global $conf, $langs;
  249. $error = 0;
  250. $sql = "DELETE FROM ".$this->db->prefix()."c_regions";
  251. $sql .= " WHERE rowid=".((int) $this->id);
  252. $this->db->begin();
  253. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  254. $resql = $this->db->query($sql);
  255. if (!$resql) {
  256. $error++;
  257. $this->errors[] = "Error ".$this->db->lasterror();
  258. }
  259. // Commit or rollback
  260. if ($error) {
  261. foreach ($this->errors as $errmsg) {
  262. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  263. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  264. }
  265. $this->db->rollback();
  266. return -1 * $error;
  267. } else {
  268. $this->db->commit();
  269. return 1;
  270. }
  271. }
  272. /**
  273. * Return a link to the object card (with optionaly the picto)
  274. *
  275. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  276. * @param string $option On what the link point to ('nolink', ...)
  277. * @param int $notooltip 1=Disable tooltip
  278. * @param string $morecss Add more css on link
  279. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  280. * @return string String with URL
  281. */
  282. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  283. {
  284. global $langs;
  285. return $langs->trans($this->name);
  286. }
  287. }