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