switcher.phtml 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. /* @var $block \Magento\Backend\Block\Store\Switcher */
  8. ?>
  9. <?php if ($websites = $block->getWebsites()): ?>
  10. <div class="store-switcher store-view">
  11. <span class="store-switcher-label"><?= /* @escapeNotVerified */ __('Store View:') ?></span>
  12. <div class="actions dropdown closable">
  13. <input type="hidden" name="store_switcher" id="store_switcher"
  14. data-role="store-view-id" data-param="<?= /* @escapeNotVerified */ $block->getStoreVarName() ?>"
  15. value="<?= $block->escapeHtml($block->getStoreId()) ?>"
  16. onchange="switchScope(this);"<?= /* @escapeNotVerified */ $block->getUiId() ?> />
  17. <input type="hidden" name="store_group_switcher" id="store_group_switcher"
  18. data-role="store-group-id" data-param="<?= /* @escapeNotVerified */ $block->getStoreGroupVarName() ?>"
  19. value="<?= $block->escapeHtml($block->getStoreGroupId()) ?>"
  20. onchange="switchScope(this);"<?= /* @escapeNotVerified */ $block->getUiId() ?> />
  21. <input type="hidden" name="website_switcher" id="website_switcher"
  22. data-role="website-id" data-param="<?= /* @escapeNotVerified */ $block->getWebsiteVarName() ?>"
  23. value="<?= $block->escapeHtml($block->getWebsiteId()) ?>"
  24. onchange="switchScope(this);"<?= /* @escapeNotVerified */ $block->getUiId() ?> />
  25. <button
  26. type="button"
  27. class="admin__action-dropdown"
  28. data-mage-init='{"dropdown":{}}'
  29. data-toggle="dropdown"
  30. aria-haspopup="true"
  31. id="store-change-button">
  32. <?= /* @escapeNotVerified */ $block->getCurrentSelectionName() ?>
  33. </button>
  34. <ul class="dropdown-menu" data-role="stores-list">
  35. <?php if ($block->hasDefaultOption()): ?>
  36. <li class="store-switcher-all <?php if ( ! ($block->getDefaultSelectionName() != $block->getCurrentSelectionName())) {
  37. echo "disabled";
  38. } ?> <?php if ( ! $block->hasScopeSelected()) {
  39. ?> current<?php
  40. } ?>">
  41. <?php if ($block->getDefaultSelectionName() != $block->getCurrentSelectionName()) {
  42. ?>
  43. <a data-role="store-view-id" data-value="" href="#">
  44. <?= /* @escapeNotVerified */ $block->getDefaultSelectionName() ?>
  45. </a>
  46. <?php
  47. } else {
  48. ?>
  49. <span><?= /* @escapeNotVerified */ $block->getDefaultSelectionName() ?></span>
  50. <?php
  51. } ?>
  52. </li>
  53. <?php endif; ?>
  54. <?php foreach ($websites as $website): ?>
  55. <?php $showWebsite = false; ?>
  56. <?php foreach ($website->getGroups() as $group): ?>
  57. <?php $showGroup = false; ?>
  58. <?php foreach ($block->getStores($group) as $store): ?>
  59. <?php if ($showWebsite == false): ?>
  60. <?php $showWebsite = true; ?>
  61. <li class="store-switcher-website <?php if ( ! ($block->isWebsiteSwitchEnabled() && ! $block->isWebsiteSelected($website))) {
  62. echo "disabled";
  63. } ?> <?php if ($block->isWebsiteSelected($website)) {
  64. ?> current<?php
  65. } ?>">
  66. <?php if ($block->isWebsiteSwitchEnabled() && ! $block->isWebsiteSelected($website)) {
  67. ?>
  68. <a data-role="website-id" data-value="<?= $block->escapeHtml($website->getId()) ?>" href="#">
  69. <?= $block->escapeHtml($website->getName()) ?>
  70. </a>
  71. <?php
  72. } else {
  73. ?>
  74. <span><?= $block->escapeHtml($website->getName()) ?></span>
  75. <?php
  76. } ?>
  77. </li>
  78. <?php endif; ?>
  79. <?php if ($showGroup == false): ?>
  80. <?php $showGroup = true; ?>
  81. <li class="store-switcher-store <?php if ( ! ($block->isStoreGroupSwitchEnabled() && ! $block->isStoreGroupSelected($group))) {
  82. echo "disabled";
  83. } ?> <?php if ($block->isStoreGroupSelected($group)) {
  84. ?> current<?php
  85. } ?>">
  86. <?php if ($block->isStoreGroupSwitchEnabled() && ! $block->isStoreGroupSelected($group)) {
  87. ?>
  88. <a data-role="store-group-id" data-value="<?= $block->escapeHtml($group->getId()) ?>" href="#">
  89. <?= $block->escapeHtml($group->getName()) ?>
  90. </a>
  91. <?php
  92. } else {
  93. ?>
  94. <span><?= $block->escapeHtml($group->getName()) ?></span>
  95. <?php
  96. } ?>
  97. </li>
  98. <?php endif; ?>
  99. <li class="store-switcher-store-view <?php if ( ! ($block->isStoreSwitchEnabled() && ! $block->isStoreSelected($store))) {
  100. echo "disabled";
  101. } ?> <?php if ($block->isStoreSelected($store)) {
  102. ?> current<?php
  103. } ?>">
  104. <?php if ($block->isStoreSwitchEnabled() && ! $block->isStoreSelected($store)) {
  105. ?>
  106. <a data-role="store-view-id" data-value="<?= $block->escapeHtml($store->getId()) ?>" href="#">
  107. <?= $block->escapeHtml($store->getName()) ?>
  108. </a>
  109. <?php
  110. } else {
  111. ?>
  112. <span><?= $block->escapeHtml($store->getName()) ?></span>
  113. <?php
  114. } ?>
  115. </li>
  116. <?php endforeach; ?>
  117. <?php endforeach; ?>
  118. <?php endforeach; ?>
  119. <?php if ($block->getShowManageStoresLink() && $block->getAuthorization()->isAllowed('Magento_Backend::store')): ?>
  120. <li class="dropdown-toolbar">
  121. <a href="<?= /* @escapeNotVerified */ $block->getUrl('*/system_store') ?>"><?= /* @escapeNotVerified */ __('Stores Configuration') ?></a>
  122. </li>
  123. <?php endif; ?>
  124. </ul>
  125. </div>
  126. <?= $block->getHintHtml() ?>
  127. </div>
  128. <script>
  129. require([
  130. 'jquery',
  131. 'Magento_Ui/js/modal/confirm'
  132. ], function(jQuery, confirm){
  133. (function($) {
  134. var $storesList = $('[data-role=stores-list]');
  135. $storesList.on('click', '[data-value]', function(event) {
  136. var val = $(event.target).data('value');
  137. var role = $(event.target).data('role');
  138. var switcher = $('[data-role='+role+']');
  139. event.preventDefault();
  140. if (!switcher.val() || val != switcher.val()) {
  141. switcher.val(val).trigger('change'); // Set the value & trigger event
  142. }
  143. });
  144. })(jQuery);
  145. var scopeSwitcherHandler;
  146. function switchScope(obj) {
  147. var switcher = jQuery(obj);
  148. var scopeId = switcher.val();
  149. var scopeParams = '';
  150. if (scopeId) {
  151. scopeParams = switcher.data('param') + '/' + scopeId + '/';
  152. }
  153. if (obj.switchParams) {
  154. scopeParams += obj.switchParams;
  155. }
  156. if ((typeof scopeSwitcherHandler) != 'undefined') {
  157. var switcherParams = {
  158. scopeId: scopeId,
  159. scopeParams: scopeParams,
  160. useConfirm: <?= (int)$block->getUseConfirm() ?>
  161. };
  162. scopeSwitcherHandler(switcherParams);
  163. } else {
  164. <?php if ($block->getUseConfirm()): ?>
  165. confirm({
  166. content: "<?= /* @escapeNotVerified */ __('Please confirm scope switching. All data that hasn\'t been saved will be lost.') ?>",
  167. actions: {
  168. confirm: function() {
  169. reload();
  170. },
  171. cancel: function() {
  172. obj.value = '<?= $block->escapeHtml($block->getStoreId()) ?>';
  173. }
  174. }
  175. });
  176. <?php else: ?>
  177. reload();
  178. <?php endif; ?>
  179. }
  180. function reload() {
  181. <?php if (!$block->isUsingIframe()): ?>
  182. var url = '<?= /* @escapeNotVerified */ $block->getSwitchUrl() ?>' + scopeParams;
  183. setLocation(url);
  184. <?php else: ?>
  185. jQuery('#preview_selected_store').val(scopeId);
  186. jQuery('#preview_form').submit();
  187. jQuery('.store-switcher .dropdown-menu li a').each(function() {
  188. var $this = jQuery(this);
  189. if ($this.data('role') === 'store-view-id' && $this.data('value') == scopeId) {
  190. jQuery('#store-change-button').html($this.text());
  191. }
  192. });
  193. jQuery('#store-change-button').click();
  194. <?php endif; ?>
  195. }
  196. }
  197. window.scopeSwitcherHandler = scopeSwitcherHandler;
  198. window.switchScope = switchScope;
  199. });
  200. </script>
  201. <?php endif; ?>