No Description

myclass.class.php 10KB

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