Store.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\System;
  7. use Magento\Framework\Data\OptionSourceInterface;
  8. /**
  9. * Core System Store Model
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Store extends \Magento\Framework\DataObject implements OptionSourceInterface
  15. {
  16. /**
  17. * Website collection
  18. * websiteId => \Magento\Store\Model\Website
  19. *
  20. * @var array
  21. */
  22. protected $_websiteCollection = [];
  23. /**
  24. * Group collection
  25. * groupId => \Magento\Store\Model\Group
  26. *
  27. * @var array
  28. */
  29. protected $_groupCollection = [];
  30. /**
  31. * Store collection
  32. * storeId => \Magento\Store\Model\Store
  33. *
  34. * @var array
  35. */
  36. protected $_storeCollection;
  37. /**
  38. * @var bool
  39. */
  40. private $_isAdminScopeAllowed = true;
  41. /**
  42. * @var \Magento\Store\Model\StoreManagerInterface
  43. */
  44. protected $_storeManager;
  45. /**
  46. * Init model
  47. * Load Website, Group and Store collections
  48. *
  49. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  50. */
  51. public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
  52. {
  53. $this->_storeManager = $storeManager;
  54. return $this->reload();
  55. }
  56. /**
  57. * Load/Reload Website collection
  58. *
  59. * @return $this
  60. */
  61. protected function _loadWebsiteCollection()
  62. {
  63. $this->_websiteCollection = $this->_storeManager->getWebsites();
  64. return $this;
  65. }
  66. /**
  67. * Load/Reload Group collection
  68. *
  69. * @return $this
  70. */
  71. protected function _loadGroupCollection()
  72. {
  73. $this->_groupCollection = [];
  74. foreach ($this->_storeManager->getWebsites() as $website) {
  75. foreach ($website->getGroups() as $group) {
  76. $this->_groupCollection[$group->getId()] = $group;
  77. }
  78. }
  79. return $this;
  80. }
  81. /**
  82. * Load/Reload Store collection
  83. *
  84. * @return $this
  85. */
  86. protected function _loadStoreCollection()
  87. {
  88. $this->_storeCollection = $this->_storeManager->getStores();
  89. return $this;
  90. }
  91. /**
  92. * Retrieve store values for form
  93. *
  94. * @param bool $empty
  95. * @param bool $all
  96. * @return array
  97. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  98. * @SuppressWarnings(PHPMD.NPathComplexity)
  99. */
  100. public function getStoreValuesForForm($empty = false, $all = false)
  101. {
  102. $options = [];
  103. if ($empty) {
  104. $options[] = ['label' => '', 'value' => ''];
  105. }
  106. if ($all && $this->_isAdminScopeAllowed) {
  107. $options[] = ['label' => __('All Store Views'), 'value' => 0];
  108. }
  109. $nonEscapableNbspChar = html_entity_decode('&#160;', ENT_NOQUOTES, 'UTF-8');
  110. foreach ($this->_websiteCollection as $website) {
  111. $websiteShow = false;
  112. foreach ($this->_groupCollection as $group) {
  113. if ($website->getId() != $group->getWebsiteId()) {
  114. continue;
  115. }
  116. $groupShow = false;
  117. foreach ($this->_storeCollection as $store) {
  118. if ($group->getId() != $store->getGroupId()) {
  119. continue;
  120. }
  121. if (!$websiteShow) {
  122. $options[] = ['label' => $website->getName(), 'value' => []];
  123. $websiteShow = true;
  124. }
  125. if (!$groupShow) {
  126. $groupShow = true;
  127. $values = [];
  128. }
  129. $values[] = [
  130. 'label' => str_repeat($nonEscapableNbspChar, 4) . $store->getName(),
  131. 'value' => $store->getId(),
  132. ];
  133. }
  134. if ($groupShow) {
  135. $options[] = [
  136. 'label' => str_repeat($nonEscapableNbspChar, 4) . $group->getName(),
  137. 'value' => $values,
  138. ];
  139. }
  140. }
  141. }
  142. return $options;
  143. }
  144. /**
  145. * Retrieve stores structure
  146. *
  147. * @param bool $isAll
  148. * @param array $storeIds
  149. * @param array $groupIds
  150. * @param array $websiteIds
  151. * @return array
  152. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  153. * @SuppressWarnings(PHPMD.NPathComplexity)
  154. */
  155. public function getStoresStructure($isAll = false, $storeIds = [], $groupIds = [], $websiteIds = [])
  156. {
  157. $out = [];
  158. $websites = $this->getWebsiteCollection();
  159. if ($isAll) {
  160. $out[] = ['value' => 0, 'label' => __('All Store Views')];
  161. }
  162. foreach ($websites as $website) {
  163. $websiteId = $website->getId();
  164. if ($websiteIds && !in_array($websiteId, $websiteIds)) {
  165. continue;
  166. }
  167. $out[$websiteId] = ['value' => $websiteId, 'label' => $website->getName()];
  168. foreach ($website->getGroups() as $group) {
  169. $groupId = $group->getId();
  170. if ($groupIds && !in_array($groupId, $groupIds)) {
  171. continue;
  172. }
  173. $out[$websiteId]['children'][$groupId] = ['value' => $groupId, 'label' => $group->getName()];
  174. foreach ($group->getStores() as $store) {
  175. $storeId = $store->getId();
  176. if ($storeIds && !in_array($storeId, $storeIds)) {
  177. continue;
  178. }
  179. $out[$websiteId]['children'][$groupId]['children'][$storeId] = [
  180. 'value' => $storeId,
  181. 'label' => $store->getName(),
  182. ];
  183. }
  184. if (empty($out[$websiteId]['children'][$groupId]['children'])) {
  185. unset($out[$websiteId]['children'][$groupId]);
  186. }
  187. }
  188. if (empty($out[$websiteId]['children'])) {
  189. unset($out[$websiteId]);
  190. }
  191. }
  192. return $out;
  193. }
  194. /**
  195. * Website label/value array getter, compatible with form dropdown options
  196. *
  197. * @param bool $empty
  198. * @param bool $all
  199. * @return array
  200. */
  201. public function getWebsiteValuesForForm($empty = false, $all = false)
  202. {
  203. $options = [];
  204. if ($empty) {
  205. $options[] = ['label' => __('-- Please Select --'), 'value' => ''];
  206. }
  207. if ($all && $this->_isAdminScopeAllowed) {
  208. $options[] = ['label' => __('Admin'), 'value' => 0];
  209. }
  210. foreach ($this->_websiteCollection as $website) {
  211. $options[] = ['label' => $website->getName(), 'value' => $website->getId()];
  212. }
  213. return $options;
  214. }
  215. /**
  216. * Get websites as id => name associative array
  217. *
  218. * @param bool $withDefault
  219. * @param string $attribute
  220. * @return array
  221. */
  222. public function getWebsiteOptionHash($withDefault = false, $attribute = 'name')
  223. {
  224. $options = [];
  225. foreach ($this->_storeManager->getWebsites((bool)$withDefault && $this->_isAdminScopeAllowed) as $website) {
  226. $options[$website->getId()] = $website->getDataUsingMethod($attribute);
  227. }
  228. return $options;
  229. }
  230. /**
  231. * Get store views as id => name associative array
  232. *
  233. * @param bool $withDefault
  234. * @param string $attribute
  235. * @return array
  236. */
  237. public function getStoreOptionHash($withDefault = false, $attribute = 'name')
  238. {
  239. $options = [];
  240. foreach ($this->_storeManager->getStores((bool)$withDefault && $this->_isAdminScopeAllowed) as $store) {
  241. $options[$store->getId()] = $store->getDataUsingMethod($attribute);
  242. }
  243. return $options;
  244. }
  245. /**
  246. * Get store groups as id => name associative array
  247. *
  248. * @param string $attribute
  249. * @return array
  250. */
  251. public function getStoreGroupOptionHash($attribute = 'name')
  252. {
  253. $options = [];
  254. foreach ($this->_groupCollection as $group) {
  255. $options[$group->getId()] = $group->getDataUsingMethod($attribute);
  256. }
  257. return $options;
  258. }
  259. /**
  260. * Retrieve Website name by Id
  261. *
  262. * @param int $websiteId
  263. * @return string|null
  264. */
  265. public function getWebsiteName($websiteId)
  266. {
  267. foreach ($this->_websiteCollection as $website) {
  268. if ($website->getId() == $websiteId) {
  269. return $website->getName();
  270. }
  271. }
  272. return null;
  273. }
  274. /**
  275. * Retrieve Group name by Id
  276. *
  277. * @param int $groupId
  278. * @return string|null
  279. */
  280. public function getGroupName($groupId)
  281. {
  282. foreach ($this->_groupCollection as $group) {
  283. if ($group->getId() == $groupId) {
  284. return $group->getName();
  285. }
  286. }
  287. return null;
  288. }
  289. /**
  290. * Retrieve Store name by Id
  291. *
  292. * @param int $storeId
  293. * @return string|null
  294. */
  295. public function getStoreName($storeId)
  296. {
  297. if (isset($this->_storeCollection[$storeId])) {
  298. return $this->_storeCollection[$storeId]->getName();
  299. }
  300. return null;
  301. }
  302. /**
  303. * Retrieve store name with website and website store
  304. *
  305. * @param int $storeId
  306. * @return \Magento\Store\Model\Store|null
  307. */
  308. public function getStoreData($storeId)
  309. {
  310. if (isset($this->_storeCollection[$storeId])) {
  311. return $this->_storeCollection[$storeId];
  312. }
  313. return null;
  314. }
  315. /**
  316. * Retrieve store name with website and website store
  317. *
  318. * @param int $storeId
  319. * @return string
  320. */
  321. public function getStoreNameWithWebsite($storeId)
  322. {
  323. $name = '';
  324. if (is_array($storeId)) {
  325. $names = [];
  326. foreach ($storeId as $id) {
  327. $names[] = $this->getStoreNameWithWebsite($id);
  328. }
  329. $name = implode(', ', $names);
  330. } else {
  331. if (isset($this->_storeCollection[$storeId])) {
  332. $data = $this->_storeCollection[$storeId];
  333. $name .= $this->getWebsiteName($data->getWebsiteId());
  334. $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
  335. $name .= ($name ? '/' : '') . $data->getName();
  336. }
  337. }
  338. return $name;
  339. }
  340. /**
  341. * Retrieve Website collection as array
  342. *
  343. * @return array
  344. */
  345. public function getWebsiteCollection()
  346. {
  347. return $this->_websiteCollection;
  348. }
  349. /**
  350. * Retrieve Group collection as array
  351. *
  352. * @return array
  353. */
  354. public function getGroupCollection()
  355. {
  356. return $this->_groupCollection;
  357. }
  358. /**
  359. * Retrieve Store collection as array
  360. *
  361. * @return array
  362. */
  363. public function getStoreCollection()
  364. {
  365. return $this->_storeCollection;
  366. }
  367. /**
  368. * Load/Reload collection(s) by type
  369. * Allowed types: website, group, store or null for all
  370. *
  371. * @param string $type
  372. * @return $this
  373. */
  374. public function reload($type = null)
  375. {
  376. if ($type === null) {
  377. $this->_loadWebsiteCollection();
  378. $this->_loadGroupCollection();
  379. $this->_loadStoreCollection();
  380. } else {
  381. switch ($type) {
  382. case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE:
  383. $this->_loadWebsiteCollection();
  384. break;
  385. case \Magento\Store\Model\ScopeInterface::SCOPE_GROUP:
  386. $this->_loadGroupCollection();
  387. break;
  388. case \Magento\Store\Model\ScopeInterface::SCOPE_STORE:
  389. $this->_loadStoreCollection();
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. return $this;
  396. }
  397. /**
  398. * Retrieve store path with website and website store
  399. *
  400. * @param int $storeId
  401. * @return string
  402. */
  403. public function getStoreNamePath($storeId)
  404. {
  405. $name = '';
  406. if (is_array($storeId)) {
  407. $names = [];
  408. foreach ($storeId as $id) {
  409. $names[] = $this->getStoreNamePath($id);
  410. }
  411. $name = implode(', ', $names);
  412. } else {
  413. if (isset($this->_storeCollection[$storeId])) {
  414. $data = $this->_storeCollection[$storeId];
  415. $name .= $this->getWebsiteName($data->getWebsiteId());
  416. $name .= ($name ? '/' : '') . $this->getGroupName($data->getGroupId());
  417. }
  418. }
  419. return $name;
  420. }
  421. /**
  422. * Specify whether to show admin-scope options
  423. *
  424. * @param bool $value
  425. * @return $this
  426. */
  427. public function setIsAdminScopeAllowed($value)
  428. {
  429. $this->_isAdminScopeAllowed = (bool)$value;
  430. return $this;
  431. }
  432. /**
  433. * Return array of options as value-label pairs
  434. *
  435. * @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
  436. */
  437. public function toOptionArray()
  438. {
  439. return $this->getStoreValuesForForm();
  440. }
  441. }