loadinplace.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /* Copyright (C) 2011-2014 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/loadinplace.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');
  37. $element = GETPOST('element', 'alpha');
  38. $table_element = GETPOST('table_element', 'alpha');
  39. $fk_element = GETPOST('fk_element', 'alpha');
  40. $id = $fk_element;
  41. // Load object according to $id and $element
  42. $object = fetchObjectByElement($id, $element);
  43. $module = $object->module;
  44. $element = $object->element;
  45. $usesublevelpermission = ($module != $element ? $element : '');
  46. if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
  47. $usesublevelpermission = '';
  48. }
  49. //print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
  50. // Security check
  51. $result = restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission, 'fk_soc', 'rowid', 0, 1); // Call with mode return
  52. if (!$result) {
  53. httponly_accessforbidden('Not allowed by restrictArea');
  54. }
  55. if (!getDolGlobalString('MAIN_USE_JQUERY_JEDITABLE')) {
  56. httponly_accessforbidden('Can be used only when option MAIN_USE_JQUERY_JEDITABLE is set');
  57. }
  58. /*
  59. * View
  60. */
  61. top_httphead();
  62. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  63. // Load original field value
  64. if (!empty($field) && !empty($element) && !empty($table_element) && !empty($fk_element)) {
  65. $ext_element = GETPOST('ext_element', 'alpha');
  66. $field = substr($field, 8); // remove prefix val_
  67. $type = GETPOST('type', 'alpha');
  68. $loadmethod = (GETPOST('loadmethod', 'alpha') ? GETPOST('loadmethod', 'alpha') : 'getValueFrom');
  69. if ($element != 'order_supplier' && $element != 'invoice_supplier' && preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
  70. $element = $regs[1];
  71. $subelement = $regs[2];
  72. }
  73. if ($element == 'propal') {
  74. $element = 'propale';
  75. } elseif ($element == 'fichinter') {
  76. $element = 'ficheinter';
  77. } elseif ($element == 'product') {
  78. $element = 'produit';
  79. } elseif ($element == 'member') {
  80. $element = 'adherent';
  81. } elseif ($element == 'order_supplier') {
  82. $element = 'fournisseur';
  83. $subelement = 'commande';
  84. } elseif ($element == 'invoice_supplier') {
  85. $element = 'fournisseur';
  86. $subelement = 'facture';
  87. }
  88. if ($user->hasRight($element, 'lire') || $user->hasRight($element, 'read')
  89. || (isset($subelement) && ($user->hasRight($element, $subelement, 'lire') || $user->hasRight($element, $subelement, 'read')))
  90. || ($element == 'payment' && $user->hasRight('facture', 'lire'))
  91. || ($element == 'payment_supplier' && $user->hasRight('fournisseur', 'facture', 'lire'))) {
  92. if ($type == 'select') {
  93. $methodname = 'load_cache_'.$loadmethod;
  94. $cachename = 'cache_'.GETPOST('loadmethod', 'alpha');
  95. $form = new Form($db);
  96. if (method_exists($form, $methodname)) {
  97. $ret = $form->$methodname();
  98. if ($ret > 0) {
  99. echo json_encode($form->$cachename);
  100. }
  101. } elseif (!empty($ext_element)) {
  102. $module = $subelement = $ext_element;
  103. $regs = array();
  104. if (preg_match('/^([^_]+)_([^_]+)/i', $ext_element, $regs)) {
  105. $module = $regs[1];
  106. $subelement = $regs[2];
  107. }
  108. dol_include_once('/'.$module.'/class/actions_'.$subelement.'.class.php');
  109. $classname = 'Actions'.ucfirst($subelement);
  110. $object = new $classname($db);
  111. $ret = $object->$methodname($fk_element);
  112. if ($ret > 0) {
  113. echo json_encode($object->$cachename);
  114. }
  115. }
  116. } else {
  117. $object = new GenericObject($db);
  118. $value = $object->$loadmethod($table_element, $fk_element, $field);
  119. echo $value;
  120. }
  121. } else {
  122. echo $langs->transnoentities('NotEnoughPermissions');
  123. }
  124. }