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

https://bitbucket.org/kdms/sh-magento · PHP · 174 lines · 80 code · 15 blank · 79 comment · 10 complexity · c6979d50960a10a3d89c3b1c93be49c1 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  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://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Country collection
  28. *
  29. * @category Mage
  30. * @package Mage_Directory
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Directory_Model_Resource_Region_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
  34. {
  35. /**
  36. * Locale region name table name
  37. *
  38. * @var string
  39. */
  40. protected $_regionNameTable;
  41. /**
  42. * Country table name
  43. *
  44. * @var string
  45. */
  46. protected $_countryTable;
  47. /**
  48. * Define main, country, locale region name tables
  49. *
  50. */
  51. protected function _construct()
  52. {
  53. $this->_init('directory/region');
  54. $this->_countryTable = $this->getTable('directory/country');
  55. $this->_regionNameTable = $this->getTable('directory/country_region_name');
  56. $this->addOrder('name', Varien_Data_Collection::SORT_ORDER_ASC);
  57. $this->addOrder('default_name', Varien_Data_Collection::SORT_ORDER_ASC);
  58. }
  59. /**
  60. * Initialize select object
  61. *
  62. * @return Mage_Directory_Model_Resource_Region_Collection
  63. */
  64. protected function _initSelect()
  65. {
  66. parent::_initSelect();
  67. $locale = Mage::app()->getLocale()->getLocaleCode();
  68. $this->addBindParam(':region_locale', $locale);
  69. $this->getSelect()->joinLeft(
  70. array('rname' => $this->_regionNameTable),
  71. 'main_table.region_id = rname.region_id AND rname.locale = :region_locale',
  72. array('name'));
  73. return $this;
  74. }
  75. /**
  76. * Filter by country_id
  77. *
  78. * @param string|array $countryId
  79. * @return Mage_Directory_Model_Resource_Region_Collection
  80. */
  81. public function addCountryFilter($countryId)
  82. {
  83. if (!empty($countryId)) {
  84. if (is_array($countryId)) {
  85. $this->addFieldToFilter('main_table.country_id', array('in' => $countryId));
  86. } else {
  87. $this->addFieldToFilter('main_table.country_id', $countryId);
  88. }
  89. }
  90. return $this;
  91. }
  92. /**
  93. * Filter by country code (ISO 3)
  94. *
  95. * @param string $countryCode
  96. * @return Mage_Directory_Model_Resource_Region_Collection
  97. */
  98. public function addCountryCodeFilter($countryCode)
  99. {
  100. $this->getSelect()
  101. ->joinLeft(
  102. array('country' => $this->_countryTable),
  103. 'main_table.country_id = country.country_id'
  104. )
  105. ->where('country.iso3_code = ?', $countryCode);
  106. return $this;
  107. }
  108. /**
  109. * Filter by Region code
  110. *
  111. * @param string|array $regionCode
  112. * @return Mage_Directory_Model_Resource_Region_Collection
  113. */
  114. public function addRegionCodeFilter($regionCode)
  115. {
  116. if (!empty($regionCode)) {
  117. if (is_array($regionCode)) {
  118. $this->addFieldToFilter('main_table.code', array('in' => $regionCode));
  119. } else {
  120. $this->addFieldToFilter('main_table.code', $regionCode);
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Filter by region name
  127. *
  128. * @param string|array $regionName
  129. * @return Mage_Directory_Model_Resource_Region_Collection
  130. */
  131. public function addRegionNameFilter($regionName)
  132. {
  133. if (!empty($regionName)) {
  134. if (is_array($regionName)) {
  135. $this->addFieldToFilter('main_table.default_name', array('in' => $regionName));
  136. } else {
  137. $this->addFieldToFilter('main_table.default_name', $regionName);
  138. }
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Convert collection items to select options array
  144. *
  145. * @return array
  146. */
  147. public function toOptionArray()
  148. {
  149. $options = $this->_toOptionArray('region_id', 'default_name', array('title' => 'default_name'));
  150. if (count($options) > 0) {
  151. array_unshift($options, array(
  152. 'title '=> null,
  153. 'value' => '0',
  154. 'label' => Mage::helper('directory')->__('-- Please select --')
  155. ));
  156. }
  157. return $options;
  158. }
  159. }