Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $_configCacheType;
17 
22 
27 
31  protected $_jsonEncoder;
32 
36  protected $directoryHelper;
37 
41  private $serializer;
42 
52  public function __construct(
53  \Magento\Framework\View\Element\Template\Context $context,
54  \Magento\Directory\Helper\Data $directoryHelper,
55  \Magento\Framework\Json\EncoderInterface $jsonEncoder,
56  \Magento\Framework\App\Cache\Type\Config $configCacheType,
57  \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
58  \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
59  array $data = []
60  ) {
61  parent::__construct($context, $data);
62  $this->directoryHelper = $directoryHelper;
63  $this->_jsonEncoder = $jsonEncoder;
64  $this->_configCacheType = $configCacheType;
65  $this->_regionCollectionFactory = $regionCollectionFactory;
66  $this->_countryCollectionFactory = $countryCollectionFactory;
67  }
68 
72  public function getLoadrRegionUrl()
73  {
74  return $this->getUrl('directory/json/childRegion');
75  }
76 
80  public function getCountryCollection()
81  {
82  $collection = $this->getData('country_collection');
83  if ($collection === null) {
84  $collection = $this->_countryCollectionFactory->create()->loadByStore();
85  $this->setData('country_collection', $collection);
86  }
87 
88  return $collection;
89  }
90 
96  protected function getTopDestinations()
97  {
98  $destinations = (string)$this->_scopeConfig->getValue(
99  'general/country/destinations',
100  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
101  );
102  return !empty($destinations) ? explode(',', $destinations) : [];
103  }
104 
112  public function getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country')
113  {
114  \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
115  if ($defValue === null) {
116  $defValue = $this->getCountryId();
117  }
118  $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . $this->_storeManager->getStore()->getCode();
119  $cache = $this->_configCacheType->load($cacheKey);
120  if ($cache) {
121  $options = $this->getSerializer()->unserialize($cache);
122  } else {
123  $options = $this->getCountryCollection()
124  ->setForegroundCountries($this->getTopDestinations())
125  ->toOptionArray();
126  $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
127  }
128  $html = $this->getLayout()->createBlock(
129  \Magento\Framework\View\Element\Html\Select::class
130  )->setName(
131  $name
132  )->setId(
133  $id
134  )->setTitle(
135  __($title)
136  )->setValue(
137  $defValue
138  )->setOptions(
139  $options
140  )->setExtraParams(
141  'data-validate="{\'validate-select\':true}"'
142  )->getHtml();
143 
144  \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
145  return $html;
146  }
147 
151  public function getRegionCollection()
152  {
153  $collection = $this->getData('region_collection');
154  if ($collection === null) {
155  $collection = $this->_regionCollectionFactory->create()->addCountryFilter($this->getCountryId())->load();
156 
157  $this->setData('region_collection', $collection);
158  }
159  return $collection;
160  }
161 
165  public function getRegionHtmlSelect()
166  {
167  \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
168  $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . $this->_storeManager->getStore()->getId();
169  $cache = $this->_configCacheType->load($cacheKey);
170  if ($cache) {
171  $options = $this->getSerializer()->unserialize($cache);
172  } else {
173  $options = $this->getRegionCollection()->toOptionArray();
174  $this->_configCacheType->save($this->getSerializer()->serialize($options), $cacheKey);
175  }
176  $html = $this->getLayout()->createBlock(
177  \Magento\Framework\View\Element\Html\Select::class
178  )->setName(
179  'region'
180  )->setTitle(
181  __('State/Province')
182  )->setId(
183  'state'
184  )->setClass(
185  'required-entry validate-state'
186  )->setValue(
187  intval($this->getRegionId())
188  )->setOptions(
189  $options
190  )->getHtml();
191  \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
192  return $html;
193  }
194 
198  public function getCountryId()
199  {
200  $countryId = $this->getData('country_id');
201  if ($countryId === null) {
202  $countryId = $this->directoryHelper->getDefaultCountry();
203  }
204  return $countryId;
205  }
206 
210  public function getRegionsJs()
211  {
212  \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
213  $regionsJs = $this->getData('regions_js');
214  if (!$regionsJs) {
215  $countryIds = [];
216  foreach ($this->getCountryCollection() as $country) {
217  $countryIds[] = $country->getCountryId();
218  }
219  $collection = $this->_regionCollectionFactory->create()->addCountryFilter($countryIds)->load();
220  $regions = [];
221  foreach ($collection as $region) {
222  if (!$region->getRegionId()) {
223  continue;
224  }
225  $regions[$region->getCountryId()][$region->getRegionId()] = [
226  'code' => $region->getCode(),
227  'name' => $region->getName(),
228  ];
229  }
230  $regionsJs = $this->_jsonEncoder->encode($regions);
231  }
232  \Magento\Framework\Profiler::stop('TEST: ' . __METHOD__);
233  return $regionsJs;
234  }
235 
242  private function getSerializer()
243  {
244  if ($this->serializer === null) {
246  ->get(\Magento\Framework\Serialize\SerializerInterface::class);
247  }
248  return $this->serializer;
249  }
250 }
$title
Definition: default.phtml:14
getCountryHtmlSelect($defValue=null, $name='country_id', $id='country', $title='Country')
Definition: Data.php:112
getData($key='', $index=null)
Definition: DataObject.php:119
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
serialize($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
Definition: DataObject.php:446
setData($key, $value=null)
Definition: DataObject.php:72
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\App\Cache\Type\Config $configCacheType, \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory, \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory, array $data=[])
Definition: Data.php:52
if(!isset($_GET['name'])) $name
Definition: log.php:14