View.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Dispatch;
  6. use Magento\Backend\Block\Widget\Container;
  7. use Magento\Backend\Block\Widget\Context;
  8. use Magento\Framework\Stdlib\DateTime\TimezoneInterfaceFactory;
  9. use Temando\Shipping\Model\DispatchInterface;
  10. use Temando\Shipping\Model\DispatchProviderInterface;
  11. /**
  12. * Temando Dispatch Layout Block
  13. *
  14. * @package Temando\Shipping\Block
  15. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link http://www.temando.com/
  18. *
  19. * @api
  20. */
  21. class View extends Container
  22. {
  23. /**
  24. * @var DispatchProviderInterface
  25. */
  26. private $dispatchProvider;
  27. /**
  28. * @var TimezoneInterfaceFactory
  29. */
  30. private $timezoneFactory;
  31. /**
  32. * View constructor.
  33. * @param Context $context
  34. * @param DispatchProviderInterface $dispatchProvider
  35. * @param TimezoneInterfaceFactory $timezoneFactory
  36. * @param mixed[] $data
  37. */
  38. public function __construct(
  39. Context $context,
  40. DispatchProviderInterface $dispatchProvider,
  41. TimezoneInterfaceFactory $timezoneFactory,
  42. array $data = []
  43. ) {
  44. $this->dispatchProvider = $dispatchProvider;
  45. $this->timezoneFactory = $timezoneFactory;
  46. parent::__construct($context, $data);
  47. }
  48. /**
  49. * Add Back Button.
  50. *
  51. * @return \Magento\Framework\View\Element\AbstractBlock
  52. */
  53. protected function _prepareLayout()
  54. {
  55. $buttonData = [
  56. 'label' => __('Back'),
  57. 'onclick' => sprintf("window.location.href = '%s';", $this->getDispatchesPageUrl()),
  58. 'class' => 'back',
  59. 'sort_order' => 10
  60. ];
  61. $this->addButton('back', $buttonData);
  62. return parent::_prepareLayout();
  63. }
  64. /**
  65. * Obtain dispatches grid url
  66. *
  67. * @return string
  68. */
  69. public function getDispatchesPageUrl()
  70. {
  71. return $this->getUrl('temando/dispatch/index');
  72. }
  73. /**
  74. * Obtain url for troubleshooting failed dispatches
  75. *
  76. * @return string
  77. */
  78. public function getSolveUrl()
  79. {
  80. return $this->getUrl('temando/dispatch/solve', [
  81. 'dispatch_id' => $this->getDispatch()->getDispatchId()
  82. ]);
  83. }
  84. /**
  85. * @return DispatchInterface|null
  86. */
  87. public function getDispatch()
  88. {
  89. return $this->dispatchProvider->getDispatch();
  90. }
  91. /**
  92. * Obtain date. Parent method fails to convert date format returned from api.
  93. *
  94. * @see \Magento\Framework\View\Element\AbstractBlock::formatDate()
  95. * @see \Magento\Framework\Stdlib\DateTime\Timezone::formatDateTime()
  96. *
  97. * @param string $date
  98. * @return \DateTime
  99. */
  100. public function getDate($date)
  101. {
  102. $timezone = $this->timezoneFactory->create();
  103. $localizedDate = $timezone->date(new \DateTime($date));
  104. return $localizedDate;
  105. }
  106. }