dolibarr  13.0.2
link.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
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  */
17 
23 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
24 
25 
29 class Link extends CommonObject
30 {
34  public $element = 'link';
35 
39  public $table_element = 'links';
40 
44  public $entity;
45 
46  public $datea;
47  public $url;
48 
52  public $label;
53 
54  public $objecttype;
55  public $objectid;
56 
57 
63  public function __construct($db)
64  {
65  $this->db = $db;
66  }
67 
68 
75  public function create($user = '')
76  {
77  global $langs, $conf;
78 
79  $error = 0;
80  $langs->load("errors");
81  // Clean parameters
82  if (empty($this->label)) {
83  $this->label = trim(basename($this->url));
84  }
85  if (empty($this->datea)) {
86  $this->datea = dol_now();
87  }
88  $this->url = trim($this->url);
89 
90  dol_syslog(get_class($this)."::create ".$this->url);
91 
92  // Check parameters
93  if (empty($this->url)) {
94  $this->error = $langs->trans("NoUrl");
95  return -1;
96  }
97 
98  $this->db->begin();
99 
100  $sql = "INSERT INTO ".MAIN_DB_PREFIX."links (entity, datea, url, label, objecttype, objectid)";
101  $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datea)."'";
102  $sql .= ", '".$this->db->escape($this->url)."'";
103  $sql .= ", '".$this->db->escape($this->label)."'";
104  $sql .= ", '".$this->db->escape($this->objecttype)."'";
105  $sql .= ", ".$this->objectid.")";
106 
107  dol_syslog(get_class($this)."::create", LOG_DEBUG);
108  $result = $this->db->query($sql);
109  if ($result) {
110  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."links");
111 
112  if ($this->id > 0) {
113  // Call trigger
114  $result = $this->call_trigger('LINK_CREATE', $user);
115  if ($result < 0) $error++;
116  // End call triggers
117  } else {
118  $error++;
119  }
120 
121  if (!$error)
122  {
123  dol_syslog(get_class($this)."::Create success id=".$this->id);
124  $this->db->commit();
125  return $this->id;
126  } else {
127  dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
128  $this->db->rollback();
129  return -3;
130  }
131  } else {
132  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
133  {
134  $this->error = $langs->trans("ErrorCompanyNameAlreadyExists", $this->name);
135  $result = -1;
136  } else {
137  $this->error = $this->db->lasterror();
138  $result = -2;
139  }
140  $this->db->rollback();
141  return $result;
142  }
143  }
144 
152  public function update($user = '', $call_trigger = 1)
153  {
154  global $langs, $conf;
155  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
156 
157  $langs->load("errors");
158  $error = 0;
159 
160  dol_syslog(get_class($this)."::Update id = ".$this->id." call_trigger = ".$call_trigger);
161 
162  // Check parameters
163  if (empty($this->url))
164  {
165  $this->error = $langs->trans("NoURL");
166  return -1;
167  }
168 
169  // Clean parameters
170  $this->url = clean_url($this->url, 1);
171  if (empty($this->label)) $this->label = basename($this->url);
172  $this->label = trim($this->label);
173 
174 
175  $this->db->begin();
176 
177  $sql = "UPDATE ".MAIN_DB_PREFIX."links SET ";
178  $sql .= "entity = ".$conf->entity;
179  $sql .= ", datea = '".$this->db->idate(dol_now())."'";
180  $sql .= ", url = '".$this->db->escape($this->url)."'";
181  $sql .= ", label = '".$this->db->escape($this->label)."'";
182  $sql .= ", objecttype = '".$this->db->escape($this->objecttype)."'";
183  $sql .= ", objectid = ".$this->objectid;
184  $sql .= " WHERE rowid = ".$this->id;
185 
186  dol_syslog(get_class($this)."::update sql = ".$sql);
187  $resql = $this->db->query($sql);
188  if ($resql)
189  {
190  if ($call_trigger)
191  {
192  // Call trigger
193  $result = $this->call_trigger('LINK_MODIFY', $user);
194  if ($result < 0) $error++;
195  // End call triggers
196  }
197 
198  if (!$error)
199  {
200  dol_syslog(get_class($this)."::Update success");
201  $this->db->commit();
202  return 1;
203  } else {
204  setEventMessages('', $this->errors, 'errors');
205  $this->db->rollback();
206  return -1;
207  }
208  } else {
209  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
210  {
211  // Doublon
212  $this->error = $langs->trans("ErrorDuplicateField");
213  $result = -1;
214  } else {
215  $this->error = $langs->trans("Error sql = ".$sql);
216  $result = -2;
217  }
218  $this->db->rollback();
219  return $result;
220  }
221  }
222 
233  public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
234  {
235  global $conf;
236 
237  $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".MAIN_DB_PREFIX."links";
238  $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".$objectid;
239  if ($conf->entity != 0) $sql .= " AND entity = ".$conf->entity;
240  if ($sortfield) {
241  if (empty($sortorder)) {
242  $sortorder = "ASC";
243  }
244  $sql .= " ORDER BY ".$sortfield." ".$sortorder;
245  }
246 
247  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
248  $resql = $this->db->query($sql);
249  if ($resql)
250  {
251  $num = $this->db->num_rows($resql);
252  dol_syslog(get_class($this)."::fetchAll ".$num."records", LOG_DEBUG);
253  if ($num > 0)
254  {
255  while ($obj = $this->db->fetch_object($resql))
256  {
257  $link = new Link($this->db);
258  $link->id = $obj->rowid;
259  $link->entity = $obj->entity;
260  $link->datea = $this->db->jdate($obj->datea);
261  $link->url = $obj->url;
262  $link->label = $obj->label;
263  $link->objecttype = $obj->objecttype;
264  $link->objectid = $obj->objectid;
265  $links[] = $link;
266  }
267  return 1;
268  } else {
269  return 0;
270  }
271  } else {
272  return -1;
273  }
274  }
275 
284  public static function count($db, $objecttype, $objectid)
285  {
286  global $conf;
287 
288  $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."links";
289  $sql .= " WHERE objecttype = '".$db->escape($objecttype)."' AND objectid = ".$objectid;
290  if ($conf->entity != 0) $sql .= " AND entity = ".$conf->entity;
291 
292  $resql = $db->query($sql);
293  if ($resql)
294  {
295  $obj = $db->fetch_object($resql);
296  if ($obj) return $obj->nb;
297  }
298  return -1;
299  }
300 
307  public function fetch($rowid = null)
308  {
309  global $conf;
310 
311  if (empty($rowid)) {
312  $rowid = $this->id;
313  }
314 
315  $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".MAIN_DB_PREFIX."links";
316  $sql .= " WHERE rowid = ".$rowid;
317  if ($conf->entity != 0) $sql .= " AND entity = ".$conf->entity;
318 
319  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
320  $resql = $this->db->query($sql);
321  if ($resql)
322  {
323  if ($this->db->num_rows($resql) > 0)
324  {
325  $obj = $this->db->fetch_object($resql);
326 
327  $this->id = $obj->rowid;
328  $this->entity = $obj->entity;
329  $this->datea = $this->db->jdate($obj->datea);
330  $this->url = $obj->url;
331  $this->label = $obj->label;
332  $this->objecttype = $obj->objecttype;
333  $this->objectid = $obj->objectid;
334  return 1;
335  } else {
336  return 0;
337  }
338  } else {
339  $this->error = $this->db->lasterror();
340  return -1;
341  }
342  }
343 
350  public function delete($user)
351  {
352  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
353  $error = 0;
354 
355  $this->db->begin();
356 
357  // Call trigger
358  $result = $this->call_trigger('LINK_DELETE', $user);
359  if ($result < 0)
360  {
361  $this->db->rollback();
362  return -1;
363  }
364  // End call triggers
365 
366  // Remove link
367  $sql = "DELETE FROM ".MAIN_DB_PREFIX."links";
368  $sql .= " WHERE rowid = ".$this->id;
369 
370  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
371  if (!$this->db->query($sql))
372  {
373  $error++;
374  $this->error = $this->db->lasterror();
375  }
376 
377  if (!$error) {
378  $this->db->commit();
379 
380  return 1;
381  } else {
382  $this->db->rollback();
383  return -1;
384  }
385  }
386 }
dol_now($mode= 'auto')
Return date for now.
clean_url($url, $http=1)
Clean an url string.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:108
$conf db
API class for accounts.
Definition: inc.php:54
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
call_trigger($triggerName, $user)
Call trigger based on this instance.
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, ...)