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 $_localeLists;
29 
35  protected $_scopeConfig;
36 
40  protected $_countryFactory;
41 
47  protected $_arrayUtils;
48 
52  protected $_localeResolver;
53 
57  protected $helperData;
58 
62  private $allowedCountriesReader;
63 
72  private $storeManager;
73 
93  public function __construct(
94  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
95  \Psr\Log\LoggerInterface $logger,
96  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
97  \Magento\Framework\Event\ManagerInterface $eventManager,
98  \Magento\Framework\Locale\ListsInterface $localeLists,
99  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
100  \Magento\Directory\Model\ResourceModel\CountryFactory $countryFactory,
101  \Magento\Framework\Stdlib\ArrayUtils $arrayUtils,
102  \Magento\Framework\Locale\ResolverInterface $localeResolver,
103  \Magento\Framework\App\Helper\AbstractHelper $helperData,
105  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
106  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
107  \Magento\Store\Model\StoreManagerInterface $storeManager = null
108  ) {
109  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
110  $this->_scopeConfig = $scopeConfig;
111  $this->_localeLists = $localeLists;
112  $this->_localeResolver = $localeResolver;
113  $this->_countryFactory = $countryFactory;
114  $this->_arrayUtils = $arrayUtils;
115  $this->helperData = $helperData;
116  $this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
117  $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
118  \Magento\Store\Model\StoreManagerInterface::class
119  );
120  }
121 
127  protected $_foregroundCountries = [];
128 
136  private function addForegroundCountriesToOptionArray($emptyLabel, $options)
137  {
138  if ($emptyLabel !== false && count($this->_foregroundCountries) !== 0 &&
139  count($options) === count($this->_foregroundCountries)
140  ) {
141  $options[] = ['value' => '', 'label' => $emptyLabel];
142  return $options;
143  }
144  return $options;
145  }
146 
152  protected function _construct()
153  {
154  $this->_init(\Magento\Directory\Model\Country::class, \Magento\Directory\Model\ResourceModel\Country::class);
155  }
156 
163  private function getAllowedCountriesReader()
164  {
165  if (!$this->allowedCountriesReader) {
166  $this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
167  }
168 
169  return $this->allowedCountriesReader;
170  }
171 
178  public function loadByStore($store = null)
179  {
180  $allowedCountries = $this->getAllowedCountriesReader()
181  ->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store);
182 
183  if (!empty($allowedCountries)) {
184  $this->addFieldToFilter("country_id", ['in' => $allowedCountries]);
185  }
186 
187  return $this;
188  }
189 
196  public function getItemById($countryId)
197  {
198  foreach ($this->_items as $country) {
199  if ($country->getCountryId() == $countryId) {
200  return $country;
201  }
202  }
203  return null;
204  }
205 
216  public function addCountryCodeFilter($countryCode, $iso = ['iso3', 'iso2'])
217  {
218  if (!empty($countryCode)) {
219  if (is_array($countryCode)) {
220  if (is_array($iso)) {
221  $whereOr = [];
222  foreach ($iso as $iso_curr) {
223  $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", ['in' => $countryCode]);
224  }
225  $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
226  } else {
227  $this->addFieldToFilter("{$iso}_code", ['in' => $countryCode]);
228  }
229  } else {
230  if (is_array($iso)) {
231  $whereOr = [];
232  foreach ($iso as $iso_curr) {
233  $whereOr[] .= $this->_getConditionSql("{$iso_curr}_code", $countryCode);
234  }
235  $this->_select->where('(' . implode(') OR (', $whereOr) . ')');
236  } else {
237  $this->addFieldToFilter("{$iso}_code", $countryCode);
238  }
239  }
240  }
241  return $this;
242  }
243 
250  public function addCountryIdFilter($countryId)
251  {
252  if (!empty($countryId)) {
253  if (is_array($countryId)) {
254  $this->addFieldToFilter("country_id", ['in' => $countryId]);
255  } else {
256  $this->addFieldToFilter("country_id", $countryId);
257  }
258  }
259  return $this;
260  }
261 
268  public function toOptionArray($emptyLabel = ' ')
269  {
270  $options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']);
271  $sort = [];
272  foreach ($options as $data) {
273  $name = (string)$this->_localeLists->getCountryTranslation($data['value']);
274  if (!empty($name)) {
275  $sort[$name] = $data['value'];
276  }
277  }
278  $this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale());
279  foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) {
280  $name = array_search($foregroundCountry, $sort);
281  unset($sort[$name]);
282  $sort = [$name => $foregroundCountry] + $sort;
283  }
284  $isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
285 
286  $options = [];
287  foreach ($sort as $label => $value) {
288  $options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
289  $option = ['value' => $value, 'label' => $label];
290  if ($this->helperData->isRegionRequired($value)) {
291  $option['is_region_required'] = true;
292  } else {
293  $option['is_region_visible'] = $isRegionVisible;
294  }
295  if ($this->helperData->isZipCodeOptional($value)) {
296  $option['is_zipcode_optional'] = true;
297  }
298  $options[] = $option;
299  }
300  if ($emptyLabel !== false && count($options) > 0) {
301  array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
302  }
303 
304  $this->addDefaultCountryToOptions($options);
305 
306  return $options;
307  }
308 
315  private function addDefaultCountryToOptions(array &$options)
316  {
317  $defaultCountry = [];
318  foreach ($this->storeManager->getWebsites() as $website) {
319  $defaultCountryConfig = $this->_scopeConfig->getValue(
320  \Magento\Directory\Helper\Data::XML_PATH_DEFAULT_COUNTRY,
321  ScopeInterface::SCOPE_WEBSITES,
322  $website
323  );
324  $defaultCountry[$defaultCountryConfig][] = $website->getId();
325  }
326 
327  foreach ($options as $key => $option) {
328  if (isset($defaultCountry[$option['value']])) {
329  $options[$key]['is_default'] = $defaultCountry[$option['value']];
330  }
331  }
332  }
333 
340  public function setForegroundCountries($foregroundCountries)
341  {
342  if (empty($foregroundCountries)) {
343  return $this;
344  }
345  $this->_foregroundCountries = (array)$foregroundCountries;
346  return $this;
347  }
348 
355  public function getCountriesWithRequiredStates()
356  {
357  $countries = [];
358  foreach ($this->getItems() as $country) {
360  if ($country->getRegionCollection()->getSize() > 0
361  && !in_array($country->getId(), $this->countriesWithNotRequiredStates)
362  ) {
363  $countries[$country->getId()] = $country;
364  }
365  }
366  return $countries;
367  }
368 }
addCountryCodeFilter($countryCode, $iso=['iso3', 'iso2'])
Definition: Collection.php:216
__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\ListsInterface $localeLists, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Directory\Model\ResourceModel\CountryFactory $countryFactory, \Magento\Framework\Stdlib\ArrayUtils $arrayUtils, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\App\Helper\AbstractHelper $helperData, array $countriesWithNotRequiredStates=[], \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null, \Magento\Store\Model\StoreManagerInterface $storeManager=null)
Definition: Collection.php:93
_toOptionArray($valueField=null, $labelField='name', $additional=[])
Definition: AbstractDb.php:641
$resource
Definition: bulk.php:12
$logger
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$connection
Definition: bulk.php:13
if(!isset($_GET['name'])) $name
Definition: log.php:14