dolibarr  9.0.0
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 {
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  }
127  else
128  {
129  dol_syslog(get_class($this)."::Create echec update " . $this->error, LOG_ERR);
130  $this->db->rollback();
131  return -3;
132  }
133  }
134  else
135  {
136  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
137  {
138 
139  $this->error=$langs->trans("ErrorCompanyNameAlreadyExists",$this->name);
140  $result=-1;
141  }
142  else
143  {
144  $this->error=$this->db->lasterror();
145  $result=-2;
146  }
147  $this->db->rollback();
148  return $result;
149  }
150  }
151 
159  public function update($user='', $call_trigger=1)
160  {
161  global $langs,$conf;
162  require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
163 
164  $langs->load("errors");
165  $error=0;
166 
167  dol_syslog(get_class($this)."::Update id = " . $this->id . " call_trigger = " . $call_trigger);
168 
169  // Check parameters
170  if (empty($this->url))
171  {
172  $this->error = $langs->trans("NoURL");
173  return -1;
174  }
175 
176  // Clean parameters
177  $this->url = clean_url($this->url,1);
178  if (empty($this->label)) $this->label = basename($this->url);
179  $this->label = trim($this->label);
180 
181 
182  $this->db->begin();
183 
184  $sql = "UPDATE " . MAIN_DB_PREFIX . "links SET ";
185  $sql .= "entity = '" . $conf->entity ."'";
186  $sql .= ", datea = '" . $this->db->idate(dol_now()) . "'";
187  $sql .= ", url = '" . $this->db->escape($this->url) . "'";
188  $sql .= ", label = '" . $this->db->escape($this->label) . "'";
189  $sql .= ", objecttype = '" . $this->db->escape($this->objecttype) . "'";
190  $sql .= ", objectid = " . $this->objectid;
191  $sql .= " WHERE rowid = " . $this->id;
192 
193  dol_syslog(get_class($this)."::update sql = " .$sql);
194  $resql = $this->db->query($sql);
195  if ($resql)
196  {
197  if ($call_trigger)
198  {
199  // Call trigger
200  $result=$this->call_trigger('LINK_MODIFY',$user);
201  if ($result < 0) $error++;
202  // End call triggers
203  }
204 
205  if (! $error)
206  {
207  dol_syslog(get_class($this) . "::Update success");
208  $this->db->commit();
209  return 1;
210  } else {
211  setEventMessages('', $this->errors, 'errors');
212  $this->db->rollback();
213  return -1;
214  }
215  }
216  else
217  {
218  if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
219  {
220  // Doublon
221  $this->error = $langs->trans("ErrorDuplicateField");
222  $result = -1;
223  }
224  else
225  {
226  $this->error = $langs->trans("Error sql = " . $sql);
227  $result = -2;
228  }
229  $this->db->rollback();
230  return $result;
231  }
232  }
233 
244  public function fetchAll(&$links, $objecttype, $objectid, $sortfield=null, $sortorder=null)
245  {
246  global $conf;
247 
248  $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM " . MAIN_DB_PREFIX . "links";
249  $sql .= " WHERE objecttype = '" . $objecttype . "' AND objectid = " . $objectid;
250  if ($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
251  if ($sortfield) {
252  if (empty($sortorder)) {
253  $sortorder = "ASC";
254  }
255  $sql .= " ORDER BY " . $sortfield . " " . $sortorder;
256  }
257 
258  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
259  $resql = $this->db->query($sql);
260  if ($resql)
261  {
262  $num = $this->db->num_rows($resql);
263  dol_syslog(get_class($this)."::fetchAll " . $num . "records", LOG_DEBUG);
264  if ($num > 0)
265  {
266  while ($obj = $this->db->fetch_object($resql))
267  {
268  $link = new Link($this->db);
269  $link->id = $obj->rowid;
270  $link->entity = $obj->entity;
271  $link->datea = $this->db->jdate($obj->datea);
272  $link->url = $obj->url;
273  $link->label = $obj->label;
274  $link->objecttype = $obj->objecttype;
275  $link->objectid = $obj->objectid;
276  $links[] = $link;
277  }
278  return 1;
279  } else {
280  return 0;
281  }
282  } else {
283  return -1;
284  }
285  }
286 
295  public static function count($db, $objecttype, $objectid)
296  {
297  global $conf;
298 
299  $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "links";
300  $sql .= " WHERE objecttype = '" . $objecttype . "' AND objectid = " . $objectid;
301  if ($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
302 
303  $resql = $db->query($sql);
304  if ($resql)
305  {
306  $obj = $db->fetch_object($resql);
307  if ($obj) return $obj->nb;
308  }
309  return -1;
310  }
311 
318  public function fetch($rowid=null)
319  {
320  global $conf;
321 
322  if (empty($rowid)) {
323  $rowid = $this->id;
324  }
325 
326  $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM " . MAIN_DB_PREFIX . "links";
327  $sql .= " WHERE rowid = " . $rowid;
328  if($conf->entity != 0) $sql .= " AND entity = " . $conf->entity;
329 
330  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
331  $resql = $this->db->query($sql);
332  if ($resql)
333  {
334  if($this->db->num_rows($resql) > 0)
335  {
336  $obj = $this->db->fetch_object($resql);
337  $this->entity = $obj->entity;
338  $this->datea = $this->db->jdate($obj->datea);
339  $this->url = $obj->url;
340  $this->label = $obj->label;
341  $this->objecttype = $obj->objecttype;
342  $this->objectid = $obj->objectid;
343  return 1;
344  }
345  else
346  {
347  return 0;
348  }
349  } else {
350  $this->error=$this->db->lasterror();
351  return -1;
352  }
353  }
354 
360  public function delete()
361  {
362  global $user, $langs, $conf;
363 
364  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
365  $error = 0;
366 
367  // Call trigger
368  $result=$this->call_trigger('LINK_DELETE',$user);
369  if ($result < 0) return -1;
370  // End call triggers
371 
372  $this->db->begin();
373 
374  // Remove link
375  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "links";
376  $sql.= " WHERE rowid = " . $this->id;
377 
378  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
379  if (! $this->db->query($sql))
380  {
381  $error++;
382  $this->error = $this->db->lasterror();
383  }
384 
385  if (! $error) {
386  $this->db->commit();
387 
388  return 1;
389  } else {
390  $this->db->rollback();
391  return -1;
392  }
393  }
394 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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:1053
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:103
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_now($mode='gmt')
Return date for now.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)