Switcher.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Store;
  7. /**
  8. * Store switcher block
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Switcher extends \Magento\Backend\Block\Template
  14. {
  15. /**
  16. * URL for store switcher hint
  17. */
  18. const HINT_URL = 'http://docs.magento.com/m2/ce/user_guide/configuration/scope.html';
  19. /**
  20. * Name of website variable
  21. *
  22. * @var string
  23. */
  24. protected $_defaultWebsiteVarName = 'website';
  25. /**
  26. * Name of store group variable
  27. *
  28. * @var string
  29. */
  30. protected $_defaultStoreGroupVarName = 'group';
  31. /**
  32. * Name of store variable
  33. *
  34. * @var string
  35. */
  36. protected $_defaultStoreVarName = 'store';
  37. /**
  38. * @var array
  39. */
  40. protected $_storeIds;
  41. /**
  42. * Url for store switcher hint
  43. *
  44. * @var string
  45. */
  46. protected $_hintUrl;
  47. /**
  48. * @var bool
  49. */
  50. protected $_hasDefaultOption = true;
  51. /**
  52. * Block template filename
  53. *
  54. * @var string
  55. */
  56. protected $_template = 'Magento_Backend::store/switcher.phtml';
  57. /**
  58. * Website factory
  59. *
  60. * @var \Magento\Store\Model\WebsiteFactory
  61. */
  62. protected $_websiteFactory;
  63. /**
  64. * Store Group Factory
  65. *
  66. * @var \Magento\Store\Model\GroupFactory
  67. */
  68. protected $_storeGroupFactory;
  69. /**
  70. * Store Factory
  71. *
  72. * @var \Magento\Store\Model\StoreFactory
  73. */
  74. protected $_storeFactory;
  75. /**
  76. * @param \Magento\Backend\Block\Template\Context $context
  77. * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
  78. * @param \Magento\Store\Model\GroupFactory $storeGroupFactory
  79. * @param \Magento\Store\Model\StoreFactory $storeFactory
  80. * @param array $data
  81. */
  82. public function __construct(
  83. \Magento\Backend\Block\Template\Context $context,
  84. \Magento\Store\Model\WebsiteFactory $websiteFactory,
  85. \Magento\Store\Model\GroupFactory $storeGroupFactory,
  86. \Magento\Store\Model\StoreFactory $storeFactory,
  87. array $data = []
  88. ) {
  89. parent::__construct($context, $data);
  90. $this->_websiteFactory = $websiteFactory;
  91. $this->_storeGroupFactory = $storeGroupFactory;
  92. $this->_storeFactory = $storeFactory;
  93. }
  94. /**
  95. * @return void
  96. */
  97. protected function _construct()
  98. {
  99. parent::_construct();
  100. $this->setUseConfirm(true);
  101. $this->setUseAjax(true);
  102. $this->setShowManageStoresLink(0);
  103. if (!$this->hasData('switch_websites')) {
  104. $this->setSwitchWebsites(false);
  105. }
  106. if (!$this->hasData('switch_store_groups')) {
  107. $this->setSwitchStoreGroups(false);
  108. }
  109. if (!$this->hasData('switch_store_views')) {
  110. $this->setSwitchStoreViews(true);
  111. }
  112. $this->setDefaultSelectionName(__('All Store Views'));
  113. }
  114. /**
  115. * @return \Magento\Store\Model\ResourceModel\Website\Collection
  116. */
  117. public function getWebsiteCollection()
  118. {
  119. $collection = $this->_websiteFactory->create()->getResourceCollection();
  120. $websiteIds = $this->getWebsiteIds();
  121. if ($websiteIds !== null) {
  122. $collection->addIdFilter($this->getWebsiteIds());
  123. }
  124. return $collection->load();
  125. }
  126. /**
  127. * Get websites
  128. *
  129. * @return \Magento\Store\Model\Website[]
  130. */
  131. public function getWebsites()
  132. {
  133. $websites = $this->_storeManager->getWebsites();
  134. if ($websiteIds = $this->getWebsiteIds()) {
  135. $websites = array_intersect_key($websites, array_flip($websiteIds));
  136. }
  137. return $websites;
  138. }
  139. /**
  140. * Check if can switch to websites
  141. *
  142. * @return bool
  143. */
  144. public function isWebsiteSwitchEnabled()
  145. {
  146. return (bool)$this->getData('switch_websites');
  147. }
  148. /**
  149. * @param string $varName
  150. * @return $this
  151. */
  152. public function setWebsiteVarName($varName)
  153. {
  154. $this->setData('website_var_name', $varName);
  155. return $this;
  156. }
  157. /**
  158. * @return string
  159. */
  160. public function getWebsiteVarName()
  161. {
  162. if ($this->hasData('website_var_name')) {
  163. return (string)$this->getData('website_var_name');
  164. } else {
  165. return (string)$this->_defaultWebsiteVarName;
  166. }
  167. }
  168. /**
  169. * @param \Magento\Store\Model\Website $website
  170. * @return bool
  171. */
  172. public function isWebsiteSelected(\Magento\Store\Model\Website $website)
  173. {
  174. return $this->getWebsiteId() === $website->getId() && $this->getStoreId() === null;
  175. }
  176. /**
  177. * @return int|null
  178. */
  179. public function getWebsiteId()
  180. {
  181. if (!$this->hasData('website_id')) {
  182. $this->setData('website_id', (int)$this->getRequest()->getParam($this->getWebsiteVarName()));
  183. }
  184. return $this->getData('website_id');
  185. }
  186. /**
  187. * @param int|\Magento\Store\Model\Website $website
  188. * @return \Magento\Store\Model\ResourceModel\Group\Collection
  189. */
  190. public function getGroupCollection($website)
  191. {
  192. if (!$website instanceof \Magento\Store\Model\Website) {
  193. $website = $this->_websiteFactory->create()->load($website);
  194. }
  195. return $website->getGroupCollection();
  196. }
  197. /**
  198. * Get store groups for specified website
  199. *
  200. * @param \Magento\Store\Model\Website|int $website
  201. * @return array
  202. */
  203. public function getStoreGroups($website)
  204. {
  205. if (!$website instanceof \Magento\Store\Model\Website) {
  206. $website = $this->_storeManager->getWebsite($website);
  207. }
  208. return $website->getGroups();
  209. }
  210. /**
  211. * Check if can switch to store group
  212. *
  213. * @return bool
  214. */
  215. public function isStoreGroupSwitchEnabled()
  216. {
  217. return (bool)$this->getData('switch_store_groups');
  218. }
  219. /**
  220. * @param string $varName
  221. * @return $this
  222. */
  223. public function setStoreGroupVarName($varName)
  224. {
  225. $this->setData('store_group_var_name', $varName);
  226. return $this;
  227. }
  228. /**
  229. * @return string
  230. */
  231. public function getStoreGroupVarName()
  232. {
  233. if ($this->hasData('store_group_var_name')) {
  234. return (string)$this->getData('store_group_var_name');
  235. } else {
  236. return (string)$this->_defaultStoreGroupVarName;
  237. }
  238. }
  239. /**
  240. * @param \Magento\Store\Model\Group $group
  241. * @return bool
  242. */
  243. public function isStoreGroupSelected(\Magento\Store\Model\Group $group)
  244. {
  245. return $this->getStoreGroupId() === $group->getId() && $this->getStoreGroupId() === null;
  246. }
  247. /**
  248. * @return int|null
  249. */
  250. public function getStoreGroupId()
  251. {
  252. if (!$this->hasData('store_group_id')) {
  253. $this->setData('store_group_id', (int)$this->getRequest()->getParam($this->getStoreGroupVarName()));
  254. }
  255. return $this->getData('store_group_id');
  256. }
  257. /**
  258. * @param \Magento\Store\Model\Group|int $group
  259. * @return \Magento\Store\Model\ResourceModel\Store\Collection
  260. */
  261. public function getStoreCollection($group)
  262. {
  263. if (!$group instanceof \Magento\Store\Model\Group) {
  264. $group = $this->_storeGroupFactory->create()->load($group);
  265. }
  266. $stores = $group->getStoreCollection();
  267. $_storeIds = $this->getStoreIds();
  268. if (!empty($_storeIds)) {
  269. $stores->addIdFilter($_storeIds);
  270. }
  271. return $stores;
  272. }
  273. /**
  274. * Get store views for specified store group
  275. *
  276. * @param \Magento\Store\Model\Group|int $group
  277. * @return \Magento\Store\Model\Store[]
  278. */
  279. public function getStores($group)
  280. {
  281. if (!$group instanceof \Magento\Store\Model\Group) {
  282. $group = $this->_storeManager->getGroup($group);
  283. }
  284. $stores = $group->getStores();
  285. if ($storeIds = $this->getStoreIds()) {
  286. foreach (array_keys($stores) as $storeId) {
  287. if (!in_array($storeId, $storeIds)) {
  288. unset($stores[$storeId]);
  289. }
  290. }
  291. }
  292. return $stores;
  293. }
  294. /**
  295. * @return int|null
  296. */
  297. public function getStoreId()
  298. {
  299. if (!$this->hasData('store_id')) {
  300. $this->setData('store_id', (int)$this->getRequest()->getParam($this->getStoreVarName()));
  301. }
  302. return $this->getData('store_id');
  303. }
  304. /**
  305. * @param \Magento\Store\Model\Store $store
  306. * @return bool
  307. */
  308. public function isStoreSelected(\Magento\Store\Model\Store $store)
  309. {
  310. return $this->getStoreId() !== null && (int)$this->getStoreId() === (int)$store->getId();
  311. }
  312. /**
  313. * Check if can switch to store views
  314. *
  315. * @return bool
  316. */
  317. public function isStoreSwitchEnabled()
  318. {
  319. return (bool)$this->getData('switch_store_views');
  320. }
  321. /**
  322. * @param string $varName
  323. * @return $this
  324. */
  325. public function setStoreVarName($varName)
  326. {
  327. $this->setData('store_var_name', $varName);
  328. return $this;
  329. }
  330. /**
  331. * @return mixed|string
  332. */
  333. public function getStoreVarName()
  334. {
  335. if ($this->hasData('store_var_name')) {
  336. return (string)$this->getData('store_var_name');
  337. } else {
  338. return (string)$this->_defaultStoreVarName;
  339. }
  340. }
  341. /**
  342. * @return string
  343. */
  344. public function getSwitchUrl()
  345. {
  346. if ($url = $this->getData('switch_url')) {
  347. return $url;
  348. }
  349. return $this->getUrl(
  350. '*/*/*',
  351. [
  352. '_current' => true,
  353. $this->getStoreVarName() => null,
  354. $this->getStoreGroupVarName() => null,
  355. $this->getWebsiteVarName() => null,
  356. ]
  357. );
  358. }
  359. /**
  360. * @return bool
  361. */
  362. public function hasScopeSelected()
  363. {
  364. return $this->getStoreId() !== null || $this->getStoreGroupId() !== null || $this->getWebsiteId() !== null;
  365. }
  366. /**
  367. * Get current selection name
  368. *
  369. * @return string
  370. */
  371. public function getCurrentSelectionName()
  372. {
  373. if (!($name = $this->getCurrentStoreName())) {
  374. if (!($name = $this->getCurrentStoreGroupName())) {
  375. if (!($name = $this->getCurrentWebsiteName())) {
  376. $name = $this->getDefaultSelectionName();
  377. }
  378. }
  379. }
  380. return $name;
  381. }
  382. /**
  383. * Get current website name
  384. *
  385. * @return string
  386. */
  387. public function getCurrentWebsiteName()
  388. {
  389. if ($this->getWebsiteId() !== null) {
  390. $website = $this->_websiteFactory->create();
  391. $website->load($this->getWebsiteId());
  392. if ($website->getId()) {
  393. return $website->getName();
  394. }
  395. }
  396. }
  397. /**
  398. * Get current store group name
  399. *
  400. * @return string
  401. */
  402. public function getCurrentStoreGroupName()
  403. {
  404. if ($this->getStoreGroupId() !== null) {
  405. $group = $this->_storeGroupFactory->create();
  406. $group->load($this->getStoreGroupId());
  407. if ($group->getId()) {
  408. return $group->getName();
  409. }
  410. }
  411. }
  412. /**
  413. * Get current store view name
  414. *
  415. * @return string
  416. */
  417. public function getCurrentStoreName()
  418. {
  419. if ($this->getStoreId() !== null) {
  420. $store = $this->_storeFactory->create();
  421. $store->load($this->getStoreId());
  422. if ($store->getId()) {
  423. return $store->getName();
  424. }
  425. }
  426. }
  427. /**
  428. * @param array $storeIds
  429. * @return $this
  430. */
  431. public function setStoreIds($storeIds)
  432. {
  433. $this->_storeIds = $storeIds;
  434. return $this;
  435. }
  436. /**
  437. * @return array
  438. */
  439. public function getStoreIds()
  440. {
  441. return $this->_storeIds;
  442. }
  443. /**
  444. * @return bool
  445. */
  446. public function isShow()
  447. {
  448. return !$this->_storeManager->isSingleStoreMode();
  449. }
  450. /**
  451. * @return string
  452. */
  453. protected function _toHtml()
  454. {
  455. if ($this->isShow()) {
  456. return parent::_toHtml();
  457. }
  458. return '';
  459. }
  460. /**
  461. * Set/Get whether the switcher should show default option
  462. *
  463. * @param bool $hasDefaultOption
  464. * @return bool
  465. */
  466. public function hasDefaultOption($hasDefaultOption = null)
  467. {
  468. if (null !== $hasDefaultOption) {
  469. $this->_hasDefaultOption = $hasDefaultOption;
  470. }
  471. return $this->_hasDefaultOption;
  472. }
  473. /**
  474. * Return url for store switcher hint
  475. *
  476. * @return string
  477. */
  478. public function getHintUrl()
  479. {
  480. return self::HINT_URL;
  481. }
  482. /**
  483. * Return store switcher hint html
  484. *
  485. * @return string
  486. */
  487. public function getHintHtml()
  488. {
  489. $html = '';
  490. $url = $this->getHintUrl();
  491. if ($url) {
  492. $html = '<div class="admin__field-tooltip tooltip">' . '<a' . ' href="' . $this->escapeUrl(
  493. $url
  494. ) . '"' . ' onclick="this.target=\'_blank\'"' . ' title="' . __(
  495. 'What is this?'
  496. ) . '"' . ' class="admin__field-tooltip-action action-help"><span>' . __(
  497. 'What is this?'
  498. ) . '</span></a></span>' . ' </div>';
  499. }
  500. return $html;
  501. }
  502. /**
  503. * Get whether iframe is being used
  504. *
  505. * @return bool
  506. */
  507. public function isUsingIframe()
  508. {
  509. if ($this->hasData('is_using_iframe')) {
  510. return (bool)$this->getData('is_using_iframe');
  511. }
  512. return false;
  513. }
  514. }