dolibarr 25.0.0-alpha
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-2026 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// $id must be defined
30// $permissiondellink must be defined
31
42'
43@phan-var-force CommonObject $object
44@phan-var-force string $action
45@phan-var-force int $permissiondellink
46@phan-var-force int $id
47';
48
49$dellinkid = GETPOSTINT('dellinkid');
50$addlink = GETPOST('addlink', 'alpha');
51$addlinkids = GETPOST('idtolinkto', 'array:int');
52$addlinkref = GETPOST('reftolinkto', 'alpha');
53$cancellink = GETPOST('cancel', 'alpha');
54
55// Link object to another object
56if ($action == 'addlink' && !empty($permissiondellink) && !$cancellink && $id > 0 && !empty($addlinkids)) {
57 $object->fetch($id);
58 $object->fetch_thirdparty();
59 foreach ($addlinkids as $addlinkid) {
60 $result = $object->add_object_linked($addlink, $addlinkid);
61 }
62 $object->clearObjectLinkedCache();
63}
64
65// Link by reference
66if ($action == 'addlinkbyref' && !empty($permissiondellink) && !$cancellink && $id > 0 && !empty($addlinkref) && !getDolGlobalString('MAIN_HIDE_LINK_BY_REF_IN_LINKTO')) {
67 $element_prop = getElementProperties($addlink);
68 if (is_array($element_prop)) {
69 dol_include_once('/' . $element_prop['classpath'] . '/' . $element_prop['classfile'] . '.class.php');
70
71 $objecttmp = new $element_prop['classname']($db);
72 '@phan-var-force CommonObject $objecttmp';
74 $ret = $objecttmp->fetch(0, $addlinkref);
75 if ($ret > 0) {
76 $object->fetch($id);
77 $object->fetch_thirdparty();
78 $result = $object->add_object_linked($addlink, $objecttmp->id);
79 if (isset($_POST['reftolinkto'])) {
80 unset($_POST['reftolinkto']);
81 }
82 $object->clearObjectLinkedCache();
83 } elseif ($ret < 0) {
84 setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
85 } else {
86 $langs->load('errors');
87 setEventMessage($langs->trans('ErrorRecordNotFound'), 'errors');
88 }
89 }
90}
91
92// Delete link in table llx_element_element
93if ($action == 'dellink' && !empty($permissiondellink) && !$cancellink && $dellinkid > 0) {
94 if (empty($object->id) && $id > 0) {
95 $object->fetch($id);
96 }
97 // The link must involve the current object, else any link could be deleted by its rowid alone
98 $linkfound = false;
99 if ($object->id > 0) {
100 $elementtypes = array_unique([$object->getElementType(), $object->element]);
101 $sqlelementtypes = "'".implode("','", array_map([$db, 'escape'], $elementtypes))."'";
102 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."element_element";
103 $sql .= " WHERE rowid = ".((int) $dellinkid);
104 $sql .= " AND ((fk_source = ".((int) $object->id)." AND sourcetype IN (".$db->sanitize($sqlelementtypes, 1)."))";
105 $sql .= " OR (fk_target = ".((int) $object->id)." AND targettype IN (".$db->sanitize($sqlelementtypes, 1).")))";
106 $resql = $db->query($sql);
107 if ($resql && $db->num_rows($resql) > 0) {
108 $linkfound = true;
109 }
110 }
111
112 $result = 0;
113 if (!$linkfound) {
114 setEventMessages($langs->trans('ErrorRecordNotFound'), null, 'errors');
115 } else {
116 $result = $object->deleteObjectLinked(0, '', 0, '', $dellinkid);
117 $object->clearObjectLinkedCache();
118 }
119
120 if ($result < 0) {
121 setEventMessages($object->error, $object->errors, 'errors');
122 }
123}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.