dolibarr  13.0.2
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 
22 class 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 ".MAIN_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 .= ", ".$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++; $this->errors[] = "Error ".$this->db->lasterror();
145  }
146 
147  if (!$error) {
148  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
149 
150  if (!$notrigger) {
151  // Call trigger
152  $result = $this->call_trigger('TASK_COMMENT_CREATE', $user);
153  if ($result < 0) {
154  $error++;
155  }
156  // End call triggers
157  }
158  }
159 
160  // Commit or rollback
161  if ($error) {
162  foreach ($this->errors as $errmsg) {
163  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
164  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
165  }
166  $this->db->rollback();
167  return -1 * $error;
168  } else {
169  $this->db->commit();
170  return $this->id;
171  }
172  }
173 
174 
182  public function fetch($id, $ref = '')
183  {
184  global $langs;
185 
186  $sql = "SELECT";
187  $sql .= " c.rowid,";
188  $sql .= " c.description,";
189  $sql .= " c.datec,";
190  $sql .= " c.tms,";
191  $sql .= " c.fk_element,";
192  $sql .= " c.element_type,";
193  $sql .= " c.fk_user_author,";
194  $sql .= " c.fk_user_modif,";
195  $sql .= " c.entity,";
196  $sql .= " c.import_key";
197  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as c";
198  $sql .= " WHERE c.rowid = ".((int) $id);
199 
200  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
201  $resql = $this->db->query($sql);
202  if ($resql) {
203  $num_rows = $this->db->num_rows($resql);
204 
205  if ($num_rows) {
206  $obj = $this->db->fetch_object($resql);
207 
208  $this->id = $obj->rowid;
209  $this->description = $obj->description;
210  $this->element_type = $obj->element_type;
211  $this->datec = $this->db->jdate($obj->datec);
212  $this->tms = $this->db->jdate($obj->tms);
213  $this->fk_user_author = $obj->fk_user_author;
214  $this->fk_user_modif = $obj->fk_user_modif;
215  $this->fk_element = $obj->fk_element;
216  $this->entity = $obj->entity;
217  $this->import_key = $obj->import_key;
218  }
219 
220  $this->db->free($resql);
221 
222  if ($num_rows) {
223  return 1;
224  } else {
225  return 0;
226  }
227  } else {
228  $this->error = "Error ".$this->db->lasterror();
229  return -1;
230  }
231  }
232 
233 
241  public function update(User $user, $notrigger = 0)
242  {
243  global $user;
244  $error = 0;
245 
246  // Clean parameters
247  if (isset($this->fk_element)) {
248  $this->fk_project = (int) trim($this->fk_element);
249  }
250  if (isset($this->description)) {
251  $this->description = trim($this->description);
252  }
253 
254 
255  // Update request
256  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
257  $sql .= " description=".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").",";
258  $sql .= " datec=".($this->datec != '' ? "'".$this->db->idate($this->datec)."'" : 'null').",";
259  $sql .= " fk_element=".(isset($this->fk_element) ? $this->fk_element : "null").",";
260  $sql .= " element_type='".$this->db->escape($this->element_type)."',";
261  $sql .= " fk_user_modif=".$user->id.",";
262  $sql .= " entity=".(!empty($this->entity) ? $this->entity : '1').",";
263  $sql .= " import_key=".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
264  $sql .= " WHERE rowid=".$this->id;
265 
266  $this->db->begin();
267 
268  dol_syslog(get_class($this)."::update", LOG_DEBUG);
269  $resql = $this->db->query($sql);
270  if (!$resql) {
271  $error++; $this->errors[] = "Error ".$this->db->lasterror();
272  }
273 
274  if (!$error) {
275  if (!$notrigger) {
276  // Call trigger
277  $result = $this->call_trigger('TASK_COMMENT_MODIFY', $user);
278  if ($result < 0) {
279  $error++;
280  }
281  // End call triggers
282  }
283  }
284 
285  // Commit or rollback
286  if ($error) {
287  foreach ($this->errors as $errmsg) {
288  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
289  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
290  }
291  $this->db->rollback();
292  return -1 * $error;
293  } else {
294  $this->db->commit();
295  return 1;
296  }
297  }
298 
299 
307  public function delete($user, $notrigger = 0)
308  {
309  global $conf, $langs;
310  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
311 
312  $error = 0;
313 
314  $this->db->begin();
315 
316  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
317  $sql .= " WHERE rowid=".$this->id;
318 
319  $resql = $this->db->query($sql);
320  if (!$resql) {
321  $error++; $this->errors[] = "Error ".$this->db->lasterror();
322  }
323 
324  if (!$error) {
325  if (!$notrigger) {
326  // Call trigger
327  $result = $this->call_trigger('TASK_COMMENT_DELETE', $user);
328  if ($result < 0) {
329  $error++;
330  }
331  // End call triggers
332  }
333  }
334 
335  // Commit or rollback
336  if ($error) {
337  foreach ($this->errors as $errmsg) {
338  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
339  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
340  }
341  $this->db->rollback();
342  return -1 * $error;
343  } else {
344  $this->db->commit();
345  return 1;
346  }
347  }
348 
349 
357  public function fetchAllFor($element_type, $fk_element)
358  {
359  global $db, $conf;
360  $this->comments = array();
361  if (!empty($element_type) && !empty($fk_element)) {
362  $sql = "SELECT";
363  $sql .= " c.rowid";
364  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as c";
365  $sql .= " WHERE c.fk_element = ".$fk_element;
366  $sql .= " AND c.element_type = '".$this->db->escape($element_type)."'";
367  $sql .= " AND c.entity = ".$conf->entity;
368  $sql .= " ORDER BY c.tms DESC";
369 
370  dol_syslog(get_class($this).'::'.__METHOD__, LOG_DEBUG);
371  $resql = $this->db->query($sql);
372  if ($resql) {
373  $num_rows = $this->db->num_rows($resql);
374  if ($num_rows > 0) {
375  while ($obj = $this->db->fetch_object($resql)) {
376  $comment = new self($db);
377  $comment->fetch($obj->rowid);
378  $this->comments[] = $comment;
379  }
380  }
381  $this->db->free($resql);
382  } else {
383  $this->errors[] = "Error ".$this->db->lasterror();
384  return -1;
385  }
386  }
387 
388  return count($this->comments);
389  }
390 }
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
Class to manage Dolibarr users.
Definition: user.class.php:44
__construct($db)
Constructor.
$conf db
API class for accounts.
Definition: inc.php:54
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
update(User $user, $notrigger=0)
Update database.
fetchAllFor($element_type, $fk_element)
Load comments linked with current task.
fetch($id, $ref= '')
Load object in memory from database.
Class to manage comment.
call_trigger($triggerName, $user)
Call trigger based on this instance.
create($user, $notrigger=0)
Create into database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)