saveinplace.php 6.6 KB

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