ticketlogs.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. /* Copyright (C) - 2013-2016 Jean-François FERRY <hello@librethic.io>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file ticket/class/ticketlogs.class.php
  19. * \ingroup ticket
  20. * \brief This file CRUD class file (Create/Read/Update/Delete) for ticket logs
  21. */
  22. // Put here all includes required by your class file
  23. require_once DOL_DOCUMENT_ROOT . "/core/class/commonobject.class.php";
  24. //require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
  25. //require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
  26. /**
  27. * Class of log for ticket
  28. */
  29. class Ticketlogs// extends CommonObject
  30. {
  31. /**
  32. * @var DoliDB Database handler.
  33. */
  34. public $db;
  35. /**
  36. * @var string Error code (or message)
  37. */
  38. public $error;
  39. /**
  40. * @var string[] Error codes (or messages)
  41. */
  42. public $errors = array();
  43. /**
  44. * @var string ID to identify managed object
  45. */
  46. public $element = 'ticketlogs';
  47. /**
  48. * @var string Name of table without prefix where object is stored
  49. */
  50. public $table_element = 'ticketlogs'; //!< Name of table without prefix where object is stored
  51. /**
  52. * @var int ID
  53. */
  54. public $id;
  55. public $fk_track_id;
  56. public $fk_user_create;
  57. public $datec = '';
  58. public $message;
  59. /**
  60. * Constructor
  61. *
  62. * @param DoliDb $db Database handler
  63. */
  64. public function __construct($db)
  65. {
  66. $this->db = $db;
  67. }
  68. /**
  69. * Create object into database
  70. *
  71. * @param User $user User that creates
  72. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  73. * @return int <0 if KO, Id of created object if OK
  74. */
  75. public function create($user, $notrigger = 0)
  76. {
  77. global $conf, $langs;
  78. $error = 0;
  79. // Clean parameters
  80. if (isset($this->fk_track_id)) {
  81. $this->fk_track_id = trim($this->fk_track_id);
  82. }
  83. if (isset($this->fk_user_create)) {
  84. $this->fk_user_create = trim($this->fk_user_create);
  85. }
  86. if (isset($this->message)) {
  87. $this->message = trim($this->message);
  88. }
  89. // Check parameters
  90. // Put here code to add control on parameters values
  91. // Insert request
  92. $sql = "INSERT INTO " . MAIN_DB_PREFIX . "ticket_logs(";
  93. $sql .= "fk_track_id,";
  94. $sql .= "fk_user_create,";
  95. $sql .= "datec,";
  96. $sql .= "message";
  97. $sql .= ") VALUES (";
  98. $sql .= " " . (!isset($this->fk_track_id) ? 'NULL' : "'" . $this->db->escape($this->fk_track_id) . "'") . ",";
  99. $sql .= " " . (!isset($this->fk_user_create) ? 'NULL' : "'" . $this->db->escape($this->fk_user_create) . "'") . ",";
  100. $sql .= " " . (!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'" . $this->db->idate($this->datec). "'") . ",";
  101. $sql .= " " . (!isset($this->message) ? 'NULL' : "'" . $this->db->escape($this->message) . "'") . "";
  102. $sql .= ")";
  103. $this->db->begin();
  104. dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG);
  105. $resql = $this->db->query($sql);
  106. if (!$resql) {
  107. $error++;
  108. $this->errors[] = "Error " . $this->db->lasterror();
  109. }
  110. if (!$error) {
  111. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "ticket_logs");
  112. if (!$notrigger) {
  113. // Uncomment this and change MYOBJECT to your own tag if you
  114. // want this action calls a trigger.
  115. //// Call triggers
  116. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  117. //$interface=new Interfaces($this->db);
  118. //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
  119. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  120. //// End call triggers
  121. }
  122. }
  123. // Commit or rollback
  124. if ($error) {
  125. foreach ($this->errors as $errmsg) {
  126. dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
  127. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  128. }
  129. $this->db->rollback();
  130. return -1 * $error;
  131. } else {
  132. $this->db->commit();
  133. return $this->id;
  134. }
  135. }
  136. /**
  137. * Load object in memory from the database
  138. *
  139. * @param int $id Id object
  140. * @return int <0 if KO, >0 if OK
  141. */
  142. public function fetch($id)
  143. {
  144. global $langs;
  145. $sql = "SELECT";
  146. $sql .= " t.rowid,";
  147. $sql .= " t.fk_track_id,";
  148. $sql .= " t.fk_user_create,";
  149. $sql .= " t.datec,";
  150. $sql .= " t.message";
  151. $sql .= " FROM " . MAIN_DB_PREFIX . "ticket_logs as t";
  152. $sql .= " WHERE t.rowid = " . $id;
  153. dol_syslog(get_class($this) . "::fetch sql=" . $sql, LOG_DEBUG);
  154. $resql = $this->db->query($sql);
  155. if ($resql) {
  156. if ($this->db->num_rows($resql)) {
  157. $obj = $this->db->fetch_object($resql);
  158. $this->id = $obj->rowid;
  159. $this->fk_track_id = $obj->fk_track_id;
  160. $this->fk_user_create = $obj->fk_user_create;
  161. $this->datec = $this->db->jdate($obj->datec);
  162. $this->message = $obj->message;
  163. }
  164. $this->db->free($resql);
  165. return 1;
  166. } else {
  167. $this->error = "Error " . $this->db->lasterror();
  168. dol_syslog(get_class($this) . "::fetch " . $this->error, LOG_ERR);
  169. return -1;
  170. }
  171. }
  172. /**
  173. * Update object into database
  174. *
  175. * @param User $user User that modifies
  176. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  177. * @return int <0 if KO, >0 if OK
  178. */
  179. public function update($user = 0, $notrigger = 0)
  180. {
  181. global $conf, $langs;
  182. $error = 0;
  183. // Clean parameters
  184. if (isset($this->fk_track_id)) {
  185. $this->fk_track_id = trim($this->fk_track_id);
  186. }
  187. if (isset($this->fk_user_create)) {
  188. $this->fk_user_create = trim($this->fk_user_create);
  189. }
  190. if (isset($this->message)) {
  191. $this->message = trim($this->message);
  192. }
  193. // Check parameters
  194. // Put here code to add a control on parameters values
  195. // Update request
  196. $sql = "UPDATE " . MAIN_DB_PREFIX . "ticket_logs SET";
  197. $sql .= " fk_track_id=" . (isset($this->fk_track_id) ? "'" . $this->db->escape($this->fk_track_id) . "'" : "null") . ",";
  198. $sql .= " fk_user_create=" . ($this->fk_user_create > 0 ? $this->fk_user_create : "null") . ",";
  199. $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ",";
  200. $sql .= " message=" . (isset($this->message) ? "'" . $this->db->escape($this->message) . "'" : "null") . "";
  201. $sql .= " WHERE rowid=" . $this->id;
  202. $this->db->begin();
  203. dol_syslog(get_class($this) . "::update sql=" . $sql, LOG_DEBUG);
  204. $resql = $this->db->query($sql);
  205. if (!$resql) {
  206. $error++;
  207. $this->errors[] = "Error " . $this->db->lasterror();
  208. }
  209. if (!$error) {
  210. if (!$notrigger) {
  211. // Uncomment this and change MYOBJECT to your own tag if you
  212. // want this action calls a trigger.
  213. //// Call triggers
  214. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  215. //$interface=new Interfaces($this->db);
  216. //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
  217. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  218. //// End call triggers
  219. }
  220. }
  221. // Commit or rollback
  222. if ($error) {
  223. foreach ($this->errors as $errmsg) {
  224. dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
  225. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  226. }
  227. $this->db->rollback();
  228. return -1 * $error;
  229. } else {
  230. $this->db->commit();
  231. return 1;
  232. }
  233. }
  234. /**
  235. * Delete object in database
  236. *
  237. * @param User $user User that deletes
  238. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  239. * @return int <0 if KO, >0 if OK
  240. */
  241. public function delete($user, $notrigger = 0)
  242. {
  243. global $conf, $langs;
  244. $error = 0;
  245. $this->db->begin();
  246. if (!$error) {
  247. if (!$notrigger) {
  248. // Uncomment this and change MYOBJECT to your own tag if you
  249. // want this action calls a trigger.
  250. //// Call triggers
  251. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  252. //$interface=new Interfaces($this->db);
  253. //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
  254. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  255. //// End call triggers
  256. }
  257. }
  258. if (!$error) {
  259. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "ticket_logs";
  260. $sql .= " WHERE rowid=" . $this->id;
  261. dol_syslog(get_class($this) . "::delete sql=" . $sql);
  262. $resql = $this->db->query($sql);
  263. if (!$resql) {
  264. $error++;
  265. $this->errors[] = "Error " . $this->db->lasterror();
  266. }
  267. }
  268. // Commit or rollback
  269. if ($error) {
  270. foreach ($this->errors as $errmsg) {
  271. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  272. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  273. }
  274. $this->db->rollback();
  275. return -1 * $error;
  276. } else {
  277. $this->db->commit();
  278. return 1;
  279. }
  280. }
  281. /**
  282. * Initialise object with example values
  283. * Id must be 0 if object instance is a specimen
  284. *
  285. * @return void
  286. */
  287. public function initAsSpecimen()
  288. {
  289. $this->id = 0;
  290. $this->fk_track_id = '';
  291. $this->fk_user_create = '';
  292. $this->datec = '';
  293. $this->message = '';
  294. }
  295. }