comment.class.php 8.8 KB

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