PrintShipment.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Sales\Controller\AbstractController;
  8. use Magento\Framework\App\Action\Context;
  9. use Magento\Framework\View\Result\PageFactory;
  10. abstract class PrintShipment extends \Magento\Framework\App\Action\Action
  11. {
  12. /**
  13. * @var OrderViewAuthorizationInterface
  14. */
  15. protected $orderAuthorization;
  16. /**
  17. * @var \Magento\Framework\Registry
  18. */
  19. protected $_coreRegistry;
  20. /**
  21. * @var PageFactory
  22. */
  23. protected $resultPageFactory;
  24. /**
  25. * @param Context $context
  26. * @param OrderViewAuthorizationInterface $orderAuthorization
  27. * @param \Magento\Framework\Registry $registry
  28. * @param PageFactory $resultPageFactory
  29. */
  30. public function __construct(
  31. Context $context,
  32. OrderViewAuthorizationInterface $orderAuthorization,
  33. \Magento\Framework\Registry $registry,
  34. PageFactory $resultPageFactory
  35. ) {
  36. $this->orderAuthorization = $orderAuthorization;
  37. $this->_coreRegistry = $registry;
  38. $this->resultPageFactory = $resultPageFactory;
  39. parent::__construct($context);
  40. }
  41. /**
  42. * Print Shipment Action
  43. *
  44. * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
  45. */
  46. public function execute()
  47. {
  48. $shipmentId = (int)$this->getRequest()->getParam('shipment_id');
  49. if ($shipmentId) {
  50. $shipment = $this->_objectManager->create(\Magento\Sales\Model\Order\Shipment::class)->load($shipmentId);
  51. $order = $shipment->getOrder();
  52. } else {
  53. $orderId = (int)$this->getRequest()->getParam('order_id');
  54. $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
  55. }
  56. if ($this->orderAuthorization->canView($order)) {
  57. $this->_coreRegistry->register('current_order', $order);
  58. if (isset($shipment)) {
  59. $this->_coreRegistry->register('current_shipment', $shipment);
  60. }
  61. /** @var \Magento\Framework\View\Result\Page $resultPage */
  62. $resultPage = $this->resultPageFactory->create();
  63. $resultPage->addHandle('print');
  64. return $resultPage;
  65. } else {
  66. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  67. $resultRedirect = $this->resultRedirectFactory->create();
  68. if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
  69. $resultRedirect->setPath('*/*/history');
  70. } else {
  71. $resultRedirect->setPath('sales/guest/form');
  72. }
  73. return $resultRedirect;
  74. }
  75. }
  76. }