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