dolibarr 22.0.5
actions_dellink.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015-2016 Laurent Destailleur <eldy@users.sourceforge.net>
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 * or see https://www.gnu.org/
19 */
20
27// $action must be defined
28// $object must be defined
29// $permissiondellink must be defined
30
41$dellinkid = GETPOSTINT('dellinkid');
42$addlink = GETPOST('addlink', 'alpha');
43$addlinkids = GETPOST('idtolinkto', 'array:int');
44$addlinkref = GETPOST('reftolinkto', 'alpha');
45$cancellink = GETPOST('cancel', 'alpha');
46
47// Link object to another object
48if ($action == 'addlink' && !empty($permissiondellink) && !$cancellink && $id > 0 && !empty($addlinkids)) {
49 $object->fetch($id);
50 $object->fetch_thirdparty();
51 foreach ($addlinkids as $addlinkid) {
52 $result = $object->add_object_linked($addlink, $addlinkid);
53 }
54 $object->clearObjectLinkedCache();
55}
56
57// Link by reference
58if ($action == 'addlinkbyref' && !empty($permissiondellink) && !$cancellink && $id > 0 && !empty($addlinkref) && !getDolGlobalString('MAIN_HIDE_LINK_BY_REF_IN_LINKTO')) {
59 $element_prop = getElementProperties($addlink);
60 if (is_array($element_prop)) {
61 dol_include_once('/' . $element_prop['classpath'] . '/' . $element_prop['classfile'] . '.class.php');
62
63 $objecttmp = new $element_prop['classname']($db);
64 '@phan-var-force CommonObject $objecttmp';
66 $ret = $objecttmp->fetch(0, $addlinkref);
67 if ($ret > 0) {
68 $object->fetch($id);
69 $object->fetch_thirdparty();
70 $result = $object->add_object_linked($addlink, $objecttmp->id);
71 if (isset($_POST['reftolinkto'])) {
72 unset($_POST['reftolinkto']);
73 }
74 $object->clearObjectLinkedCache();
75 } elseif ($ret < 0) {
76 setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
77 } else {
78 $langs->load('errors');
79 setEventMessage($langs->trans('ErrorRecordNotFound'), 'errors');
80 }
81 }
82}
83
84// Delete link in table llx_element_element
85if ($action == 'dellink' && !empty($permissiondellink) && !$cancellink && $dellinkid > 0) {
86 if (empty($object->id) && $id > 0) {
87 $object->fetch($id);
88 }
89 // The link must involve the current object, else any link could be deleted by its rowid alone
90 $linkfound = false;
91 if ($object->id > 0) {
92 $elementtypes = array_unique([$object->getElementType(), $object->element]);
93 $sqlelementtypes = "'".implode("','", array_map([$db, 'escape'], $elementtypes))."'";
94 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."element_element";
95 $sql .= " WHERE rowid = ".((int) $dellinkid);
96 $sql .= " AND ((fk_source = ".((int) $object->id)." AND sourcetype IN (".$db->sanitize($sqlelementtypes, 1)."))";
97 $sql .= " OR (fk_target = ".((int) $object->id)." AND targettype IN (".$db->sanitize($sqlelementtypes, 1).")))";
98 $resql = $db->query($sql);
99 if ($resql && $db->num_rows($resql) > 0) {
100 $linkfound = true;
101 }
102 }
103
104 $result = 0;
105 if (!$linkfound) {
106 setEventMessages($langs->trans('ErrorRecordNotFound'), null, 'errors');
107 } else {
108 $result = $object->deleteObjectLinked(0, '', 0, '', $dellinkid);
109 $object->clearObjectLinkedCache();
110 }
111
112 if ($result < 0) {
113 setEventMessages($object->error, $object->errors, 'errors');
114 }
115}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
getElementProperties($elementType)
Get an array with properties of an element.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.