objectonoff.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /* Copyright (C) 2015-2023 Laurent Destailleur <eldy@users.sourceforge.net>
  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/objectonoff.php
  19. * \brief File to set status for an object. Called when ajax_object_onoff() is used.
  20. * This Ajax service is oftenly called when option MAIN_DIRECT_STATUS_UPDATE is set.
  21. */
  22. if (!defined('NOTOKENRENEWAL')) {
  23. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  24. }
  25. if (!defined('NOREQUIREMENU')) {
  26. define('NOREQUIREMENU', '1');
  27. }
  28. if (!defined('NOREQUIREHTML')) {
  29. define('NOREQUIREHTML', '1');
  30. }
  31. if (!defined('NOREQUIREAJAX')) {
  32. define('NOREQUIREAJAX', '1');
  33. }
  34. if (!defined('NOREQUIRESOC')) {
  35. define('NOREQUIRESOC', '1');
  36. }
  37. if (!defined('NOREQUIRETRAN')) {
  38. define('NOREQUIRETRAN', '1');
  39. }
  40. // Load Dolibarr environment
  41. require '../../main.inc.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
  43. $action = GETPOST('action', 'aZ09');
  44. $backtopage = GETPOST('backtopage');
  45. $id = GETPOST('id', 'int');
  46. $element = GETPOST('element', 'alpha'); // 'myobject' (myobject=mymodule) or 'myobject@mymodule' or 'myobject_mysubobject' (myobject=mymodule)
  47. $field = GETPOST('field', 'alpha');
  48. $value = GETPOST('value', 'int');
  49. $format = 'int';
  50. // Load object according to $id and $element
  51. $object = fetchObjectByElement($id, $element);
  52. if (!is_object($object)) {
  53. httponly_accessforbidden("Bad value for combination of parameters element/field: Object not found."); // This includes the exit.
  54. }
  55. $object->fields[$field] = array('type' => $format, 'enabled' => 1);
  56. $module = $object->module;
  57. $element = $object->element;
  58. $usesublevelpermission = ($module != $element ? $element : '');
  59. if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
  60. $usesublevelpermission = '';
  61. }
  62. //print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
  63. // Security check
  64. if (!empty($user->socid)) {
  65. $socid = $user->socid;
  66. if (!empty($object->socid) && $socid != $object->socid) {
  67. httponly_accessforbidden("Access on object not allowed for this external user."); // This includes the exit.
  68. }
  69. }
  70. // We check permission.
  71. // Check is done on $user->rights->element->create or $user->rights->element->subelement->create (because $action = 'set')
  72. if (preg_match('/status$/', $field) || ($field == 'evenunsubscribe' && $object->table_element == 'mailing')) {
  73. restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission);
  74. } elseif ($element == 'product' && in_array($field, array('tosell', 'tobuy', 'tobatch'))) { // Special case for products
  75. restrictedArea($user, 'produit|service', $object, 'product&product', '', '', 'rowid');
  76. } else {
  77. httponly_accessforbidden("Bad value for combination of parameters element/field: Field not supported."); // This includes the exit.
  78. }
  79. /*
  80. * View
  81. */
  82. top_httphead();
  83. print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  84. // Registering new values
  85. if (($action == 'set') && !empty($id)) {
  86. $triggerkey = strtoupper(($module != $element ? $module.'_' : '').$element).'_UPDATE';
  87. // Special case
  88. if ($triggerkey == 'SOCIETE_UPDATE') {
  89. $triggerkey = 'COMPANY_MODIFY';
  90. }
  91. if ($triggerkey == 'PRODUCT_UPDATE') {
  92. $triggerkey = 'PRODUCT_MODIFY';
  93. }
  94. $result = $object->setValueFrom($field, $value, $object->table_element, $id, $format, '', $user, $triggerkey);
  95. if ($result < 0) {
  96. print $object->error;
  97. http_response_code(500);
  98. exit;
  99. }
  100. if ($backtopage) {
  101. header('Location: '.$backtopage);
  102. exit;
  103. }
  104. }