skeleton_class.class.php 9.3 KB

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