dolibarr 24.0.0-beta
api_objectlinks.class.php
1<?php
2/* Copyright (C) 2025 Jon Bendtsen <jon.bendtsen.github@jonb.dk>
3 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025 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
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
23require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
24require_once DOL_DOCUMENT_ROOT.'/core/class/objectlink.class.php';
25
26
34{
38 public static $FIELDS = array(
39 'fk_source',
40 'sourcetype',
41 'fk_target',
42 'targettype'
43 );
44
48 public $objectlink;
49
53 private $notrigger;
54
58 public function __construct()
59 {
60 global $db;
61 $this->db = $db;
62 $this->objectlink = new ObjectLink($this->db);
63 }
64
81 public function getById($id)
82 {
83 return $this->_fetch($id);
84 }
85
86
87
95 private function _setObjectLinkField($field, $value)
96 {
97 $clean_field = $this->_checkValForAPI($field, $value, $this->objectlink);
98
102 $intFields = array(
103 'fk_source',
104 'fk_target'
105 );
106
107 if (in_array($field, $intFields)) {
108 $this->objectlink->$field = (int) $clean_field; // Clean by _checkValForAPI previously
109 } else {
110 $this->objectlink->$field = (string) $clean_field; // Clean by _checkValForAPI previously
111 }
112 }
113
114
136 public function create($request_data = null)
137 {
138 // Check mandatory fields
139 $result = $this->_validate($request_data);
140
141 foreach ($request_data as $field => $value) {
142 if ($field == 'notrigger') {
143 $this->notrigger = (int) $value;
144 } else {
145 $this->_setObjectLinkField($field, $value);
146 }
147 }
148
149 // Permission check
150 $srctype = $this->objectlink->sourcetype;
151 if ($this->objectlink->sourcetype == 'subscription') {
152 $srctype = 'adherent';
153 }
154 if ($this->objectlink->sourcetype == 'conferenceorboothattendee') {
155 $srctype = 'projet';
156 }
157 $tgttype = $this->objectlink->targettype;
158 if ($this->objectlink->targettype == 'subscription') {
159 $tgttype = 'adherent';
160 }
161 if ($this->objectlink->targettype == 'conferenceorboothattendee') {
162 $tgttype = 'projet';
163 }
164 if (!DolibarrApiAccess::$user->hasRight((string) $srctype, 'creer') && !DolibarrApiAccess::$user->hasRight((string) $srctype, 'write')) {
165 throw new RestException(403, 'denied access to create the objectlinks sourcetype='.$this->objectlink->sourcetype);
166 }
167 if (!DolibarrApiAccess::$user->hasRight((string) $tgttype, 'creer') && !DolibarrApiAccess::$user->hasRight((string) $tgttype, 'write')) {
168 throw new RestException(403, 'denied access to create the objectlinks targettype='.$this->objectlink->targettype);
169 }
170
171 $result = $this->objectlink->create(DolibarrApiAccess::$user, $this->objectlink->fk_source, $this->objectlink->sourcetype, $this->objectlink->fk_target, $this->objectlink->targettype, $this->objectlink->relationtype, $this->notrigger);
172
173 if ($result < 0) {
174 throw new RestException(500, 'when create objectlink : '.$this->objectlink->error);
175 }
176
177 if ($result == 0) {
178 throw new RestException(304, 'Object link already exists');
179 }
180
181 return array(
182 'success' => array(
183 'code' => 200,
184 'message' => 'object link created'
185 )
186 );
187 }
188
203 public function deleteById($id)
204 {
205 // Reverse permission check. First we find out which kind of objects are linked, and if the user has rights to that then we delete it.
206 $result = $this->objectlink->fetch($id);
207 if ($result) {
208 $srctype = $this->objectlink->sourcetype;
209 if ($this->objectlink->sourcetype == 'subscription') {
210 $srctype = 'adherent';
211 }
212 if ($this->objectlink->sourcetype == 'conferenceorboothattendee') {
213 $srctype = 'projet';
214 }
215 $tgttype = $this->objectlink->targettype;
216 if ($this->objectlink->targettype == 'subscription') {
217 $tgttype = 'adherent';
218 }
219 if ($this->objectlink->targettype == 'conferenceorboothattendee') {
220 $tgttype = 'projet';
221 }
222 if (!DolibarrApiAccess::$user->hasRight(((string) $srctype), 'creer') && !DolibarrApiAccess::$user->hasRight(((string) $srctype), 'write')) {
223 throw new RestException(403, 'denied access to the objectlinks sourcetype');
224 }
225 if (!DolibarrApiAccess::$user->hasRight(((string) $tgttype), 'creer') && !DolibarrApiAccess::$user->hasRight(((string) $tgttype), 'write')) {
226 throw new RestException(403, 'denied access to the objectlinks targettype');
227 }
228 } else {
229 throw new RestException(404, 'Object Link not found');
230 }
231
232 if (!$this->objectlink->delete(DolibarrApiAccess::$user)) {
233 throw new RestException(500, 'Error when delete objectlink : '.$this->objectlink->error);
234 }
235
236 return array(
237 'success' => array(
238 'code' => 200,
239 'message' => 'object link deleted'
240 )
241 );
242 }
243
262 public function getByValues($fk_source, $sourcetype, $fk_target, $targettype, $relationtype = null)
263 {
264 $request_data = array(
265 'fk_source' => ((int) $fk_source),
266 'sourcetype' => (string) $sourcetype,
267 'fk_target' => ((int) $fk_target),
268 'targettype' => (string) $targettype,
269 'relationtype' => $relationtype,
270 );
271
272 // Check mandatory fields
273 $result = $this->_validate($request_data);
274
275 foreach ($request_data as $field => $value) {
276 $this->_setObjectLinkField($field, $value);
277 }
278
279 // Permission check
280 $srctype = $this->objectlink->sourcetype;
281 if ($this->objectlink->sourcetype == 'subscription') {
282 $srctype = 'adherent';
283 }
284 if ($this->objectlink->sourcetype == 'conferenceorboothattendee') {
285 $srctype = 'projet';
286 }
287 $tgttype = $this->objectlink->targettype;
288 if ($this->objectlink->targettype == 'subscription') {
289 $tgttype = 'adherent';
290 }
291 if ($this->objectlink->targettype == 'conferenceorboothattendee') {
292 $tgttype = 'projet';
293 }
294 if (!DolibarrApiAccess::$user->hasRight((string) $srctype, 'creer') && !DolibarrApiAccess::$user->hasRight((string) $srctype, 'write')) {
295 throw new RestException(403, 'denied access to get the objectlinks sourcetype='.$this->objectlink->sourcetype);
296 }
297 if (!DolibarrApiAccess::$user->hasRight((string) $tgttype, 'creer') && !DolibarrApiAccess::$user->hasRight((string) $tgttype, 'write')) {
298 throw new RestException(403, 'denied access to get the objectlinks targettype='.$this->objectlink->targettype);
299 }
300
301 $findresult = $this->objectlink->fetchByValues($this->objectlink->fk_source, $this->objectlink->sourcetype, $this->objectlink->fk_target, $this->objectlink->targettype, $this->objectlink->relationtype);
302
303 if ($findresult < 0) {
304 throw new RestException(500, 'Error when finding objectlink : '.$this->objectlink->error);
305 } elseif ($findresult > 0) {
306 return $this->_cleanObjectDatas($this->objectlink);
307 } else {
308 throw new RestException(404, 'Object Link not found');
309 }
310 }
311
312
332 public function deleteByValues($fk_source, $sourcetype, $fk_target, $targettype, $relationtype = null, $notrigger = 0)
333 {
334 $request_data = array(
335 'fk_source' => ((int) $fk_source),
336 'sourcetype' => (string) $sourcetype,
337 'fk_target' => ((int) $fk_target),
338 'targettype' => (string) $targettype,
339 'relationtype' => $relationtype,
340 );
341
342 // Check mandatory fields
343 $result = $this->_validate($request_data);
344
345 foreach ($request_data as $field => $value) {
346 $this->_setObjectLinkField($field, $value);
347 }
348
349 // Permission check
350 $srctype = $this->objectlink->sourcetype;
351 if ($this->objectlink->sourcetype == 'subscription') {
352 $srctype = 'adherent';
353 }
354 if ($this->objectlink->sourcetype == 'conferenceorboothattendee') {
355 $srctype = 'projet';
356 }
357 $tgttype = $this->objectlink->targettype;
358 if ($this->objectlink->targettype == 'subscription') {
359 $tgttype = 'adherent';
360 }
361 if ($this->objectlink->targettype == 'conferenceorboothattendee') {
362 $tgttype = 'projet';
363 }
364 if (!DolibarrApiAccess::$user->hasRight((string) $srctype, 'creer') && !DolibarrApiAccess::$user->hasRight((string) $srctype, 'write')) {
365 throw new RestException(403, 'denied access to delete the objectlinks sourcetype='.$this->objectlink->sourcetype);
366 }
367 if (!DolibarrApiAccess::$user->hasRight((string) $tgttype, 'creer') && !DolibarrApiAccess::$user->hasRight((string) $tgttype, 'write')) {
368 throw new RestException(403, 'denied access to delete the objectlinks targettype='.$this->objectlink->targettype);
369 }
370
371 $findresult = $this->objectlink->fetchByValues($this->objectlink->fk_source, $this->objectlink->sourcetype, $this->objectlink->fk_target, $this->objectlink->targettype, $this->objectlink->relationtype);
372
373 if ($findresult < 0) {
374 throw new RestException(500, 'Error when finding objectlink : '.$this->objectlink->error);
375 } elseif ($findresult > 0) {
376 $result = $this->objectlink->delete(DolibarrApiAccess::$user, $notrigger);
377
378 if ($result < 0) {
379 throw new RestException(500, 'Error when delete objectlink : '.$this->objectlink->error);
380 }
381
382 return array(
383 'success' => array(
384 'code' => 200,
385 'message' => 'object link deleted'
386 )
387 );
388 } else {
389 throw new RestException(404, 'Object Link not found');
390 }
391 }
392
406 private function _fetch($id)
407 {
408 $result = $this->objectlink->fetch($id);
409 if ($result) {
410 $srctype = $this->objectlink->sourcetype;
411 if ($this->objectlink->sourcetype == 'subscription') {
412 $srctype = 'adherent';
413 }
414 if ($this->objectlink->sourcetype == 'conferenceorboothattendee') {
415 $srctype = 'projet';
416 }
417 $tgttype = $this->objectlink->targettype;
418 if ($this->objectlink->targettype == 'subscription') {
419 $tgttype = 'adherent';
420 }
421 if ($this->objectlink->targettype == 'conferenceorboothattendee') {
422 $tgttype = 'projet';
423 }
424 if (!DolibarrApiAccess::$user->hasRight(((string) $srctype), 'lire') && !DolibarrApiAccess::$user->hasRight(((string) $srctype), 'read')) {
425 throw new RestException(403, 'denied access to the objectlinks sourcetype');
426 }
427 if (!DolibarrApiAccess::$user->hasRight(((string) $tgttype), 'lire') && !DolibarrApiAccess::$user->hasRight(((string) $tgttype), 'read')) {
428 throw new RestException(403, 'denied access to the objectlinks targettype');
429 }
430 } else {
431 throw new RestException(404, 'Object Link not found');
432 }
433
434 return $this->_cleanObjectDatas($this->objectlink);
435 }
436
437 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
450 protected function _cleanObjectDatas($object)
451 {
452 // phpcs:enable
453 $object = parent::_cleanObjectDatas($object);
454
455 unset($object->module);
456 unset($object->entity);
457 unset($object->import_key);
458 unset($object->array_languages);
459 unset($object->contacts_ids);
460 unset($object->linkedObjectsIds);
461 unset($object->canvas);
462 unset($object->fk_project);
463 unset($object->contact_id);
464 unset($object->user);
465 unset($object->origin_type);
466 unset($object->origin_id);
467 unset($object->ref);
468 unset($object->ref_ext);
469 unset($object->statut);
470 unset($object->status);
471 unset($object->country_id);
472 unset($object->country_code);
473 unset($object->state_id);
474 unset($object->region_id);
475 unset($object->barcode_type);
476 unset($object->barcode_type_coder);
477 unset($object->mode_reglement_id);
478 unset($object->cond_reglement_id);
479 unset($object->demand_reason_id);
480 unset($object->transport_mode_id);
481 unset($object->shipping_method_id);
482 unset($object->shipping_method);
483 unset($object->fk_multicurrency);
484 unset($object->multicurrency_code);
485 unset($object->multicurrency_tx);
486 unset($object->multicurrency_total_ht);
487 unset($object->multicurrency_total_tva);
488 unset($object->multicurrency_total_ttc);
489 unset($object->multicurrency_total_localtax1);
490 unset($object->multicurrency_total_localtax2);
491 unset($object->last_main_doc);
492 unset($object->fk_account);
493 unset($object->note_public);
494 unset($object->note_private);
495 unset($object->total_ht);
496 unset($object->total_tva);
497 unset($object->total_localtax1);
498 unset($object->total_localtax2);
499 unset($object->total_ttc);
500 unset($object->lines);
501 unset($object->actiontypecode);
502 unset($object->name);
503 unset($object->lastname);
504 unset($object->firstname);
505 unset($object->civility_id);
506 unset($object->date_creation);
507 unset($object->date_validation);
508 unset($object->date_modification);
509 unset($object->tms);
510 unset($object->date_cloture);
511 unset($object->user_creation_id);
512 unset($object->user_validation_id);
513 unset($object->user_closing_id);
514 unset($object->user_modification_id);
515 unset($object->fk_user_creat);
516 unset($object->fk_user_modif);
517 unset($object->totalpaid);
518 unset($object->totalcreditnotes);
519 unset($object->totaldeposits);
520 unset($object->totalpaid_multicurrency);
521 unset($object->totalcreditnotes_multicurrency);
522 unset($object->totaldeposits_multicurrency);
523 unset($object->product);
524 unset($object->cond_reglement_supplier_id);
525 unset($object->deposit_percent);
526 unset($object->retained_warranty_fk_cond_reglement);
527 unset($object->warehouse_id);
528 unset($object->target);
529 unset($object->array_options);
530 unset($object->extraparams);
531 unset($object->specimen);
532
533 return $object;
534 }
535
536 // source before modifications was api_orders.class.php
546 private function _validate($data)
547 {
548 $objectlink = array();
549 foreach (ObjectLinks::$FIELDS as $field) {
550 if (!isset($data[$field])) {
551 throw new RestException(400, $field." field missing");
552 }
553 $objectlink[$field] = $data[$field];
554 }
555 return $objectlink;
556 }
557}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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
buildzip.php