dolibarr 21.0.0-alpha
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
82 public $oldcopy;
83
84
90 public function __construct($db)
91 {
92 $this->db = $db;
93 }
94
95
103 public function create($user, $notrigger = 0)
104 {
105 global $user;
106
107 $error = 0;
108
109 // Insert request
110 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
111 $sql .= "description";
112 $sql .= ", datec";
113 $sql .= ", fk_element";
114 $sql .= ", element_type";
115 $sql .= ", fk_user_author";
116 $sql .= ", fk_user_modif";
117 $sql .= ", entity";
118 $sql .= ", import_key";
119 $sql .= ") VALUES (";
120 $sql .= "'".$this->db->escape($this->description)."'";
121 $sql .= ", ".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
122 $sql .= ", '".(isset($this->fk_element) ? $this->fk_element : "null")."'";
123 $sql .= ", '".$this->db->escape($this->element_type)."'";
124 $sql .= ", '".(isset($this->fk_user_author) ? $this->fk_user_author : "null")."'";
125 $sql .= ", ".((int) $user->id);
126 $sql .= ", ".(!empty($this->entity) ? $this->entity : '1');
127 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
128 $sql .= ")";
129
130 //var_dump($this->db);
131 //echo $sql;
132
133 $this->db->begin();
134
135 dol_syslog(get_class($this)."::create", LOG_DEBUG);
136 $resql = $this->db->query($sql);
137 if (!$resql) {
138 $error++;
139 $this->errors[] = "Error ".$this->db->lasterror();
140 }
141
142 if (!$error) {
143 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
144
145 if (!$notrigger) {
146 // Call trigger
147 $result = $this->call_trigger('TASK_COMMENT_CREATE', $user);
148 if ($result < 0) {
149 $error++;
150 }
151 // End call triggers
152 }
153 }
154
155 // Commit or rollback
156 if ($error) {
157 foreach ($this->errors as $errmsg) {
158 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
159 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
160 }
161 $this->db->rollback();
162 return -1 * $error;
163 } else {
164 $this->db->commit();
165 return $this->id;
166 }
167 }
168
169
177 public function fetch($id, $ref = '')
178 {
179 global $langs;
180
181 $sql = "SELECT";
182 $sql .= " c.rowid,";
183 $sql .= " c.description,";
184 $sql .= " c.datec,";
185 $sql .= " c.tms,";
186 $sql .= " c.fk_element,";
187 $sql .= " c.element_type,";
188 $sql .= " c.fk_user_author,";
189 $sql .= " c.fk_user_modif,";
190 $sql .= " c.entity,";
191 $sql .= " c.import_key";
192 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
193 $sql .= " WHERE c.rowid = ".((int) $id);
194
195 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
196 $resql = $this->db->query($sql);
197 if ($resql) {
198 $num_rows = $this->db->num_rows($resql);
199
200 if ($num_rows) {
201 $obj = $this->db->fetch_object($resql);
202
203 $this->id = $obj->rowid;
204 $this->description = $obj->description;
205 $this->element_type = $obj->element_type;
206 $this->datec = $this->db->jdate($obj->datec);
207 $this->tms = $this->db->jdate($obj->tms);
208 $this->fk_user_author = $obj->fk_user_author;
209 $this->fk_user_modif = $obj->fk_user_modif;
210 $this->fk_element = $obj->fk_element;
211 $this->entity = $obj->entity;
212 $this->import_key = $obj->import_key;
213 }
214
215 $this->db->free($resql);
216
217 if ($num_rows) {
218 return 1;
219 } else {
220 return 0;
221 }
222 } else {
223 $this->error = "Error ".$this->db->lasterror();
224 return -1;
225 }
226 }
227
228
236 public function update(User $user, $notrigger = 0)
237 {
238 global $user;
239 $error = 0;
240
241 // Clean parameters
242 if (isset($this->fk_element)) {
243 $this->fk_project = (int) trim((string) $this->fk_element);
244 }
245 if (isset($this->description)) {
246 $this->description = trim($this->description);
247 }
248
249
250 // Update request
251 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
252 $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
253 $sql .= " datec=".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null').",";
254 $sql .= " fk_element=".(isset($this->fk_element) ? $this->fk_element : "null").",";
255 $sql .= " element_type='".$this->db->escape($this->element_type)."',";
256 $sql .= " fk_user_modif=".$user->id.",";
257 $sql .= " entity=".(!empty($this->entity) ? $this->entity : '1').",";
258 $sql .= " import_key=".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
259 $sql .= " WHERE rowid=".((int) $this->id);
260
261 $this->db->begin();
262
263 dol_syslog(get_class($this)."::update", LOG_DEBUG);
264 $resql = $this->db->query($sql);
265 if (!$resql) {
266 $error++;
267 $this->errors[] = "Error ".$this->db->lasterror();
268 }
269
270 if (!$error) {
271 if (!$notrigger) {
272 // Call trigger
273 $result = $this->call_trigger('TASK_COMMENT_MODIFY', $user);
274 if ($result < 0) {
275 $error++;
276 }
277 // End call triggers
278 }
279 }
280
281 // Commit or rollback
282 if ($error) {
283 foreach ($this->errors as $errmsg) {
284 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
285 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
286 }
287 $this->db->rollback();
288 return -1 * $error;
289 } else {
290 $this->db->commit();
291 return 1;
292 }
293 }
294
295
303 public function delete($user, $notrigger = 0)
304 {
305 global $conf, $langs;
306 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
307
308 $error = 0;
309
310 $this->db->begin();
311
312 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
313 $sql .= " WHERE rowid=".((int) $this->id);
314
315 $resql = $this->db->query($sql);
316 if (!$resql) {
317 $error++;
318 $this->errors[] = "Error ".$this->db->lasterror();
319 }
320
321 if (!$error) {
322 if (!$notrigger) {
323 // Call trigger
324 $result = $this->call_trigger('TASK_COMMENT_DELETE', $user);
325 if ($result < 0) {
326 $error++;
327 }
328 // End call triggers
329 }
330 }
331
332 // Commit or rollback
333 if ($error) {
334 foreach ($this->errors as $errmsg) {
335 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
336 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
337 }
338 $this->db->rollback();
339 return -1 * $error;
340 } else {
341 $this->db->commit();
342 return 1;
343 }
344 }
345
346
354 public function fetchAllFor($element_type, $fk_element)
355 {
356 global $db, $conf;
357
358 $this->comments = array();
359
360 if (!empty($element_type) && !empty($fk_element)) {
361 $sql = "SELECT";
362 $sql .= " c.rowid";
363 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
364 $sql .= " WHERE c.fk_element = ".((int) $fk_element);
365 $sql .= " AND c.element_type = '".$this->db->escape($element_type)."'";
366 $sql .= " AND c.entity = ".$conf->entity;
367 $sql .= " ORDER BY c.tms DESC";
368
369 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
370 $resql = $this->db->query($sql);
371 if ($resql) {
372 $num_rows = $this->db->num_rows($resql);
373 if ($num_rows > 0) {
374 while ($obj = $this->db->fetch_object($resql)) {
375 $comment = new self($db);
376 $comment->fetch($obj->rowid);
377 $this->comments[] = $comment;
378 }
379 }
380 $this->db->free($resql);
381 } else {
382 $this->errors[] = "Error ".$this->db->lasterror();
383 return -1;
384 }
385 }
386
387 return count($this->comments);
388 }
389}
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.