dolibarr 25.0.0-alpha
objectfield.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 John BOTELLA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
34if (!defined('NOTOKENRENEWAL')) {
35 define('NOTOKENRENEWAL', '1'); // Disables token renewal, the page is called several times in a row
36}
37if (!defined('NOREQUIREMENU')) {
38 define('NOREQUIREMENU', '1');
39}
40if (!defined('NOREQUIREAJAX')) {
41 define('NOREQUIREAJAX', '1');
42}
43if (!defined('NOREDIRECTBYMAINTOLOGIN')) {
44 define('NOREDIRECTBYMAINTOLOGIN', '1');
45}
46
47// Load Dolibarr environment
48require '../../main.inc.php';
49require_once DOL_DOCUMENT_ROOT.'/core/class/jsonResponse.class.php';
50
58$element = GETPOST('element', 'aZ09');
59$id = GETPOSTINT('id');
60$field = GETPOST('field', 'aZ09');
61$fieldlist = GETPOST('fields', 'aZ09comma');
62$tms = GETPOSTINT('tms');
63$withinput = GETPOSTINT('input');
64
65$isupdate = ($_SERVER['REQUEST_METHOD'] === 'POST');
66
67$langs->load('errors');
68
69top_httphead('application/json');
70
71$jsonResponse = new JsonResponse();
72
73// Update only with POST, so the CSRF token is always checked. An invalid token empties $_POST, so $field is empty.
74if (!$isupdate && $_SERVER['REQUEST_METHOD'] !== 'GET') {
75 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorBadParameters'), JsonResponse::HTTP_METHOD_NOT_ALLOWED);
76 $jsonResponse->output();
77}
78if (empty($element) || empty($id) || ($isupdate ? empty($field) : !GETPOSTISSET('fields'))) {
79 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorBadParameters'), JsonResponse::HTTP_BAD_REQUEST);
80 $jsonResponse->output();
81}
82
83// Load object
84$object = fetchObjectByElement($id, $element);
85if (!is_object($object) || $object->id <= 0) {
86 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorRecordNotFound'), JsonResponse::HTTP_NOT_FOUND);
87 $jsonResponse->output();
88}
89
90// A line is protected by the permissions of its parent object (ex: a line of evaluation by its evaluation)
91$objecttocheck = $object;
92if ($object instanceof CommonObjectLine && !empty($object->parent_element) && !empty($object->fk_parent_attribute)) {
93 $parentattribute = $object->fk_parent_attribute;
94 $objecttocheck = fetchObjectByElement((int) $object->$parentattribute, $object->parent_element);
95 if (!is_object($objecttocheck) || $objecttocheck->id <= 0) {
96 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorRecordNotFound'), JsonResponse::HTTP_NOT_FOUND);
97 $jsonResponse->output();
98 }
99}
100
101// Security check, restrictedArea() tests the read or write permission and the access of external users.
102// Objects of a module with several objects (ex: evaluation of hrm, modules built with ModuleBuilder) have their
103// permissions at a second level named like the element, like in CommonObject::getRightsForUser().
104$features = empty($objecttocheck->module) ? $objecttocheck->element : $objecttocheck->module;
105$feature2 = '';
106if (!empty($objecttocheck->module) && $objecttocheck->module != $objecttocheck->element && isset($user->rights->{$objecttocheck->module}->{$objecttocheck->element})) {
107 $feature2 = $objecttocheck->element;
108}
109$result = restrictedArea($user, $features, $objecttocheck, $objecttocheck->table_element, $feature2, 'fk_soc', 'rowid', 0, 1, $isupdate ? 'write' : 'read');
110if (!$result) {
111 $jsonResponse->setError($langs->transnoentitiesnoconv('NotEnoughPermissions'), JsonResponse::HTTP_FORBIDDEN);
112 $jsonResponse->output();
113}
114
122function objectFieldGetOutput($object, $fieldname)
123{
124 return array(
125 'value' => $object->getFieldUiValue($fieldname),
126 'html' => $object->ajaxOutputField($fieldname),
127 );
128}
129
130// Read the values of fields
131if (!$isupdate) {
132 $fields = array();
133 foreach (explode(',', $fieldlist) as $fieldname) {
134 // Fields not readable are ignored, the page may display fields not defined in $fields
135 if ($fieldname !== '' && $object->isFieldReadableFromUi($user, $fieldname)) {
136 $fields[$fieldname] = objectFieldGetOutput($object, $fieldname);
137 // Input only for fields the user can change, so the page opens the edition of a field only when it can be saved
138 if ($withinput) {
139 $fields[$fieldname]['input'] = $object->isFieldEditableFromUi($user, $fieldname) ? $object->ajaxInputField($fieldname) : null;
140 }
141 }
142 }
143
144 $jsonResponse->data = array(
145 'element' => $element,
146 'id' => $id,
147 'fields' => $fields,
148 'tms' => empty($object->date_modification) ? null : (int) $object->date_modification,
149 );
150
151 $jsonResponse->setSuccess();
152 $jsonResponse->output();
153}
154
155// The user interface can only change fields with an explicit opt-in, never isFieldEditAllowed() alone
156if (!$object->isFieldEditableFromUi($user, $field)) {
157 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorFieldIsNotEditable'), JsonResponse::HTTP_FORBIDDEN);
158 $jsonResponse->output();
159}
160
161// Optional optimistic lock: refuse if the object was modified since the date of modification known by the page
162if ($tms > 0 && !empty($object->date_modification) && $object->date_modification > $tms) {
163 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorRecordModifiedSinceDisplay'), JsonResponse::HTTP_CONFLICT);
164 $jsonResponse->output();
165}
166
167// Get the posted value, the object knows the inputs of its fields
168$value = $object->getFieldValueFromAjaxPost($field);
169if ($value === null) {
170 // Input not posted or type of field not supported
171 $jsonResponse->setError($langs->transnoentitiesnoconv('ErrorBadParameters'), JsonResponse::HTTP_BAD_REQUEST);
172 $jsonResponse->output();
173}
174
175
176/*
177 * Actions
178 */
179
180// Values before the update, to return the other fields changed by the update
181$valuesbefore = array();
182foreach (array_keys($object->fields) as $fieldname) {
183 if ($object->isFieldReadableFromUi($user, $fieldname)) {
184 $valuesbefore[$fieldname] = $object->getFieldUiValue($fieldname);
185 }
186}
187
188$result = $object->updateFieldFromUi($user, $field, $value);
189if ($result < 0) {
190 $jsonResponse->setError(dol_html_entity_decode($object->error, ENT_QUOTES | ENT_HTML5), JsonResponse::HTTP_BAD_REQUEST);
191 $jsonResponse->output();
192}
193
194// Reload object to return the value really saved and the new date of modification
195$reloadedobject = fetchObjectByElement($id, $element);
196if (is_object($reloadedobject) && $reloadedobject->id > 0) {
197 $object = $reloadedobject;
198}
199
200// The updated field and the other fields changed by the update
201$fields = array($field => objectFieldGetOutput($object, $field));
202foreach ($valuesbefore as $fieldname => $valuebefore) {
203 $valueafter = $object->getFieldUiValue($fieldname);
204 if ($fieldname != $field && $valueafter !== $valuebefore) {
205 $fields[$fieldname] = objectFieldGetOutput($object, $fieldname);
206 }
207}
208
209$jsonResponse->data = array(
210 'element' => $element,
211 'id' => $id,
212 'field' => $field,
213 'value' => $fields[$field]['value'],
214 'html' => $fields[$field]['html'],
215 'fields' => $fields,
216 'tms' => empty($object->date_modification) ? null : (int) $object->date_modification,
217);
218
219$jsonResponse->setSuccess();
220$jsonResponse->output();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class JsonResponse used for ajax responses in Dolibarr.
dol_html_entity_decode($a, $b, $c='UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
if(! $result) objectFieldGetOutput($object, $fieldname)
Return the value of a field of an object and its output html.
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.