Collection.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Model\ResourceModel\Region;
  7. use Magento\Directory\Model\AllowedCountries;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Regions collection
  12. *
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  19. {
  20. /**
  21. * Locale region name table name
  22. *
  23. * @var string
  24. */
  25. protected $_regionNameTable;
  26. /**
  27. * Country table name
  28. *
  29. * @var string
  30. */
  31. protected $_countryTable;
  32. /**
  33. * @var \Magento\Framework\Locale\ResolverInterface
  34. */
  35. protected $_localeResolver;
  36. /**
  37. * @var AllowedCountries
  38. */
  39. private $allowedCountriesReader;
  40. /**
  41. * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
  42. * @param \Psr\Log\LoggerInterface $logger
  43. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  44. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  45. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  46. * @param mixed $connection
  47. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  48. */
  49. public function __construct(
  50. \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
  51. \Psr\Log\LoggerInterface $logger,
  52. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  53. \Magento\Framework\Event\ManagerInterface $eventManager,
  54. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  55. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  56. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  57. ) {
  58. $this->_localeResolver = $localeResolver;
  59. $this->_resource = $resource;
  60. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  61. }
  62. /**
  63. * Define main, country, locale region name tables
  64. *
  65. * @return void
  66. */
  67. protected function _construct()
  68. {
  69. $this->_init(\Magento\Directory\Model\Region::class, \Magento\Directory\Model\ResourceModel\Region::class);
  70. $this->_countryTable = $this->getTable('directory_country');
  71. $this->_regionNameTable = $this->getTable('directory_country_region_name');
  72. $this->addOrder('name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
  73. $this->addOrder('default_name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
  74. }
  75. /**
  76. * Initialize select object
  77. *
  78. * @return $this
  79. */
  80. protected function _initSelect()
  81. {
  82. parent::_initSelect();
  83. $locale = $this->_localeResolver->getLocale();
  84. $this->addBindParam(':region_locale', $locale);
  85. $this->getSelect()->joinLeft(
  86. ['rname' => $this->_regionNameTable],
  87. 'main_table.region_id = rname.region_id AND rname.locale = :region_locale',
  88. ['name']
  89. );
  90. return $this;
  91. }
  92. /**
  93. * Return Allowed Countries reader
  94. *
  95. * @return \Magento\Directory\Model\AllowedCountries
  96. * @deprecated 100.1.4
  97. */
  98. private function getAllowedCountriesReader()
  99. {
  100. if (!$this->allowedCountriesReader) {
  101. $this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
  102. }
  103. return $this->allowedCountriesReader;
  104. }
  105. /**
  106. * Set allowed countries filter based on the given store.
  107. * This is a convenience method for collection filtering based on store configuration settings.
  108. *
  109. * @param null|int|string|\Magento\Store\Model\Store $store
  110. * @return \Magento\Directory\Model\ResourceModel\Region\Collection
  111. * @since 100.1.4
  112. */
  113. public function addAllowedCountriesFilter($store = null)
  114. {
  115. $allowedCountries = $this->getAllowedCountriesReader()
  116. ->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store);
  117. if (!empty($allowedCountries)) {
  118. $this->addFieldToFilter('main_table.country_id', ['in' => $allowedCountries]);
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Filter by country_id
  124. *
  125. * @param string|array $countryId
  126. * @return $this
  127. */
  128. public function addCountryFilter($countryId)
  129. {
  130. if (!empty($countryId)) {
  131. if (is_array($countryId)) {
  132. $this->addFieldToFilter('main_table.country_id', ['in' => $countryId]);
  133. } else {
  134. $this->addFieldToFilter('main_table.country_id', $countryId);
  135. }
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Filter by country code (ISO 3)
  141. *
  142. * @param string $countryCode
  143. * @return $this
  144. */
  145. public function addCountryCodeFilter($countryCode)
  146. {
  147. $this->getSelect()->joinLeft(
  148. ['country' => $this->_countryTable],
  149. 'main_table.country_id = country.country_id'
  150. )->where(
  151. 'country.iso3_code = ?',
  152. $countryCode
  153. );
  154. return $this;
  155. }
  156. /**
  157. * Filter by Region code
  158. *
  159. * @param string|array $regionCode
  160. * @return $this
  161. */
  162. public function addRegionCodeFilter($regionCode)
  163. {
  164. if (!empty($regionCode)) {
  165. if (is_array($regionCode)) {
  166. $this->addFieldToFilter('main_table.code', ['in' => $regionCode]);
  167. } else {
  168. $this->addFieldToFilter('main_table.code', $regionCode);
  169. }
  170. }
  171. return $this;
  172. }
  173. /**
  174. * Filter by region name
  175. *
  176. * @param string|array $regionName
  177. * @return $this
  178. */
  179. public function addRegionNameFilter($regionName)
  180. {
  181. if (!empty($regionName)) {
  182. if (is_array($regionName)) {
  183. $this->addFieldToFilter('main_table.default_name', ['in' => $regionName]);
  184. } else {
  185. $this->addFieldToFilter('main_table.default_name', $regionName);
  186. }
  187. }
  188. return $this;
  189. }
  190. /**
  191. * Filter region by its code or name
  192. *
  193. * @param string|array $region
  194. * @return $this
  195. */
  196. public function addRegionCodeOrNameFilter($region)
  197. {
  198. if (!empty($region)) {
  199. $condition = is_array($region) ? ['in' => $region] : $region;
  200. $this->addFieldToFilter(
  201. ['main_table.code', 'main_table.default_name'],
  202. [$condition, $condition]
  203. );
  204. }
  205. return $this;
  206. }
  207. /**
  208. * Convert collection items to select options array
  209. *
  210. * @return array
  211. */
  212. public function toOptionArray()
  213. {
  214. $options = [];
  215. $propertyMap = [
  216. 'value' => 'region_id',
  217. 'title' => 'default_name',
  218. 'country_id' => 'country_id',
  219. ];
  220. foreach ($this as $item) {
  221. $option = [];
  222. foreach ($propertyMap as $code => $field) {
  223. $option[$code] = $item->getData($field);
  224. }
  225. $option['label'] = $item->getName();
  226. $options[] = $option;
  227. }
  228. if (count($options) > 0) {
  229. array_unshift(
  230. $options,
  231. ['title' => '', 'value' => '', 'label' => __('Please select a region, state or province.')]
  232. );
  233. }
  234. return $options;
  235. }
  236. }