Data.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Block;
  7. /**
  8. * Directory data block
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class Data extends \Magento\Framework\View\Element\Template
  13. {
  14. /**
  15. * @var \Magento\Framework\App\Cache\Type\Config
  16. */
  17. protected $_configCacheType;
  18. /**
  19. * @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
  20. */
  21. protected $_regionCollectionFactory;
  22. /**
  23. * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
  24. */
  25. protected $_countryCollectionFactory;
  26. /**
  27. * @var \Magento\Framework\Json\EncoderInterface
  28. */
  29. protected $_jsonEncoder;
  30. /**
  31. * @var \Magento\Directory\Helper\Data
  32. */
  33. protected $directoryHelper;
  34. /**
  35. * @var \Magento\Framework\Serialize\SerializerInterface
  36. */
  37. private $serializer;
  38. /**
  39. * @param \Magento\Framework\View\Element\Template\Context $context
  40. * @param \Magento\Directory\Helper\Data $directoryHelper
  41. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  42. * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
  43. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  44. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
  45. * @param array $data
  46. */
  47. public function __construct(
  48. \Magento\Framework\View\Element\Template\Context $context,
  49. \Magento\Directory\Helper\Data $directoryHelper,
  50. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  51. \Magento\Framework\App\Cache\Type\Config $configCacheType,
  52. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
  53. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
  54. array $data = []
  55. ) {
  56. parent::__construct($context, $data);
  57. $this->directoryHelper = $directoryHelper;
  58. $this->_jsonEncoder = $jsonEncoder;
  59. $this->_configCacheType = $configCacheType;
  60. $this->_regionCollectionFactory = $regionCollectionFactory;
  61. $this->_countryCollectionFactory = $countryCollectionFactory;
  62. }
  63. /**
  64. * Returns load url for regions
  65. *
  66. * @return string
  67. */
  68. public function getLoadrRegionUrl()
  69. {
  70. return $this->getUrl('directory/json/childRegion');
  71. }
  72. /**
  73. * Returns country collection instance
  74. *
  75. * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  76. */
  77. public function getCountryCollection()
  78. {
  79. $collection = $this->getData('country_collection');
  80. if ($collection === null) {
  81. $collection = $this->_countryCollectionFactory->create()->loadByStore();
  82. $this->setData('country_collection', $collection);
  83. }
  84. return $collection;
  85. }
  86. /**
  87. * Retrieve list of top destinations countries
  88. *
  89. * @return array
  90. */
  91. protected function getTopDestinations()
  92. {
  93. $destinations = (string)$this->_scopeConfig->getValue(
  94. 'general/country/destinations',
  95. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  96. );
  97. return !empty($destinations) ? explode(',', $destinations) : [];
  98. }
  99. /**
  100. * Returns country html select
  101. *
  102. * @param null|string $defValue
  103. * @param string $name
  104. * @param string $id
  105. * @param string $title
  106. * @return string
  107. */
  108. public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country')
  109. {
  110. \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
  111. if ($defValue === null) {
  112. $defValue = $this->getCountryId();
  113. }
  114. $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . $this->_storeManager->getStore()->getCode();
  115. $cache = $this->_configCacheType->load($cacheKey);
  116. if ($cache) {
  117. $options = $this->getSerializer()->unserialize($cache);
  118. } else {
  119. $options = $this->getCountryCollection()
  120. ->setForegroundCountries($this->getTopDestinations())
  121. ->toOptionArray();
  122. $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
  123. }
  124. $html = $this->getLayout()->createBlock(
  125. \Magento\Framework\View\Element\Html\Select::class
  126. )->setName(
  127. $name
  128. )->setId(
  129. $id
  130. )->setTitle(
  131. __($title)
  132. )->setValue(
  133. $defValue
  134. )->setOptions(
  135. $options
  136. )->setExtraParams(
  137. 'data-validate="{\'validate-select\':true}"'
  138. )->getHtml();
  139. \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
  140. return $html;
  141. }
  142. /**
  143. * Returns region collection
  144. *
  145. * @return \Magento\Directory\Model\ResourceModel\Region\Collection
  146. */
  147. public function getRegionCollection()
  148. {
  149. $collection = $this->getData('region_collection');
  150. if ($collection === null) {
  151. $collection = $this->_regionCollectionFactory->create()->addCountryFilter($this->getCountryId())->load();
  152. $this->setData('region_collection', $collection);
  153. }
  154. return $collection;
  155. }
  156. /**
  157. * Returns region html select
  158. *
  159. * @return string
  160. */
  161. public function getRegionHtmlSelect()
  162. {
  163. \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
  164. $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . $this->_storeManager->getStore()->getId();
  165. $cache = $this->_configCacheType->load($cacheKey);
  166. if ($cache) {
  167. $options = $this->getSerializer()->unserialize($cache);
  168. } else {
  169. $options = $this->getRegionCollection()->toOptionArray();
  170. $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
  171. }
  172. $html = $this->getLayout()->createBlock(
  173. \Magento\Framework\View\Element\Html\Select::class
  174. )->setName(
  175. 'region'
  176. )->setTitle(
  177. __('State/Province')
  178. )->setId(
  179. 'state'
  180. )->setClass(
  181. 'required-entry validate-state'
  182. )->setValue(
  183. (int)$this->getRegionId()
  184. )->setOptions(
  185. $options
  186. )->getHtml();
  187. \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
  188. return $html;
  189. }
  190. /**
  191. * Returns country id
  192. *
  193. * @return string
  194. */
  195. public function getCountryId()
  196. {
  197. $countryId = $this->getData('country_id');
  198. if ($countryId === null) {
  199. $countryId = $this->directoryHelper->getDefaultCountry();
  200. }
  201. return $countryId;
  202. }
  203. /**
  204. * Returns regions js
  205. *
  206. * @return string
  207. */
  208. public function getRegionsJs()
  209. {
  210. \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
  211. $regionsJs = $this->getData('regions_js');
  212. if (!$regionsJs) {
  213. $countryIds = [];
  214. foreach ($this->getCountryCollection() as $country) {
  215. $countryIds[] = $country->getCountryId();
  216. }
  217. $collection = $this->_regionCollectionFactory->create()->addCountryFilter($countryIds)->load();
  218. $regions = [];
  219. foreach ($collection as $region) {
  220. if (!$region->getRegionId()) {
  221. continue;
  222. }
  223. $regions[$region->getCountryId()][$region->getRegionId()] = [
  224. 'code' => $region->getCode(),
  225. 'name' => $region->getName(),
  226. ];
  227. }
  228. $regionsJs = $this->_jsonEncoder->encode($regions);
  229. }
  230. \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
  231. return $regionsJs;
  232. }
  233. /**
  234. * Get serializer
  235. *
  236. * @return \Magento\Framework\Serialize\SerializerInterface
  237. * @deprecated 100.2.0
  238. */
  239. private function getSerializer()
  240. {
  241. if ($this->serializer === null) {
  242. $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
  243. ->get(\Magento\Framework\Serialize\SerializerInterface::class);
  244. }
  245. return $this->serializer;
  246. }
  247. }