Group.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Store group model
  8. */
  9. namespace Magento\Store\Model;
  10. /**
  11. * Class Group
  12. *
  13. * @api
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. * @since 100.0.2
  16. */
  17. class Group extends \Magento\Framework\Model\AbstractExtensibleModel implements
  18. \Magento\Framework\DataObject\IdentityInterface,
  19. \Magento\Store\Api\Data\GroupInterface,
  20. \Magento\Framework\App\ScopeInterface
  21. {
  22. const ENTITY = 'store_group';
  23. const CACHE_TAG = 'store_group';
  24. /**
  25. * @var bool
  26. */
  27. protected $_cacheTag = true;
  28. /**
  29. * @var string
  30. */
  31. protected $_eventPrefix = 'store_group';
  32. /**
  33. * @var string
  34. */
  35. protected $_eventObject = 'store_group';
  36. /**
  37. * Group Store collection array
  38. *
  39. * @var \Magento\Store\Model\ResourceModel\Store\Collection[]
  40. */
  41. protected $_stores;
  42. /**
  43. * Group store ids array
  44. *
  45. * @var int[]
  46. */
  47. protected $_storeIds = [];
  48. /**
  49. * Group store codes array
  50. *
  51. * @var string[]
  52. */
  53. protected $_storeCodes = [];
  54. /**
  55. * The number of stores in a group
  56. *
  57. * @var int
  58. */
  59. protected $_storesCount = 0;
  60. /**
  61. * Group default store
  62. *
  63. * @var \Magento\Store\Model\Store
  64. */
  65. protected $_defaultStore;
  66. /**
  67. * @var bool
  68. */
  69. private $_isReadOnly = false;
  70. /**
  71. * @var \Magento\Config\Model\ResourceModel\Config\Data
  72. */
  73. protected $_configDataResource;
  74. /**
  75. * @var \Magento\Store\Model\Store
  76. */
  77. protected $_storeListFactory;
  78. /**
  79. * @var \Magento\Store\Model\StoreManagerInterface
  80. */
  81. protected $_storeManager;
  82. /**
  83. * @var \Magento\Framework\Event\ManagerInterface
  84. */
  85. private $eventManager;
  86. /**
  87. * @param \Magento\Framework\Model\Context $context
  88. * @param \Magento\Framework\Registry $registry
  89. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  90. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  91. * @param \Magento\Config\Model\ResourceModel\Config\Data $configDataResource
  92. * @param \Magento\Store\Model\Store $store
  93. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  94. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  95. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  96. * @param array $data
  97. * @param \Magento\Framework\Event\ManagerInterface|null $eventManager
  98. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  99. */
  100. public function __construct(
  101. \Magento\Framework\Model\Context $context,
  102. \Magento\Framework\Registry $registry,
  103. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  104. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  105. \Magento\Config\Model\ResourceModel\Config\Data $configDataResource,
  106. \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory,
  107. \Magento\Store\Model\StoreManagerInterface $storeManager,
  108. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  109. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  110. array $data = [],
  111. \Magento\Framework\Event\ManagerInterface $eventManager = null
  112. ) {
  113. $this->_configDataResource = $configDataResource;
  114. $this->_storeListFactory = $storeListFactory;
  115. $this->_storeManager = $storeManager;
  116. $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
  117. ->get(\Magento\Framework\Event\ManagerInterface::class);
  118. parent::__construct(
  119. $context,
  120. $registry,
  121. $extensionFactory,
  122. $customAttributeFactory,
  123. $resource,
  124. $resourceCollection,
  125. $data
  126. );
  127. }
  128. /**
  129. * Init model
  130. *
  131. * @return void
  132. */
  133. protected function _construct()
  134. {
  135. $this->_init(\Magento\Store\Model\ResourceModel\Group::class);
  136. }
  137. /**
  138. * Load store collection and set internal data
  139. *
  140. * @return void
  141. */
  142. protected function _loadStores()
  143. {
  144. $this->_stores = [];
  145. $this->_storesCount = 0;
  146. foreach ($this->getStoreCollection() as $store) {
  147. $this->_stores[$store->getId()] = $store;
  148. $this->_storeIds[$store->getId()] = $store->getId();
  149. $this->_storeCodes[$store->getId()] = $store->getCode();
  150. if ($this->getDefaultStoreId() == $store->getId()) {
  151. $this->_defaultStore = $store;
  152. }
  153. $this->_storesCount++;
  154. }
  155. }
  156. /**
  157. * Set website stores
  158. *
  159. * @param \Magento\Store\Model\Store[] $stores
  160. * @return void
  161. */
  162. public function setStores($stores)
  163. {
  164. $this->_stores = [];
  165. $this->_storesCount = 0;
  166. foreach ($stores as $store) {
  167. $this->_stores[$store->getId()] = $store;
  168. $this->_storeIds[$store->getId()] = $store->getId();
  169. $this->_storeCodes[$store->getId()] = $store->getCode();
  170. if ($this->getDefaultStoreId() == $store->getId()) {
  171. $this->_defaultStore = $store;
  172. }
  173. $this->_storesCount++;
  174. }
  175. }
  176. /**
  177. * Retrieve new (not loaded) Store collection object with group filter
  178. *
  179. * @return \Magento\Store\Model\ResourceModel\Store\Collection
  180. */
  181. public function getStoreCollection()
  182. {
  183. return $this->_storeListFactory->create()->addGroupFilter($this->getId());
  184. }
  185. /**
  186. * Retrieve website store objects
  187. *
  188. * @return \Magento\Store\Model\ResourceModel\Store\Collection[]
  189. */
  190. public function getStores()
  191. {
  192. if ($this->_stores === null) {
  193. $this->_loadStores();
  194. }
  195. return $this->_stores;
  196. }
  197. /**
  198. * Retrieve website store ids
  199. *
  200. * @return int[]
  201. */
  202. public function getStoreIds()
  203. {
  204. if ($this->_stores === null) {
  205. $this->_loadStores();
  206. }
  207. return $this->_storeIds;
  208. }
  209. /**
  210. * Retrieve website store codes
  211. *
  212. * @return array
  213. */
  214. public function getStoreCodes()
  215. {
  216. if ($this->_stores === null) {
  217. $this->_loadStores();
  218. }
  219. return $this->_storeCodes;
  220. }
  221. /**
  222. * @return int
  223. */
  224. public function getStoresCount()
  225. {
  226. if ($this->_stores === null) {
  227. $this->_loadStores();
  228. }
  229. return $this->_storesCount;
  230. }
  231. /**
  232. * Retrieve default store model
  233. *
  234. * @return \Magento\Store\Model\Store
  235. */
  236. public function getDefaultStore()
  237. {
  238. if (!$this->hasDefaultStoreId()) {
  239. return false;
  240. }
  241. if ($this->_stores === null) {
  242. $this->_loadStores();
  243. }
  244. return $this->_defaultStore;
  245. }
  246. /**
  247. * Get most suitable store by locale
  248. * If no store with given locale is found - default store is returned
  249. * If group has no stores - null is returned
  250. *
  251. * @param string $locale
  252. * @return \Magento\Store\Model\Store|null
  253. */
  254. public function getDefaultStoreByLocale($locale)
  255. {
  256. if ($this->getDefaultStore() && $this->getDefaultStore()->getLocaleCode() == $locale) {
  257. return $this->getDefaultStore();
  258. } else {
  259. $stores = $this->getStoresByLocale($locale);
  260. if (count($stores)) {
  261. return $stores[0];
  262. } else {
  263. return $this->getDefaultStore() ? $this->getDefaultStore() : null;
  264. }
  265. }
  266. }
  267. /**
  268. * Retrieve list of stores with given locale
  269. *
  270. * @param string $locale
  271. * @return \Magento\Store\Model\Store[]
  272. */
  273. public function getStoresByLocale($locale)
  274. {
  275. $stores = [];
  276. foreach ($this->getStores() as $store) {
  277. /* @var $store \Magento\Store\Model\Store */
  278. if ($store->getLocaleCode() == $locale) {
  279. $stores[] = $store;
  280. }
  281. }
  282. return $stores;
  283. }
  284. /**
  285. * Set relation to the website
  286. *
  287. * @param Website $website
  288. * @return void
  289. */
  290. public function setWebsite(Website $website)
  291. {
  292. $this->setWebsiteId($website->getId());
  293. }
  294. /**
  295. * Retrieve website model
  296. *
  297. * @return Website|bool
  298. */
  299. public function getWebsite()
  300. {
  301. if ($this->getWebsiteId() === null) {
  302. return false;
  303. }
  304. return $this->_storeManager->getWebsite($this->getWebsiteId());
  305. }
  306. /**
  307. * Is can delete group
  308. *
  309. * @return bool
  310. */
  311. public function isCanDelete()
  312. {
  313. if (!$this->getId()) {
  314. return false;
  315. }
  316. return $this->getWebsite()->getGroupsCount() > 1;
  317. }
  318. /**
  319. * @return mixed
  320. */
  321. public function getDefaultStoreId()
  322. {
  323. return $this->_getData('default_store_id');
  324. }
  325. /**
  326. * @inheritdoc
  327. */
  328. public function setDefaultStoreId($defaultStoreId)
  329. {
  330. return $this->setData('default_store_id', $defaultStoreId);
  331. }
  332. /**
  333. * @return mixed
  334. */
  335. public function getRootCategoryId()
  336. {
  337. return $this->_getData('root_category_id');
  338. }
  339. /**
  340. * @inheritdoc
  341. */
  342. public function setRootCategoryId($rootCategoryId)
  343. {
  344. return $this->setData('root_category_id', $rootCategoryId);
  345. }
  346. /**
  347. * @return mixed
  348. */
  349. public function getWebsiteId()
  350. {
  351. return $this->_getData('website_id');
  352. }
  353. /**
  354. * @inheritdoc
  355. */
  356. public function setWebsiteId($websiteId)
  357. {
  358. return $this->setData('website_id', $websiteId);
  359. }
  360. /**
  361. * @return $this
  362. */
  363. public function beforeDelete()
  364. {
  365. $this->_configDataResource->clearScopeData(
  366. \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
  367. $this->getStoreIds()
  368. );
  369. return parent::beforeDelete();
  370. }
  371. /**
  372. * @inheritdoc
  373. * @since 100.1.0
  374. */
  375. public function afterDelete()
  376. {
  377. $group = $this;
  378. $this->getResource()->addCommitCallback(function () use ($group) {
  379. $this->_storeManager->reinitStores();
  380. $this->eventManager->dispatch($this->_eventPrefix . '_delete', ['group' => $group]);
  381. });
  382. $result = parent::afterDelete();
  383. if ($this->getId() === $this->getWebsite()->getDefaultGroupId()) {
  384. $ids = $this->getWebsite()->getGroupIds();
  385. if (!empty($ids) && count($ids) > 1) {
  386. unset($ids[$this->getId()]);
  387. $defaultId = current($ids);
  388. } else {
  389. $defaultId = null;
  390. }
  391. $this->getWebsite()->setDefaultGroupId($defaultId);
  392. $this->getWebsite()->save();
  393. }
  394. return $result;
  395. }
  396. /**
  397. * @inheritdoc
  398. * @since 100.2.5
  399. */
  400. public function afterSave()
  401. {
  402. $group = $this;
  403. $this->getResource()->addCommitCallback(function () use ($group) {
  404. $this->_storeManager->reinitStores();
  405. $this->eventManager->dispatch($this->_eventPrefix . '_save', ['group' => $group]);
  406. });
  407. return parent::afterSave();
  408. }
  409. /**
  410. * Get/Set isReadOnly flag
  411. *
  412. * @param bool $value
  413. * @return bool
  414. */
  415. public function isReadOnly($value = null)
  416. {
  417. if (null !== $value) {
  418. $this->_isReadOnly = (bool)$value;
  419. }
  420. return $this->_isReadOnly;
  421. }
  422. /**
  423. * Get identities
  424. *
  425. * @return array
  426. */
  427. public function getIdentities()
  428. {
  429. return [self::CACHE_TAG];
  430. }
  431. /**
  432. * {@inheritdoc}
  433. */
  434. public function getName()
  435. {
  436. return $this->getData('name');
  437. }
  438. /**
  439. * @inheritdoc
  440. */
  441. public function setName($name)
  442. {
  443. return $this->setData('name', $name);
  444. }
  445. /**
  446. * @inheritdoc
  447. * @since 100.1.0
  448. */
  449. public function getCode()
  450. {
  451. return $this->getData('code');
  452. }
  453. /**
  454. * @inheritdoc
  455. * @since 100.2.0
  456. */
  457. public function setCode($code)
  458. {
  459. return $this->setData('code', $code);
  460. }
  461. /**
  462. * {@inheritdoc}
  463. */
  464. public function getExtensionAttributes()
  465. {
  466. return $this->_getExtensionAttributes();
  467. }
  468. /**
  469. * {@inheritdoc}
  470. */
  471. public function setExtensionAttributes(
  472. \Magento\Store\Api\Data\GroupExtensionInterface $extensionAttributes
  473. ) {
  474. return $this->_setExtensionAttributes($extensionAttributes);
  475. }
  476. /**
  477. * {@inheritdoc}
  478. * @since 100.1.0
  479. */
  480. public function getScopeType()
  481. {
  482. return ScopeInterface::SCOPE_GROUP;
  483. }
  484. /**
  485. * {@inheritdoc}
  486. * @since 100.1.0
  487. */
  488. public function getScopeTypeName()
  489. {
  490. return 'Store';
  491. }
  492. }