PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Directory/Model/Resource/Country/Collection.php

https://github.com/cosmocommerce/magento-mirror
PHP | 177 lines | 90 code | 12 blank | 75 comment | 16 complexity | 010b480d34ebe095f13e96d3f52b212f MD5 | raw file
  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) 2014 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. * Directory Country Resource Collection
  28. *
  29. * @category Mage
  30. * @package Mage_Directory
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Directory_Model_Resource_Country_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * Define main table
  37. */
  38. protected function _construct()
  39. {
  40. $this->_init('directory/country');
  41. }
  42. /**
  43. * Get Store Config
  44. *
  45. * @param string $path
  46. * @param mixed|null $store
  47. * @return string
  48. */
  49. protected function _getStoreConfig($path, $store = null)
  50. {
  51. return Mage::getStoreConfig($path, $store);
  52. }
  53. /**
  54. * Load allowed countries for specific store
  55. *
  56. * @param mixed $store
  57. * @return Mage_Directory_Model_Resource_Country_Collection
  58. */
  59. public function loadByStore($store = null)
  60. {
  61. $allowCountries = explode(',', (string)$this->_getStoreConfig('general/country/allow', $store));
  62. if (!empty($allowCountries)) {
  63. $this->addFieldToFilter("country_id", array('in' => $allowCountries));
  64. }
  65. return $this;
  66. }
  67. /**
  68. * Loads Item By Id
  69. *
  70. * @param string $countryId
  71. * @return Mage_Directory_Model_Resource_Country
  72. */
  73. public function getItemById($countryId)
  74. {
  75. foreach ($this->_items as $country) {
  76. if ($country->getCountryId() == $countryId) {
  77. return $country;
  78. }
  79. }
  80. return Mage::getResourceModel('directory/country');
  81. }
  82. /**
  83. * Add filter by country code to collection.
  84. * $countryCode can be either array of country codes or string representing one country code.
  85. * $iso can be either array containing 'iso2', 'iso3' values or string with containing one of that values directly.
  86. * The collection will contain countries where at least one of contry $iso fields matches $countryCode.
  87. *
  88. * @param string|array $countryCode
  89. * @param string|array $iso
  90. * @return Mage_Directory_Model_Resource_Country_Collection
  91. */
  92. public function addCountryCodeFilter($countryCode, $iso = array('iso3', 'iso2'))
  93. {
  94. if (!empty($countryCode)) {
  95. if (is_array($countryCode)) {
  96. if (is_array($iso)) {
  97. $whereOr = array();
  98. foreach ($iso as $iso_curr) {
  99. $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", array('in' => $countryCode));
  100. }
  101. $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
  102. } else {
  103. $this->addFieldToFilter("{$iso}_code", array('in' => $countryCode));
  104. }
  105. } else {
  106. if (is_array($iso)) {
  107. $whereOr = array();
  108. foreach ($iso as $iso_curr) {
  109. $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", $countryCode);
  110. }
  111. $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
  112. } else {
  113. $this->addFieldToFilter("{$iso}_code", $countryCode);
  114. }
  115. }
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Add filter by country code(s) to collection
  121. *
  122. * @param string|array $countryId
  123. * @return Mage_Directory_Model_Resource_Country_Collection
  124. */
  125. public function addCountryIdFilter($countryId)
  126. {
  127. if (!empty($countryId)) {
  128. if (is_array($countryId)) {
  129. $this->addFieldToFilter("country_id", array('in' => $countryId));
  130. } else {
  131. $this->addFieldToFilter("country_id", $countryId);
  132. }
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Convert collection items to select options array
  138. *
  139. * @param string $emptyLabel
  140. * @return array
  141. */
  142. public function toOptionArray($emptyLabel = ' ')
  143. {
  144. $options = $this->_toOptionArray('country_id', 'name', array('title' => 'iso2_code'));
  145. $sort = array();
  146. foreach ($options as $data) {
  147. $name = Mage::app()->getLocale()->getCountryTranslation($data['value']);
  148. if (!empty($name)) {
  149. $sort[$name] = $data['value'];
  150. }
  151. }
  152. Mage::helper('core/string')->ksortMultibyte($sort);
  153. $options = array();
  154. foreach ($sort as $label => $value) {
  155. $options[] = array(
  156. 'value' => $value,
  157. 'label' => $label
  158. );
  159. }
  160. if (count($options) > 0 && $emptyLabel !== false) {
  161. array_unshift($options, array('value' => '', 'label' => $emptyLabel));
  162. }
  163. return $options;
  164. }
  165. }