00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Directory_Model_Mysql4_Region_Collection extends Varien_Data_Collection_Db
00035 {
00036 protected $_regionTable;
00037 protected $_regionNameTable;
00038 protected $_countryTable;
00039
00040 public function __construct()
00041 {
00042 parent::__construct(Mage::getSingleton('core/resource')->getConnection('directory_read'));
00043
00044 $this->_countryTable = Mage::getSingleton('core/resource')->getTableName('directory/country');
00045 $this->_regionTable = Mage::getSingleton('core/resource')->getTableName('directory/country_region');
00046 $this->_regionNameTable = Mage::getSingleton('core/resource')->getTableName('directory/country_region_name');
00047
00048 $locale = Mage::app()->getLocale()->getLocaleCode();
00049
00050 $this->_select->from(array('region'=>$this->_regionTable),
00051 array('region_id'=>'region_id', 'country_id'=>'country_id', 'code'=>'code', 'default_name'=>'default_name')
00052 );
00053 $this->_select->joinLeft(array('rname'=>$this->_regionNameTable),
00054 "region.region_id=rname.region_id AND rname.locale='$locale'", array('name'));
00055
00056 $this->setItemObjectClass(Mage::getConfig()->getModelClassName('directory/region'));
00057 }
00058
00059 public function addCountryFilter($countryId)
00060 {
00061 if (!empty($countryId)) {
00062 if (is_array($countryId)) {
00063 $this->addFieldToFilter('region.country_id', array('in'=>$countryId));
00064 } else {
00065 $this->addFieldToFilter('region.country_id', $countryId);
00066 }
00067 }
00068 return $this;
00069 }
00070
00071 public function addCountryCodeFilter($countryCode)
00072 {
00073 $this->_select->joinLeft(array('country'=>$this->_countryTable), 'region.country_id=country.country_id');
00074 $this->_select->where("country.iso3_code = '{$countryCode}'");
00075 return $this;
00076 }
00077
00078 public function addRegionCodeFilter($regionCode)
00079 {
00080 if (!empty($regionCode)) {
00081 if (is_array($regionCode)) {
00082 $this->_select->where("region.code IN ('".implode("','", $regionCode)."')");
00083 } else {
00084 $this->_select->where("region.code = '{$regionCode}'");
00085 }
00086 }
00087 return $this;
00088 }
00089
00090 public function addRegionNameFilter($regionName)
00091 {
00092 if (!empty($regionName)) {
00093 if (is_array($regionName)) {
00094 $this->_select->where("region.default_name in ('".implode("','", $regionName)."')");
00095 } else {
00096 $this->_select->where("region.default_name = '{$regionName}'");
00097 }
00098 }
00099 return $this;
00100 }
00101
00102 public function toOptionArray()
00103 {
00104 $options = array();
00105 foreach ($this as $item) {
00106 $options[] = array(
00107 'value' => $item->getId(),
00108 'label' => $item->getName()
00109 );
00110 }
00111 if (count($options)>0) {
00112 array_unshift($options, array('title'=>null, 'value'=>'0', 'label'=>Mage::helper('directory')->__('-- Please select --')));
00113 }
00114 return $options;
00115 }
00116 }