dolibarr  16.0.5
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 ".$this->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 .= ", ".((int) $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($this->db->prefix()."links");
111 
112  if ($this->id > 0) {
113  // Call trigger
114  $result = $this->call_trigger('LINK_CREATE', $user);
115  if ($result < 0) {
116  $error++;
117  }
118  // End call triggers
119  } else {
120  $error++;
121  }
122 
123  if (!$error) {
124  dol_syslog(get_class($this)."::Create success id=".$this->id);
125  $this->db->commit();
126  return $this->id;
127  } else {
128  dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
129  $this->db->rollback();
130  return -3;
131  }
132  } else {
133  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
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  $this->error = $langs->trans("NoURL");
165  return -1;
166  }
167 
168  // Clean parameters
169  $this->url = clean_url($this->url, 1);
170  if (empty($this->label)) {
171  $this->label = basename($this->url);
172  }
173  $this->label = trim($this->label);
174 
175 
176  $this->db->begin();
177 
178  $sql = "UPDATE ".$this->db->prefix()."links SET ";
179  $sql .= "entity = ".$conf->entity;
180  $sql .= ", datea = '".$this->db->idate(dol_now())."'";
181  $sql .= ", url = '".$this->db->escape($this->url)."'";
182  $sql .= ", label = '".$this->db->escape($this->label)."'";
183  $sql .= ", objecttype = '".$this->db->escape($this->objecttype)."'";
184  $sql .= ", objectid = ".$this->objectid;
185  $sql .= " WHERE rowid = ".((int) $this->id);
186 
187  dol_syslog(get_class($this)."::update sql = ".$sql);
188  $resql = $this->db->query($sql);
189  if ($resql) {
190  if ($call_trigger) {
191  // Call trigger
192  $result = $this->call_trigger('LINK_MODIFY', $user);
193  if ($result < 0) {
194  $error++;
195  }
196  // End call triggers
197  }
198 
199  if (!$error) {
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  // Doublon
211  $this->error = $langs->trans("ErrorDuplicateField");
212  $result = -1;
213  } else {
214  $this->error = $langs->trans("Error sql = ".$sql);
215  $result = -2;
216  }
217  $this->db->rollback();
218  return $result;
219  }
220  }
221 
232  public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
233  {
234  global $conf;
235 
236  $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix()."links";
237  $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".((int) $objectid);
238  if ($conf->entity != 0) {
239  $sql .= " AND entity = ".$conf->entity;
240  }
241  if ($sortfield) {
242  if (empty($sortorder)) {
243  $sortorder = "ASC";
244  }
245  $sql .= " ORDER BY ".$sortfield." ".$sortorder;
246  }
247 
248  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
249  $resql = $this->db->query($sql);
250  if ($resql) {
251  $num = $this->db->num_rows($resql);
252  dol_syslog(get_class($this)."::fetchAll num=".((int) $num), LOG_DEBUG);
253  if ($num > 0) {
254  while ($obj = $this->db->fetch_object($resql)) {
255  $link = new Link($this->db);
256  $link->id = $obj->rowid;
257  $link->entity = $obj->entity;
258  $link->datea = $this->db->jdate($obj->datea);
259  $link->url = $obj->url;
260  $link->label = $obj->label;
261  $link->objecttype = $obj->objecttype;
262  $link->objectid = $obj->objectid;
263  $links[] = $link;
264  }
265  return 1;
266  } else {
267  return 0;
268  }
269  } else {
270  return -1;
271  }
272  }
273 
282  public static function count($dbs, $objecttype, $objectid)
283  {
284  global $conf;
285 
286  $sql = "SELECT COUNT(rowid) as nb FROM ".$dbs->prefix()."links";
287  $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
288  if ($conf->entity != 0) {
289  $sql .= " AND entity = ".$conf->entity;
290  }
291 
292  $resql = $dbs->query($sql);
293  if ($resql) {
294  $obj = $dbs->fetch_object($resql);
295  if ($obj) {
296  return $obj->nb;
297  }
298  }
299  return -1;
300  }
301 
308  public function fetch($rowid = null)
309  {
310  global $conf;
311 
312  if (empty($rowid)) {
313  $rowid = $this->id;
314  }
315 
316  $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix()."links";
317  $sql .= " WHERE rowid = ".((int) $rowid);
318  if ($conf->entity != 0) {
319  $sql .= " AND entity = ".$conf->entity;
320  }
321 
322  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
323  $resql = $this->db->query($sql);
324  if ($resql) {
325  if ($this->db->num_rows($resql) > 0) {
326  $obj = $this->db->fetch_object($resql);
327 
328  $this->id = $obj->rowid;
329  $this->entity = $obj->entity;
330  $this->datea = $this->db->jdate($obj->datea);
331  $this->url = $obj->url;
332  $this->label = $obj->label;
333  $this->objecttype = $obj->objecttype;
334  $this->objectid = $obj->objectid;
335  return 1;
336  } else {
337  return 0;
338  }
339  } else {
340  $this->error = $this->db->lasterror();
341  return -1;
342  }
343  }
344 
351  public function delete($user)
352  {
353  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
354  $error = 0;
355 
356  $this->db->begin();
357 
358  // Call trigger
359  $result = $this->call_trigger('LINK_DELETE', $user);
360  if ($result < 0) {
361  $this->db->rollback();
362  return -1;
363  }
364  // End call triggers
365 
366  // Remove link
367  $sql = "DELETE FROM ".$this->db->prefix()."links";
368  $sql .= " WHERE rowid = ".((int) $this->id);
369 
370  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
371  if (!$this->db->query($sql)) {
372  $error++;
373  $this->error = $this->db->lasterror();
374  }
375 
376  if (!$error) {
377  $this->db->commit();
378 
379  return 1;
380  } else {
381  $this->db->rollback();
382  return -1;
383  }
384  }
385 }
clean_url
clean_url($url, $http=1)
Clean an url string.
Definition: functions2.lib.php:825
db
$conf db
API class for accounts.
Definition: inc.php:41
name
$conf db name
Definition: repair.php:122
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137