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