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 setEventMessages('', $this->errors, 'errors');
253 $this->db->rollback();
254 return -1;
255 }
256 } else {
257 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
258 // Duplicate
259 $this->error = $langs->trans("ErrorDuplicateField");
260 $result = -1;
261 } else {
262 $this->error = $langs->trans("Error sql")."= $sql";
263 $result = -2;
264 }
265 $this->db->rollback();
266 return $result;
267 }
268 }
269
280 public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
281 {
282 global $conf;
283
284 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid, share,share_pass FROM ".$this->db->prefix()."links";
285 $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".((int) $objectid);
286 if ($conf->entity != 0) {
287 $sql .= " AND entity = ".((int) $conf->entity);
288 }
289 if ($sortfield) {
290 if (empty($sortorder)) {
291 $sortorder = "ASC";
292 }
293 $sql .= $this->db->order($sortfield, $sortorder);
294 }
295
296 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
297 $resql = $this->db->query($sql);
298 if ($resql) {
299 $num = $this->db->num_rows($resql);
300 dol_syslog(get_class($this)."::fetchAll num=".((int) $num), LOG_DEBUG);
301 if ($num > 0) {
302 while ($obj = $this->db->fetch_object($resql)) {
303 $link = new Link($this->db);
304 $link->id = (int) $obj->rowid;
305 $link->entity = $obj->entity;
306 $link->datea = $this->db->jdate($obj->datea);
307 $link->url = $obj->url;
308 $link->label = $obj->label;
309 $link->objecttype = $obj->objecttype;
310 $link->objectid = $obj->objectid;
311 $link->share = $obj->share;
312 $link->share_pass = $obj->share_pass;
313
314 // Retrieve all extrafields for link
315 $link->fetch_optionals();
316
317 $links[] = $link;
318 }
319 return 1;
320 } else {
321 return 0;
322 }
323 } else {
324 return -1;
325 }
326 }
327
336 public static function count($dbs, $objecttype, $objectid)
337 {
338 global $conf;
339
340 $sql = "SELECT COUNT(rowid) as nb FROM ".$dbs->prefix()."links";
341 $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
342 if ($conf->entity != 0) {
343 $sql .= " AND entity = ".((int) $conf->entity);
344 }
345
346 $resql = $dbs->query($sql);
347 if ($resql) {
348 $obj = $dbs->fetch_object($resql);
349 if ($obj) {
350 return $obj->nb;
351 }
352 }
353 return -1;
354 }
355
363 public function fetch($rowid = null, $hashforshare = '')
364 {
365 global $conf;
366
367 if (empty($rowid)) {
368 $rowid = $this->id;
369 }
370
371 if (empty($rowid) && empty($hashforshare)) {
372 $this->error = 'ErrorBadParameters';
373 return -1;
374 }
375
376 $sqlwhere = [];
377
378 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid, share, share_pass FROM ".$this->db->prefix()."links";
379 if (!empty((int) $rowid)) {
380 $sqlwhere[] = " rowid = ".((int) $rowid);
381 }
382 if (!empty($hashforshare)) {
383 $sqlwhere[] = " share = '".$this->db->escape($hashforshare)."'";
384 }
385
386 if ($conf->entity != 0) {
387 $sqlwhere[] = " entity = ".((int) $conf->entity);
388 }
389 if (count($sqlwhere) > 0) {
390 $sql .= ' WHERE '.implode(' AND ', $sqlwhere);
391 }
392
393 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
394 $resql = $this->db->query($sql);
395 if ($resql) {
396 if ($this->db->num_rows($resql) > 0) {
397 $obj = $this->db->fetch_object($resql);
398
399 $this->id = $obj->rowid;
400 $this->entity = $obj->entity;
401 $this->datea = $this->db->jdate($obj->datea);
402 $this->url = $obj->url;
403 $this->label = $obj->label;
404 $this->objecttype = $obj->objecttype;
405 $this->objectid = $obj->objectid;
406 $this->share = $obj->share;
407 $this->share_pass = $obj->share_pass;
408
409 // Retrieve all extrafields for link
410 $this->fetch_optionals();
411
412 return 1;
413 } else {
414 return 0;
415 }
416 } else {
417 $this->error = $this->db->lasterror();
418 return -1;
419 }
420 }
421
429 public function delete(User $user, $notrigger = 0)
430 {
431 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
432 $error = 0;
433
434 $this->db->begin();
435
436 if (!$notrigger) {
437 // Call trigger
438 $result = $this->call_trigger('LINK_DELETE', $user);
439 if ($result < 0) {
440 $this->db->rollback();
441 return -1;
442 }
443 // End call triggers
444 }
445
446 // Remove link
447 $sql = "DELETE FROM ".$this->db->prefix()."links";
448 $sql .= " WHERE rowid = ".((int) $this->id);
449
450 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
451 if (!$this->db->query($sql)) {
452 $error++;
453 $this->error = $this->db->lasterror();
454 }
455
456 // Removed extrafields
457 if (!$error) {
458 $result = $this->deleteExtraFields();
459 if ($result < 0) {
460 $error++;
461 }
462 }
463
464 if (!$error) {
465 $this->db->commit();
466
467 return 1;
468 } else {
469 $this->db->rollback();
470 return -1;
471 }
472 }
473}
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.