saveinplace.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 load field value. used only when option "Edit In Place" is set (MAIN_USE_JQUERY_JEDITABLE).
  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. $id = $fk_element;
  41. /* Example:
  42. field:editval_ref_customer (8 first chars will removed to know name of property)
  43. element:contrat
  44. table_element:contrat
  45. fk_element:4
  46. type:string
  47. value:aaa
  48. loadmethod:
  49. savemethod:
  50. savemethodname:
  51. */
  52. // Load object according to $id and $element
  53. $object = fetchObjectByElement($id, $element);
  54. $module = $object->module;
  55. $element = $object->element;
  56. $usesublevelpermission = ($module != $element ? $element : '');
  57. if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
  58. $usesublevelpermission = '';
  59. }
  60. //print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
  61. // Security check
  62. $result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
  63. if (!$result) {
  64. httponly_accessforbidden('Not allowed by restrictArea');
  65. }
  66. if (!getDolGlobalString('MAIN_USE_JQUERY_JEDITABLE')) {
  67. httponly_accessforbidden('Can be used only when option MAIN_USE_JQUERY_JEDITABLE is set');
  68. }
  69. /*
  70. * View
  71. */
  72. top_httphead();
  73. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  74. //print_r($_POST);
  75. // Load original field value
  76. if (!empty($field) && !empty($element) && !empty($table_element) && !empty($fk_element)) {
  77. $ext_element = GETPOST('ext_element', 'alpha', 2);
  78. $field = substr($field, 8); // remove prefix val_
  79. $type = GETPOST('type', 'alpha', 2);
  80. $value = ($type == 'ckeditor' ? GETPOST('value', '', 2) : GETPOST('value', 'alpha', 2));
  81. $loadmethod = GETPOST('loadmethod', 'alpha', 2);
  82. $savemethod = GETPOST('savemethod', 'alpha', 2);
  83. $savemethodname = (!empty($savemethod) ? $savemethod : 'setValueFrom');
  84. $newelement = $element;
  85. $view = '';
  86. $format = 'text';
  87. $return = array();
  88. $error = 0;
  89. if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
  90. $element = $regs[1];
  91. $subelement = $regs[2];
  92. }
  93. if ($element == 'propal') {
  94. $newelement = 'propale';
  95. } elseif ($element == 'fichinter') {
  96. $newelement = 'ficheinter';
  97. } elseif ($element == 'product') {
  98. $newelement = 'produit';
  99. } elseif ($element == 'member') {
  100. $newelement = 'adherent';
  101. } elseif ($element == 'order_supplier') {
  102. $newelement = 'fournisseur';
  103. $subelement = 'commande';
  104. } elseif ($element == 'invoice_supplier') {
  105. $newelement = 'fournisseur';
  106. $subelement = 'facture';
  107. } else {
  108. $newelement = $element;
  109. }
  110. $_POST['action'] = 'update'; // Hack so restrictarea will test permissions on write too
  111. $feature = $newelement;
  112. $feature2 = $subelement;
  113. $object_id = $fk_element;
  114. if ($feature == 'expedition' || $feature == 'shipping') {
  115. $feature = 'commande';
  116. $object_id = 0;
  117. }
  118. if ($feature == 'shipping') {
  119. $feature = 'commande';
  120. }
  121. if ($feature == 'payment') {
  122. $feature = 'facture';
  123. }
  124. if ($feature == 'payment_supplier') {
  125. $feature = 'fournisseur';
  126. $feature2 = 'facture';
  127. }
  128. //var_dump(GETPOST('action','aZ09'));
  129. //var_dump($newelement.'-'.$subelement."-".$feature."-".$object_id);
  130. $check_access = restrictedArea($user, $feature, $object_id, '', $feature2);
  131. //var_dump($user->rights);
  132. /*
  133. if (!empty($user->rights->$newelement->creer) || !empty($user->rights->$newelement->create) || !empty($user->rights->$newelement->write)
  134. || (isset($subelement) && (!empty($user->rights->$newelement->$subelement->creer) || !empty($user->rights->$newelement->$subelement->write)))
  135. || ($element == 'payment' && $user->rights->facture->paiement)
  136. || ($element == 'payment_supplier' && $user->rights->fournisseur->facture->creer))
  137. */
  138. if ($check_access) {
  139. // Clean parameters
  140. $newvalue = trim($value);
  141. if ($type == 'numeric') {
  142. $newvalue = price2num($newvalue);
  143. // Check parameters
  144. if (!is_numeric($newvalue)) {
  145. $error++;
  146. $return['error'] = $langs->trans('ErrorBadValue');
  147. }
  148. } elseif ($type == 'datepicker') {
  149. $timestamp = GETPOST('timestamp', 'int', 2);
  150. $format = 'date';
  151. $newvalue = ($timestamp / 1000);
  152. } elseif ($type == 'select') {
  153. $loadmethodname = 'load_cache_'.$loadmethod;
  154. $loadcachename = 'cache_'.$loadmethod;
  155. $loadviewname = 'view_'.$loadmethod;
  156. $form = new Form($db);
  157. if (method_exists($form, $loadmethodname)) {
  158. $ret = $form->$loadmethodname();
  159. if ($ret > 0) {
  160. $loadcache = $form->$loadcachename;
  161. $value = $loadcache[$newvalue];
  162. if (!empty($form->$loadviewname)) {
  163. $loadview = $form->$loadviewname;
  164. $view = $loadview[$newvalue];
  165. }
  166. } else {
  167. $error++;
  168. $return['error'] = $form->error;
  169. }
  170. } else {
  171. $module = $subelement = $ext_element;
  172. if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
  173. $module = $regs[1];
  174. $subelement = $regs[2];
  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. $loadcache = $object->$loadcachename;
  182. $value = $loadcache[$newvalue];
  183. if (!empty($object->$loadviewname)) {
  184. $loadview = $object->$loadviewname;
  185. $view = $loadview[$newvalue];
  186. }
  187. } else {
  188. $error++;
  189. $return['error'] = $object->error;
  190. }
  191. }
  192. }
  193. if (!$error) {
  194. if ((isset($object) && !is_object($object)) || empty($savemethod)) {
  195. $object = new GenericObject($db);
  196. }
  197. // Specific for add_object_linked()
  198. // TODO add a function for variable treatment
  199. $object->ext_fk_element = $newvalue;
  200. $object->ext_element = $ext_element;
  201. $object->fk_element = $fk_element;
  202. $object->element = $element;
  203. $ret = $object->$savemethodname($field, $newvalue, $table_element, $fk_element, $format);
  204. if ($ret > 0) {
  205. if ($type == 'numeric') {
  206. $value = price($newvalue);
  207. } elseif ($type == 'textarea') {
  208. $value = dol_nl2br($newvalue);
  209. }
  210. $return['value'] = $value;
  211. $return['view'] = (!empty($view) ? $view : $value);
  212. } else {
  213. $return['error'] = $object->error;
  214. }
  215. }
  216. echo json_encode($return);
  217. } else {
  218. echo $langs->trans('NotEnoughPermissions');
  219. }
  220. }