cstate.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /* Copyright (C) 2016 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/cstate.class.php
  19. * \ingroup core
  20. * \brief This file is a CRUD class file (Create/Read/Update/Delete) for c_departements dictionary
  21. */
  22. /**
  23. * Class to manage dictionary States (used by imports)
  24. */
  25. class Cstate // extends CommonObject
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. /**
  32. * @var string Error code (or message)
  33. */
  34. public $error = '';
  35. /**
  36. * @var string[] Error codes (or messages)
  37. */
  38. public $errors = array();
  39. //var $element='cstate'; //!< Id that identify managed objects
  40. //var $table_element='cstate'; //!< Name of table without prefix where object is stored
  41. /**
  42. * @var int ID
  43. */
  44. public $id;
  45. public $code_departement;
  46. public $code;
  47. /**
  48. * @var string name
  49. */
  50. public $name = '';
  51. /**
  52. * @var string
  53. * @deprecated
  54. * @see $name
  55. */
  56. public $nom = '';
  57. public $label;
  58. public $active;
  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. $error = 0;
  78. // Clean parameters
  79. if (isset($this->code_departement)) {
  80. $this->code_departement = trim($this->code_departement);
  81. }
  82. if (isset($this->nom)) {
  83. $this->nom = trim($this->nom);
  84. }
  85. if (isset($this->active)) {
  86. $this->active = trim($this->active);
  87. }
  88. // Check parameters
  89. // Put here code to add control on parameters values
  90. // Insert request
  91. $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
  92. $sql .= "rowid,";
  93. $sql .= "code_departement,";
  94. $sql .= "nom,";
  95. $sql .= "active";
  96. $sql .= ") VALUES (";
  97. $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
  98. $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
  99. $sql .= " ".(!isset($this->nom) ? 'NULL' : "'".$this->db->escape($this->nom)."'").",";
  100. $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'")."";
  101. $sql .= ")";
  102. $this->db->begin();
  103. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  104. $resql = $this->db->query($sql);
  105. if (!$resql) {
  106. $error++;
  107. $this->errors[] = "Error ".$this->db->lasterror();
  108. }
  109. if (!$error) {
  110. $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
  111. }
  112. // Commit or rollback
  113. if ($error) {
  114. foreach ($this->errors as $errmsg) {
  115. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  116. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  117. }
  118. $this->db->rollback();
  119. return -1 * $error;
  120. } else {
  121. $this->db->commit();
  122. return $this->id;
  123. }
  124. }
  125. /**
  126. * Load object in memory from database
  127. *
  128. * @param int $id Id object
  129. * @param string $code Code
  130. * @return int <0 if KO, >0 if OK
  131. */
  132. public function fetch($id, $code = '')
  133. {
  134. $sql = "SELECT";
  135. $sql .= " t.rowid,";
  136. $sql .= " t.code_departement,";
  137. $sql .= " t.nom,";
  138. $sql .= " t.active";
  139. $sql .= " FROM ".$this->db->prefix()."c_departements as t";
  140. if ($id) {
  141. $sql .= " WHERE t.rowid = ".((int) $id);
  142. } elseif ($code) {
  143. $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
  144. }
  145. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  146. $resql = $this->db->query($sql);
  147. if ($resql) {
  148. if ($this->db->num_rows($resql)) {
  149. $obj = $this->db->fetch_object($resql);
  150. $this->id = $obj->rowid;
  151. $this->code_departement = $obj->code_departement; //deprecated
  152. $this->code = $obj->code_departement;
  153. $this->nom = $obj->nom; //deprecated
  154. $this->name = $obj->nom;
  155. $this->active = $obj->active;
  156. }
  157. $this->db->free($resql);
  158. return 1;
  159. } else {
  160. $this->error = "Error ".$this->db->lasterror();
  161. return -1;
  162. }
  163. }
  164. /**
  165. * Update object into database
  166. *
  167. * @param User $user User that modify
  168. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  169. * @return int <0 if KO, >0 if OK
  170. */
  171. public function update($user = null, $notrigger = 0)
  172. {
  173. global $conf, $langs;
  174. $error = 0;
  175. // Clean parameters
  176. if (isset($this->code_departement)) {
  177. $this->code_departement = trim($this->code_departement);
  178. }
  179. if (isset($this->nom)) {
  180. $this->nom = trim($this->nom);
  181. }
  182. if (isset($this->active)) {
  183. $this->active = trim($this->active);
  184. }
  185. // Check parameters
  186. // Put here code to add control on parameters values
  187. // Update request
  188. $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
  189. $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
  190. $sql .= " nom=".(isset($this->nom) ? "'".$this->db->escape($this->nom)."'" : "null").",";
  191. $sql .= " active=".(isset($this->active) ? $this->active : "null")."";
  192. $sql .= " WHERE rowid=".((int) $this->id);
  193. $this->db->begin();
  194. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  195. $resql = $this->db->query($sql);
  196. if (!$resql) {
  197. $error++;
  198. $this->errors[] = "Error ".$this->db->lasterror();
  199. }
  200. // Commit or rollback
  201. if ($error) {
  202. foreach ($this->errors as $errmsg) {
  203. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  204. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  205. }
  206. $this->db->rollback();
  207. return -1 * $error;
  208. } else {
  209. $this->db->commit();
  210. return 1;
  211. }
  212. }
  213. /**
  214. * Delete object in database
  215. *
  216. * @param User $user User that delete
  217. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  218. * @return int <0 if KO, >0 if OK
  219. */
  220. public function delete($user, $notrigger = 0)
  221. {
  222. global $conf, $langs;
  223. $error = 0;
  224. $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
  225. $sql .= " WHERE rowid=".((int) $this->id);
  226. $this->db->begin();
  227. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  228. $resql = $this->db->query($sql);
  229. if (!$resql) {
  230. $error++;
  231. $this->errors[] = "Error ".$this->db->lasterror();
  232. }
  233. // Commit or rollback
  234. if ($error) {
  235. foreach ($this->errors as $errmsg) {
  236. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  237. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  238. }
  239. $this->db->rollback();
  240. return -1 * $error;
  241. } else {
  242. $this->db->commit();
  243. return 1;
  244. }
  245. }
  246. }