1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Directory
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Directory Country Resource Collection
30: *
31: * @category Mage
32: * @package Mage_Directory
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Directory_Model_Resource_Country_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: /**
38: * Define main table
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('directory/country');
44: }
45:
46: /**
47: * Load allowed countries for current store
48: *
49: * @param mixed $store
50: * @return Mage_Directory_Model_Resource_Country_Collection
51: */
52: public function loadByStore($store = null)
53: {
54: $allowCountries = explode(',', (string)Mage::getStoreConfig('general/country/allow', $store));
55: if (!empty($allowCountries)) {
56: $this->addFieldToFilter("country_id", array('in' => $allowCountries));
57: }
58: return $this;
59: }
60:
61: /**
62: * Loads Item By Id
63: *
64: * @param string $countryId
65: * @return Mage_Directory_Model_Resource_Country
66: */
67: public function getItemById($countryId)
68: {
69: foreach ($this->_items as $country) {
70: if ($country->getCountryId() == $countryId) {
71: return $country;
72: }
73: }
74: return Mage::getResourceModel('directory/country');
75: }
76:
77: /**
78: * Add filter by country code to collection.
79: * $countryCode can be either array of country codes or string representing one country code.
80: * $iso can be either array containing 'iso2', 'iso3' values or string with containing one of that values directly.
81: * The collection will contain countries where at least one of contry $iso fields matches $countryCode.
82: *
83: * @param string|array $countryCode
84: * @param string|array $iso
85: * @return Mage_Directory_Model_Resource_Country_Collection
86: */
87: public function addCountryCodeFilter($countryCode, $iso = array('iso3', 'iso2'))
88: {
89: if (!empty($countryCode)) {
90: if (is_array($countryCode)) {
91: if (is_array($iso)) {
92: $whereOr = array();
93: foreach ($iso as $iso_curr) {
94: $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", array('in' => $countryCode));
95: }
96: $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
97: } else {
98: $this->addFieldToFilter("{$iso}_code", array('in'=>$countryCode));
99: }
100: } else {
101: if (is_array($iso)) {
102: $whereOr = array();
103: foreach ($iso as $iso_curr) {
104: $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", $countryCode);
105: }
106: $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
107: } else {
108: $this->addFieldToFilter("{$iso}_code", $countryCode);
109: }
110: }
111: }
112: return $this;
113: }
114:
115: /**
116: * Add filter by country code(s) to collection
117: *
118: * @param string|array $countryId
119: * @return Mage_Directory_Model_Resource_Country_Collection
120: */
121: public function addCountryIdFilter($countryId)
122: {
123: if (!empty($countryId)) {
124: if (is_array($countryId)) {
125: $this->addFieldToFilter("country_id", array('in' => $countryId));
126: } else {
127: $this->addFieldToFilter("country_id", $countryId);
128: }
129: }
130: return $this;
131: }
132:
133: /**
134: * Convert collection items to select options array
135: *
136: * @param string $emptyLabel
137: * @return array
138: */
139: public function toOptionArray($emptyLabel = ' ')
140: {
141: $options = $this->_toOptionArray('country_id', 'name', array('title'=>'iso2_code'));
142:
143: $sort = array();
144: foreach ($options as $data) {
145: $name = Mage::app()->getLocale()->getCountryTranslation($data['value']);
146: if (!empty($name)) {
147: $sort[$name] = $data['value'];
148: }
149: }
150:
151: Mage::helper('core/string')->ksortMultibyte($sort);
152: $options = array();
153: foreach ($sort as $label=>$value) {
154: $options[] = array(
155: 'value' => $value,
156: 'label' => $label
157: );
158: }
159:
160: if (count($options) > 0 && $emptyLabel !== false) {
161: array_unshift($options, array('value' => '', 'label' => $emptyLabel));
162: }
163:
164: return $options;
165: }
166: }
167: