saveinplace.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /**
  18. * \file htdocs/core/ajax/saveinplace.php
  19. * \brief File to save field value
  20. */
  21. if (!defined('NOTOKENRENEWAL')) {
  22. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  23. }
  24. if (!defined('NOREQUIREMENU')) {
  25. define('NOREQUIREMENU', '1');
  26. }
  27. if (!defined('NOREQUIREAJAX')) {
  28. define('NOREQUIREAJAX', '1');
  29. }
  30. if (!defined('NOREQUIRESOC')) {
  31. define('NOREQUIRESOC', '1');
  32. }
  33. // Load Dolibarr environment
  34. require '../../main.inc.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
  36. $field = GETPOST('field', 'alpha', 2);
  37. $element = GETPOST('element', 'alpha', 2);
  38. $table_element = GETPOST('table_element', 'alpha', 2);
  39. $fk_element = GETPOST('fk_element', 'alpha', 2);
  40. /* Example:
  41. field:editval_ref_customer (8 first chars will removed to know name of property)
  42. element:contrat
  43. table_element:contrat
  44. fk_element:4
  45. type:string
  46. value:aaa
  47. loadmethod:
  48. savemethod:
  49. savemethodname:
  50. */
  51. /*
  52. * View
  53. */
  54. top_httphead();
  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. // Load original field value
  58. if (!empty($field) && !empty($element) && !empty($table_element) && !empty($fk_element)) {
  59. $ext_element = GETPOST('ext_element', 'alpha', 2);
  60. $field = substr($field, 8); // remove prefix val_
  61. $type = GETPOST('type', 'alpha', 2);
  62. $value = ($type == 'ckeditor' ? GETPOST('value', '', 2) : GETPOST('value', 'alpha', 2));
  63. $loadmethod = GETPOST('loadmethod', 'alpha', 2);
  64. $savemethod = GETPOST('savemethod', 'alpha', 2);
  65. $savemethodname = (!empty($savemethod) ? $savemethod : 'setValueFrom');
  66. $newelement = $element;
  67. $view = '';
  68. $format = 'text';
  69. $return = array();
  70. $error = 0;
  71. if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
  72. $element = $regs[1];
  73. $subelement = $regs[2];
  74. }
  75. if ($element == 'propal') {
  76. $newelement = 'propale';
  77. } elseif ($element == 'fichinter') {
  78. $newelement = 'ficheinter';
  79. } elseif ($element == 'product') {
  80. $newelement = 'produit';
  81. } elseif ($element == 'member') {
  82. $newelement = 'adherent';
  83. } elseif ($element == 'order_supplier') {
  84. $newelement = 'fournisseur';
  85. $subelement = 'commande';
  86. } elseif ($element == 'invoice_supplier') {
  87. $newelement = 'fournisseur';
  88. $subelement = 'facture';
  89. } else {
  90. $newelement = $element;
  91. }
  92. $_POST['action'] = 'update'; // Hack so restrictarea will test permissions on write too
  93. $feature = $newelement;
  94. $feature2 = $subelement;
  95. $object_id = $fk_element;
  96. if ($feature == 'expedition' || $feature == 'shipping') {
  97. $feature = 'commande';
  98. $object_id = 0;
  99. }
  100. if ($feature == 'shipping') {
  101. $feature = 'commande';
  102. }
  103. if ($feature == 'payment') {
  104. $feature = 'facture';
  105. }
  106. if ($feature == 'payment_supplier') {
  107. $feature = 'fournisseur';
  108. $feature2 = 'facture';
  109. }
  110. //var_dump(GETPOST('action','aZ09'));
  111. //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
  112. $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
  113. //var_dump($user->rights);
  114. /*
  115. if (!empty($user->rights->$newelement->creer) || !empty($user->rights->$newelement->create) || !empty($user->rights->$newelement->write)
  116. || (isset($subelement) && (!empty($user->rights->$newelement->$subelement->creer) || !empty($user->rights->$newelement->$subelement->write)))
  117. || ($element == 'payment' && $user->rights->facture->paiement)
  118. || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
  119. */
  120. if ($check_access) {
  121. // Clean parameters
  122. $newvalue = trim($value);
  123. if ($type == 'numeric') {
  124. $newvalue = price2num($newvalue);
  125. // Check parameters
  126. if (!is_numeric($newvalue)) {
  127. $error++;
  128. $return['error'] = $langs->trans('ErrorBadValue');
  129. }
  130. } elseif ($type == 'datepicker') {
  131. $timestamp = GETPOST('timestamp', 'int', 2);
  132. $format = 'date';
  133. $newvalue = ($timestamp / 1000);
  134. } elseif ($type == 'select') {
  135. $loadmethodname = 'load_cache_'.$loadmethod;
  136. $loadcachename = 'cache_'.$loadmethod;
  137. $loadviewname = 'view_'.$loadmethod;
  138. $form = new Form($db);
  139. if (method_exists($form, $loadmethodname)) {
  140. $ret = $form->$loadmethodname();
  141. if ($ret > 0) {
  142. $loadcache = $form->$loadcachename;
  143. $value = $loadcache[$newvalue];
  144. if (!empty($form->$loadviewname)) {
  145. $loadview = $form->$loadviewname;
  146. $view = $loadview[$newvalue];
  147. }
  148. } else {
  149. $error++;
  150. $return['error'] = $form->error;
  151. }
  152. } else {
  153. $module = $subelement = $ext_element;
  154. if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
  155. $module = $regs[1];
  156. $subelement = $regs[2];
  157. }
  158. dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
  159. $classname = 'Actions'.ucfirst($subelement);
  160. $object = new $classname($db);
  161. $ret = $object->$loadmethodname();
  162. if ($ret > 0) {
  163. $loadcache = $object->$loadcachename;
  164. $value = $loadcache[$newvalue];
  165. if (!empty($object->$loadviewname)) {
  166. $loadview = $object->$loadviewname;
  167. $view = $loadview[$newvalue];
  168. }
  169. } else {
  170. $error++;
  171. $return['error'] = $object->error;
  172. }
  173. }
  174. }
  175. if (!$error) {
  176. if ((isset($object) && !is_object($object)) || empty($savemethod)) {
  177. $object = new GenericObject($db);
  178. }
  179. // Specific for add_object_linked()
  180. // TODO add a function for variable treatment
  181. $object->ext_fk_element = $newvalue;
  182. $object->ext_element = $ext_element;
  183. $object->fk_element = $fk_element;
  184. $object->element = $element;
  185. $ret = $object->$savemethodname($field, $newvalue, $table_element, $fk_element, $format);
  186. if ($ret > 0) {
  187. if ($type == 'numeric') {
  188. $value = price($newvalue);
  189. } elseif ($type == 'textarea') {
  190. $value = dol_nl2br($newvalue);
  191. }
  192. $return['value'] = $value;
  193. $return['view'] = (!empty($view) ? $view : $value);
  194. } else {
  195. $return['error'] = $object->error;
  196. }
  197. }
  198. echo json_encode($return);
  199. } else {
  200. echo $langs->trans('NotEnoughPermissions');
  201. }
  202. }