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