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