dolibarr  13.0.2
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')) 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  $element = $regs[1];
77  $subelement = $regs[2];
78  }
79 
80  if ($element == 'propal') {
81  $newelement = 'propale';
82  } elseif ($element == 'fichinter') {
83  $newelement = 'ficheinter';
84  } elseif ($element == 'product') {
85  $newelement = 'produit';
86  } elseif ($element == 'member') {
87  $newelement = 'adherent';
88  } elseif ($element == 'order_supplier') {
89  $newelement = 'fournisseur';
90  $subelement = 'commande';
91  } elseif ($element == 'invoice_supplier') {
92  $newelement = 'fournisseur';
93  $subelement = 'facture';
94  } else {
95  $newelement = $element;
96  }
97 
98  $_POST['action'] = 'update'; // Hack so restrictarea will test permissions on write too
99  $feature = $newelement;
100  $feature2 = $subelement;
101  $object_id = $fk_element;
102  if ($feature == 'expedition' || $feature == 'shipping') {
103  $feature = 'commande';
104  $object_id = 0;
105  }
106  if ($feature == 'shipping') {
107  $feature = 'commande';
108  }
109  if ($feature == 'payment') {
110  $feature = 'facture';
111  }
112  if ($feature == 'payment_supplier') {
113  $feature = 'fournisseur';
114  $feature2 = 'facture';
115  }
116  //var_dump(GETPOST('action','aZ09'));
117  //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
118  $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
119  //var_dump($user->rights);
120  /*
121  if (! empty($user->rights->$newelement->creer) || ! empty($user->rights->$newelement->create) || ! empty($user->rights->$newelement->write)
122  || (isset($subelement) && (! empty($user->rights->$newelement->$subelement->creer) || ! empty($user->rights->$newelement->$subelement->write)))
123  || ($element == 'payment' && $user->rights->facture->paiement)
124  || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
125  */
126 
127  if ($check_access)
128  {
129  // Clean parameters
130  $newvalue = trim($value);
131 
132  if ($type == 'numeric')
133  {
134  $newvalue = price2num($newvalue);
135 
136  // Check parameters
137  if (!is_numeric($newvalue))
138  {
139  $error++;
140  $return['error'] = $langs->trans('ErrorBadValue');
141  }
142  } elseif ($type == 'datepicker') {
143  $timestamp = GETPOST('timestamp', 'int', 2);
144  $format = 'date';
145  $newvalue = ($timestamp / 1000);
146  } elseif ($type == 'select') {
147  $loadmethodname = 'load_cache_'.$loadmethod;
148  $loadcachename = 'cache_'.$loadmethod;
149  $loadviewname = 'view_'.$loadmethod;
150 
151  $form = new Form($db);
152  if (method_exists($form, $loadmethodname))
153  {
154  $ret = $form->$loadmethodname();
155  if ($ret > 0)
156  {
157  $loadcache = $form->$loadcachename;
158  $value = $loadcache[$newvalue];
159 
160  if (!empty($form->$loadviewname))
161  {
162  $loadview = $form->$loadviewname;
163  $view = $loadview[$newvalue];
164  }
165  } else {
166  $error++;
167  $return['error'] = $form->error;
168  }
169  } else {
170  $module = $subelement = $ext_element;
171  if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs))
172  {
173  $module = $regs[1];
174  $subelement = $regs[2];
175  }
176 
177  dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
178  $classname = 'Actions'.ucfirst($subelement);
179  $object = new $classname($db);
180  $ret = $object->$loadmethodname();
181  if ($ret > 0)
182  {
183  $loadcache = $object->$loadcachename;
184  $value = $loadcache[$newvalue];
185 
186  if (!empty($object->$loadviewname))
187  {
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  {
200  if ((isset($object) && !is_object($object)) || empty($savemethod)) $object = new GenericObject($db);
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  {
212  if ($type == 'numeric') $value = price($newvalue);
213  elseif ($type == 'textarea') $value = dol_nl2br($newvalue);
214 
215  $return['value'] = $value;
216  $return['view'] = (!empty($view) ? $view : $value);
217  } else {
218  $return['error'] = $object->error;
219  }
220  }
221 
222  echo json_encode($return);
223  } else {
224  echo $langs->trans('NotEnoughPermissions');
225  }
226 }
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.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype= 'text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1214
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 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 &#39;...
if(!GETPOST('transkey', 'alphanohtml')&&!GETPOST('transphrase', 'alphanohtml')) else
View.
Definition: notice.php:44
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.
Class of a generic business object.