dolibarr 21.0.4
saveinplace.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011-2012 Regis Houssin <regis.houssin@inodbox.com>
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 */
19
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', '1'); // Disables token renewal
27}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREAJAX')) {
32 define('NOREQUIREAJAX', '1');
33}
34if (!defined('NOREQUIRESOC')) {
35 define('NOREQUIRESOC', '1');
36}
37
38// Load Dolibarr environment
39require '../../main.inc.php';
40require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
49$field = GETPOST('field', 'alpha', 2);
50$element = GETPOST('element', 'alpha', 2);
51$table_element = GETPOST('table_element', 'alpha', 2);
52$fk_element = GETPOST('fk_element', 'alpha', 2);
53$id = $fk_element;
54
55/* Example:
56field:editval_ref_customer (8 first chars will removed to know name of property)
57element:contrat
58table_element:contrat
59fk_element:4
60type:string
61value:aaa
62loadmethod:
63savemethod:
64savemethodname:
65*/
66
67// Load object according to $id and $element
68$element_ref = '';
69if (is_numeric($fk_element)) {
70 $id = (int) $fk_element;
71} else {
72 $element_ref = $fk_element;
73 $id = 0;
74}
75$object = fetchObjectByElement($id, $element, $element_ref);
76
77$module = $object->module;
78$element = $object->element;
79$usesublevelpermission = ($module != $element ? $element : '');
80if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
81 $usesublevelpermission = '';
82}
83
84//print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
85
86// Security check
87$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
88if (!$result) {
89 httponly_accessforbidden('Not allowed by restrictArea');
90}
91
92if (!getDolGlobalString('MAIN_USE_JQUERY_JEDITABLE')) {
93 httponly_accessforbidden('Can be used only when option MAIN_USE_JQUERY_JEDITABLE is set');
94}
95
96
97/*
98 * View
99 */
100
102
103//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
104//print_r($_POST);
105
106// Load original field value
107if (!empty($field) && !empty($element) && !empty($table_element) && !empty($fk_element)) {
108 $ext_element = GETPOST('ext_element', 'alpha', 2);
109 $field = substr($field, 8); // remove prefix val_
110 $type = GETPOST('type', 'alpha', 2);
111 $value = ($type == 'ckeditor' ? GETPOST('value', '', 2) : GETPOST('value', 'alpha', 2));
112 $loadmethod = GETPOST('loadmethod', 'alpha', 2);
113 $savemethod = GETPOST('savemethod', 'alpha', 2);
114 $newelement = $element;
115 $subelement = null;
116
117 $view = '';
118 $format = 'text';
119 $return = array();
120 $error = 0;
121
122 if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
123 $element = $regs[1];
124 $subelement = $regs[2];
125 }
126
127 if ($element == 'propal') {
128 $newelement = 'propale';
129 } elseif ($element == 'fichinter') {
130 $newelement = 'ficheinter';
131 } elseif ($element == 'product') {
132 $newelement = 'produit';
133 } elseif ($element == 'member') {
134 $newelement = 'adherent';
135 } elseif ($element == 'order_supplier') {
136 $newelement = 'fournisseur';
137 $subelement = 'commande';
138 } elseif ($element == 'invoice_supplier') {
139 $newelement = 'fournisseur';
140 $subelement = 'facture';
141 } else {
142 $newelement = $element;
143 }
144
145 $_POST['action'] = 'update'; // Keep this. It is a hack so restrictarea will test permissions on write too
146
147 $feature = $newelement;
148 $feature2 = $subelement;
149 $object_id = $fk_element;
150 if ($feature == 'expedition' || $feature == 'shipping') {
151 $feature = 'commande';
152 $object_id = 0;
153 }
154 if ($feature == 'shipping') {
155 $feature = 'commande';
156 }
157 if ($feature == 'payment') {
158 $feature = 'facture';
159 }
160 if ($feature == 'payment_supplier') {
161 $feature = 'fournisseur';
162 $feature2 = 'facture';
163 }
164 //var_dump(GETPOST('action','aZ09'));
165 //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
166 $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
167 //var_dump($user->rights);
168 /*
169 if (!empty($user->rights->$newelement->creer) || !empty($user->rights->$newelement->create) || !empty($user->rights->$newelement->write)
170 || (isset($subelement) && (!empty($user->rights->$newelement->$subelement->creer) || !empty($user->rights->$newelement->$subelement->write)))
171 || ($element == 'payment' && $user->rights->facture->paiement)
172 || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
173 */
174
175 if ($check_access) {
176 // Clean parameters
177 $newvalue = trim($value);
178
179 if ($type == 'numeric') {
180 $newvalue = price2num($newvalue);
181
182 // Check parameters
183 if (!is_numeric($newvalue)) {
184 $error++;
185 $return['error'] = $langs->trans('ErrorBadValue');
186 }
187 } elseif ($type == 'datepicker') {
188 $timestamp = GETPOSTINT('timestamp', 2);
189 $format = 'date';
190 $newvalue = ($timestamp / 1000);
191 } elseif ($type == 'select') {
192 $loadmethodname = 'load_cache_'.$loadmethod;
193 $loadcachename = 'cache_'.$loadmethod;
194 $loadviewname = 'view_'.$loadmethod;
195
196 $form = new Form($db);
197 if (method_exists($form, $loadmethodname)) {
198 $ret = $form->$loadmethodname();
199 if ($ret > 0) {
200 $loadcache = $form->$loadcachename;
201 $value = $loadcache[$newvalue];
202
203 if (!empty($form->$loadviewname)) {
204 $loadview = $form->$loadviewname;
205 $view = $loadview[$newvalue];
206 }
207 } else {
208 $error++;
209 $return['error'] = $form->error;
210 }
211 } else {
212 $module = $subelement = $ext_element;
213 if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
214 $module = $regs[1];
215 $subelement = $regs[2];
216 }
217
218 dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
219 $classname = 'Actions'.ucfirst($subelement);
220 $object = new $classname($db);
221 '@phan-var-force CommonHookActions $object';
222 $ret = $object->$loadmethodname();
223 if ($ret > 0) {
224 $loadcache = $object->$loadcachename;
225 $value = $loadcache[$newvalue];
226
227 if (!empty($object->$loadviewname)) {
228 $loadview = $object->$loadviewname;
229 $view = $loadview[$newvalue];
230 }
231 } else {
232 $error++;
233 $return['error'] = $object->error;
234 }
235 }
236 }
237
238 if (!$error && isset($object) && is_object($object)) {
239 // Specific for add_object_linked()
240 // TODO add a function for variable treatment
241 $object->ext_fk_element = $newvalue;
242 $object->ext_element = $ext_element;
243 $object->fk_element = $fk_element;
244 $object->element = $element;
245
246 $ret = $object->setValueFrom($field, $newvalue, $object->table_element, $fk_element, $format);
247 if ($ret > 0) {
248 if ($type == 'numeric') {
249 $value = price($newvalue);
250 } elseif ($type == 'textarea') {
251 $value = dol_nl2br($newvalue);
252 }
253
254 $return['value'] = $value;
255 $return['view'] = (!empty($view) ? $view : $value);
256 } else {
257 $return['error'] = $object->error;
258 }
259 }
260
261 echo json_encode($return);
262 } else {
263 echo $langs->trans('NotEnoughPermissions');
264 }
265}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage generation of HTML components Only common components must be here.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
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.
fetchObjectByElement($element_id, $element_type, $element_ref='', $useCache=0, $maxCacheByType=10)
Fetch an object from its id and element_type Inclusion of classes is automatic.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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, $mode=0)
Check permissions of a user to show a page and an object.