1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Directory
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Directory Region Resource Model
30: *
31: * @category Mage
32: * @package Mage_Directory
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Directory_Model_Resource_Region extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Table with localized region names
39: *
40: * @var string
41: */
42: protected $_regionNameTable;
43:
44: /**
45: * Define main and locale region name tables
46: *
47: */
48: protected function _construct()
49: {
50: $this->_init('directory/country_region', 'region_id');
51: $this->_regionNameTable = $this->getTable('directory/country_region_name');
52: }
53:
54: /**
55: * Retrieve select object for load object data
56: *
57: * @param string $field
58: * @param mixed $value
59: * @param Mage_Core_Model_Abstract $object
60: *
61: * @return Varien_Db_Select
62: */
63: protected function _getLoadSelect($field, $value, $object)
64: {
65: $select = parent::_getLoadSelect($field, $value, $object);
66: $adapter = $this->_getReadAdapter();
67:
68: $locale = Mage::app()->getLocale()->getLocaleCode();
69: $systemLocale = Mage::app()->getDistroLocaleCode();
70:
71: $regionField = $adapter->quoteIdentifier($this->getMainTable() . '.' . $this->getIdFieldName());
72:
73: $condition = $adapter->quoteInto('lrn.locale = ?', $locale);
74: $select->joinLeft(
75: array('lrn' => $this->_regionNameTable),
76: "{$regionField} = lrn.region_id AND {$condition}",
77: array());
78:
79: if ($locale != $systemLocale) {
80: $nameExpr = $adapter->getCheckSql('lrn.region_id is null', 'srn.name', 'lrn.name');
81: $condition = $adapter->quoteInto('srn.locale = ?', $systemLocale);
82: $select->joinLeft(
83: array('srn' => $this->_regionNameTable),
84: "{$regionField} = srn.region_id AND {$condition}",
85: array('name' => $nameExpr));
86: } else {
87: $select->columns(array('name'), 'lrn');
88: }
89:
90: return $select;
91: }
92:
93: /**
94: * Load object by country id and code or default name
95: *
96: * @param Mage_Core_Model_Abstract $object
97: * @param int $countryId
98: * @param string $value
99: * @param string $field
100: *
101: * @return Mage_Directory_Model_Resource_Region
102: */
103: protected function _loadByCountry($object, $countryId, $value, $field)
104: {
105: $adapter = $this->_getReadAdapter();
106: $locale = Mage::app()->getLocale()->getLocaleCode();
107: $joinCondition = $adapter->quoteInto('rname.region_id = region.region_id AND rname.locale = ?', $locale);
108: $select = $adapter->select()
109: ->from(array('region' => $this->getMainTable()))
110: ->joinLeft(
111: array('rname' => $this->_regionNameTable),
112: $joinCondition,
113: array('name'))
114: ->where('region.country_id = ?', $countryId)
115: ->where("region.{$field} = ?", $value);
116:
117: $data = $adapter->fetchRow($select);
118: if ($data) {
119: $object->setData($data);
120: }
121:
122: $this->_afterLoad($object);
123:
124: return $this;
125: }
126:
127: /**
128: * Loads region by region code and country id
129: *
130: * @param Mage_Directory_Model_Region $region
131: * @param string $regionCode
132: * @param string $countryId
133: *
134: * @return Mage_Directory_Model_Resource_Region
135: */
136: public function loadByCode(Mage_Directory_Model_Region $region, $regionCode, $countryId)
137: {
138: return $this->_loadByCountry($region, $countryId, (string)$regionCode, 'code');
139: }
140:
141: /**
142: * Load data by country id and default region name
143: *
144: * @param Mage_Directory_Model_Region $region
145: * @param string $regionName
146: * @param string $countryId
147: *
148: * @return Mage_Directory_Model_Resource_Region
149: */
150: public function loadByName(Mage_Directory_Model_Region $region, $regionName, $countryId)
151: {
152: return $this->_loadByCountry($region, $countryId, (string)$regionName, 'default_name');
153: }
154: }
155: