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_Country_Collection extends Varien_Data_Collection_Db
00035 {
00036 protected $_countryTable;
00037
00038 public function __construct()
00039 {
00040 parent::__construct(Mage::getSingleton('core/resource')->getConnection('directory_read'));
00041
00042 $this->_countryTable = Mage::getSingleton('core/resource')->getTableName('directory/country');
00043
00044 $this->_select->from(array('country'=>$this->_countryTable));
00045 $this->setItemObjectClass(Mage::getConfig()->getModelClassName('directory/country'));
00046 }
00047
00048 public function loadByStore()
00049 {
00050 $allowCountries = explode(',', (string)Mage::getStoreConfig('general/country/allow'));
00051 if (!empty($allowCountries)) {
00052 $this->addFieldToFilter("country.country_id", array('in'=>$allowCountries));
00053 }
00054
00055 $this->load();
00056
00057 return $this;
00058 }
00059
00060 public function getItemById($countryId)
00061 {
00062 foreach ($this->_items as $country) {
00063 if ($country->getCountryId() == $countryId) {
00064 return $country;
00065 }
00066 }
00067 return Mage::getResourceModel('directory/country');
00068 }
00069
00070 public function addCountryCodeFilter($countryCode, $iso=array(0 => 'iso3', 'iso2'))
00071 {
00072 if (!empty($countryCode)) {
00073 $where_expr = '';
00074 if (is_array($countryCode)) {
00075 if (is_array($iso)) {
00076 $i = 0;
00077 foreach ($iso as $iso_curr) {
00078 $where_expr .= ($i++ > 0 ? ' OR ' : '');
00079 $where_expr .= "country.{$iso_curr}_code IN ('".implode("','", $countryCode)."')";
00080 }
00081 } else {
00082 $where_expr = "country.{$iso}_code IN ('".implode("','", $countryCode)."')";
00083 }
00084 } else {
00085 if (is_array($iso)) {
00086 $i = 0;
00087 foreach ($iso as $iso_curr) {
00088 $where_expr .= ($i++ > 0 ? ' OR ' : '');
00089 $where_expr = "country.{$iso_curr}_code = '{$countryCode}'";
00090 }
00091 } else {
00092 $where_expr = "country.{$iso}_code = '{$countryCode}'";
00093 }
00094 }
00095 $this->_select->where($where_expr);
00096 }
00097 return $this;
00098 }
00099
00100 public function addCountryIdFilter($countryId)
00101 {
00102 if (!empty($countryId)) {
00103 if (is_array($countryId)) {
00104 $this->_select->where("country.country_id IN ('".implode("','", $countryId)."')");
00105 } else {
00106 $this->_select->where("country.country_id = '{$countryId}'");
00107 }
00108 }
00109 return $this;
00110 }
00111
00112 public function toOptionArray($emptyLabel = ' ')
00113 {
00114 $options = $this->_toOptionArray('country_id', 'name', array('title'=>'iso2_code'));
00115
00116 $sort = array();
00117 foreach ($options as $index=>$data) {
00118 $name = Mage::app()->getLocale()->getCountryTranslation($data['value']);
00119 if (!empty($name)) {
00120 $sort[$name] = $data['value'];
00121 }
00122 }
00123
00124 ksort($sort);
00125 $options = array();
00126 foreach ($sort as $label=>$value) {
00127 $options[] = array(
00128 'value' => $value,
00129 'label' => $label
00130 );
00131 }
00132
00133 if (count($options)>0 && $emptyLabel !== false) {
00134 array_unshift($options, array('value'=>'', 'label'=>$emptyLabel));
00135 }
00136 return $options;
00137 }
00138 }