skeleton_class.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /* Copyright (C) 2007-2011 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 2 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. * \author Put author name here
  23. * \remarks Put here some comments
  24. */
  25. // Put here all includes required by your class file
  26. //require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
  27. //require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php");
  28. //require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  29. /**
  30. * \class Skeleton_class
  31. * \brief Put here description of your class
  32. * \remarks Put here some comments
  33. */
  34. class Skeleton_class // extends CommonObject
  35. {
  36. var $db; //!< To store db handler
  37. var $error; //!< To return error code (or message)
  38. var $errors=array(); //!< To return several error codes (or messages)
  39. //var $element='skeleton'; //!< Id that identify managed objects
  40. //var $table_element='skeleton'; //!< Name of table without prefix where object is stored
  41. var $id;
  42. var $prop1;
  43. var $prop2;
  44. //...
  45. /**
  46. * Constructor
  47. * @param DB Database handler
  48. */
  49. function Skeleton_class($DB)
  50. {
  51. $this->db = $DB;
  52. return 1;
  53. }
  54. /**
  55. * Create object into database
  56. * @param user User that create
  57. * @param 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->prop1)) $this->prop1=trim($this->prop1);
  66. if (isset($this->prop2)) $this->prop2=trim($this->prop2);
  67. //...
  68. // Check parameters
  69. // Put here code to add control on parameters values
  70. // Insert request
  71. $sql = "INSERT INTO ".MAIN_DB_PREFIX."mytable(";
  72. $sql.= " field1,";
  73. $sql.= " field2";
  74. //...
  75. $sql.= ") VALUES (";
  76. $sql.= " '".$this->prop1."',";
  77. $sql.= " '".$this->prop2."'";
  78. //...
  79. $sql.= ")";
  80. $this->db->begin();
  81. dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
  82. $resql=$this->db->query($sql);
  83. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  84. if (! $error)
  85. {
  86. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mytable");
  87. if (! $notrigger)
  88. {
  89. // Uncomment this and change MYOBJECT to your own tag if you
  90. // want this action call a trigger.
  91. //// Call triggers
  92. //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  93. //$interface=new Interfaces($this->db);
  94. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  95. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  96. //// End call triggers
  97. }
  98. }
  99. // Commit or rollback
  100. if ($error)
  101. {
  102. foreach($this->errors as $errmsg)
  103. {
  104. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  105. $this->error.=($this->error?', '.$errmsg:$errmsg);
  106. }
  107. $this->db->rollback();
  108. return -1*$error;
  109. }
  110. else
  111. {
  112. $this->db->commit();
  113. return $this->id;
  114. }
  115. }
  116. /**
  117. * Load object in memory from database
  118. * @param 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. * @param user User that modify
  156. * @param notrigger 0=launch triggers after, 1=disable triggers
  157. * @return int <0 if KO, >0 if OK
  158. */
  159. function update($user=0, $notrigger=0)
  160. {
  161. global $conf, $langs;
  162. $error=0;
  163. // Clean parameters
  164. if (isset($this->prop1)) $this->prop1=trim($this->prop1);
  165. if (isset($this->prop2)) $this->prop2=trim($this->prop2);
  166. //...
  167. // Check parameters
  168. // Put here code to add control on parameters values
  169. // Update request
  170. $sql = "UPDATE ".MAIN_DB_PREFIX."mytable SET";
  171. $sql.= " field1=".(isset($this->field1)?"'".$this->db->escape($this->field1)."'":"null").",";
  172. $sql.= " field2=".(isset($this->field2)?"'".$this->db->escape($this->field2)."'":"null")."";
  173. //...
  174. $sql.= " WHERE rowid=".$this->id;
  175. $this->db->begin();
  176. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  177. $resql = $this->db->query($sql);
  178. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  179. if (! $error)
  180. {
  181. if (! $notrigger)
  182. {
  183. // Uncomment this and change MYOBJECT to your own tag if you
  184. // want this action call a trigger.
  185. //// Call triggers
  186. //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  187. //$interface=new Interfaces($this->db);
  188. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  189. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  190. //// End call triggers
  191. }
  192. }
  193. // Commit or rollback
  194. if ($error)
  195. {
  196. foreach($this->errors as $errmsg)
  197. {
  198. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  199. $this->error.=($this->error?', '.$errmsg:$errmsg);
  200. }
  201. $this->db->rollback();
  202. return -1*$error;
  203. }
  204. else
  205. {
  206. $this->db->commit();
  207. return 1;
  208. }
  209. }
  210. /**
  211. * Delete object in database
  212. * @param user User that delete
  213. * @param notrigger 0=launch triggers after, 1=disable triggers
  214. * @return int <0 if KO, >0 if OK
  215. */
  216. function delete($user, $notrigger=0)
  217. {
  218. global $conf, $langs;
  219. $error=0;
  220. $sql = "DELETE FROM ".MAIN_DB_PREFIX."mytable";
  221. $sql.= " WHERE rowid=".$this->id;
  222. $this->db->begin();
  223. dol_syslog(get_class($this)."::delete sql=".$sql);
  224. $resql = $this->db->query($sql);
  225. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  226. if (! $error)
  227. {
  228. if (! $notrigger)
  229. {
  230. // Uncomment this and change MYOBJECT to your own tag if you
  231. // want this action call a trigger.
  232. //// Call triggers
  233. //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
  234. //$interface=new Interfaces($this->db);
  235. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  236. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  237. //// End call triggers
  238. }
  239. }
  240. // Commit or rollback
  241. if ($error)
  242. {
  243. foreach($this->errors as $errmsg)
  244. {
  245. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  246. $this->error.=($this->error?', '.$errmsg:$errmsg);
  247. }
  248. $this->db->rollback();
  249. return -1*$error;
  250. }
  251. else
  252. {
  253. $this->db->commit();
  254. return 1;
  255. }
  256. }
  257. /**
  258. * Load an object from its id and create a new one in database
  259. * @param fromid Id of object to clone
  260. * @return int New id of clone
  261. */
  262. function createFromClone($fromid)
  263. {
  264. global $user,$langs;
  265. $error=0;
  266. $object=new Skeleton_class($this->db);
  267. $this->db->begin();
  268. // Load source object
  269. $object->fetch($fromid);
  270. $object->id=0;
  271. $object->statut=0;
  272. // Clear fields
  273. // ...
  274. // Create clone
  275. $result=$object->create($user);
  276. // Other options
  277. if ($result < 0)
  278. {
  279. $this->error=$object->error;
  280. $error++;
  281. }
  282. if (! $error)
  283. {
  284. }
  285. // End
  286. if (! $error)
  287. {
  288. $this->db->commit();
  289. return $object->id;
  290. }
  291. else
  292. {
  293. $this->db->rollback();
  294. return -1;
  295. }
  296. }
  297. /**
  298. * Initialisz object with example values
  299. * Id must be 0 if object instance is a specimen.
  300. */
  301. function initAsSpecimen()
  302. {
  303. $this->id=0;
  304. $this->prop1='prop1';
  305. $this->prop2='prop2';
  306. }
  307. }
  308. ?>