You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

phcp.class.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) ---Put here your own copyright and developer email---
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file dev/skeletons/phcp.class.php
  20. * \ingroup mymodule othermodule1 othermodule2
  21. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  22. * Initialy built by build_class_from_table on 2013-09-12 16:46
  23. */
  24. // Put here all includes required by your class file
  25. require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  26. //require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
  27. //require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  28. /**
  29. * Put here description of your class
  30. */
  31. class Phcp extends CommonObject
  32. {
  33. var $db; //!< To store db handler
  34. var $error; //!< To return error code (or message)
  35. var $errors=array(); //!< To return several error codes (or messages)
  36. var $element='phcp'; //!< Id that identify managed objects
  37. var $table_element='phcp'; //!< Name of table without prefix where object is stored
  38. var $id;
  39. var $nom;
  40. var $ref_ext;
  41. var $ref_int;
  42. var $fk_phcp_type;
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDb $db Database handler
  47. */
  48. function __construct($db)
  49. {
  50. $this->db = $db;
  51. return 1;
  52. }
  53. /**
  54. * Create object into database
  55. *
  56. * @param User $user User that creates
  57. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  58. * @return int <0 if KO, Id of created object if OK
  59. */
  60. function create($user, $notrigger=0)
  61. {
  62. global $conf, $langs;
  63. $error=0;
  64. // Clean parameters
  65. if (isset($this->nom)) $this->nom=trim($this->nom);
  66. if (isset($this->ref_ext)) $this->ref_ext=trim($this->ref_ext);
  67. if (isset($this->ref_int)) $this->ref_int=trim($this->ref_int);
  68. if (isset($this->fk_phcp_type)) $this->fk_phcp_type=trim($this->fk_phcp_type);
  69. // Check parameters
  70. // Put here code to add control on parameters values
  71. // Insert request
  72. $sql = "INSERT INTO ".MAIN_DB_PREFIX."phcp(";
  73. $sql.= "nom,";
  74. $sql.= "ref_ext,";
  75. $sql.= "ref_int,";
  76. $sql.= "fk_phcp_type";
  77. $sql.= ") VALUES (";
  78. $sql.= " ".(! isset($this->nom)?'NULL':"'".$this->db->escape($this->nom)."'").",";
  79. $sql.= " ".(! isset($this->ref_ext)?'NULL':"'".$this->db->escape($this->ref_ext)."'").",";
  80. $sql.= " ".(! isset($this->ref_int)?'NULL':"'".$this->db->escape($this->ref_int)."'").",";
  81. $sql.= " ".(! isset($this->fk_phcp_type)?'NULL':"'".$this->fk_phcp_type."'")."";
  82. $sql.= ")";
  83. $this->db->begin();
  84. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  85. $resql=$this->db->query($sql);
  86. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  87. if (! $error)
  88. {
  89. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."phcp");
  90. if (! $notrigger)
  91. {
  92. // Uncomment this and change MYOBJECT to your own tag if you
  93. // want this action calls a trigger.
  94. //// Call triggers
  95. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  96. //$interface=new Interfaces($this->db);
  97. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  98. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  99. //// End call triggers
  100. }
  101. }
  102. // Commit or rollback
  103. if ($error)
  104. {
  105. foreach($this->errors as $errmsg)
  106. {
  107. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  108. $this->error.=($this->error?', '.$errmsg:$errmsg);
  109. }
  110. $this->db->rollback();
  111. return -1*$error;
  112. }
  113. else
  114. {
  115. $this->db->commit();
  116. return $this->id;
  117. }
  118. }
  119. /**
  120. * Load object in memory from the database
  121. *
  122. * @param int $id Id object
  123. * @return int <0 if KO, >0 if OK
  124. */
  125. function fetch($id)
  126. {
  127. global $langs;
  128. $sql = "SELECT";
  129. $sql.= " t.rowid,";
  130. $sql.= " t.nom,";
  131. $sql.= " t.ref_ext,";
  132. $sql.= " t.ref_int,";
  133. $sql.= " t.fk_phcp_type";
  134. $sql.= " FROM ".MAIN_DB_PREFIX."phcp as t";
  135. $sql.= " WHERE t.rowid = ".$id;
  136. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  137. $resql=$this->db->query($sql);
  138. if ($resql)
  139. {
  140. if ($this->db->num_rows($resql))
  141. {
  142. $obj = $this->db->fetch_object($resql);
  143. $this->id = $obj->rowid;
  144. $this->nom = $obj->nom;
  145. $this->ref_ext = $obj->ref_ext;
  146. $this->ref_int = $obj->ref_int;
  147. $this->fk_phcp_type = $obj->fk_phcp_type;
  148. }
  149. $this->db->free($resql);
  150. return 1;
  151. }
  152. else
  153. {
  154. $this->error="Error ".$this->db->lasterror();
  155. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  156. return -1;
  157. }
  158. }
  159. /**
  160. * Update object into database
  161. *
  162. * @param User $user User that modifies
  163. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  164. * @return int <0 if KO, >0 if OK
  165. */
  166. function update($user=0, $notrigger=0)
  167. {
  168. global $conf, $langs;
  169. $error=0;
  170. // Clean parameters
  171. if (isset($this->nom)) $this->nom=trim($this->nom);
  172. if (isset($this->ref_ext)) $this->ref_ext=trim($this->ref_ext);
  173. if (isset($this->ref_int)) $this->ref_int=trim($this->ref_int);
  174. if (isset($this->fk_phcp_type)) $this->fk_phcp_type=trim($this->fk_phcp_type);
  175. // Check parameters
  176. // Put here code to add a control on parameters values
  177. // Update request
  178. $sql = "UPDATE ".MAIN_DB_PREFIX."phcp SET";
  179. $sql.= " nom=".(isset($this->nom)?"'".$this->db->escape($this->nom)."'":"null").",";
  180. $sql.= " ref_ext=".(isset($this->ref_ext)?"'".$this->db->escape($this->ref_ext)."'":"null").",";
  181. $sql.= " ref_int=".(isset($this->ref_int)?"'".$this->db->escape($this->ref_int)."'":"null").",";
  182. $sql.= " fk_phcp_type=".(isset($this->fk_phcp_type)?$this->fk_phcp_type:"null")."";
  183. $sql.= " WHERE rowid=".$this->id;
  184. $this->db->begin();
  185. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  186. $resql = $this->db->query($sql);
  187. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  188. if (! $error)
  189. {
  190. if (! $notrigger)
  191. {
  192. // Uncomment this and change MYOBJECT to your own tag if you
  193. // want this action calls a trigger.
  194. //// Call triggers
  195. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  196. //$interface=new Interfaces($this->db);
  197. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  198. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  199. //// End call triggers
  200. }
  201. }
  202. // Commit or rollback
  203. if ($error)
  204. {
  205. foreach($this->errors as $errmsg)
  206. {
  207. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  208. $this->error.=($this->error?', '.$errmsg:$errmsg);
  209. }
  210. $this->db->rollback();
  211. return -1*$error;
  212. }
  213. else
  214. {
  215. $this->db->commit();
  216. return 1;
  217. }
  218. }
  219. /**
  220. * Delete object in database
  221. *
  222. * @param User $user User that deletes
  223. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  224. * @return int <0 if KO, >0 if OK
  225. */
  226. function delete($user, $notrigger=0)
  227. {
  228. global $conf, $langs;
  229. $error=0;
  230. $this->db->begin();
  231. if (! $error)
  232. {
  233. if (! $notrigger)
  234. {
  235. // Uncomment this and change MYOBJECT to your own tag if you
  236. // want this action calls a trigger.
  237. //// Call triggers
  238. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  239. //$interface=new Interfaces($this->db);
  240. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  241. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  242. //// End call triggers
  243. }
  244. }
  245. if (! $error)
  246. {
  247. $sql = "DELETE FROM ".MAIN_DB_PREFIX."phcp";
  248. $sql.= " WHERE rowid=".$this->id;
  249. dol_syslog(get_class($this)."::delete sql=".$sql);
  250. $resql = $this->db->query($sql);
  251. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  252. }
  253. // Commit or rollback
  254. if ($error)
  255. {
  256. foreach($this->errors as $errmsg)
  257. {
  258. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  259. $this->error.=($this->error?', '.$errmsg:$errmsg);
  260. }
  261. $this->db->rollback();
  262. return -1*$error;
  263. }
  264. else
  265. {
  266. $this->db->commit();
  267. return 1;
  268. }
  269. }
  270. /**
  271. * Load an object from its id and create a new one in database
  272. *
  273. * @param int $fromid Id of object to clone
  274. * @return int New id of clone
  275. */
  276. function createFromClone($fromid)
  277. {
  278. global $user,$langs;
  279. $error=0;
  280. $object=new Phcp($this->db);
  281. $this->db->begin();
  282. // Load source object
  283. $object->fetch($fromid);
  284. $object->id=0;
  285. $object->statut=0;
  286. // Clear fields
  287. // ...
  288. // Create clone
  289. $result=$object->create($user);
  290. // Other options
  291. if ($result < 0)
  292. {
  293. $this->error=$object->error;
  294. $error++;
  295. }
  296. if (! $error)
  297. {
  298. }
  299. // End
  300. if (! $error)
  301. {
  302. $this->db->commit();
  303. return $object->id;
  304. }
  305. else
  306. {
  307. $this->db->rollback();
  308. return -1;
  309. }
  310. }
  311. /**
  312. * Initialise object with example values
  313. * Id must be 0 if object instance is a specimen
  314. *
  315. * @return void
  316. */
  317. function initAsSpecimen()
  318. {
  319. $this->id=0;
  320. $this->nom='';
  321. $this->ref_ext='';
  322. $this->ref_int='';
  323. $this->fk_phcp_type='';
  324. }
  325. }
  326. ?>