dolibarr  16.0.5
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 
23 if (!defined('NOTOKENRENEWAL')) {
24  define('NOTOKENRENEWAL', '1'); // Disables token renewal
25 }
26 if (!defined('NOREQUIREMENU')) {
27  define('NOREQUIREMENU', '1');
28 }
29 if (!defined('NOREQUIREAJAX')) {
30  define('NOREQUIREAJAX', '1');
31 }
32 if (!defined('NOREQUIRESOC')) {
33  define('NOREQUIRESOC', '1');
34 }
35 
36 require '../../main.inc.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
38 
39 $field = GETPOST('field', 'alpha', 2);
40 $element = GETPOST('element', 'alpha', 2);
41 $table_element = GETPOST('table_element', 'alpha', 2);
42 $fk_element = GETPOST('fk_element', 'alpha', 2);
43 
44 /* Example:
45 field:editval_ref_customer (8 first chars will removed to know name of property)
46 element:contrat
47 table_element:contrat
48 fk_element:4
49 type:string
50 value:aaa
51 loadmethod:
52 savemethod:
53 savemethodname:
54 */
55 
56 
57 /*
58  * View
59  */
60 
61 top_httphead();
62 
63 //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
64 //print_r($_POST);
65 
66 // Load original field value
67 if (!empty($field) && !empty($element) && !empty($table_element) && !empty($fk_element)) {
68  $ext_element = GETPOST('ext_element', 'alpha', 2);
69  $field = substr($field, 8); // remove prefix val_
70  $type = GETPOST('type', 'alpha', 2);
71  $value = ($type == 'ckeditor' ? GETPOST('value', '', 2) : GETPOST('value', 'alpha', 2));
72  $loadmethod = GETPOST('loadmethod', 'alpha', 2);
73  $savemethod = GETPOST('savemethod', 'alpha', 2);
74  $savemethodname = (!empty($savemethod) ? $savemethod : 'setValueFrom');
75  $newelement = $element;
76 
77  $view = '';
78  $format = 'text';
79  $return = array();
80  $error = 0;
81 
82  if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
83  $element = $regs[1];
84  $subelement = $regs[2];
85  }
86 
87  if ($element == 'propal') {
88  $newelement = 'propale';
89  } elseif ($element == 'fichinter') {
90  $newelement = 'ficheinter';
91  } elseif ($element == 'product') {
92  $newelement = 'produit';
93  } elseif ($element == 'member') {
94  $newelement = 'adherent';
95  } elseif ($element == 'order_supplier') {
96  $newelement = 'fournisseur';
97  $subelement = 'commande';
98  } elseif ($element == 'invoice_supplier') {
99  $newelement = 'fournisseur';
100  $subelement = 'facture';
101  } else {
102  $newelement = $element;
103  }
104 
105  $_POST['action'] = 'update'; // Hack so restrictarea will test permissions on write too
106 
107  $feature = $newelement;
108  $feature2 = $subelement;
109  $object_id = $fk_element;
110  if ($feature == 'expedition' || $feature == 'shipping') {
111  $feature = 'commande';
112  $object_id = 0;
113  }
114  if ($feature == 'shipping') {
115  $feature = 'commande';
116  }
117  if ($feature == 'payment') {
118  $feature = 'facture';
119  }
120  if ($feature == 'payment_supplier') {
121  $feature = 'fournisseur';
122  $feature2 = 'facture';
123  }
124  //var_dump(GETPOST('action','aZ09'));
125  //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
126  $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
127  //var_dump($user->rights);
128  /*
129  if (! empty($user->rights->$newelement->creer) || ! empty($user->rights->$newelement->create) || ! empty($user->rights->$newelement->write)
130  || (isset($subelement) && (! empty($user->rights->$newelement->$subelement->creer) || ! empty($user->rights->$newelement->$subelement->write)))
131  || ($element == 'payment' && $user->rights->facture->paiement)
132  || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
133  */
134 
135  if ($check_access) {
136  // Clean parameters
137  $newvalue = trim($value);
138 
139  if ($type == 'numeric') {
140  $newvalue = price2num($newvalue);
141 
142  // Check parameters
143  if (!is_numeric($newvalue)) {
144  $error++;
145  $return['error'] = $langs->trans('ErrorBadValue');
146  }
147  } elseif ($type == 'datepicker') {
148  $timestamp = GETPOST('timestamp', 'int', 2);
149  $format = 'date';
150  $newvalue = ($timestamp / 1000);
151  } elseif ($type == 'select') {
152  $loadmethodname = 'load_cache_'.$loadmethod;
153  $loadcachename = 'cache_'.$loadmethod;
154  $loadviewname = 'view_'.$loadmethod;
155 
156  $form = new Form($db);
157  if (method_exists($form, $loadmethodname)) {
158  $ret = $form->$loadmethodname();
159  if ($ret > 0) {
160  $loadcache = $form->$loadcachename;
161  $value = $loadcache[$newvalue];
162 
163  if (!empty($form->$loadviewname)) {
164  $loadview = $form->$loadviewname;
165  $view = $loadview[$newvalue];
166  }
167  } else {
168  $error++;
169  $return['error'] = $form->error;
170  }
171  } else {
172  $module = $subelement = $ext_element;
173  if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
174  $module = $regs[1];
175  $subelement = $regs[2];
176  }
177 
178  dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
179  $classname = 'Actions'.ucfirst($subelement);
180  $object = new $classname($db);
181  $ret = $object->$loadmethodname();
182  if ($ret > 0) {
183  $loadcache = $object->$loadcachename;
184  $value = $loadcache[$newvalue];
185 
186  if (!empty($object->$loadviewname)) {
187  $loadview = $object->$loadviewname;
188  $view = $loadview[$newvalue];
189  }
190  } else {
191  $error++;
192  $return['error'] = $object->error;
193  }
194  }
195  }
196 
197  if (!$error) {
198  if ((isset($object) && !is_object($object)) || empty($savemethod)) {
199  $object = new GenericObject($db);
200  }
201 
202  // Specific for add_object_linked()
203  // TODO add a function for variable treatment
204  $object->ext_fk_element = $newvalue;
205  $object->ext_element = $ext_element;
206  $object->fk_element = $fk_element;
207  $object->element = $element;
208 
209  $ret = $object->$savemethodname($field, $newvalue, $table_element, $fk_element, $format);
210  if ($ret > 0) {
211  if ($type == 'numeric') {
212  $value = price($newvalue);
213  } elseif ($type == 'textarea') {
214  $value = dol_nl2br($newvalue);
215  }
216 
217  $return['value'] = $value;
218  $return['view'] = (!empty($view) ? $view : $value);
219  } else {
220  $return['error'] = $object->error;
221  }
222  }
223 
224  echo json_encode($return);
225  } else {
226  echo $langs->trans('NotEnoughPermissions');
227  }
228 }
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_nl2br
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
Definition: functions.lib.php:6963
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1407
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
GenericObject
Class of a generic business object.
Definition: genericobject.class.php:30
price
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.
Definition: functions.lib.php:5541