get_attribute_values.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  3. * Copyright (C) 2022 Open-Dsi <support@open-dsi.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. if (!defined('NOTOKENRENEWAL')) {
  19. define('NOTOKENRENEWAL', '1');
  20. }
  21. if (!defined('NOREQUIREMENU')) {
  22. define('NOREQUIREMENU', '1');
  23. }
  24. if (!defined('NOREQUIREHTML')) {
  25. define('NOREQUIREHTML', '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.'/product/class/product.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php';
  38. // Security check
  39. if (!isModEnabled('variants')) {
  40. accessforbidden('Module not enabled');
  41. }
  42. if ($user->socid > 0) { // Protection if external user
  43. accessforbidden();
  44. }
  45. $result = restrictedArea($user, 'variants');
  46. /*
  47. * View
  48. */
  49. top_httphead('application/json');
  50. $id = GETPOST('id', 'int');
  51. if (!$id) {
  52. print json_encode(array(
  53. 'error' => 'ID not set'
  54. ));
  55. exit();
  56. }
  57. $prodattr = new ProductAttribute($db);
  58. if ($prodattr->fetch($id) < 0) {
  59. print json_encode(array(
  60. 'error' => 'Attribute not found'
  61. ));
  62. exit();
  63. }
  64. $prodattrval = new ProductAttributeValue($db);
  65. $res = $prodattrval->fetchAllByProductAttribute($id, false, 1);
  66. if ($res == -1) {
  67. print json_encode(array(
  68. 'error' => 'Internal error'
  69. ));
  70. exit();
  71. }
  72. print json_encode($res);