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