dolibarr 21.0.0-alpha
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';
41
42$field = GETPOST('field', 'alpha', 2);
43$element = GETPOST('element', 'alpha', 2);
44$table_element = GETPOST('table_element', 'alpha', 2);
45$fk_element = GETPOST('fk_element', 'alpha', 2);
46$id = $fk_element;
47
48/* Example:
49field:editval_ref_customer (8 first chars will removed to know name of property)
50element:contrat
51table_element:contrat
52fk_element:4
53type:string
54value:aaa
55loadmethod:
56savemethod:
57savemethodname:
58*/
59
60// Load object according to $id and $element
61$element_ref = '';
62if (is_numeric($fk_element)) {
63 $id = (int) $fk_element;
64} else {
65 $element_ref = $fk_element;
66 $id = 0;
67}
68$object = fetchObjectByElement($id, $element, $element_ref);
69
70$module = $object->module;
71$element = $object->element;
72$usesublevelpermission = ($module != $element ? $element : '');
73if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
74 $usesublevelpermission = '';
75}
76
77//print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
78
79// Security check
80$result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
81if (!$result) {
82 httponly_accessforbidden('Not allowed by restrictArea');
83}
84
85if (!getDolGlobalString('MAIN_USE_JQUERY_JEDITABLE')) {
86 httponly_accessforbidden('Can be used only when option MAIN_USE_JQUERY_JEDITABLE is set');
87}
88
89
90/*
91 * View
92 */
93
95
96//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
97//print_r($_POST);
98
99// Load original field value
100if (!empty($field) && !empty($element) && !empty($table_element) && !empty($fk_element)) {
101 $ext_element = GETPOST('ext_element', 'alpha', 2);
102 $field = substr($field, 8); // remove prefix val_
103 $type = GETPOST('type', 'alpha', 2);
104 $value = ($type == 'ckeditor' ? GETPOST('value', '', 2) : GETPOST('value', 'alpha', 2));
105 $loadmethod = GETPOST('loadmethod', 'alpha', 2);
106 $savemethod = GETPOST('savemethod', 'alpha', 2);
107 $savemethodname = (!empty($savemethod) ? $savemethod : 'setValueFrom');
108 $newelement = $element;
109 $subelement = null;
110
111 $view = '';
112 $format = 'text';
113 $return = array();
114 $error = 0;
115
116 if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
117 $element = $regs[1];
118 $subelement = $regs[2];
119 }
120
121 if ($element == 'propal') {
122 $newelement = 'propale';
123 } elseif ($element == 'fichinter') {
124 $newelement = 'ficheinter';
125 } elseif ($element == 'product') {
126 $newelement = 'produit';
127 } elseif ($element == 'member') {
128 $newelement = 'adherent';
129 } elseif ($element == 'order_supplier') {
130 $newelement = 'fournisseur';
131 $subelement = 'commande';
132 } elseif ($element == 'invoice_supplier') {
133 $newelement = 'fournisseur';
134 $subelement = 'facture';
135 } else {
136 $newelement = $element;
137 }
138
139 $_POST['action'] = 'update'; // Keep this. It is a hack so restrictarea will test permissions on write too
140
141 $feature = $newelement;
142 $feature2 = $subelement;
143 $object_id = $fk_element;
144 if ($feature == 'expedition' || $feature == 'shipping') {
145 $feature = 'commande';
146 $object_id = 0;
147 }
148 if ($feature == 'shipping') {
149 $feature = 'commande';
150 }
151 if ($feature == 'payment') {
152 $feature = 'facture';
153 }
154 if ($feature == 'payment_supplier') {
155 $feature = 'fournisseur';
156 $feature2 = 'facture';
157 }
158 //var_dump(GETPOST('action','aZ09'));
159 //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
160 $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
161 //var_dump($user->rights);
162 /*
163 if (!empty($user->rights->$newelement->creer) || !empty($user->rights->$newelement->create) || !empty($user->rights->$newelement->write)
164 || (isset($subelement) && (!empty($user->rights->$newelement->$subelement->creer) || !empty($user->rights->$newelement->$subelement->write)))
165 || ($element == 'payment' && $user->rights->facture->paiement)
166 || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
167 */
168
169 if ($check_access) {
170 // Clean parameters
171 $newvalue = trim($value);
172
173 if ($type == 'numeric') {
174 $newvalue = price2num($newvalue);
175
176 // Check parameters
177 if (!is_numeric($newvalue)) {
178 $error++;
179 $return['error'] = $langs->trans('ErrorBadValue');
180 }
181 } elseif ($type == 'datepicker') {
182 $timestamp = GETPOSTINT('timestamp', 2);
183 $format = 'date';
184 $newvalue = ($timestamp / 1000);
185 } elseif ($type == 'select') {
186 $loadmethodname = 'load_cache_'.$loadmethod;
187 $loadcachename = 'cache_'.$loadmethod;
188 $loadviewname = 'view_'.$loadmethod;
189
190 $form = new Form($db);
191 if (method_exists($form, $loadmethodname)) {
192 $ret = $form->$loadmethodname();
193 if ($ret > 0) {
194 $loadcache = $form->$loadcachename;
195 $value = $loadcache[$newvalue];
196
197 if (!empty($form->$loadviewname)) {
198 $loadview = $form->$loadviewname;
199 $view = $loadview[$newvalue];
200 }
201 } else {
202 $error++;
203 $return['error'] = $form->error;
204 }
205 } else {
206 $module = $subelement = $ext_element;
207 if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
208 $module = $regs[1];
209 $subelement = $regs[2];
210 }
211
212 dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
213 $classname = 'Actions'.ucfirst($subelement);
214 $object = new $classname($db);
215 '@phan-var-force CommonHookActions $object';
216 $ret = $object->$loadmethodname();
217 if ($ret > 0) {
218 $loadcache = $object->$loadcachename;
219 $value = $loadcache[$newvalue];
220
221 if (!empty($object->$loadviewname)) {
222 $loadview = $object->$loadviewname;
223 $view = $loadview[$newvalue];
224 }
225 } else {
226 $error++;
227 $return['error'] = $object->error;
228 }
229 }
230 }
231
232 if (!$error) {
233 if ((isset($object) && !is_object($object)) || empty($savemethod)) {
234 $object = new GenericObject($db);
235 }
236
237 // Specific for add_object_linked()
238 // TODO add a function for variable treatment
239 $object->ext_fk_element = $newvalue;
240 $object->ext_element = $ext_element;
241 $object->fk_element = $fk_element;
242 $object->element = $element;
243
244 $ret = $object->$savemethodname($field, $newvalue, $table_element, $fk_element, $format);
245 if ($ret > 0) {
246 if ($type == 'numeric') {
247 $value = price($newvalue);
248 } elseif ($type == 'textarea') {
249 $value = dol_nl2br($newvalue);
250 }
251
252 $return['value'] = $value;
253 $return['view'] = (!empty($view) ? $view : $value);
254 } else {
255 $return['error'] = $object->error;
256 }
257 }
258
259 echo json_encode($return);
260 } else {
261 echo $langs->trans('NotEnoughPermissions');
262 }
263}
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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.