JsonProductInfo.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Controller\Adminhtml\Product;
  7. use Magento\Framework\App\Action\HttpGetActionInterface;
  8. use Magento\Review\Controller\Adminhtml\Product as ProductController;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Framework\Registry;
  11. use Magento\Review\Model\ReviewFactory;
  12. use Magento\Review\Model\RatingFactory;
  13. use Magento\Catalog\Api\ProductRepositoryInterface;
  14. use Magento\Framework\DataObject;
  15. use Magento\Framework\Controller\ResultFactory;
  16. /**
  17. * Represents product info in json
  18. */
  19. class JsonProductInfo extends ProductController implements HttpGetActionInterface
  20. {
  21. /**
  22. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  23. */
  24. protected $productRepository;
  25. /**
  26. * @param \Magento\Backend\App\Action\Context $context
  27. * @param \Magento\Framework\Registry $coreRegistry
  28. * @param \Magento\Review\Model\ReviewFactory $reviewFactory
  29. * @param \Magento\Review\Model\RatingFactory $ratingFactory
  30. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  31. */
  32. public function __construct(
  33. Context $context,
  34. Registry $coreRegistry,
  35. ReviewFactory $reviewFactory,
  36. RatingFactory $ratingFactory,
  37. ProductRepositoryInterface $productRepository
  38. ) {
  39. $this->productRepository = $productRepository;
  40. parent::__construct($context, $coreRegistry, $reviewFactory, $ratingFactory);
  41. }
  42. /**
  43. * Execute controller
  44. *
  45. * @return \Magento\Framework\Controller\Result\Json
  46. */
  47. public function execute()
  48. {
  49. $response = new DataObject();
  50. $id = $this->getRequest()->getParam('id');
  51. if ((int)$id > 0) {
  52. $product = $this->productRepository->getById($id);
  53. $response->setId($id);
  54. $response->addData($product->getData());
  55. $response->setError(0);
  56. } else {
  57. $response->setError(1);
  58. $response->setMessage(__('We can\'t retrieve the product ID.'));
  59. }
  60. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  61. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  62. $resultJson->setData($response->toArray());
  63. return $resultJson;
  64. }
  65. }