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