dolibarr 21.0.0-beta
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 $savemethodname = (!empty($savemethod) ? $savemethod : 'setValueFrom');
115 $newelement = $element;
116 $subelement = null;
117
118 $view = '';
119 $format = 'text';
120 $return = array();
121 $error = 0;
122
123 if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
124 $element = $regs[1];
125 $subelement = $regs[2];
126 }
127
128 if ($element == 'propal') {
129 $newelement = 'propale';
130 } elseif ($element == 'fichinter') {
131 $newelement = 'ficheinter';
132 } elseif ($element == 'product') {
133 $newelement = 'produit';
134 } elseif ($element == 'member') {
135 $newelement = 'adherent';
136 } elseif ($element == 'order_supplier') {
137 $newelement = 'fournisseur';
138 $subelement = 'commande';
139 } elseif ($element == 'invoice_supplier') {
140 $newelement = 'fournisseur';
141 $subelement = 'facture';
142 } else {
143 $newelement = $element;
144 }
145
146 $_POST['action'] = 'update'; // Keep this. It is a hack so restrictarea will test permissions on write too
147
148 $feature = $newelement;
149 $feature2 = $subelement;
150 $object_id = $fk_element;
151 if ($feature == 'expedition' || $feature == 'shipping') {
152 $feature = 'commande';
153 $object_id = 0;
154 }
155 if ($feature == 'shipping') {
156 $feature = 'commande';
157 }
158 if ($feature == 'payment') {
159 $feature = 'facture';
160 }
161 if ($feature == 'payment_supplier') {
162 $feature = 'fournisseur';
163 $feature2 = 'facture';
164 }
165 //var_dump(GETPOST('action','aZ09'));
166 //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
167 $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
168 //var_dump($user->rights);
169 /*
170 if (!empty($user->rights->$newelement->creer) || !empty($user->rights->$newelement->create) || !empty($user->rights->$newelement->write)
171 || (isset($subelement) && (!empty($user->rights->$newelement->$subelement->creer) || !empty($user->rights->$newelement->$subelement->write)))
172 || ($element == 'payment' && $user->rights->facture->paiement)
173 || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
174 */
175
176 if ($check_access) {
177 // Clean parameters
178 $newvalue = trim($value);
179
180 if ($type == 'numeric') {
181 $newvalue = price2num($newvalue);
182
183 // Check parameters
184 if (!is_numeric($newvalue)) {
185 $error++;
186 $return['error'] = $langs->trans('ErrorBadValue');
187 }
188 } elseif ($type == 'datepicker') {
189 $timestamp = GETPOSTINT('timestamp', 2);
190 $format = 'date';
191 $newvalue = ($timestamp / 1000);
192 } elseif ($type == 'select') {
193 $loadmethodname = 'load_cache_'.$loadmethod;
194 $loadcachename = 'cache_'.$loadmethod;
195 $loadviewname = 'view_'.$loadmethod;
196
197 $form = new Form($db);
198 if (method_exists($form, $loadmethodname)) {
199 $ret = $form->$loadmethodname();
200 if ($ret > 0) {
201 $loadcache = $form->$loadcachename;
202 $value = $loadcache[$newvalue];
203
204 if (!empty($form->$loadviewname)) {
205 $loadview = $form->$loadviewname;
206 $view = $loadview[$newvalue];
207 }
208 } else {
209 $error++;
210 $return['error'] = $form->error;
211 }
212 } else {
213 $module = $subelement = $ext_element;
214 if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
215 $module = $regs[1];
216 $subelement = $regs[2];
217 }
218
219 dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
220 $classname = 'Actions'.ucfirst($subelement);
221 $object = new $classname($db);
222 '@phan-var-force CommonHookActions $object';
223 $ret = $object->$loadmethodname();
224 if ($ret > 0) {
225 $loadcache = $object->$loadcachename;
226 $value = $loadcache[$newvalue];
227
228 if (!empty($object->$loadviewname)) {
229 $loadview = $object->$loadviewname;
230 $view = $loadview[$newvalue];
231 }
232 } else {
233 $error++;
234 $return['error'] = $object->error;
235 }
236 }
237 }
238
239 if (!$error) {
240 if ((isset($object) && !is_object($object)) || empty($savemethod)) {
241 $object = new GenericObject($db);
242 }
243
244 // Specific for add_object_linked()
245 // TODO add a function for variable treatment
246 $object->ext_fk_element = $newvalue;
247 $object->ext_element = $ext_element;
248 $object->fk_element = $fk_element;
249 $object->element = $element;
250
251 $ret = $object->$savemethodname($field, $newvalue, $table_element, $fk_element, $format);
252 if ($ret > 0) {
253 if ($type == 'numeric') {
254 $value = price($newvalue);
255 } elseif ($type == 'textarea') {
256 $value = dol_nl2br($newvalue);
257 }
258
259 $return['value'] = $value;
260 $return['view'] = (!empty($view) ? $view : $value);
261 } else {
262 $return['error'] = $object->error;
263 }
264 }
265
266 echo json_encode($return);
267 } else {
268 echo $langs->trans('NotEnoughPermissions');
269 }
270}
$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.
Class of a generic business object.
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.