dolibarr 22.0.5
objectlink.class.php
1<?php
2/* Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT.'/core/class/doldeprecationhandler.class.php';
25
32{
33 const TRIGGER_PREFIX = 'OBJECTLINK';
37 public $element = 'objectlink';
38
42 public $table_element = 'element_element';
43
47 public $fk_source;
48
52 public $sourcetype;
53
57 public $fk_target;
58
62 public $targettype;
63
67 public $relationtype;
68
74 public function __construct($db)
75 {
76 $this->db = $db;
77 }
78
85 public function fetch($rowid)
86 {
87 $sql = "SELECT rowid, fk_source, sourcetype, fk_target,";
88 $sql .= " targettype, relationtype FROM";
89 $sql .= " ".MAIN_DB_PREFIX.$this->table_element;
90 $sql .= " WHERE rowid = ".((int) $rowid);
91
92 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
93 $result = $this->db->query($sql);
94 if ($result) {
95 $obj = $this->db->fetch_object($result);
96 if ($obj) {
97 $this->id = $obj->rowid;
98 //$this->entity = $obj->entity;
99
100 $this->fk_source = (int) $obj->fk_source;
101 $this->sourcetype = (string) $obj->sourcetype;
102 $this->fk_target = (int) $obj->fk_target;
103 $this->targettype = (string) $obj->targettype;
104 $this->relationtype = $obj->relationtype;
105
106 return 1;
107 } else {
108 $this->error = 'Object link with id '.((string) $rowid).' not found sql='.$sql;
109 return 0;
110 }
111 } else {
112 $this->error = $this->db->error();
113 return -1;
114 }
115 }
116
127 public function fetchByValues($fk_source, $sourcetype, $fk_target, $targettype, $relationtype = null)
128 {
129 $sql = "SELECT rowid, fk_source, sourcetype, fk_target,";
130 $sql .= " targettype, relationtype FROM";
131 $sql .= " ".MAIN_DB_PREFIX.$this->table_element;
132 $sql .= " WHERE fk_source=".((int) $fk_source);
133 $sql .= " AND sourcetype='".$this->db->escape($sourcetype)."'";
134 $sql .= " AND fk_target=".((int) $fk_target);
135 $sql .= " AND targettype='".$this->db->escape($targettype)."'";
136 if ($relationtype) {
137 $sql .= " AND relationtype='".$this->db->escape($relationtype)."'";
138 }
139
140 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
141 $result = $this->db->query($sql);
142 if ($result) {
143 $obj = $this->db->fetch_object($result);
144 if ($obj) {
145 $this->id = $obj->rowid;
146 //$this->entity = $obj->entity;
147
148 $this->fk_source = (int) $obj->fk_source;
149 $this->sourcetype = (string) $obj->sourcetype;
150 $this->fk_target = (int) $obj->fk_target;
151 $this->targettype = (string) $obj->targettype;
152 $this->relationtype = $obj->relationtype;
153
154 return 1;
155 } else {
156 $this->error = 'Object link not found sql='.$sql;
157 return 0;
158 }
159 } else {
160 $this->error = $this->db->error();
161 return -1;
162 }
163 }
164
172 public function delete($user, $notrigger = 0)
173 {
174
175 global $conf, $langs;
176 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
177
178 $error = 0;
179
180 dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
181
182 $this->db->begin();
183
184 if (!$notrigger) {
185 // Call trigger
186 $result = $this->call_trigger(self::TRIGGER_PREFIX.'_DELETE', $user);
187 if ($result < 0) {
188 $error++;
189 }
190 // End call triggers
191 }
192
193 // Delete object link
194 if (!$error) {
195 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
196 $res = $this->db->query($sql);
197 if (!$res) {
198 $error++;
199 $this->error = $this->db->lasterror();
200 $this->errors[] = $this->error;
201 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
202 }
203 }
204
205 if (!$error) {
206 dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
207 $this->db->commit();
208 return 1;
209 } else {
210 $this->db->rollback();
211 return -1;
212 }
213 }
214
227 public function create($user, $fk_source, $sourcetype, $fk_target, $targettype, $relationtype = null, $notrigger = 0)
228 {
229 global $conf, $langs;
230 $error = 0;
231
232 $alreadyexists = $this->fetchByValues($fk_source, $sourcetype, $fk_target, $targettype, $relationtype);
233 if ($alreadyexists == 1) {
234 return 0;
235 }
236
237 // create sourceobject and targetobject, make sure they exist with the respective numbers
238 $sourceobject = $this->_makeobject($fk_source, $sourcetype);
239 if ($sourceobject < 0 ) {
240 $this->error = "Error when looking for Object id=".$fk_source." of type=".$sourcetype;
241 return -2;
242 }
243 if ($sourceobject == 0 ) {
244 $this->error = "Object id ".$fk_source." of type ".$sourcetype." does not exist";
245 return -1;
246 }
247
248 $targetobject = $this->_makeobject($fk_target, $targettype);
249 if ($targetobject < 0 ) {
250 $this->error = "Error when looking for Object id=".$fk_target." of type=".$targettype;
251 return -2;
252 }
253 if ($targetobject == 0 ) {
254 $this->error = "Object id ".$fk_target." of type ".$targettype." does not exist";
255 return -1;
256 }
257
258 dol_syslog(get_class($this)."::create user=".$user->id);
259
260 $this->db->begin();
261
262 if (!$notrigger) {
263 // Call trigger
264 $result = $this->call_trigger(self::TRIGGER_PREFIX.'_CREATE', $user);
265 if ($result < 0) {
266 $error++;
267 }
268 // End call triggers
269 }
270
271 $sql = "INSERT INTO ".MAIN_DB_PREFIX."$this->table_element";
272 if ($relationtype) {
273 $sql .= " (fk_source, sourcetype, fk_target, targettype, relationtype )";
274 } else {
275 $sql .= " (fk_source, sourcetype, fk_target, targettype )";
276 }
277 $sql .= " VALUES (".((int) $this->fk_source).", '".$this->db->escape($sourcetype)."', ";
278 $sql .= ((int) $this->fk_target).", '".$this->db->escape($targettype)."'";
279 if ($relationtype) {
280 $sql .= ", '".$this->db->escape($relationtype)."'";
281 }
282 $sql .= ")";
283
284 dol_syslog(get_class($this)."::create", LOG_DEBUG);
285 $resql = $this->db->query($sql);
286 if ($resql) {
287 $this->db->commit();
288 return 1;
289 } else {
290 $this->error = $this->db->lasterror();
291 $this->db->rollback();
292 return -1;
293 }
294 }
305 private function _makeobject($objectid, $objecttype)
306 {
307 if ($objecttype == 'adherent') {
308 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
309 $newobject = new Adherent($this->db);
310 $result = $newobject->fetch($objectid);
311 return $result;
312 }
313 if ($objecttype == 'commande') {
314 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
315 $newobject = new Commande($this->db);
316 $result = $newobject->fetch($objectid);
317 return $result;
318 }
319 if ($objecttype == 'facture') {
320 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
321 $newobject = new Facture($this->db);
322 $result = $newobject->fetch($objectid);
323 return $result;
324 }
325 if ($objecttype == 'propal') {
326 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
327 $newobject = new Propal($this->db);
328 $result = $newobject->fetch($objectid);
329 return $result;
330 }
331 if ($objecttype == 'subscription') {
332 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
333 $newobject = new Subscription($this->db);
334 $result = $newobject->fetch($objectid);
335 return $result;
336 }
337 dol_syslog("objectlink->_makeobject called with unknown objecttype=".$objecttype, LOG_ERR);
338 return -2;
339 }
340}
Class to manage members of a foundation.
Class to manage customers orders.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage invoices.
Class to manage proposals.
Class to manage subscriptions of foundation members.
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