Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
8 
12 
22 {
28  protected $_regionNameTable;
29 
35  protected $_countryTable;
36 
40  protected $_localeResolver;
41 
45  private $allowedCountriesReader;
46 
56  public function __construct(
57  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
58  \Psr\Log\LoggerInterface $logger,
59  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
60  \Magento\Framework\Event\ManagerInterface $eventManager,
61  \Magento\Framework\Locale\ResolverInterface $localeResolver,
62  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
63  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
64  ) {
65  $this->_localeResolver = $localeResolver;
66  $this->_resource = $resource;
67  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
68  }
69 
75  protected function _construct()
76  {
77  $this->_init(\Magento\Directory\Model\Region::class, \Magento\Directory\Model\ResourceModel\Region::class);
78 
79  $this->_countryTable = $this->getTable('directory_country');
80  $this->_regionNameTable = $this->getTable('directory_country_region_name');
81 
82  $this->addOrder('name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
83  $this->addOrder('default_name', \Magento\Framework\Data\Collection::SORT_ORDER_ASC);
84  }
85 
91  protected function _initSelect()
92  {
93  parent::_initSelect();
94  $locale = $this->_localeResolver->getLocale();
95 
96  $this->addBindParam(':region_locale', $locale);
97  $this->getSelect()->joinLeft(
98  ['rname' => $this->_regionNameTable],
99  'main_table.region_id = rname.region_id AND rname.locale = :region_locale',
100  ['name']
101  );
102 
103  return $this;
104  }
105 
112  private function getAllowedCountriesReader()
113  {
114  if (!$this->allowedCountriesReader) {
115  $this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
116  }
117 
118  return $this->allowedCountriesReader;
119  }
120 
129  public function addAllowedCountriesFilter($store = null)
130  {
131  $allowedCountries = $this->getAllowedCountriesReader()
132  ->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store);
133 
134  if (!empty($allowedCountries)) {
135  $this->addFieldToFilter('main_table.country_id', ['in' => $allowedCountries]);
136  }
137 
138  return $this;
139  }
140 
147  public function addCountryFilter($countryId)
148  {
149  if (!empty($countryId)) {
150  if (is_array($countryId)) {
151  $this->addFieldToFilter('main_table.country_id', ['in' => $countryId]);
152  } else {
153  $this->addFieldToFilter('main_table.country_id', $countryId);
154  }
155  }
156  return $this;
157  }
158 
165  public function addCountryCodeFilter($countryCode)
166  {
167  $this->getSelect()->joinLeft(
168  ['country' => $this->_countryTable],
169  'main_table.country_id = country.country_id'
170  )->where(
171  'country.iso3_code = ?',
172  $countryCode
173  );
174 
175  return $this;
176  }
177 
184  public function addRegionCodeFilter($regionCode)
185  {
186  if (!empty($regionCode)) {
187  if (is_array($regionCode)) {
188  $this->addFieldToFilter('main_table.code', ['in' => $regionCode]);
189  } else {
190  $this->addFieldToFilter('main_table.code', $regionCode);
191  }
192  }
193  return $this;
194  }
195 
202  public function addRegionNameFilter($regionName)
203  {
204  if (!empty($regionName)) {
205  if (is_array($regionName)) {
206  $this->addFieldToFilter('main_table.default_name', ['in' => $regionName]);
207  } else {
208  $this->addFieldToFilter('main_table.default_name', $regionName);
209  }
210  }
211  return $this;
212  }
213 
220  public function addRegionCodeOrNameFilter($region)
221  {
222  if (!empty($region)) {
223  $condition = is_array($region) ? ['in' => $region] : $region;
224  $this->addFieldToFilter(
225  ['main_table.code', 'main_table.default_name'],
226  [$condition, $condition]
227  );
228  }
229  return $this;
230  }
231 
237  public function toOptionArray()
238  {
239  $options = [];
240  $propertyMap = [
241  'value' => 'region_id',
242  'title' => 'default_name',
243  'country_id' => 'country_id',
244  ];
245 
246  foreach ($this as $item) {
247  $option = [];
248  foreach ($propertyMap as $code => $field) {
249  $option[$code] = $item->getData($field);
250  }
251  $option['label'] = $item->getName();
252  $options[] = $option;
253  }
254 
255  if (count($options) > 0) {
256  array_unshift(
257  $options,
258  ['title' => '', 'value' => '', 'label' => __('Please select a region, state or province.')]
259  );
260  }
261  return $options;
262  }
263 }
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:56
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
$logger
addOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:286
$connection
Definition: bulk.php:13
$code
Definition: info.phtml:12