dolibarr 25.0.0-alpha
objectlink.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
3 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
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
25require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
33{
38 public $TRIGGER_PREFIX = 'OBJECTLINK';
39
43 public $element = 'objectlink';
44
48 public $table_element = 'element_element';
49
53 public $fk_source;
54
58 public $sourcetype;
59
63 public $fk_target;
64
68 public $targettype;
69
73 public $relationtype;
74
80 public function __construct($db)
81 {
82 $this->db = $db;
83 }
84
91 public function fetch($rowid)
92 {
93 $sql = "SELECT rowid, fk_source, sourcetype, fk_target,";
94 $sql .= " targettype, relationtype FROM";
95 $sql .= " ".MAIN_DB_PREFIX.$this->table_element;
96 $sql .= " WHERE rowid = ".((int) $rowid);
97
98 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
99 $result = $this->db->query($sql);
100 if ($result) {
101 $obj = $this->db->fetch_object($result);
102 if ($obj) {
103 $this->id = $obj->rowid;
104 //$this->entity = $obj->entity;
105
106 $this->fk_source = (int) $obj->fk_source;
107 $this->sourcetype = (string) $obj->sourcetype;
108 $this->fk_target = (int) $obj->fk_target;
109 $this->targettype = (string) $obj->targettype;
110 $this->relationtype = $obj->relationtype;
111
112 return 1;
113 } else {
114 $this->error = 'Object link with id '.((string) $rowid).' not found sql='.$sql;
115 return 0;
116 }
117 } else {
118 $this->error = $this->db->error();
119 return -1;
120 }
121 }
122
133 public function fetchByValues($fk_source, $sourcetype, $fk_target, $targettype, $relationtype = null)
134 {
135 $sql = "SELECT rowid, fk_source, sourcetype, fk_target,";
136 $sql .= " targettype, relationtype FROM";
137 $sql .= " ".MAIN_DB_PREFIX.$this->table_element;
138 $sql .= " WHERE fk_source=".((int) $fk_source);
139 $sql .= " AND sourcetype='".$this->db->escape($sourcetype)."'";
140 $sql .= " AND fk_target=".((int) $fk_target);
141 $sql .= " AND targettype='".$this->db->escape($targettype)."'";
142 if ($relationtype) {
143 $sql .= " AND relationtype='".$this->db->escape($relationtype)."'";
144 }
145
146 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
147 $result = $this->db->query($sql);
148 if ($result) {
149 $obj = $this->db->fetch_object($result);
150 if ($obj) {
151 $this->id = $obj->rowid;
152 //$this->entity = $obj->entity;
153
154 $this->fk_source = (int) $obj->fk_source;
155 $this->sourcetype = (string) $obj->sourcetype;
156 $this->fk_target = (int) $obj->fk_target;
157 $this->targettype = (string) $obj->targettype;
158 $this->relationtype = $obj->relationtype;
159
160 return 1;
161 } else {
162 $this->error = 'Object link not found sql='.$sql;
163 return 0;
164 }
165 } else {
166 $this->error = $this->db->error();
167 return -1;
168 }
169 }
170
178 public function delete($user, $notrigger = 0)
179 {
180
181 global $conf, $langs;
182 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
183
184 $error = 0;
185
186 dol_syslog(get_class($this)."::delete ".$this->id, LOG_DEBUG);
187
188 $this->db->begin();
189
190 if (!$notrigger) {
191 // Call trigger
192 $result = $this->call_trigger($this->TRIGGER_PREFIX.'_DELETE', $user);
193 if ($result < 0) {
194 $error++;
195 }
196 // End call triggers
197 }
198
199 // Delete object link
200 if (!$error) {
201 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element." WHERE rowid = ".((int) $this->id);
202 $res = $this->db->query($sql);
203 if (!$res) {
204 $error++;
205 $this->error = $this->db->lasterror();
206 $this->errors[] = $this->error;
207 dol_syslog(get_class($this)."::delete error ".$this->error, LOG_ERR);
208 }
209 }
210
211 if (!$error) {
212 dol_syslog(get_class($this)."::delete ".$this->id." by ".$user->id, LOG_DEBUG);
213 $this->db->commit();
214 return 1;
215 } else {
216 $this->db->rollback();
217 return -1;
218 }
219 }
220
233 public function create($user, $fk_source, $sourcetype, $fk_target, $targettype, $relationtype = null, $notrigger = 0)
234 {
235 global $conf, $langs;
236 $error = 0;
237
238 $alreadyexists = $this->fetchByValues($fk_source, $sourcetype, $fk_target, $targettype, $relationtype);
239 if ($alreadyexists == 1) {
240 return 0;
241 }
242
243 // create sourceobject and targetobject, make sure they exist with the respective numbers
244 $sourceobject = $this->_makeobject($fk_source, $sourcetype);
245 if ($sourceobject < 0 ) {
246 $this->error = "Error when looking for Object id=".$fk_source." of type=".$sourcetype;
247 return -2;
248 }
249 if ($sourceobject == 0 ) {
250 $this->error = "Object id ".$fk_source." of type ".$sourcetype." does not exist";
251 return -1;
252 }
253
254 $targetobject = $this->_makeobject($fk_target, $targettype);
255 if ($targetobject < 0 ) {
256 $this->error = "Error when looking for Object id=".$fk_target." of type=".$targettype;
257 return -2;
258 }
259 if ($targetobject == 0 ) {
260 $this->error = "Object id ".$fk_target." of type ".$targettype." does not exist";
261 return -1;
262 }
263
264 dol_syslog(get_class($this)."::create user=".$user->id);
265
266 $this->db->begin();
267
268 if (!$notrigger) {
269 // Call trigger
270 $result = $this->call_trigger($this->TRIGGER_PREFIX.'_CREATE', $user);
271 if ($result < 0) {
272 $error++;
273 }
274 // End call triggers
275 }
276
277 $sql = "INSERT INTO ".MAIN_DB_PREFIX."$this->table_element";
278 if ($relationtype) {
279 $sql .= " (fk_source, sourcetype, fk_target, targettype, relationtype )";
280 } else {
281 $sql .= " (fk_source, sourcetype, fk_target, targettype )";
282 }
283 $sql .= " VALUES (".((int) $this->fk_source).", '".$this->db->escape($sourcetype)."', ";
284 $sql .= ((int) $this->fk_target).", '".$this->db->escape($targettype)."'";
285 if ($relationtype) {
286 $sql .= ", '".$this->db->escape($relationtype)."'";
287 }
288 $sql .= ")";
289
290 dol_syslog(get_class($this)."::create", LOG_DEBUG);
291 $resql = $this->db->query($sql);
292 if ($resql) {
293 $this->db->commit();
294 return 1;
295 } else {
296 $this->error = $this->db->lasterror();
297 $this->db->rollback();
298 return -1;
299 }
300 }
311 private function _makeobject($objectid, $objecttype)
312 {
313 if ($objecttype == 'adherent') {
314 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
315 $newobject = new Adherent($this->db);
316 $result = $newobject->fetch($objectid);
317 return $result;
318 }
319 if ($objecttype == 'commande') {
320 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
321 $newobject = new Commande($this->db);
322 $result = $newobject->fetch($objectid);
323 return $result;
324 }
325 if ($objecttype == 'facture') {
326 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
327 $newobject = new Facture($this->db);
328 $result = $newobject->fetch($objectid);
329 return $result;
330 }
331 if ($objecttype == 'invoice_supplier') {
332 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
333 $newobject = new FactureFournisseur($this->db);
334 $result = $newobject->fetch($objectid);
335 return $result;
336 }
337 if ($objecttype == 'propal') {
338 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
339 $newobject = new Propal($this->db);
340 $result = $newobject->fetch($objectid);
341 return $result;
342 }
343 if ($objecttype == 'subscription') {
344 require_once DOL_DOCUMENT_ROOT.'/adherents/class/subscription.class.php';
345 $newobject = new Subscription($this->db);
346 $result = $newobject->fetch($objectid);
347 return $result;
348 }
349 dol_syslog("objectlink->_makeobject called with unknown objecttype=".$objecttype, LOG_ERR);
350 return -2;
351 }
352}
Class to manage members of a foundation.
Class to manage customers orders.
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage proposals.
Class to manage subscriptions of foundation members.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string