AddComment.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment;
  8. use Magento\Sales\Model\Order\Email\Sender\ShipmentCommentSender;
  9. use Magento\Backend\App\Action;
  10. use Magento\Framework\View\Result\LayoutFactory;
  11. class AddComment extends \Magento\Backend\App\Action
  12. {
  13. /**
  14. * Authorization level of a basic admin session
  15. *
  16. * @see _isAllowed()
  17. */
  18. const ADMIN_RESOURCE = 'Magento_Sales::shipment';
  19. /**
  20. * @var \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader
  21. */
  22. protected $shipmentLoader;
  23. /**
  24. * @var ShipmentCommentSender
  25. */
  26. protected $shipmentCommentSender;
  27. /**
  28. * @var LayoutFactory
  29. */
  30. protected $resultLayoutFactory;
  31. /**
  32. * @param Action\Context $context
  33. * @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader
  34. * @param ShipmentCommentSender $shipmentCommentSender
  35. * @param LayoutFactory $resultLayoutFactory
  36. */
  37. public function __construct(
  38. Action\Context $context,
  39. \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader,
  40. ShipmentCommentSender $shipmentCommentSender,
  41. LayoutFactory $resultLayoutFactory
  42. ) {
  43. $this->shipmentLoader = $shipmentLoader;
  44. $this->shipmentCommentSender = $shipmentCommentSender;
  45. $this->resultLayoutFactory = $resultLayoutFactory;
  46. parent::__construct($context);
  47. }
  48. /**
  49. * Add comment to shipment history
  50. *
  51. * @return void
  52. */
  53. public function execute()
  54. {
  55. try {
  56. $this->getRequest()->setParam('shipment_id', $this->getRequest()->getParam('id'));
  57. $data = $this->getRequest()->getPost('comment');
  58. if (empty($data['comment'])) {
  59. throw new \Magento\Framework\Exception\LocalizedException(
  60. __('The comment is missing. Enter and try again.')
  61. );
  62. }
  63. $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id'));
  64. $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id'));
  65. $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment'));
  66. $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking'));
  67. $shipment = $this->shipmentLoader->load();
  68. $shipment->addComment(
  69. $data['comment'],
  70. isset($data['is_customer_notified']),
  71. isset($data['is_visible_on_front'])
  72. );
  73. $this->shipmentCommentSender->send($shipment, !empty($data['is_customer_notified']), $data['comment']);
  74. $shipment->save();
  75. $resultLayout = $this->resultLayoutFactory->create();
  76. $resultLayout->addDefaultHandle();
  77. $response = $resultLayout->getLayout()->getBlock('shipment_comments')->toHtml();
  78. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  79. $response = ['error' => true, 'message' => $e->getMessage()];
  80. } catch (\Exception $e) {
  81. $response = ['error' => true, 'message' => __('Cannot add new comment.')];
  82. }
  83. if (is_array($response)) {
  84. $response = $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($response);
  85. $this->getResponse()->representJson($response);
  86. } else {
  87. $this->getResponse()->setBody($response);
  88. }
  89. }
  90. }