Region.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model\ResourceModel;
  7. /**
  8. * Region Resource Model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Region extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Table with localized region names
  17. *
  18. * @var string
  19. */
  20. protected $_regionNameTable;
  21. /**
  22. * @var \Magento\Framework\Locale\ResolverInterface
  23. */
  24. protected $_localeResolver;
  25. /**
  26. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  27. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  28. * @param string $connectionName
  29. */
  30. public function __construct(
  31. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  32. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  33. $connectionName = null
  34. ) {
  35. parent::__construct($context, $connectionName);
  36. $this->_localeResolver = $localeResolver;
  37. }
  38. /**
  39. * Define main and locale region name tables
  40. *
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. $this->_init('directory_country_region', 'region_id');
  46. $this->_regionNameTable = $this->getTable('directory_country_region_name');
  47. }
  48. /**
  49. * Retrieve select object for load object data
  50. *
  51. * @param string $field
  52. * @param mixed $value
  53. * @param \Magento\Framework\Model\AbstractModel $object
  54. * @return \Magento\Framework\DB\Select
  55. */
  56. protected function _getLoadSelect($field, $value, $object)
  57. {
  58. $select = parent::_getLoadSelect($field, $value, $object);
  59. $connection = $this->getConnection();
  60. $locale = $this->_localeResolver->getLocale();
  61. $systemLocale = \Magento\Framework\AppInterface::DISTRO_LOCALE_CODE;
  62. $regionField = $connection->quoteIdentifier($this->getMainTable() . '.' . $this->getIdFieldName());
  63. $condition = $connection->quoteInto('lrn.locale = ?', $locale);
  64. $select->joinLeft(
  65. ['lrn' => $this->_regionNameTable],
  66. "{$regionField} = lrn.region_id AND {$condition}",
  67. []
  68. );
  69. if ($locale != $systemLocale) {
  70. $nameExpr = $connection->getCheckSql('lrn.region_id is null', 'srn.name', 'lrn.name');
  71. $condition = $connection->quoteInto('srn.locale = ?', $systemLocale);
  72. $select->joinLeft(
  73. ['srn' => $this->_regionNameTable],
  74. "{$regionField} = srn.region_id AND {$condition}",
  75. ['name' => $nameExpr]
  76. );
  77. } else {
  78. $select->columns(['name'], 'lrn');
  79. }
  80. return $select;
  81. }
  82. /**
  83. * Load object by country id and code or default name
  84. *
  85. * @param \Magento\Framework\Model\AbstractModel $object
  86. * @param int $countryId
  87. * @param string $value
  88. * @param string $field
  89. * @return $this
  90. */
  91. protected function _loadByCountry($object, $countryId, $value, $field)
  92. {
  93. $connection = $this->getConnection();
  94. $locale = $this->_localeResolver->getLocale();
  95. $joinCondition = $connection->quoteInto('rname.region_id = region.region_id AND rname.locale = ?', $locale);
  96. $select = $connection->select()->from(
  97. ['region' => $this->getMainTable()]
  98. )->joinLeft(
  99. ['rname' => $this->_regionNameTable],
  100. $joinCondition,
  101. ['name']
  102. )->where(
  103. 'region.country_id = ?',
  104. $countryId
  105. )->where(
  106. "region.{$field} = ?",
  107. $value
  108. );
  109. $data = $connection->fetchRow($select);
  110. if ($data) {
  111. $object->setData($data);
  112. }
  113. $this->_afterLoad($object);
  114. return $this;
  115. }
  116. /**
  117. * Loads region by region code and country id
  118. *
  119. * @param \Magento\Directory\Model\Region $region
  120. * @param string $regionCode
  121. * @param string $countryId
  122. *
  123. * @return $this
  124. */
  125. public function loadByCode(\Magento\Directory\Model\Region $region, $regionCode, $countryId)
  126. {
  127. return $this->_loadByCountry($region, $countryId, (string)$regionCode, 'code');
  128. }
  129. /**
  130. * Load data by country id and default region name
  131. *
  132. * @param \Magento\Directory\Model\Region $region
  133. * @param string $regionName
  134. * @param string $countryId
  135. * @return $this
  136. */
  137. public function loadByName(\Magento\Directory\Model\Region $region, $regionName, $countryId)
  138. {
  139. return $this->_loadByCountry($region, $countryId, (string)$regionName, 'default_name');
  140. }
  141. }