dolibarr 25.0.0-alpha
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-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 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 $isextrafieldmanaged = 1;
47
51 public $entity;
52
56 public $datea;
57
61 public $url;
62
66 public $label;
67
71 public $objecttype;
72
76 public $objectid;
77
81 public $share;
82
86 public $share_pass;
87
93 public function __construct($db)
94 {
95 $this->db = $db;
96 }
97
98
105 public function create(User $user)
106 {
107 global $langs, $conf;
108
109 $error = 0;
110 $langs->loadLangs(array("errors", "admin"));
111 // Clean parameters
112 if (empty($this->label)) {
113 $this->label = trim(basename($this->url));
114 }
115 if (empty($this->datea)) {
116 $this->datea = dol_now();
117 }
118 $this->url = trim($this->url);
119
120 dol_syslog(get_class($this)."::create ".$this->url);
121
122 // Check parameters
123 if (empty($this->url)) {
124 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("URL"));
125 return -1;
126 }
127
128 $this->db->begin();
129
130 $sql = "INSERT INTO ".$this->db->prefix()."links (entity, datea, url, label, objecttype, objectid, share,share_pass)";
131 $sql .= " VALUES (".((int) $conf->entity).", '".$this->db->idate($this->datea)."'";
132 $sql .= ", '".$this->db->escape($this->url)."'";
133 $sql .= ", '".$this->db->escape($this->label)."'";
134 $sql .= ", '".$this->db->escape($this->objecttype)."'";
135 $sql .= ", ".((int) $this->objectid);
136 $sql .= ', '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null");
137 $sql .= ', '.(!empty($this->share_pass) ? "'".$this->db->escape($this->share_pass)."'" : "null").")";
138
139 dol_syslog(get_class($this)."::create", LOG_DEBUG);
140 $result = $this->db->query($sql);
141 if ($result) {
142 $this->id = $this->db->last_insert_id($this->db->prefix()."links");
143
144 if ($this->id > 0) {
145 // Actions on extra fields
146 $result = $this->insertExtraFields();
147 if ($result < 0) {
148 $error++;
149 }
150
151 if (!$error) {
152 // Call trigger
153 $result = $this->call_trigger('LINK_CREATE', $user);
154 if ($result < 0) {
155 $error++;
156 }
157 // End call triggers
158 }
159 } else {
160 $error++;
161 }
162
163 if (!$error) {
164 dol_syslog(get_class($this)."::Create success id=".$this->id);
165 $this->db->commit();
166 return $this->id;
167 } else {
168 dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
169 $this->db->rollback();
170 return -3;
171 }
172 } else {
173 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
174 $this->error = $langs->trans("ErrorDuplicateField");
175 $result = -1;
176 } else {
177 $this->error = $this->db->lasterror();
178 $result = -2;
179 }
180 $this->db->rollback();
181 return $result;
182 }
183 }
184
192 public function update(User $user, $call_trigger = 1)
193 {
194 global $langs, $conf;
195 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
196
197 $langs->loadLangs(array("errors", "admin"));
198 $error = 0;
199
200 dol_syslog(get_class($this)."::Update id = ".$this->id." call_trigger = ".$call_trigger);
201
202 // Check parameters
203 if (empty($this->url)) {
204 $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("URL"));
205 return -1;
206 }
207
208 // Clean parameters
209 $this->url = clean_url($this->url, 1);
210 if (empty($this->label)) {
211 $this->label = basename($this->url);
212 }
213 $this->label = trim($this->label);
214
215
216 $this->db->begin();
217
218 $sql = "UPDATE ".$this->db->prefix()."links SET ";
219 $sql .= "entity = ".((int) $conf->entity);
220 $sql .= ", datea = '".$this->db->idate(dol_now())."'";
221 $sql .= ", url = '".$this->db->escape($this->url)."'";
222 $sql .= ", label = '".$this->db->escape($this->label)."'";
223 $sql .= ", objecttype = '".$this->db->escape($this->objecttype)."'";
224 $sql .= ", objectid = ".((int) $this->objectid);
225 $sql .= ', share = '.(!empty($this->share) ? "'".$this->db->escape($this->share)."'" : "null");
226 $sql .= ', share_pass = '.(!empty($this->share_pass) ? "'".$this->db->escape($this->share_pass)."'" : "null");
227 $sql .= " WHERE rowid = ".((int) $this->id);
228
229 dol_syslog(get_class($this)."::update sql = ".$sql);
230 $resql = $this->db->query($sql);
231 if ($resql) {
232 // Actions on extra fields
233 $result = $this->insertExtraFields();
234 if ($result < 0) {
235 $error++;
236 }
237
238 if (!$error && $call_trigger) {
239 // Call trigger
240 $result = $this->call_trigger('LINK_MODIFY', $user);
241 if ($result < 0) {
242 $error++;
243 }
244 // End call triggers
245 }
246
247 if (!$error) {
248 dol_syslog(get_class($this)."::Update success");
249 $this->db->commit();
250 return 1;
251 } else {
252 $this->db->rollback();
253 return -1;
254 }
255 } else {
256 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
257 // Duplicate
258 $this->error = $langs->trans("ErrorDuplicateField");
259 $result = -1;
260 } else {
261 $this->error = $langs->trans("Error sql")."= $sql";
262 $result = -2;
263 }
264 $this->db->rollback();
265 return $result;
266 }
267 }
268
279 public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
280 {
281 global $conf;
282
283 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid, share,share_pass FROM ".$this->db->prefix()."links";
284 $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".((int) $objectid);
285 if ($conf->entity != 0) {
286 $sql .= " AND entity = ".((int) $conf->entity);
287 }
288 if ($sortfield) {
289 if (empty($sortorder)) {
290 $sortorder = "ASC";
291 }
292 $sql .= $this->db->order($sortfield, $sortorder);
293 }
294
295 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
296 $resql = $this->db->query($sql);
297 if ($resql) {
298 $num = $this->db->num_rows($resql);
299 dol_syslog(get_class($this)."::fetchAll num=".((int) $num), LOG_DEBUG);
300 if ($num > 0) {
301 while ($obj = $this->db->fetch_object($resql)) {
302 $link = new Link($this->db);
303 $link->id = (int) $obj->rowid;
304 $link->entity = $obj->entity;
305 $link->datea = $this->db->jdate($obj->datea);
306 $link->url = $obj->url;
307 $link->label = $obj->label;
308 $link->objecttype = $obj->objecttype;
309 $link->objectid = $obj->objectid;
310 $link->share = $obj->share;
311 $link->share_pass = $obj->share_pass;
312
313 // Retrieve all extrafields for link
314 $link->fetch_optionals();
315
316 $links[] = $link;
317 }
318 return 1;
319 } else {
320 return 0;
321 }
322 } else {
323 return -1;
324 }
325 }
326
335 public static function count($dbs, $objecttype, $objectid)
336 {
337 global $conf;
338
339 $sql = "SELECT COUNT(rowid) as nb FROM ".$dbs->prefix()."links";
340 $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
341 if ($conf->entity != 0) {
342 $sql .= " AND entity = ".((int) $conf->entity);
343 }
344
345 $resql = $dbs->query($sql);
346 if ($resql) {
347 $obj = $dbs->fetch_object($resql);
348 if ($obj) {
349 return $obj->nb;
350 }
351 }
352 return -1;
353 }
354
362 public function fetch($rowid = null, $hashforshare = '')
363 {
364 global $conf;
365
366 if (empty($rowid)) {
367 $rowid = $this->id;
368 }
369
370 if (empty($rowid) && empty($hashforshare)) {
371 $this->error = 'ErrorBadParameters';
372 return -1;
373 }
374
375 $sqlwhere = [];
376
377 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid, share, share_pass FROM ".$this->db->prefix()."links";
378 if (!empty((int) $rowid)) {
379 $sqlwhere[] = " rowid = ".((int) $rowid);
380 }
381 if (!empty($hashforshare)) {
382 $sqlwhere[] = " share = '".$this->db->escape($hashforshare)."'";
383 }
384
385 if ($conf->entity != 0) {
386 $sqlwhere[] = " entity = ".((int) $conf->entity);
387 }
388 if (count($sqlwhere) > 0) {
389 $sql .= ' WHERE '.implode(' AND ', $sqlwhere);
390 }
391
392 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
393 $resql = $this->db->query($sql);
394 if ($resql) {
395 if ($this->db->num_rows($resql) > 0) {
396 $obj = $this->db->fetch_object($resql);
397
398 $this->id = $obj->rowid;
399 $this->entity = $obj->entity;
400 $this->datea = $this->db->jdate($obj->datea);
401 $this->url = $obj->url;
402 $this->label = $obj->label;
403 $this->objecttype = $obj->objecttype;
404 $this->objectid = $obj->objectid;
405 $this->share = $obj->share;
406 $this->share_pass = $obj->share_pass;
407
408 // Retrieve all extrafields for link
409 $this->fetch_optionals();
410
411 return 1;
412 } else {
413 return 0;
414 }
415 } else {
416 $this->error = $this->db->lasterror();
417 return -1;
418 }
419 }
420
428 public function delete(User $user, $notrigger = 0)
429 {
430 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
431 $error = 0;
432
433 $this->db->begin();
434
435 if (!$notrigger) {
436 // Call trigger
437 $result = $this->call_trigger('LINK_DELETE', $user);
438 if ($result < 0) {
439 $this->db->rollback();
440 return -1;
441 }
442 // End call triggers
443 }
444
445 // Remove link
446 $sql = "DELETE FROM ".$this->db->prefix()."links";
447 $sql .= " WHERE rowid = ".((int) $this->id);
448
449 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
450 if (!$this->db->query($sql)) {
451 $error++;
452 $this->error = $this->db->lasterror();
453 }
454
455 // Removed extrafields
456 if (!$error) {
457 $result = $this->deleteExtraFields();
458 if ($result < 0) {
459 $error++;
460 }
461 }
462
463 if (!$error) {
464 $this->db->commit();
465
466 return 1;
467 } else {
468 $this->db->rollback();
469 return -1;
470 }
471 }
472}
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
clean_url($url, $http=1)
Clean an url string.
dol_now($mode='gmt')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.