ShipmentCancelButton.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Block\Adminhtml\PageAction;
  6. use Magento\Backend\Block\Template\Context;
  7. use Magento\Backend\Block\Widget\Button;
  8. use Magento\Framework\UrlInterface;
  9. use Temando\Shipping\Model\Shipment\ShipmentProviderInterface;
  10. /**
  11. * Action Button to Cancel Shipment Action
  12. *
  13. * @api
  14. * @package Temando\Shipping\Block
  15. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  16. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link https://www.temando.com/
  18. */
  19. class ShipmentCancelButton extends Button
  20. {
  21. /**
  22. * @var ShipmentProviderInterface
  23. */
  24. private $shipmentProvider;
  25. /**
  26. * @var UrlInterface
  27. */
  28. private $urlBuilder;
  29. /**
  30. * @param Context $context
  31. * @param ShipmentProviderInterface $shipmentProvider
  32. * @param UrlInterface $urlBuilder
  33. * @param mixed[] $data
  34. */
  35. public function __construct(
  36. Context $context,
  37. ShipmentProviderInterface $shipmentProvider,
  38. UrlInterface $urlBuilder,
  39. array $data = []
  40. ) {
  41. $this->shipmentProvider = $shipmentProvider;
  42. $this->urlBuilder = $urlBuilder;
  43. parent::__construct($context, $data);
  44. }
  45. /**
  46. * Append confirm component to button
  47. *
  48. * @return string
  49. */
  50. public function getAfterHtml()
  51. {
  52. $shipment = $this->shipmentProvider->getShipment();
  53. $salesShipment = $this->shipmentProvider->getSalesShipment();
  54. if (!$shipment || !$salesShipment) {
  55. return $this->getData('after_html');
  56. }
  57. $url = $this->urlBuilder->getUrl('temando/shipment/cancel', [
  58. 'shipment_id' => $shipment->getShipmentId(),
  59. 'sales_shipment_id' => $salesShipment->getEntityId(),
  60. ]);
  61. $this->jsLayout = [
  62. 'title' => $this->escapeHtml($this->getData('label')),
  63. 'message' => $this->escapeHtml(__('Are you sure you want to cancel this shipment?')),
  64. 'url' => $this->escapeUrl($url),
  65. 'loader' => true,
  66. ];
  67. $confirmComponent = <<<HTML
  68. <script type="text/x-magento-init">
  69. {
  70. "#{$this->getData('id')}": {
  71. "Temando_Shipping/js/modal/confirm": {$this->getJsLayout()}
  72. }
  73. }
  74. </script>
  75. HTML;
  76. return $this->getData('after_html') . $confirmComponent;
  77. }
  78. /**
  79. * Add button data
  80. *
  81. * @return string
  82. */
  83. protected function _toHtml()
  84. {
  85. $shipment = $this->shipmentProvider->getShipment();
  86. if (!$shipment->isCancelable()) {
  87. return '';
  88. }
  89. $this->setData('label', __('Cancel Shipment'));
  90. $this->setData('class', 'cancel');
  91. $this->setData('id', 'cancel');
  92. return parent::_toHtml();
  93. }
  94. }