dolibarr 18.0.6
comment.class.php
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
22class Comment extends CommonObject
23{
27 public $element = 'comment';
28
32 public $table_element = 'comment';
33
37 public $fk_element;
38
42 public $element_type;
43
47 public $description;
48
54 public $tms;
55
61 public $datec;
62
66 public $fk_user_author;
67
71 public $fk_user_modif;
72
76 public $entity;
77
81 public $import_key;
82
83 public $comments = array();
84
88 public $oldcopy;
89
90
96 public function __construct($db)
97 {
98 $this->db = $db;
99 }
100
101
109 public function create($user, $notrigger = 0)
110 {
111 global $user;
112
113 $error = 0;
114
115 // Insert request
116 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
117 $sql .= "description";
118 $sql .= ", datec";
119 $sql .= ", fk_element";
120 $sql .= ", element_type";
121 $sql .= ", fk_user_author";
122 $sql .= ", fk_user_modif";
123 $sql .= ", entity";
124 $sql .= ", import_key";
125 $sql .= ") VALUES (";
126 $sql .= "'".$this->db->escape($this->description)."'";
127 $sql .= ", ".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null');
128 $sql .= ", '".(isset($this->fk_element) ? $this->fk_element : "null")."'";
129 $sql .= ", '".$this->db->escape($this->element_type)."'";
130 $sql .= ", '".(isset($this->fk_user_author) ? $this->fk_user_author : "null")."'";
131 $sql .= ", ".((int) $user->id);
132 $sql .= ", ".(!empty($this->entity) ? $this->entity : '1');
133 $sql .= ", ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
134 $sql .= ")";
135
136 //var_dump($this->db);
137 //echo $sql;
138
139 $this->db->begin();
140
141 dol_syslog(get_class($this)."::create", LOG_DEBUG);
142 $resql = $this->db->query($sql);
143 if (!$resql) {
144 $error++;
145 $this->errors[] = "Error ".$this->db->lasterror();
146 }
147
148 if (!$error) {
149 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
150
151 if (!$notrigger) {
152 // Call trigger
153 $result = $this->call_trigger('TASK_COMMENT_CREATE', $user);
154 if ($result < 0) {
155 $error++;
156 }
157 // End call triggers
158 }
159 }
160
161 // Commit or rollback
162 if ($error) {
163 foreach ($this->errors as $errmsg) {
164 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
165 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
166 }
167 $this->db->rollback();
168 return -1 * $error;
169 } else {
170 $this->db->commit();
171 return $this->id;
172 }
173 }
174
175
183 public function fetch($id, $ref = '')
184 {
185 global $langs;
186
187 $sql = "SELECT";
188 $sql .= " c.rowid,";
189 $sql .= " c.description,";
190 $sql .= " c.datec,";
191 $sql .= " c.tms,";
192 $sql .= " c.fk_element,";
193 $sql .= " c.element_type,";
194 $sql .= " c.fk_user_author,";
195 $sql .= " c.fk_user_modif,";
196 $sql .= " c.entity,";
197 $sql .= " c.import_key";
198 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
199 $sql .= " WHERE c.rowid = ".((int) $id);
200
201 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
202 $resql = $this->db->query($sql);
203 if ($resql) {
204 $num_rows = $this->db->num_rows($resql);
205
206 if ($num_rows) {
207 $obj = $this->db->fetch_object($resql);
208
209 $this->id = $obj->rowid;
210 $this->description = $obj->description;
211 $this->element_type = $obj->element_type;
212 $this->datec = $this->db->jdate($obj->datec);
213 $this->tms = $this->db->jdate($obj->tms);
214 $this->fk_user_author = $obj->fk_user_author;
215 $this->fk_user_modif = $obj->fk_user_modif;
216 $this->fk_element = $obj->fk_element;
217 $this->entity = $obj->entity;
218 $this->import_key = $obj->import_key;
219 }
220
221 $this->db->free($resql);
222
223 if ($num_rows) {
224 return 1;
225 } else {
226 return 0;
227 }
228 } else {
229 $this->error = "Error ".$this->db->lasterror();
230 return -1;
231 }
232 }
233
234
242 public function update(User $user, $notrigger = 0)
243 {
244 global $user;
245 $error = 0;
246
247 // Clean parameters
248 if (isset($this->fk_element)) {
249 $this->fk_project = (int) trim($this->fk_element);
250 }
251 if (isset($this->description)) {
252 $this->description = trim($this->description);
253 }
254
255
256 // Update request
257 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
258 $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
259 $sql .= " datec=".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null').",";
260 $sql .= " fk_element=".(isset($this->fk_element) ? $this->fk_element : "null").",";
261 $sql .= " element_type='".$this->db->escape($this->element_type)."',";
262 $sql .= " fk_user_modif=".$user->id.",";
263 $sql .= " entity=".(!empty($this->entity) ? $this->entity : '1').",";
264 $sql .= " import_key=".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
265 $sql .= " WHERE rowid=".((int) $this->id);
266
267 $this->db->begin();
268
269 dol_syslog(get_class($this)."::update", LOG_DEBUG);
270 $resql = $this->db->query($sql);
271 if (!$resql) {
272 $error++;
273 $this->errors[] = "Error ".$this->db->lasterror();
274 }
275
276 if (!$error) {
277 if (!$notrigger) {
278 // Call trigger
279 $result = $this->call_trigger('TASK_COMMENT_MODIFY', $user);
280 if ($result < 0) {
281 $error++;
282 }
283 // End call triggers
284 }
285 }
286
287 // Commit or rollback
288 if ($error) {
289 foreach ($this->errors as $errmsg) {
290 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
291 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
292 }
293 $this->db->rollback();
294 return -1 * $error;
295 } else {
296 $this->db->commit();
297 return 1;
298 }
299 }
300
301
309 public function delete($user, $notrigger = 0)
310 {
311 global $conf, $langs;
312 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
313
314 $error = 0;
315
316 $this->db->begin();
317
318 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
319 $sql .= " WHERE rowid=".((int) $this->id);
320
321 $resql = $this->db->query($sql);
322 if (!$resql) {
323 $error++;
324 $this->errors[] = "Error ".$this->db->lasterror();
325 }
326
327 if (!$error) {
328 if (!$notrigger) {
329 // Call trigger
330 $result = $this->call_trigger('TASK_COMMENT_DELETE', $user);
331 if ($result < 0) {
332 $error++;
333 }
334 // End call triggers
335 }
336 }
337
338 // Commit or rollback
339 if ($error) {
340 foreach ($this->errors as $errmsg) {
341 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
342 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
343 }
344 $this->db->rollback();
345 return -1 * $error;
346 } else {
347 $this->db->commit();
348 return 1;
349 }
350 }
351
352
360 public function fetchAllFor($element_type, $fk_element)
361 {
362 global $db, $conf;
363
364 $this->comments = array();
365
366 if (!empty($element_type) && !empty($fk_element)) {
367 $sql = "SELECT";
368 $sql .= " c.rowid";
369 $sql .= " FROM ".$this->db->prefix().$this->table_element." as c";
370 $sql .= " WHERE c.fk_element = ".((int) $fk_element);
371 $sql .= " AND c.element_type = '".$this->db->escape($element_type)."'";
372 $sql .= " AND c.entity = ".$conf->entity;
373 $sql .= " ORDER BY c.tms DESC";
374
375 dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
376 $resql = $this->db->query($sql);
377 if ($resql) {
378 $num_rows = $this->db->num_rows($resql);
379 if ($num_rows > 0) {
380 while ($obj = $this->db->fetch_object($resql)) {
381 $comment = new self($db);
382 $comment->fetch($obj->rowid);
383 $this->comments[] = $comment;
384 }
385 }
386 $this->db->free($resql);
387 } else {
388 $this->errors[] = "Error ".$this->db->lasterror();
389 return -1;
390 }
391 }
392
393 return count($this->comments);
394 }
395}
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.