*/ class Mage_Directory_Model_Resource_Country_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract { /** * Define main table */ protected function _construct() { $this->_init('directory/country'); } /** * Get Store Config * * @param string $path * @param mixed|null $store * @return string */ protected function _getStoreConfig($path, $store = null) { return Mage::getStoreConfig($path, $store); } /** * Load allowed countries for specific store * * @param mixed $store * @return Mage_Directory_Model_Resource_Country_Collection */ public function loadByStore($store = null) { $allowCountries = explode(',', (string)$this->_getStoreConfig('general/country/allow', $store)); if (!empty($allowCountries)) { $this->addFieldToFilter("country_id", array('in' => $allowCountries)); } return $this; } /** * Loads Item By Id * * @param string $countryId * @return Mage_Directory_Model_Resource_Country */ public function getItemById($countryId) { foreach ($this->_items as $country) { if ($country->getCountryId() == $countryId) { return $country; } } return Mage::getResourceModel('directory/country'); } /** * Add filter by country code to collection. * $countryCode can be either array of country codes or string representing one country code. * $iso can be either array containing 'iso2', 'iso3' values or string with containing one of that values directly. * The collection will contain countries where at least one of contry $iso fields matches $countryCode. * * @param string|array $countryCode * @param string|array $iso * @return Mage_Directory_Model_Resource_Country_Collection */ public function addCountryCodeFilter($countryCode, $iso = array('iso3', 'iso2')) { if (!empty($countryCode)) { if (is_array($countryCode)) { if (is_array($iso)) { $whereOr = array(); foreach ($iso as $iso_curr) { $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", array('in' => $countryCode)); } $this->_select->where('(' . implode(') OR (', $whereOr) . ')'); } else { $this->addFieldToFilter("{$iso}_code", array('in' => $countryCode)); } } else { if (is_array($iso)) { $whereOr = array(); foreach ($iso as $iso_curr) { $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", $countryCode); } $this->_select->where('(' . implode(') OR (', $whereOr) . ')'); } else { $this->addFieldToFilter("{$iso}_code", $countryCode); } } } return $this; } /** * Add filter by country code(s) to collection * * @param string|array $countryId * @return Mage_Directory_Model_Resource_Country_Collection */ public function addCountryIdFilter($countryId) { if (!empty($countryId)) { if (is_array($countryId)) { $this->addFieldToFilter("country_id", array('in' => $countryId)); } else { $this->addFieldToFilter("country_id", $countryId); } } return $this; } /** * Convert collection items to select options array * * @param string $emptyLabel * @return array */ public function toOptionArray($emptyLabel = ' ') { $options = $this->_toOptionArray('country_id', 'name', array('title' => 'iso2_code')); $sort = array(); foreach ($options as $data) { $name = Mage::app()->getLocale()->getCountryTranslation($data['value']); if (!empty($name)) { $sort[$name] = $data['value']; } } Mage::helper('core/string')->ksortMultibyte($sort); $options = array(); foreach ($sort as $label => $value) { $options[] = array( 'value' => $value, 'label' => $label ); } if (count($options) > 0 && $emptyLabel !== false) { array_unshift($options, array('value' => '', 'label' => $emptyLabel)); } return $options; } }