dolibarr 22.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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
27
31class Link extends CommonObject
32{
36 public $element = 'link';
37
41 public $table_element = 'links';
42
46 public $entity;
47
51 public $datea;
52
56 public $url;
57
61 public $label;
62
66 public $objecttype;
67
71 public $objectid;
72
76 public $share;
77
81 public $share_pass;
82
88 public function __construct($db)
89 {
90 $this->db = $db;
91 }
92
93
100 public function create(User $user)
101 {
102 global $langs, $conf;
103
104 $error = 0;
105 $langs->load("errors");
106 // Clean parameters
107 if (empty($this->label)) {
108 $this->label = trim(basename($this->url));
109 }
110 if (empty($this->datea)) {
111 $this->datea = dol_now();
112 }
113 $this->url = trim($this->url);
114
115 dol_syslog(get_class($this)."::create ".$this->url);
116
117 // Check parameters
118 if (empty($this->url)) {
119 $this->error = $langs->trans("NoURL");
120 return -1;
121 }
122
123 $this->db->begin();
124
125 $sql = "INSERT INTO ".$this->db->prefix()."links (entity, datea, url, label, objecttype, objectid, share,share_pass)";
126 $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datea)."'";
127 $sql .= ", '".$this->db->escape($this->url)."'";
128 $sql .= ", '".$this->db->escape($this->label)."'";
129 $sql .= ", '".$this->db->escape($this->objecttype)."'";
130 $sql .= ", ".((int) $this->objectid);
131 $sql .= ', '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null");
132 $sql .= ', '.(!empty($this->share_pass) ? "'".$this->db->escape($this->share_pass)."'" : "null").")";
133
134 dol_syslog(get_class($this)."::create", LOG_DEBUG);
135 $result = $this->db->query($sql);
136 if ($result) {
137 $this->id = $this->db->last_insert_id($this->db->prefix()."links");
138
139 if ($this->id > 0) {
140 // Call trigger
141 $result = $this->call_trigger('LINK_CREATE', $user);
142 if ($result < 0) {
143 $error++;
144 }
145 // End call triggers
146 } else {
147 $error++;
148 }
149
150 if (!$error) {
151 dol_syslog(get_class($this)."::Create success id=".$this->id);
152 $this->db->commit();
153 return $this->id;
154 } else {
155 dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
156 $this->db->rollback();
157 return -3;
158 }
159 } else {
160 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
161 $this->error = $langs->trans("ErrorCompanyNameAlreadyExists", (string) $this->name);
162 $result = -1;
163 } else {
164 $this->error = $this->db->lasterror();
165 $result = -2;
166 }
167 $this->db->rollback();
168 return $result;
169 }
170 }
171
179 public function update(User $user, $call_trigger = 1)
180 {
181 global $langs, $conf;
182 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
183
184 $langs->load("errors");
185 $error = 0;
186
187 dol_syslog(get_class($this)."::Update id = ".$this->id." call_trigger = ".$call_trigger);
188
189 // Check parameters
190 if (empty($this->url)) {
191 $this->error = $langs->trans("NoURL");
192 return -1;
193 }
194
195 // Clean parameters
196 $this->url = clean_url($this->url, 1);
197 if (empty($this->label)) {
198 $this->label = basename($this->url);
199 }
200 $this->label = trim($this->label);
201
202
203 $this->db->begin();
204
205 $sql = "UPDATE ".$this->db->prefix()."links SET ";
206 $sql .= "entity = ".((int) $conf->entity);
207 $sql .= ", datea = '".$this->db->idate(dol_now())."'";
208 $sql .= ", url = '".$this->db->escape($this->url)."'";
209 $sql .= ", label = '".$this->db->escape($this->label)."'";
210 $sql .= ", objecttype = '".$this->db->escape($this->objecttype)."'";
211 $sql .= ", objectid = ".((int) $this->objectid);
212 $sql .= ', share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null");
213 $sql .= ', share_pass = '.(!empty($this->share_pass) ? "'".$this->db->escape($this->share_pass)."'" : "null");
214 $sql .= " WHERE rowid = ".((int) $this->id);
215
216 dol_syslog(get_class($this)."::update sql = ".$sql);
217 $resql = $this->db->query($sql);
218 if ($resql) {
219 if ($call_trigger) {
220 // Call trigger
221 $result = $this->call_trigger('LINK_MODIFY', $user);
222 if ($result < 0) {
223 $error++;
224 }
225 // End call triggers
226 }
227
228 if (!$error) {
229 dol_syslog(get_class($this)."::Update success");
230 $this->db->commit();
231 return 1;
232 } else {
233 setEventMessages('', $this->errors, 'errors');
234 $this->db->rollback();
235 return -1;
236 }
237 } else {
238 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
239 // Duplicate
240 $this->error = $langs->trans("ErrorDuplicateField");
241 $result = -1;
242 } else {
243 $this->error = $langs->trans("Error sql")."= $sql";
244 $result = -2;
245 }
246 $this->db->rollback();
247 return $result;
248 }
249 }
250
261 public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
262 {
263 global $conf;
264
265 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid, share,share_pass FROM ".$this->db->prefix()."links";
266 $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".((int) $objectid);
267 if ($conf->entity != 0) {
268 $sql .= " AND entity = ".((int) $conf->entity);
269 }
270 if ($sortfield) {
271 if (empty($sortorder)) {
272 $sortorder = "ASC";
273 }
274 $sql .= " ORDER BY ".$sortfield." ".$sortorder;
275 }
276
277 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
278 $resql = $this->db->query($sql);
279 if ($resql) {
280 $num = $this->db->num_rows($resql);
281 dol_syslog(get_class($this)."::fetchAll num=".((int) $num), LOG_DEBUG);
282 if ($num > 0) {
283 while ($obj = $this->db->fetch_object($resql)) {
284 $link = new Link($this->db);
285 $link->id = (int) $obj->rowid;
286 $link->entity = $obj->entity;
287 $link->datea = $this->db->jdate($obj->datea);
288 $link->url = $obj->url;
289 $link->label = $obj->label;
290 $link->objecttype = $obj->objecttype;
291 $link->objectid = $obj->objectid;
292 $link->share = $obj->share;
293 $link->share_pass = $obj->share_pass;
294 $links[] = $link;
295 }
296 return 1;
297 } else {
298 return 0;
299 }
300 } else {
301 return -1;
302 }
303 }
304
313 public static function count($dbs, $objecttype, $objectid)
314 {
315 global $conf;
316
317 $sql = "SELECT COUNT(rowid) as nb FROM ".$dbs->prefix()."links";
318 $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
319 if ($conf->entity != 0) {
320 $sql .= " AND entity = ".$conf->entity;
321 }
322
323 $resql = $dbs->query($sql);
324 if ($resql) {
325 $obj = $dbs->fetch_object($resql);
326 if ($obj) {
327 return $obj->nb;
328 }
329 }
330 return -1;
331 }
332
340 public function fetch($rowid = null, $hashforshare = '')
341 {
342 global $conf;
343
344 if (empty($rowid)) {
345 $rowid = $this->id;
346 }
347
348 $sqlwhere=[];
349
350 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid, share, share_pass FROM ".$this->db->prefix()."links";
351 if (!empty((int) $rowid)) {
352 $sqlwhere[] = " rowid = ".((int) $rowid);
353 }
354 if (!empty($hashforshare)) {
355 $sqlwhere[] = " share = '".$this->db->escape($hashforshare)."'";
356 }
357
358 if ($conf->entity != 0) {
359 $sqlwhere[] = " entity = ".$conf->entity;
360 }
361 if (count($sqlwhere)>0) {
362 $sql .=' WHERE '.implode(' AND ', $sqlwhere);
363 }
364
365 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
366 $resql = $this->db->query($sql);
367 if ($resql) {
368 if ($this->db->num_rows($resql) > 0) {
369 $obj = $this->db->fetch_object($resql);
370
371 $this->id = $obj->rowid;
372 $this->entity = $obj->entity;
373 $this->datea = $this->db->jdate($obj->datea);
374 $this->url = $obj->url;
375 $this->label = $obj->label;
376 $this->objecttype = $obj->objecttype;
377 $this->objectid = $obj->objectid;
378 $this->share = $obj->share;
379 $this->share_pass = $obj->share_pass;
380 return 1;
381 } else {
382 return 0;
383 }
384 } else {
385 $this->error = $this->db->lasterror();
386 return -1;
387 }
388 }
389
396 public function delete($user)
397 {
398 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
399 $error = 0;
400
401 $this->db->begin();
402
403 // Call trigger
404 $result = $this->call_trigger('LINK_DELETE', $user);
405 if ($result < 0) {
406 $this->db->rollback();
407 return -1;
408 }
409 // End call triggers
410
411 // Remove link
412 $sql = "DELETE FROM ".$this->db->prefix()."links";
413 $sql .= " WHERE rowid = ".((int) $this->id);
414
415 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
416 if (!$this->db->query($sql)) {
417 $error++;
418 $this->error = $this->db->lasterror();
419 }
420
421 if (!$error) {
422 $this->db->commit();
423
424 return 1;
425 } else {
426 $this->db->rollback();
427 return -1;
428 }
429 }
430}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr users.
clean_url($url, $http=1)
Clean an url string.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:161