PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/ajax/saveinplace.php

https://github.com/asterix14/dolibarr
PHP | 159 lines | 110 code | 18 blank | 31 comment | 42 complexity | 904eea35ce5f709dfde44fa11f717f93 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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. /**
  18. * \file htdocs/core/ajax/saveinplace.php
  19. * \brief File to save field value
  20. */
  21. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
  22. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  23. //if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  24. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  25. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  26. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  27. require('../../main.inc.php');
  28. require_once(DOL_DOCUMENT_ROOT."/core/class/genericobject.class.php");
  29. /*
  30. * View
  31. */
  32. top_httphead();
  33. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  34. //print_r($_POST);
  35. // Load original field value
  36. if((isset($_POST['field']) && ! empty($_POST['field']))
  37. && (isset($_POST['element']) && ! empty($_POST['element']))
  38. && (isset($_POST['table_element']) && ! empty($_POST['table_element']))
  39. && (isset($_POST['fk_element']) && ! empty($_POST['fk_element'])))
  40. {
  41. $element = GETPOST('element');
  42. $table_element = GETPOST('table_element');
  43. $fk_element = GETPOST('fk_element');
  44. $ext_element = GETPOST('ext_element');
  45. //$ext_table_element = GETPOST('ext_table_element');
  46. //$ext_fk_element = GETPOST('ext_fk_element');
  47. $field = substr(GETPOST('field'), 4); // remove prefix val_
  48. $value = GETPOST('value');
  49. $type = GETPOST('type');
  50. $savemethodname = (GETPOST('savemethod') ? GETPOST('savemethod') : 'setValueFrom');
  51. $format='text';
  52. $return=array();
  53. $error=0;
  54. if (preg_match('/^([^_]+)_([^_]+)/i',$element,$regs))
  55. {
  56. $element = $regs[1];
  57. $subelement = $regs[2];
  58. }
  59. if ($element == 'propal') $element = 'propale';
  60. else if ($element == 'fichinter') $element = 'ficheinter';
  61. if ($user->rights->$element->creer || $user->rights->$element->write
  62. || $user->rights->$element->$subelement->creer || $user->rights->$element->$subelement->write)
  63. {
  64. // Clean parameters
  65. $newvalue = trim($value);
  66. if ($type == 'numeric')
  67. {
  68. $newvalue = price2num($newvalue);
  69. // Check parameters
  70. if (! is_numeric($newvalue))
  71. {
  72. $error++;
  73. $return['error'] = $langs->trans('ErrorBadValue');
  74. }
  75. }
  76. else if ($type == 'datepicker')
  77. {
  78. $timestamp = GETPOST('timestamp');
  79. $format = 'date';
  80. $newvalue = ($timestamp / 1000);
  81. }
  82. else if ($type == 'select')
  83. {
  84. $loadmethodname = 'load_cache_'.GETPOST('loadmethod');
  85. $loadcachename = 'cache_'.GETPOST('loadmethod');
  86. $form = new Form($db);
  87. if (method_exists($form, $loadmethodname))
  88. {
  89. $ret = $form->$loadmethodname();
  90. if ($ret > 0)
  91. {
  92. $loadcache = $form->$loadcachename;
  93. $value = $loadcache[$newvalue];
  94. }
  95. else
  96. {
  97. $error++;
  98. $return['error'] = $form->error;
  99. }
  100. }
  101. else
  102. {
  103. dol_include_once('/'.$ext_element.'/class/actions_'.$ext_element.'.class.php');
  104. $classname = 'Actions'.ucfirst($ext_element);
  105. $object = new $classname($db);
  106. $ret = $object->$loadmethodname();
  107. if ($ret > 0)
  108. {
  109. $loadcache = $object->$loadcachename;
  110. $value = $loadcache[$newvalue];
  111. }
  112. else
  113. {
  114. $error++;
  115. $return['error'] = $object->error;
  116. }
  117. }
  118. }
  119. if (! $error)
  120. {
  121. if (! is_object($object)) $object = new GenericObject($db);
  122. $ret=$object->$savemethodname($field, $newvalue, $table_element, $fk_element, $format);
  123. if ($ret > 0)
  124. {
  125. if ($type == 'numeric') $value = price($newvalue);
  126. else if ($type == 'textarea') $value = dol_nl2br($newvalue);
  127. $return['value'] = $value;
  128. }
  129. else
  130. {
  131. $return['error'] = $object->error;
  132. }
  133. }
  134. echo json_encode($return);
  135. }
  136. else
  137. {
  138. echo $langs->trans('NotEnoughPermissions');
  139. }
  140. }
  141. ?>