myclass.class.php 8.6 KB

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