Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Switcher.php
Go to the documentation of this file.
1 <?php
8 
16 {
20  const HINT_URL = 'http://docs.magento.com/m2/ce/user_guide/configuration/scope.html';
21 
27  protected $_defaultWebsiteVarName = 'website';
28 
34  protected $_defaultStoreGroupVarName = 'group';
35 
41  protected $_defaultStoreVarName = 'store';
42 
46  protected $_storeIds;
47 
53  protected $_hintUrl;
54 
58  protected $_hasDefaultOption = true;
59 
65  protected $_template = 'Magento_Backend::store/switcher.phtml';
66 
72  protected $_websiteFactory;
73 
80 
86  protected $_storeFactory;
87 
95  public function __construct(
96  \Magento\Backend\Block\Template\Context $context,
97  \Magento\Store\Model\WebsiteFactory $websiteFactory,
98  \Magento\Store\Model\GroupFactory $storeGroupFactory,
99  \Magento\Store\Model\StoreFactory $storeFactory,
100  array $data = []
101  ) {
102  parent::__construct($context, $data);
103  $this->_websiteFactory = $websiteFactory;
104  $this->_storeGroupFactory = $storeGroupFactory;
105  $this->_storeFactory = $storeFactory;
106  }
107 
111  protected function _construct()
112  {
113  parent::_construct();
114 
115  $this->setUseConfirm(true);
116  $this->setUseAjax(true);
117 
118  $this->setShowManageStoresLink(0);
119 
120  if (!$this->hasData('switch_websites')) {
121  $this->setSwitchWebsites(false);
122  }
123  if (!$this->hasData('switch_store_groups')) {
124  $this->setSwitchStoreGroups(false);
125  }
126  if (!$this->hasData('switch_store_views')) {
127  $this->setSwitchStoreViews(true);
128  }
129  $this->setDefaultSelectionName(__('All Store Views'));
130  }
131 
135  public function getWebsiteCollection()
136  {
137  $collection = $this->_websiteFactory->create()->getResourceCollection();
138 
139  $websiteIds = $this->getWebsiteIds();
140  if ($websiteIds !== null) {
141  $collection->addIdFilter($this->getWebsiteIds());
142  }
143 
144  return $collection->load();
145  }
146 
152  public function getWebsites()
153  {
154  $websites = $this->_storeManager->getWebsites();
155  if ($websiteIds = $this->getWebsiteIds()) {
156  $websites = array_intersect_key($websites, array_flip($websiteIds));
157  }
158  return $websites;
159  }
160 
166  public function isWebsiteSwitchEnabled()
167  {
168  return (bool)$this->getData('switch_websites');
169  }
170 
175  public function setWebsiteVarName($varName)
176  {
177  $this->setData('website_var_name', $varName);
178  return $this;
179  }
180 
184  public function getWebsiteVarName()
185  {
186  if ($this->hasData('website_var_name')) {
187  return (string)$this->getData('website_var_name');
188  } else {
189  return (string)$this->_defaultWebsiteVarName;
190  }
191  }
192 
197  public function isWebsiteSelected(\Magento\Store\Model\Website $website)
198  {
199  return $this->getWebsiteId() === $website->getId() && $this->getStoreId() === null;
200  }
201 
205  public function getWebsiteId()
206  {
207  if (!$this->hasData('website_id')) {
208  $this->setData('website_id', (int)$this->getRequest()->getParam($this->getWebsiteVarName()));
209  }
210  return $this->getData('website_id');
211  }
212 
217  public function getGroupCollection($website)
218  {
219  if (!$website instanceof \Magento\Store\Model\Website) {
220  $website = $this->_websiteFactory->create()->load($website);
221  }
222  return $website->getGroupCollection();
223  }
224 
231  public function getStoreGroups($website)
232  {
233  if (!$website instanceof \Magento\Store\Model\Website) {
234  $website = $this->_storeManager->getWebsite($website);
235  }
236  return $website->getGroups();
237  }
238 
244  public function isStoreGroupSwitchEnabled()
245  {
246  return (bool)$this->getData('switch_store_groups');
247  }
248 
253  public function setStoreGroupVarName($varName)
254  {
255  $this->setData('store_group_var_name', $varName);
256  return $this;
257  }
258 
262  public function getStoreGroupVarName()
263  {
264  if ($this->hasData('store_group_var_name')) {
265  return (string)$this->getData('store_group_var_name');
266  } else {
267  return (string)$this->_defaultStoreGroupVarName;
268  }
269  }
270 
275  public function isStoreGroupSelected(\Magento\Store\Model\Group $group)
276  {
277  return $this->getStoreGroupId() === $group->getId() && $this->getStoreGroupId() === null;
278  }
279 
283  public function getStoreGroupId()
284  {
285  if (!$this->hasData('store_group_id')) {
286  $this->setData('store_group_id', (int)$this->getRequest()->getParam($this->getStoreGroupVarName()));
287  }
288  return $this->getData('store_group_id');
289  }
290 
295  public function getStoreCollection($group)
296  {
297  if (!$group instanceof \Magento\Store\Model\Group) {
298  $group = $this->_storeGroupFactory->create()->load($group);
299  }
300  $stores = $group->getStoreCollection();
301  $_storeIds = $this->getStoreIds();
302  if (!empty($_storeIds)) {
303  $stores->addIdFilter($_storeIds);
304  }
305  return $stores;
306  }
307 
314  public function getStores($group)
315  {
316  if (!$group instanceof \Magento\Store\Model\Group) {
317  $group = $this->_storeManager->getGroup($group);
318  }
319  $stores = $group->getStores();
320  if ($storeIds = $this->getStoreIds()) {
321  foreach (array_keys($stores) as $storeId) {
322  if (!in_array($storeId, $storeIds)) {
323  unset($stores[$storeId]);
324  }
325  }
326  }
327  return $stores;
328  }
329 
333  public function getStoreId()
334  {
335  if (!$this->hasData('store_id')) {
336  $this->setData('store_id', (int)$this->getRequest()->getParam($this->getStoreVarName()));
337  }
338  return $this->getData('store_id');
339  }
340 
345  public function isStoreSelected(\Magento\Store\Model\Store $store)
346  {
347  return $this->getStoreId() !== null && (int)$this->getStoreId() === (int)$store->getId();
348  }
349 
355  public function isStoreSwitchEnabled()
356  {
357  return (bool)$this->getData('switch_store_views');
358  }
359 
364  public function setStoreVarName($varName)
365  {
366  $this->setData('store_var_name', $varName);
367  return $this;
368  }
369 
373  public function getStoreVarName()
374  {
375  if ($this->hasData('store_var_name')) {
376  return (string)$this->getData('store_var_name');
377  } else {
378  return (string)$this->_defaultStoreVarName;
379  }
380  }
381 
385  public function getSwitchUrl()
386  {
387  if ($url = $this->getData('switch_url')) {
388  return $url;
389  }
390  return $this->getUrl(
391  '*/*/*',
392  [
393  '_current' => true,
394  $this->getStoreVarName() => null,
395  $this->getStoreGroupVarName() => null,
396  $this->getWebsiteVarName() => null,
397  ]
398  );
399  }
400 
404  public function hasScopeSelected()
405  {
406  return $this->getStoreId() !== null || $this->getStoreGroupId() !== null || $this->getWebsiteId() !== null;
407  }
408 
414  public function getCurrentSelectionName()
415  {
416  if (!($name = $this->getCurrentStoreName())) {
417  if (!($name = $this->getCurrentStoreGroupName())) {
418  if (!($name = $this->getCurrentWebsiteName())) {
419  $name = $this->getDefaultSelectionName();
420  }
421  }
422  }
423  return $name;
424  }
425 
431  public function getCurrentWebsiteName()
432  {
433  if ($this->getWebsiteId() !== null) {
434  $website = $this->_websiteFactory->create();
435  $website->load($this->getWebsiteId());
436  if ($website->getId()) {
437  return $website->getName();
438  }
439  }
440  }
441 
447  public function getCurrentStoreGroupName()
448  {
449  if ($this->getStoreGroupId() !== null) {
450  $group = $this->_storeGroupFactory->create();
451  $group->load($this->getStoreGroupId());
452  if ($group->getId()) {
453  return $group->getName();
454  }
455  }
456  }
457 
463  public function getCurrentStoreName()
464  {
465  if ($this->getStoreId() !== null) {
466  $store = $this->_storeFactory->create();
467  $store->load($this->getStoreId());
468  if ($store->getId()) {
469  return $store->getName();
470  }
471  }
472  }
473 
478  public function setStoreIds($storeIds)
479  {
480  $this->_storeIds = $storeIds;
481  return $this;
482  }
483 
487  public function getStoreIds()
488  {
489  return $this->_storeIds;
490  }
491 
495  public function isShow()
496  {
497  return !$this->_storeManager->isSingleStoreMode();
498  }
499 
503  protected function _toHtml()
504  {
505  if ($this->isShow()) {
506  return parent::_toHtml();
507  }
508  return '';
509  }
510 
517  public function hasDefaultOption($hasDefaultOption = null)
518  {
519  if (null !== $hasDefaultOption) {
520  $this->_hasDefaultOption = $hasDefaultOption;
521  }
523  }
524 
530  public function getHintUrl()
531  {
532  return self::HINT_URL;
533  }
534 
540  public function getHintHtml()
541  {
542  $html = '';
543  $url = $this->getHintUrl();
544  if ($url) {
545  $html = '<div class="admin__field-tooltip tooltip">' . '<a' . ' href="' . $this->escapeUrl(
546  $url
547  ) . '"' . ' onclick="this.target=\'_blank\'"' . ' title="' . __(
548  'What is this?'
549  ) . '"' . ' class="admin__field-tooltip-action action-help"><span>' . __(
550  'What is this?'
551  ) . '</span></a></span>' . ' </div>';
552  }
553  return $html;
554  }
555 
561  public function isUsingIframe()
562  {
563  if ($this->hasData('is_using_iframe')) {
564  return (bool)$this->getData('is_using_iframe');
565  }
566  return false;
567  }
568 }
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Store\Model\WebsiteFactory $websiteFactory, \Magento\Store\Model\GroupFactory $storeGroupFactory, \Magento\Store\Model\StoreFactory $storeFactory, array $data=[])
Definition: Switcher.php:95
getData($key='', $index=null)
Definition: DataObject.php:119
isWebsiteSelected(\Magento\Store\Model\Website $website)
Definition: Switcher.php:197
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
isStoreGroupSelected(\Magento\Store\Model\Group $group)
Definition: Switcher.php:275
isStoreSelected(\Magento\Store\Model\Store $store)
Definition: Switcher.php:345
setData($key, $value=null)
Definition: DataObject.php:72
hasDefaultOption($hasDefaultOption=null)
Definition: Switcher.php:517
if(!isset($_GET['name'])) $name
Definition: log.php:14