/app/code/Magento/Store/Model/System/Store.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 473 lines · 270 code · 35 blank · 168 comment · 39 complexity · b2ab569802999c7b5615942dd389413c MD5 · raw file

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