ComponentContainer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  6. use Magento\Backend\Block\Widget\Container;
  7. use Magento\Backend\Block\Widget\Context;
  8. use Magento\Directory\Helper\Data as DirectoryHelper;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
  11. use Magento\Framework\View\Element\Block\ArgumentInterface;
  12. use Temando\Shipping\ViewModel\PageActionsInterface;
  13. /**
  14. * Default block for all pages that display Temando components
  15. *
  16. * @package Temando\Shipping\Block
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link http://www.temando.com/
  20. *
  21. * @api
  22. */
  23. class ComponentContainer extends Container
  24. {
  25. /**
  26. * @var RemoteAddress
  27. */
  28. private $remoteAddress;
  29. /**
  30. * @param Context $context
  31. * @param RemoteAddress $remoteAddress
  32. * @param mixed[] $data
  33. */
  34. public function __construct(
  35. Context $context,
  36. RemoteAddress $remoteAddress,
  37. array $data = []
  38. ) {
  39. $this->remoteAddress = $remoteAddress;
  40. parent::__construct($context, $data);
  41. }
  42. /**
  43. * Obtain view model for display data.
  44. *
  45. * There is no getter in the core like it is offered for the jsLayout argument…
  46. *
  47. * @return ArgumentInterface|null
  48. */
  49. public function getViewModel()
  50. {
  51. return $this->getData('viewModel');
  52. }
  53. /**
  54. * Obtain componentry assets base url.
  55. *
  56. * @return string
  57. */
  58. public function getAssetsUrl()
  59. {
  60. return $this->getViewFileUrl('Temando_Shipping') . '/';
  61. }
  62. /**
  63. * @return string
  64. */
  65. public function getLocale()
  66. {
  67. $localeCode = $this->_scopeConfig->getValue(
  68. DirectoryHelper::XML_PATH_DEFAULT_LOCALE,
  69. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  70. );
  71. return strtolower(str_replace('_', '-', $localeCode));
  72. }
  73. /**
  74. * Obtain Language Code.
  75. *
  76. * @return string
  77. */
  78. public function getLanguage()
  79. {
  80. return substr_replace($this->getLocale(), '', 2);
  81. }
  82. /**
  83. * Obtain merchant IP address.
  84. *
  85. * @return string
  86. */
  87. public function getIpAddress()
  88. {
  89. return $this->remoteAddress->getRemoteAddress();
  90. }
  91. /**
  92. * Add Action Buttons.
  93. *
  94. * @return \Magento\Framework\View\Element\AbstractBlock
  95. */
  96. protected function _prepareLayout()
  97. {
  98. $viewModel = $this->getViewModel();
  99. if (!$viewModel instanceof PageActionsInterface) {
  100. return parent::_prepareLayout();
  101. }
  102. $actions = $viewModel->getMainActions();
  103. if (!empty($actions) && is_array($actions)) {
  104. array_walk($actions, function ($buttonData, $buttonId) {
  105. $this->buttonList->add($buttonId, $buttonData);
  106. });
  107. }
  108. return parent::_prepareLayout();
  109. }
  110. }