dolibarr 24.0.2
updateextrafield.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-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
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', 1); // Disables token renewal
27}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREHTML')) {
32 define('NOREQUIREHTML', '1');
33}
34if (!defined('NOREQUIREAJAX')) {
35 define('NOREQUIREAJAX', '1');
36}
37if (!defined('NOREQUIRESOC')) {
38 define('NOREQUIRESOC', '1');
39}
40
41// Load Dolibarr environment
42include '../../main.inc.php';
52$objectType = GETPOST('objectType', 'aZ09'); // modulepart
53$objectId = GETPOST('objectId', 'aZ09');
54$field = GETPOST('field', 'aZ09');
55$value = GETPOST('value', 'alpha');
56
57$module = getElementProperties($objectType)['module'];
58$element_ref = '';
59if (is_numeric($objectId)) {
60 $objectId = (int) $objectId;
61} else {
62 $element_ref = $objectId;
63 $objectId = 0;
64}
65$object = fetchObjectByElement($objectId, $objectType, $element_ref);
67if (empty($object->element)) {
68 httponly_accessforbidden('Failed to get object with fetchObjectByElement(id=' . $objectId . ', objecttype=' . $objectType . ')');
69}
70
71$module = $object->module;
72$element = $object->element;
73
74// Security check
75$usesublevelpermission = ($module != $element ? $element : '');
76if ($usesublevelpermission && !$user->hasRight($module, $element, 'write') && !$user->hasRight($module, 'write')) { // There is no permission on object defined, we will check permission on module directly
77 $usesublevelpermission = '';
78}
79// print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
80
81// Security check with mode 'write', so restrictedArea() tests the write permission and not only the read
82// permission, because this page makes a write operation on the object (update of an extrafield value).
83$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1, 'write'); // Call with nodie return
84if (!$result) {
85 httponly_accessforbidden('Not allowed by restrictArea');
86}
87
88
89
90/*
91 * View
92 */
93
94dol_syslog("Call ajax core/ajax/updateextrafield.php");
95
97
98// Update the object field with the new value
99if ($object->id > 0 && $field && isset($value)) {
100 // Fetch optionals attributes and labels
101 $extrafields->fetch_name_optionals_label($object->table_element);
102
103 // TODO Test specific permission of extrafield $field for object $object. It is stored into
104 // $extrafields->attributes[$object->table_element]['label']['perms'][$key]
105
106 $object->array_options['options_'.$field] = $value;
107 if ($object instanceof Societe) {
108 $result = $object->update($object->id, $user);
109 } else {
110 $result = $object->update($user);
111 }
112
113 if ($result < 0) {
114 print json_encode(['status' => 'error', 'message' => 'Error updating '. $field]);
115 } else {
116 print json_encode(['status' => 'success', 'message' => $field . ' updated successfully']);
117 }
118}
119
120$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage third parties objects (customers, suppliers, prospects...)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getElementProperties($elementType)
Get an array with properties of an element.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.