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 * 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
73
79 public function __construct($db)
80 {
81 $this->db = $db;
82 }
83
84
91 public function create(User $user)
92 {
93 global $langs, $conf;
94
95 $error = 0;
96 $langs->load("errors");
97 // Clean parameters
98 if (empty($this->label)) {
99 $this->label = trim(basename($this->url));
100 }
101 if (empty($this->datea)) {
102 $this->datea = dol_now();
103 }
104 $this->url = trim($this->url);
105
106 dol_syslog(get_class($this)."::create ".$this->url);
107
108 // Check parameters
109 if (empty($this->url)) {
110 $this->error = $langs->trans("NoURL");
111 return -1;
112 }
113
114 $this->db->begin();
115
116 $sql = "INSERT INTO ".$this->db->prefix()."links (entity, datea, url, label, objecttype, objectid)";
117 $sql .= " VALUES (".$conf->entity.", '".$this->db->idate($this->datea)."'";
118 $sql .= ", '".$this->db->escape($this->url)."'";
119 $sql .= ", '".$this->db->escape($this->label)."'";
120 $sql .= ", '".$this->db->escape($this->objecttype)."'";
121 $sql .= ", ".((int) $this->objectid).")";
122
123 dol_syslog(get_class($this)."::create", LOG_DEBUG);
124 $result = $this->db->query($sql);
125 if ($result) {
126 $this->id = $this->db->last_insert_id($this->db->prefix()."links");
127
128 if ($this->id > 0) {
129 // Call trigger
130 $result = $this->call_trigger('LINK_CREATE', $user);
131 if ($result < 0) {
132 $error++;
133 }
134 // End call triggers
135 } else {
136 $error++;
137 }
138
139 if (!$error) {
140 dol_syslog(get_class($this)."::Create success id=".$this->id);
141 $this->db->commit();
142 return $this->id;
143 } else {
144 dol_syslog(get_class($this)."::Create echec update ".$this->error, LOG_ERR);
145 $this->db->rollback();
146 return -3;
147 }
148 } else {
149 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
150 $this->error = $langs->trans("ErrorCompanyNameAlreadyExists", $this->name);
151 $result = -1;
152 } else {
153 $this->error = $this->db->lasterror();
154 $result = -2;
155 }
156 $this->db->rollback();
157 return $result;
158 }
159 }
160
168 public function update(User $user, $call_trigger = 1)
169 {
170 global $langs, $conf;
171 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
172
173 $langs->load("errors");
174 $error = 0;
175
176 dol_syslog(get_class($this)."::Update id = ".$this->id." call_trigger = ".$call_trigger);
177
178 // Check parameters
179 if (empty($this->url)) {
180 $this->error = $langs->trans("NoURL");
181 return -1;
182 }
183
184 // Clean parameters
185 $this->url = clean_url($this->url, 1);
186 if (empty($this->label)) {
187 $this->label = basename($this->url);
188 }
189 $this->label = trim($this->label);
190
191
192 $this->db->begin();
193
194 $sql = "UPDATE ".$this->db->prefix()."links SET ";
195 $sql .= "entity = ".((int) $conf->entity);
196 $sql .= ", datea = '".$this->db->idate(dol_now())."'";
197 $sql .= ", url = '".$this->db->escape($this->url)."'";
198 $sql .= ", label = '".$this->db->escape($this->label)."'";
199 $sql .= ", objecttype = '".$this->db->escape($this->objecttype)."'";
200 $sql .= ", objectid = ".$this->objectid;
201 $sql .= " WHERE rowid = ".((int) $this->id);
202
203 dol_syslog(get_class($this)."::update sql = ".$sql);
204 $resql = $this->db->query($sql);
205 if ($resql) {
206 if ($call_trigger) {
207 // Call trigger
208 $result = $this->call_trigger('LINK_MODIFY', $user);
209 if ($result < 0) {
210 $error++;
211 }
212 // End call triggers
213 }
214
215 if (!$error) {
216 dol_syslog(get_class($this)."::Update success");
217 $this->db->commit();
218 return 1;
219 } else {
220 setEventMessages('', $this->errors, 'errors');
221 $this->db->rollback();
222 return -1;
223 }
224 } else {
225 if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
226 // Duplicate
227 $this->error = $langs->trans("ErrorDuplicateField");
228 $result = -1;
229 } else {
230 $this->error = $langs->trans("Error sql")."= $sql";
231 $result = -2;
232 }
233 $this->db->rollback();
234 return $result;
235 }
236 }
237
248 public function fetchAll(&$links, $objecttype, $objectid, $sortfield = null, $sortorder = null)
249 {
250 global $conf;
251
252 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix()."links";
253 $sql .= " WHERE objecttype = '".$this->db->escape($objecttype)."' AND objectid = ".((int) $objectid);
254 if ($conf->entity != 0) {
255 $sql .= " AND entity = ".((int) $conf->entity);
256 }
257 if ($sortfield) {
258 if (empty($sortorder)) {
259 $sortorder = "ASC";
260 }
261 $sql .= " ORDER BY ".$sortfield." ".$sortorder;
262 }
263
264 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
265 $resql = $this->db->query($sql);
266 if ($resql) {
267 $num = $this->db->num_rows($resql);
268 dol_syslog(get_class($this)."::fetchAll num=".((int) $num), LOG_DEBUG);
269 if ($num > 0) {
270 while ($obj = $this->db->fetch_object($resql)) {
271 $link = new Link($this->db);
272 $link->id = (int) $obj->rowid;
273 $link->entity = $obj->entity;
274 $link->datea = $this->db->jdate($obj->datea);
275 $link->url = $obj->url;
276 $link->label = $obj->label;
277 $link->objecttype = $obj->objecttype;
278 $link->objectid = $obj->objectid;
279 $links[] = $link;
280 }
281 return 1;
282 } else {
283 return 0;
284 }
285 } else {
286 return -1;
287 }
288 }
289
298 public static function count($dbs, $objecttype, $objectid)
299 {
300 global $conf;
301
302 $sql = "SELECT COUNT(rowid) as nb FROM ".$dbs->prefix()."links";
303 $sql .= " WHERE objecttype = '".$dbs->escape($objecttype)."' AND objectid = ".((int) $objectid);
304 if ($conf->entity != 0) {
305 $sql .= " AND entity = ".$conf->entity;
306 }
307
308 $resql = $dbs->query($sql);
309 if ($resql) {
310 $obj = $dbs->fetch_object($resql);
311 if ($obj) {
312 return $obj->nb;
313 }
314 }
315 return -1;
316 }
317
324 public function fetch($rowid = null)
325 {
326 global $conf;
327
328 if (empty($rowid)) {
329 $rowid = $this->id;
330 }
331
332 $sql = "SELECT rowid, entity, datea, url, label, objecttype, objectid FROM ".$this->db->prefix()."links";
333 $sql .= " WHERE rowid = ".((int) $rowid);
334 if ($conf->entity != 0) {
335 $sql .= " AND entity = ".$conf->entity;
336 }
337
338 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
339 $resql = $this->db->query($sql);
340 if ($resql) {
341 if ($this->db->num_rows($resql) > 0) {
342 $obj = $this->db->fetch_object($resql);
343
344 $this->id = $obj->rowid;
345 $this->entity = $obj->entity;
346 $this->datea = $this->db->jdate($obj->datea);
347 $this->url = $obj->url;
348 $this->label = $obj->label;
349 $this->objecttype = $obj->objecttype;
350 $this->objectid = $obj->objectid;
351 return 1;
352 } else {
353 return 0;
354 }
355 } else {
356 $this->error = $this->db->lasterror();
357 return -1;
358 }
359 }
360
367 public function delete($user)
368 {
369 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
370 $error = 0;
371
372 $this->db->begin();
373
374 // Call trigger
375 $result = $this->call_trigger('LINK_DELETE', $user);
376 if ($result < 0) {
377 $this->db->rollback();
378 return -1;
379 }
380 // End call triggers
381
382 // Remove link
383 $sql = "DELETE FROM ".$this->db->prefix()."links";
384 $sql .= " WHERE rowid = ".((int) $this->id);
385
386 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
387 if (!$this->db->query($sql)) {
388 $error++;
389 $this->error = $this->db->lasterror();
390 }
391
392 if (!$error) {
393 $this->db->commit();
394
395 return 1;
396 } else {
397 $this->db->rollback();
398 return -1;
399 }
400 }
401}
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.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:140