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 class Mage_Directory_Model_Mysql4_Region
00029 {
00030     protected $_regionTable;
00031     protected $_regionNameTable;
00032 
00033 
00034 
00035 
00036 
00037 
00038     protected $_read;
00039 
00040 
00041 
00042 
00043 
00044 
00045     protected $_write;
00046 
00047     public function __construct()
00048     {
00049         $resource = Mage::getSingleton('core/resource');
00050         $this->_regionTable     = $resource->getTableName('directory/country_region');
00051         $this->_regionNameTable = $resource->getTableName('directory/country_region_name');
00052         $this->_read    = $resource->getConnection('directory_read');
00053         $this->_write   = $resource->getConnection('directory_write');
00054     }
00055 
00056     public function getIdFieldName()
00057     {
00058         return 'region_id';
00059     }
00060 
00061     public function load(Mage_Directory_Model_Region $region, $regionId)
00062     {
00063         $locale = Mage::app()->getLocale()->getLocaleCode();
00064         $systemLocale = Mage::app()->getDistroLocaleCode();
00065 
00066         $select = $this->_read->select()
00067             ->from(array('region'=>$this->_regionTable))
00068             ->where('region.region_id=?', $regionId)
00069             ->join(array('rname'=>$this->_regionNameTable),
00070                 'rname.region_id=region.region_id AND (rname.locale=\''.$locale.'\' OR rname.locale=\''.$systemLocale.'\')',
00071                 array('name', new Zend_Db_Expr('CASE rname.locale WHEN \''.$systemLocale.'\' THEN 1 ELSE 0 END sort_locale')))
00072             ->order('sort_locale')
00073             ->limit(1);
00074 
00075         $region->setData($this->_read->fetchRow($select));
00076         return $this;
00077     }
00078 
00079     public function loadByCode(Mage_Directory_Model_Region $region, $regionCode, $countryId)
00080     {
00081         $locale = Mage::app()->getLocale()->getLocaleCode();
00082 
00083         $select = $this->_read->select()
00084             ->from(array('region'=>$this->_regionTable))
00085             ->where('region.country_id=?', $countryId)
00086             ->where('region.code=?', $regionCode)
00087             ->join(array('rname'=>$this->_regionNameTable),
00088                 'rname.region_id=region.region_id AND rname.locale=\''.$locale.'\'',
00089                 array('name'));
00090 
00091         $region->setData($this->_read->fetchRow($select));
00092         return $this;
00093     }
00094 
00095     public function loadByName(Mage_Directory_Model_Region $region, $regionName, $countryId)
00096     {
00097         $locale = Mage::app()->getLocale()->getLocaleCode();
00098 
00099         $select = $this->_read->select()
00100             ->from(array('region'=>$this->_regionTable))
00101             ->where('region.country_id=?', $countryId)
00102             ->where('region.default_name=?', $regionName)
00103             ->join(array('rname'=>$this->_regionNameTable),
00104                 'rname.region_id=region.region_id AND rname.locale=\''.$locale.'\'',
00105                 array('name'));
00106 
00107         $region->setData($this->_read->fetchRow($select));
00108         return $this;
00109     }
00110 }