Country.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Field renderer for PayPal merchant country selector
  8. */
  9. namespace Magento\Paypal\Block\Adminhtml\System\Config\Field;
  10. use Magento\Paypal\Model\Config\StructurePlugin;
  11. class Country extends \Magento\Config\Block\System\Config\Form\Field
  12. {
  13. /**
  14. * Config path for merchant country selector
  15. */
  16. const FIELD_CONFIG_PATH = 'paypal/general/merchant_country';
  17. /**
  18. * Request parameter name for default country
  19. */
  20. const REQUEST_PARAM_DEFAULT_COUNTRY = 'paypal_default_country';
  21. /**
  22. * Country of default scope
  23. *
  24. * @var string
  25. */
  26. protected $_defaultCountry;
  27. /**
  28. * @var \Magento\Backend\Model\Url
  29. */
  30. protected $_url;
  31. /**
  32. * @var \Magento\Framework\View\Helper\Js
  33. */
  34. protected $_jsHelper;
  35. /**
  36. * @var \Magento\Directory\Helper\Data
  37. */
  38. protected $directoryHelper;
  39. /**
  40. * @param \Magento\Backend\Block\Template\Context $context
  41. * @param \Magento\Backend\Model\Url $url
  42. * @param \Magento\Framework\View\Helper\Js $jsHelper
  43. * @param \Magento\Directory\Helper\Data $directoryHelper
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Backend\Block\Template\Context $context,
  48. \Magento\Backend\Model\Url $url,
  49. \Magento\Framework\View\Helper\Js $jsHelper,
  50. \Magento\Directory\Helper\Data $directoryHelper,
  51. array $data = []
  52. ) {
  53. parent::__construct($context, $data);
  54. $this->_url = $url;
  55. $this->_jsHelper = $jsHelper;
  56. $this->directoryHelper = $directoryHelper;
  57. }
  58. /**
  59. * Render country field considering request parameter
  60. *
  61. * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
  62. * @return string
  63. */
  64. public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
  65. {
  66. $country = $this->getRequest()->getParam(StructurePlugin::REQUEST_PARAM_COUNTRY);
  67. if ($country) {
  68. $element->setValue($country);
  69. }
  70. if ($element->getCanUseDefaultValue()) {
  71. $this->_defaultCountry = $this->_scopeConfig->getValue(self::FIELD_CONFIG_PATH);
  72. if (!$this->_defaultCountry) {
  73. $this->_defaultCountry = $this->directoryHelper->getDefaultCountry();
  74. }
  75. if ($country) {
  76. $shouldInherit = $country == $this->_defaultCountry
  77. && $this->getRequest()->getParam(self::REQUEST_PARAM_DEFAULT_COUNTRY);
  78. $element->setInherit($shouldInherit);
  79. }
  80. if ($element->getInherit()) {
  81. $this->_defaultCountry = null;
  82. }
  83. }
  84. return parent::render($element);
  85. }
  86. /**
  87. * Get country selector html
  88. *
  89. * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
  90. * @return string
  91. */
  92. protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
  93. {
  94. $urlParams = [
  95. 'section' => $this->getRequest()->getParam('section'),
  96. 'website' => $this->getRequest()->getParam('website'),
  97. 'store' => $this->getRequest()->getParam('store'),
  98. StructurePlugin::REQUEST_PARAM_COUNTRY => '__country__',
  99. ];
  100. $urlString = $this->_escaper->escapeUrl($this->_url->getUrl('*/*/*', $urlParams));
  101. $jsString = '
  102. $("' . $element->getHtmlId() . '").observe("change", function () {
  103. location.href = \'' . $urlString . '\'.replace("__country__", this.value);
  104. });
  105. ';
  106. if ($this->_defaultCountry) {
  107. $urlParams[self::REQUEST_PARAM_DEFAULT_COUNTRY] = '__default__';
  108. $urlString = $this->_escaper->escapeUrl($this->_url->getUrl('*/*/*', $urlParams));
  109. $jsParentCountry = $this->_escaper->escapeJs($this->_defaultCountry);
  110. $jsString .= '
  111. $("' . $element->getHtmlId() . '_inherit").observe("click", function () {
  112. if (this.checked) {
  113. location.href = \'' . $urlString . '\'.replace("__country__", \'' . $jsParentCountry . '\')
  114. .replace("__default__", "1");
  115. }
  116. });
  117. ';
  118. }
  119. return parent::_getElementHtml($element) . $this->_jsHelper->getScript(
  120. 'require([\'prototype\'], function(){document.observe("dom:loaded", function() {' . $jsString . '});});'
  121. );
  122. }
  123. }