LocationEdit.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\ViewModel\Location;
  6. use Magento\Framework\App\RequestInterface;
  7. use Magento\Framework\View\Element\Block\ArgumentInterface;
  8. use Temando\Shipping\Model\LocationInterface;
  9. use Temando\Shipping\ViewModel\PageActionsInterface;
  10. use Temando\Shipping\ViewModel\ShippingApiInterface;
  11. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccess;
  12. use Temando\Shipping\ViewModel\DataProvider\ShippingApiAccessInterface;
  13. use Temando\Shipping\ViewModel\DataProvider\LocationUrl;
  14. use Temando\Shipping\ViewModel\DataProvider\EntityUrlInterface;
  15. /**
  16. * View model for location new/edit JS component.
  17. *
  18. * @package Temando\Shipping\ViewModel
  19. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  20. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  21. * @link http://www.temando.com/
  22. */
  23. class LocationEdit implements ArgumentInterface, PageActionsInterface, ShippingApiInterface
  24. {
  25. /**
  26. * @var RequestInterface
  27. */
  28. private $request;
  29. /**
  30. * @var ShippingApiAccess
  31. */
  32. private $apiAccess;
  33. /**
  34. * @var LocationUrl
  35. */
  36. private $locationUrl;
  37. /**
  38. * LocationEdit constructor.
  39. * @param RequestInterface $request
  40. * @param ShippingApiAccess $apiAccess
  41. * @param LocationUrl $locationUrl
  42. */
  43. public function __construct(
  44. RequestInterface $request,
  45. ShippingApiAccess $apiAccess,
  46. LocationUrl $locationUrl
  47. ) {
  48. $this->request = $request;
  49. $this->apiAccess = $apiAccess;
  50. $this->locationUrl = $locationUrl;
  51. }
  52. /**
  53. * Obtain array of button data.
  54. *
  55. * @see \Temando\Shipping\Block\Adminhtml\ComponentContainer::_prepareLayout
  56. * @see \Magento\Backend\Block\Widget\Button\ButtonList::add
  57. *
  58. * @return mixed[][]
  59. */
  60. public function getMainActions(): array
  61. {
  62. $buttonId = 'back';
  63. $buttonData = [
  64. 'label' => __('Back'),
  65. 'onclick' => sprintf("window.location.href = '%s';", $this->locationUrl->getListActionUrl()),
  66. 'class' => 'back',
  67. 'sort_order' => 10
  68. ];
  69. $mainActions = [
  70. $buttonId => $buttonData,
  71. ];
  72. return $mainActions;
  73. }
  74. /**
  75. * @return ShippingApiAccessInterface
  76. */
  77. public function getShippingApiAccess(): ShippingApiAccessInterface
  78. {
  79. return $this->apiAccess;
  80. }
  81. /**
  82. * @return EntityUrlInterface|LocationUrl
  83. */
  84. public function getLocationUrl(): EntityUrlInterface
  85. {
  86. return $this->locationUrl;
  87. }
  88. /**
  89. * Obtain the Temando location id that is passed from grid to edit component.
  90. * Think of it as a GUID rather than a location id in the local storage.
  91. *
  92. * @return string The Temando location id
  93. */
  94. public function getLocationId(): string
  95. {
  96. $locationId = $this->request->getParam(LocationInterface::LOCATION_ID);
  97. return preg_replace('/[^\w0-9-_]/', '', $locationId);
  98. }
  99. }