comment.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * Class to manage comment
  20. */
  21. class Comment extends CommonObject
  22. {
  23. /**
  24. * @var string ID to identify managed object
  25. */
  26. public $element = 'comment';
  27. /**
  28. * @var string Name of table without prefix where object is stored
  29. */
  30. public $table_element = 'comment';
  31. /**
  32. * @var int ID of parent key (it's not the name of a field)
  33. */
  34. public $fk_element;
  35. /**
  36. * @var string element type
  37. */
  38. public $element_type;
  39. /**
  40. * @var string description
  41. */
  42. public $description;
  43. /**
  44. * Date modification record (tms)
  45. *
  46. * @var integer
  47. */
  48. public $tms;
  49. /**
  50. * Date creation record (datec)
  51. *
  52. * @var integer
  53. */
  54. public $datec;
  55. /**
  56. * @var int ID
  57. */
  58. public $fk_user_author;
  59. /**
  60. * @var int ID
  61. */
  62. public $fk_user_modif;
  63. /**
  64. * @var int Entity
  65. */
  66. public $entity;
  67. /**
  68. * @var string import key
  69. */
  70. public $import_key;
  71. public $comments = array();
  72. /**
  73. * @var Comment Object oldcopy
  74. */
  75. public $oldcopy;
  76. /**
  77. * Constructor
  78. *
  79. * @param DoliDB $db Database handler
  80. */
  81. public function __construct($db)
  82. {
  83. $this->db = $db;
  84. }
  85. /**
  86. * Create into database
  87. *
  88. * @param User $user User that create
  89. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  90. * @return int <0 if KO, Id of created object if OK
  91. */
  92. public function create($user, $notrigger = 0)
  93. {
  94. global $user;
  95. $error = 0;
  96. // Insert request
  97. $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
  98. $sql .= "description";
  99. $sql .= ", datec";
  100. $sql .= ", fk_element";
  101. $sql .= ", element_type";
  102. $sql .= ", fk_user_author";
  103. $sql .= ", fk_user_modif";
  104. $sql .= ", entity";
  105. $sql .= ", import_key";
  106. $sql .= ") VALUES (";
  107. $sql .= "'".$this->db->escape($this->description)."'";
  108. $sql .= ", ".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
  109. $sql .= ", '".(isset($this->fk_element) ? $this->fk_element : "null")."'";
  110. $sql .= ", '".$this->db->escape($this->element_type)."'";
  111. $sql .= ", '".(isset($this->fk_user_author) ? $this->fk_user_author : "null")."'";
  112. $sql .= ", ".((int) $user->id);
  113. $sql .= ", ".(!empty($this->entity) ? $this->entity : '1');
  114. $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  115. $sql .= ")";
  116. //var_dump($this->db);
  117. //echo $sql;
  118. $this->db->begin();
  119. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  120. $resql = $this->db->query($sql);
  121. if (!$resql) {
  122. $error++;
  123. $this->errors[] = "Error ".$this->db->lasterror();
  124. }
  125. if (!$error) {
  126. $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
  127. if (!$notrigger) {
  128. // Call trigger
  129. $result = $this->call_trigger('TASK_COMMENT_CREATE', $user);
  130. if ($result < 0) {
  131. $error++;
  132. }
  133. // End call triggers
  134. }
  135. }
  136. // Commit or rollback
  137. if ($error) {
  138. foreach ($this->errors as $errmsg) {
  139. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  140. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  141. }
  142. $this->db->rollback();
  143. return -1 * $error;
  144. } else {
  145. $this->db->commit();
  146. return $this->id;
  147. }
  148. }
  149. /**
  150. * Load object in memory from database
  151. *
  152. * @param int $id Id object
  153. * @param int $ref ref object
  154. * @return int <0 if KO, 0 if not found, >0 if OK
  155. */
  156. public function fetch($id, $ref = '')
  157. {
  158. global $langs;
  159. $sql = "SELECT";
  160. $sql .= " c.rowid,";
  161. $sql .= " c.description,";
  162. $sql .= " c.datec,";
  163. $sql .= " c.tms,";
  164. $sql .= " c.fk_element,";
  165. $sql .= " c.element_type,";
  166. $sql .= " c.fk_user_author,";
  167. $sql .= " c.fk_user_modif,";
  168. $sql .= " c.entity,";
  169. $sql .= " c.import_key";
  170. $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
  171. $sql .= " WHERE c.rowid = ".((int) $id);
  172. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  173. $resql = $this->db->query($sql);
  174. if ($resql) {
  175. $num_rows = $this->db->num_rows($resql);
  176. if ($num_rows) {
  177. $obj = $this->db->fetch_object($resql);
  178. $this->id = $obj->rowid;
  179. $this->description = $obj->description;
  180. $this->element_type = $obj->element_type;
  181. $this->datec = $this->db->jdate($obj->datec);
  182. $this->tms = $this->db->jdate($obj->tms);
  183. $this->fk_user_author = $obj->fk_user_author;
  184. $this->fk_user_modif = $obj->fk_user_modif;
  185. $this->fk_element = $obj->fk_element;
  186. $this->entity = $obj->entity;
  187. $this->import_key = $obj->import_key;
  188. }
  189. $this->db->free($resql);
  190. if ($num_rows) {
  191. return 1;
  192. } else {
  193. return 0;
  194. }
  195. } else {
  196. $this->error = "Error ".$this->db->lasterror();
  197. return -1;
  198. }
  199. }
  200. /**
  201. * Update database
  202. *
  203. * @param User $user User that modify
  204. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  205. * @return int <=0 if KO, >0 if OK
  206. */
  207. public function update(User $user, $notrigger = 0)
  208. {
  209. global $user;
  210. $error = 0;
  211. // Clean parameters
  212. if (isset($this->fk_element)) {
  213. $this->fk_project = (int) trim($this->fk_element);
  214. }
  215. if (isset($this->description)) {
  216. $this->description = trim($this->description);
  217. }
  218. // Update request
  219. $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
  220. $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
  221. $sql .= " datec=".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  222. $sql .= " fk_element=".(isset($this->fk_element) ? $this->fk_element : "null").",";
  223. $sql .= " element_type='".$this->db->escape($this->element_type)."',";
  224. $sql .= " fk_user_modif=".$user->id.",";
  225. $sql .= " entity=".(!empty($this->entity) ? $this->entity : '1').",";
  226. $sql .= " import_key=".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  227. $sql .= " WHERE rowid=".((int) $this->id);
  228. $this->db->begin();
  229. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  230. $resql = $this->db->query($sql);
  231. if (!$resql) {
  232. $error++;
  233. $this->errors[] = "Error ".$this->db->lasterror();
  234. }
  235. if (!$error) {
  236. if (!$notrigger) {
  237. // Call trigger
  238. $result = $this->call_trigger('TASK_COMMENT_MODIFY', $user);
  239. if ($result < 0) {
  240. $error++;
  241. }
  242. // End call triggers
  243. }
  244. }
  245. // Commit or rollback
  246. if ($error) {
  247. foreach ($this->errors as $errmsg) {
  248. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  249. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  250. }
  251. $this->db->rollback();
  252. return -1 * $error;
  253. } else {
  254. $this->db->commit();
  255. return 1;
  256. }
  257. }
  258. /**
  259. * Delete task from database
  260. *
  261. * @param User $user User that delete
  262. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  263. * @return int <0 if KO, >0 if OK
  264. */
  265. public function delete($user, $notrigger = 0)
  266. {
  267. global $conf, $langs;
  268. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  269. $error = 0;
  270. $this->db->begin();
  271. $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
  272. $sql .= " WHERE rowid=".((int) $this->id);
  273. $resql = $this->db->query($sql);
  274. if (!$resql) {
  275. $error++;
  276. $this->errors[] = "Error ".$this->db->lasterror();
  277. }
  278. if (!$error) {
  279. if (!$notrigger) {
  280. // Call trigger
  281. $result = $this->call_trigger('TASK_COMMENT_DELETE', $user);
  282. if ($result < 0) {
  283. $error++;
  284. }
  285. // End call triggers
  286. }
  287. }
  288. // Commit or rollback
  289. if ($error) {
  290. foreach ($this->errors as $errmsg) {
  291. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  292. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  293. }
  294. $this->db->rollback();
  295. return -1 * $error;
  296. } else {
  297. $this->db->commit();
  298. return 1;
  299. }
  300. }
  301. /**
  302. * Load comments linked with current task
  303. *
  304. * @param string $element_type Element type
  305. * @param int $fk_element Id of element
  306. * @return array Comment array
  307. */
  308. public function fetchAllFor($element_type, $fk_element)
  309. {
  310. global $db, $conf;
  311. $this->comments = array();
  312. if (!empty($element_type) && !empty($fk_element)) {
  313. $sql = "SELECT";
  314. $sql .= " c.rowid";
  315. $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
  316. $sql .= " WHERE c.fk_element = ".((int) $fk_element);
  317. $sql .= " AND c.element_type = '".$this->db->escape($element_type)."'";
  318. $sql .= " AND c.entity = ".$conf->entity;
  319. $sql .= " ORDER BY c.tms DESC";
  320. dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
  321. $resql = $this->db->query($sql);
  322. if ($resql) {
  323. $num_rows = $this->db->num_rows($resql);
  324. if ($num_rows > 0) {
  325. while ($obj = $this->db->fetch_object($resql)) {
  326. $comment = new self($db);
  327. $comment->fetch($obj->rowid);
  328. $this->comments[] = $comment;
  329. }
  330. }
  331. $this->db->free($resql);
  332. } else {
  333. $this->errors[] = "Error ".$this->db->lasterror();
  334. return -1;
  335. }
  336. }
  337. return count($this->comments);
  338. }
  339. }