dolibarr 21.0.0-beta
comment.class.php
1<?php
2/* Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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 <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
23class Comment extends CommonObject
24{
28 public $element = 'comment';
29
33 public $table_element = 'comment';
34
38 public $fk_element;
39
43 public $element_type;
44
48 public $description;
49
55 public $datec;
56
60 public $fk_user_author;
61
65 public $fk_user_modif;
66
70 public $entity;
71
75 public $import_key;
76
77 public $comments = array();
78
79
85 public function __construct($db)
86 {
87 $this->db = $db;
88 }
89
90
98 public function create($user, $notrigger = 0)
99 {
100 global $user;
101
102 $error = 0;
103
104 // Insert request
105 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
106 $sql .= "description";
107 $sql .= ", datec";
108 $sql .= ", fk_element";
109 $sql .= ", element_type";
110 $sql .= ", fk_user_author";
111 $sql .= ", fk_user_modif";
112 $sql .= ", entity";
113 $sql .= ", import_key";
114 $sql .= ") VALUES (";
115 $sql .= "'".$this->db->escape($this->description)."'";
116 $sql .= ", ".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
117 $sql .= ", '".(isset($this->fk_element) ? $this->fk_element : "null")."'";
118 $sql .= ", '".$this->db->escape($this->element_type)."'";
119 $sql .= ", '".(isset($this->fk_user_author) ? $this->fk_user_author : "null")."'";
120 $sql .= ", ".((int) $user->id);
121 $sql .= ", ".(!empty($this->entity) ? $this->entity : '1');
122 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
123 $sql .= ")";
124
125 //var_dump($this->db);
126 //echo $sql;
127
128 $this->db->begin();
129
130 dol_syslog(get_class($this)."::create", LOG_DEBUG);
131 $resql = $this->db->query($sql);
132 if (!$resql) {
133 $error++;
134 $this->errors[] = "Error ".$this->db->lasterror();
135 }
136
137 if (!$error) {
138 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
139
140 if (!$notrigger) {
141 // Call trigger
142 $result = $this->call_trigger('TASK_COMMENT_CREATE', $user);
143 if ($result < 0) {
144 $error++;
145 }
146 // End call triggers
147 }
148 }
149
150 // Commit or rollback
151 if ($error) {
152 foreach ($this->errors as $errmsg) {
153 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
154 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
155 }
156 $this->db->rollback();
157 return -1 * $error;
158 } else {
159 $this->db->commit();
160 return $this->id;
161 }
162 }
163
164
172 public function fetch($id, $ref = '')
173 {
174 global $langs;
175
176 $sql = "SELECT";
177 $sql .= " c.rowid,";
178 $sql .= " c.description,";
179 $sql .= " c.datec,";
180 $sql .= " c.tms,";
181 $sql .= " c.fk_element,";
182 $sql .= " c.element_type,";
183 $sql .= " c.fk_user_author,";
184 $sql .= " c.fk_user_modif,";
185 $sql .= " c.entity,";
186 $sql .= " c.import_key";
187 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
188 $sql .= " WHERE c.rowid = ".((int) $id);
189
190 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
191 $resql = $this->db->query($sql);
192 if ($resql) {
193 $num_rows = $this->db->num_rows($resql);
194
195 if ($num_rows) {
196 $obj = $this->db->fetch_object($resql);
197
198 $this->id = $obj->rowid;
199 $this->description = $obj->description;
200 $this->element_type = $obj->element_type;
201 $this->datec = $this->db->jdate($obj->datec);
202 $this->tms = $this->db->jdate($obj->tms);
203 $this->fk_user_author = $obj->fk_user_author;
204 $this->fk_user_modif = $obj->fk_user_modif;
205 $this->fk_element = $obj->fk_element;
206 $this->entity = $obj->entity;
207 $this->import_key = $obj->import_key;
208 }
209
210 $this->db->free($resql);
211
212 if ($num_rows) {
213 return 1;
214 } else {
215 return 0;
216 }
217 } else {
218 $this->error = "Error ".$this->db->lasterror();
219 return -1;
220 }
221 }
222
223
231 public function update(User $user, $notrigger = 0)
232 {
233 global $user;
234 $error = 0;
235
236 // Clean parameters
237 if (isset($this->fk_element)) {
238 $this->fk_project = (int) trim((string) $this->fk_element);
239 }
240 if (isset($this->description)) {
241 $this->description = trim($this->description);
242 }
243
244
245 // Update request
246 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
247 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
248 $sql .= " datec = ".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null').",";
249 $sql .= " fk_element = ".(isset($this->fk_element) ? $this->fk_element : "null").",";
250 $sql .= " element_type = '".$this->db->escape($this->element_type)."',";
251 $sql .= " fk_user_modif = ".((int) $user->id).",";
252 $sql .= " entity = ".(!empty($this->entity) ? $this->entity : '1').",";
253 $sql .= " import_key = ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
254 $sql .= " WHERE rowid = ".((int) $this->id);
255
256 $this->db->begin();
257
258 dol_syslog(get_class($this)."::update", LOG_DEBUG);
259 $resql = $this->db->query($sql);
260 if (!$resql) {
261 $error++;
262 $this->errors[] = "Error ".$this->db->lasterror();
263 }
264
265 if (!$error) {
266 if (!$notrigger) {
267 // Call trigger
268 $result = $this->call_trigger('TASK_COMMENT_MODIFY', $user);
269 if ($result < 0) {
270 $error++;
271 }
272 // End call triggers
273 }
274 }
275
276 // Commit or rollback
277 if ($error) {
278 foreach ($this->errors as $errmsg) {
279 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
280 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
281 }
282 $this->db->rollback();
283 return -1 * $error;
284 } else {
285 $this->db->commit();
286 return 1;
287 }
288 }
289
290
298 public function delete($user, $notrigger = 0)
299 {
300 global $conf, $langs;
301 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
302
303 $error = 0;
304
305 $this->db->begin();
306
307 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
308 $sql .= " WHERE rowid=".((int) $this->id);
309
310 $resql = $this->db->query($sql);
311 if (!$resql) {
312 $error++;
313 $this->errors[] = "Error ".$this->db->lasterror();
314 }
315
316 if (!$error) {
317 if (!$notrigger) {
318 // Call trigger
319 $result = $this->call_trigger('TASK_COMMENT_DELETE', $user);
320 if ($result < 0) {
321 $error++;
322 }
323 // End call triggers
324 }
325 }
326
327 // Commit or rollback
328 if ($error) {
329 foreach ($this->errors as $errmsg) {
330 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
331 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
332 }
333 $this->db->rollback();
334 return -1 * $error;
335 } else {
336 $this->db->commit();
337 return 1;
338 }
339 }
340
341
349 public function fetchAllFor($element_type, $fk_element)
350 {
351 global $db, $conf;
352
353 $this->comments = array();
354
355 if (!empty($element_type) && !empty($fk_element)) {
356 $sql = "SELECT";
357 $sql .= " c.rowid";
358 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
359 $sql .= " WHERE c.fk_element = ".((int) $fk_element);
360 $sql .= " AND c.element_type = '".$this->db->escape($element_type)."'";
361 $sql .= " AND c.entity = ".$conf->entity;
362 $sql .= " ORDER BY c.tms DESC";
363
364 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
365 $resql = $this->db->query($sql);
366 if ($resql) {
367 $num_rows = $this->db->num_rows($resql);
368 if ($num_rows > 0) {
369 while ($obj = $this->db->fetch_object($resql)) {
370 $comment = new self($db);
371 $comment->fetch($obj->rowid);
372 $this->comments[] = $comment;
373 }
374 }
375 $this->db->free($resql);
376 } else {
377 $this->errors[] = "Error ".$this->db->lasterror();
378 return -1;
379 }
380 }
381
382 return count($this->comments);
383 }
384}
Class to manage comment.
__construct($db)
Constructor.
create($user, $notrigger=0)
Create into database.
fetch($id, $ref='')
Load object in memory from database.
update(User $user, $notrigger=0)
Update database.
fetchAllFor($element_type, $fk_element)
Load comments linked with current task into ->comments.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79