1: <?php
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: class Mage_Directory_Model_Resource_Region_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_regionNameTable;
43:
44: 45: 46: 47: 48:
49: protected $_countryTable;
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('directory/region');
58:
59: $this->_countryTable = $this->getTable('directory/country');
60: $this->_regionNameTable = $this->getTable('directory/country_region_name');
61:
62: $this->addOrder('name', Varien_Data_Collection::SORT_ORDER_ASC);
63: $this->addOrder('default_name', Varien_Data_Collection::SORT_ORDER_ASC);
64: }
65:
66: 67: 68: 69: 70:
71: protected function _initSelect()
72: {
73: parent::_initSelect();
74: $locale = Mage::app()->getLocale()->getLocaleCode();
75:
76: $this->addBindParam(':region_locale', $locale);
77: $this->getSelect()->joinLeft(
78: array('rname' => $this->_regionNameTable),
79: 'main_table.region_id = rname.region_id AND rname.locale = :region_locale',
80: array('name'));
81:
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function addCountryFilter($countryId)
92: {
93: if (!empty($countryId)) {
94: if (is_array($countryId)) {
95: $this->addFieldToFilter('main_table.country_id', array('in' => $countryId));
96: } else {
97: $this->addFieldToFilter('main_table.country_id', $countryId);
98: }
99: }
100: return $this;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function addCountryCodeFilter($countryCode)
110: {
111: $this->getSelect()
112: ->joinLeft(
113: array('country' => $this->_countryTable),
114: 'main_table.country_id = country.country_id'
115: )
116: ->where('country.iso3_code = ?', $countryCode);
117:
118: return $this;
119: }
120:
121: 122: 123: 124: 125: 126:
127: public function addRegionCodeFilter($regionCode)
128: {
129: if (!empty($regionCode)) {
130: if (is_array($regionCode)) {
131: $this->addFieldToFilter('main_table.code', array('in' => $regionCode));
132: } else {
133: $this->addFieldToFilter('main_table.code', $regionCode);
134: }
135: }
136: return $this;
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function addRegionNameFilter($regionName)
146: {
147: if (!empty($regionName)) {
148: if (is_array($regionName)) {
149: $this->addFieldToFilter('main_table.default_name', array('in' => $regionName));
150: } else {
151: $this->addFieldToFilter('main_table.default_name', $regionName);
152: }
153: }
154: return $this;
155: }
156:
157: 158: 159: 160: 161:
162: public function toOptionArray()
163: {
164: $options = $this->_toOptionArray('region_id', 'default_name', array('title' => 'default_name'));
165: if (count($options) > 0) {
166: array_unshift($options, array(
167: 'title '=> null,
168: 'value' => '0',
169: 'label' => Mage::helper('directory')->__('-- Please select --')
170: ));
171: }
172: return $options;
173: }
174: }
175: