dolibarr 21.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 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25
26
30class Link extends CommonObject
31{
35 public $element = 'link';
36
40 public $table_element = 'links';
41
45 public $entity;
46
47 public $datea;
48 public $url;
49
53 public $label;
54
55 public $objecttype;
56 public $objectid;
57
58
64 public function __construct($db)
65 {
66 $this->db = $db;
67 }
68
69
76 public function create(User $user)
77 {
78 global $langs, $conf;
79
80 $error = 0;
81 $langs->load("errors");
82 // Clean parameters
83 if (empty($this->label)) {
84 $this->label = trim(basename($this->url));
85 }
86 if (empty($this->datea)) {
87 $this->datea = dol_now();
88 }
89 $this->url = trim($this->url);
90
91 dol_syslog(get_class($this)."::create ".$this->url);
92
93 // Check parameters
94 if (empty($this->url)) {
95 $this->error = $langs->trans("NoURL");
96 return -1;
97 }
98
99 $this->db->begin();
100
101 $sql = "INSERT INTO ".$this->db->prefix()."links (entity, datea, url, label, objecttype, objectid)";
102 $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datea)."'";
103 $sql .= ", '".$this->db->escape($this->url)."'";
104 $sql .= ", '".$this->db->escape($this->label)."'";
105 $sql .= ", '".$this->db->escape($this->objecttype)."'";
106 $sql .= ", ".((int) $this->objectid).")";
107
108 dol_syslog(get_class($this)."::create", LOG_DEBUG);
109 $result = $this->db->query($sql);
110 if ($result) {
111 $this->id = $this->db->last_insert_id($this->db->prefix()."links");
112
113 if ($this->id > 0) {
114 // Call trigger
115 $result = $this->call_trigger('LINK_CREATE', $user);
116 if ($result < 0) {
117 $error++;
118 }
119 // End call triggers
120 } else {
121 $error++;
122 }
123
124 if (!$error) {
125 dol_syslog(get_class($this)."::Create success id=".$this->id);
126 $this->db->commit();
127 return $this->id;
128 } else {
129 dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
130 $this->db->rollback();
131 return -3;
132 }
133 } else {
134 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
135 $this->error = $langs->trans("ErrorCompanyNameAlreadyExists", $this->name);
136 $result = -1;
137 } else {
138 $this->error = $this->db->lasterror();
139 $result = -2;
140 }
141 $this->db->rollback();
142 return $result;
143 }
144 }
145
153 public function update(User $user, $call_trigger = 1)
154 {
155 global $langs, $conf;
156 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
157
158 $langs->load("errors");
159 $error = 0;
160
161 dol_syslog(get_class($this)."::Update id = ".$this->id." call_trigger = ".$call_trigger);
162
163 // Check parameters
164 if (empty($this->url)) {
165 $this->error = $langs->trans("NoURL");
166 return -1;
167 }
168
169 // Clean parameters
170 $this->url = clean_url($this->url, 1);
171 if (empty($this->label)) {
172 $this->label = basename($this->url);
173 }
174 $this->label = trim($this->label);
175
176
177 $this->db->begin();
178
179 $sql = "UPDATE ".$this->db->prefix()."links SET ";
180 $sql .= "entity = ".$conf->entity;
181 $sql .= ", datea = '".$this->db->idate(dol_now())."'";
182 $sql .= ", url = '".$this->db->escape($this->url)."'";
183 $sql .= ", label = '".$this->db->escape($this->label)."'";
184 $sql .= ", objecttype = '".$this->db->escape($this->objecttype)."'";
185 $sql .= ", objectid = ".$this->objectid;
186 $sql .= " WHERE rowid = ".((int) $this->id);
187
188 dol_syslog(get_class($this)."::update sql = ".$sql);
189 $resql = $this->db->query($sql);
190 if ($resql) {
191 if ($call_trigger) {
192 // Call trigger
193 $result = $this->call_trigger('LINK_MODIFY', $user);
194 if ($result < 0) {
195 $error++;
196 }
197 // End call triggers
198 }
199
200 if (!$error) {
201 dol_syslog(get_class($this)."::Update success");
202 $this->db->commit();
203 return 1;
204 } else {
205 setEventMessages('', $this->errors, 'errors');
206 $this->db->rollback();
207 return -1;
208 }
209 } else {
210 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
211 // Duplicate
212 $this->error = $langs->trans("ErrorDuplicateField");
213 $result = -1;
214 } else {
215 $this->error = $langs->trans("Error sql")."= $sql";
216 $result = -2;
217 }
218 $this->db->rollback();
219 return $result;
220 }
221 }
222
233 public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
234 {
235 global $conf;
236
237 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix()."links";
238 $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".((int) $objectid);
239 if ($conf->entity != 0) {
240 $sql .= " AND entity = ".((int) $conf->entity);
241 }
242 if ($sortfield) {
243 if (empty($sortorder)) {
244 $sortorder = "ASC";
245 }
246 $sql .= " ORDER BY ".$sortfield." ".$sortorder;
247 }
248
249 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
250 $resql = $this->db->query($sql);
251 if ($resql) {
252 $num = $this->db->num_rows($resql);
253 dol_syslog(get_class($this)."::fetchAll num=".((int) $num), LOG_DEBUG);
254 if ($num > 0) {
255 while ($obj = $this->db->fetch_object($resql)) {
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($dbs, $objecttype, $objectid)
284 {
285 global $conf;
286
287 $sql = "SELECT COUNT(rowid) as nb FROM ".$dbs->prefix()."links";
288 $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
289 if ($conf->entity != 0) {
290 $sql .= " AND entity = ".$conf->entity;
291 }
292
293 $resql = $dbs->query($sql);
294 if ($resql) {
295 $obj = $dbs->fetch_object($resql);
296 if ($obj) {
297 return $obj->nb;
298 }
299 }
300 return -1;
301 }
302
309 public function fetch($rowid = null)
310 {
311 global $conf;
312
313 if (empty($rowid)) {
314 $rowid = $this->id;
315 }
316
317 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix()."links";
318 $sql .= " WHERE rowid = ".((int) $rowid);
319 if ($conf->entity != 0) {
320 $sql .= " AND entity = ".$conf->entity;
321 }
322
323 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
324 $resql = $this->db->query($sql);
325 if ($resql) {
326 if ($this->db->num_rows($resql) > 0) {
327 $obj = $this->db->fetch_object($resql);
328
329 $this->id = $obj->rowid;
330 $this->entity = $obj->entity;
331 $this->datea = $this->db->jdate($obj->datea);
332 $this->url = $obj->url;
333 $this->label = $obj->label;
334 $this->objecttype = $obj->objecttype;
335 $this->objectid = $obj->objectid;
336 return 1;
337 } else {
338 return 0;
339 }
340 } else {
341 $this->error = $this->db->lasterror();
342 return -1;
343 }
344 }
345
352 public function delete($user)
353 {
354 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
355 $error = 0;
356
357 $this->db->begin();
358
359 // Call trigger
360 $result = $this->call_trigger('LINK_DELETE', $user);
361 if ($result < 0) {
362 $this->db->rollback();
363 return -1;
364 }
365 // End call triggers
366
367 // Remove link
368 $sql = "DELETE FROM ".$this->db->prefix()."links";
369 $sql .= " WHERE rowid = ".((int) $this->id);
370
371 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
372 if (!$this->db->query($sql)) {
373 $error++;
374 $this->error = $this->db->lasterror();
375 }
376
377 if (!$error) {
378 $this->db->commit();
379
380 return 1;
381 } else {
382 $this->db->rollback();
383 return -1;
384 }
385 }
386}
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.
dol_now($mode='auto')
Return date for now.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:142