Website.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. /**
  8. * Core Website model
  9. *
  10. * @api
  11. * @method string getGroupTitle()
  12. * @method string getStoreTitle()
  13. * @method int getStoreId()
  14. * @method int getGroupId()
  15. * @method int getWebsiteId()
  16. * @method bool hasWebsiteId()
  17. * @method int getSortOrder()
  18. * @method \Magento\Store\Model\Website setSortOrder($value)
  19. * @method int getIsDefault()
  20. * @method \Magento\Store\Model\Website setIsDefault($value)
  21. * @SuppressWarnings(PHPMD.TooManyFields)
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. * @since 100.0.2
  24. */
  25. class Website extends \Magento\Framework\Model\AbstractExtensibleModel implements
  26. \Magento\Framework\DataObject\IdentityInterface,
  27. \Magento\Framework\App\ScopeInterface,
  28. \Magento\Store\Api\Data\WebsiteInterface
  29. {
  30. const ENTITY = 'store_website';
  31. const CACHE_TAG = 'website';
  32. /**
  33. * @var bool
  34. */
  35. protected $_cacheTag = true;
  36. /**
  37. * @var string
  38. */
  39. protected $_eventPrefix = 'website';
  40. /**
  41. * @var string
  42. */
  43. protected $_eventObject = 'website';
  44. /**
  45. * Cache configuration array
  46. *
  47. * @var array
  48. */
  49. protected $_configCache = [];
  50. /**
  51. * Website Group Collection array
  52. *
  53. * @var \Magento\Store\Model\Store[]
  54. */
  55. protected $_groups;
  56. /**
  57. * Website group ids array
  58. *
  59. * @var array
  60. */
  61. protected $_groupIds = [];
  62. /**
  63. * The number of groups in a website
  64. *
  65. * @var int
  66. */
  67. protected $_groupsCount;
  68. /**
  69. * Website Store collection array
  70. *
  71. * @var array
  72. */
  73. protected $_stores;
  74. /**
  75. * Website store ids array
  76. *
  77. * @var array
  78. */
  79. protected $_storeIds = [];
  80. /**
  81. * Website store codes array
  82. *
  83. * @var array
  84. */
  85. protected $_storeCodes = [];
  86. /**
  87. * The number of stores in a website
  88. *
  89. * @var int
  90. */
  91. protected $_storesCount = 0;
  92. /**
  93. * Website default group
  94. *
  95. * @var \Magento\Store\Model\Store
  96. */
  97. protected $_defaultGroup;
  98. /**
  99. * Website default store
  100. *
  101. * @var Store
  102. */
  103. protected $_defaultStore;
  104. /**
  105. * is can delete website
  106. *
  107. * @var bool
  108. */
  109. protected $_isCanDelete;
  110. /**
  111. * @var bool
  112. */
  113. private $_isReadOnly = false;
  114. /**
  115. * @var \Magento\Config\Model\ResourceModel\Config\Data
  116. */
  117. protected $_configDataResource;
  118. /**
  119. * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
  120. */
  121. protected $storeListFactory;
  122. /**
  123. * @var \Magento\Store\Model\GroupFactory
  124. */
  125. protected $_storeGroupFactory;
  126. /**
  127. * @var WebsiteFactory
  128. */
  129. protected $_websiteFactory;
  130. /**
  131. * @var StoreManagerInterface
  132. */
  133. protected $_storeManager;
  134. /**
  135. * @var \Magento\Directory\Model\CurrencyFactory
  136. */
  137. protected $_currencyFactory;
  138. /**
  139. * @param \Magento\Framework\Model\Context $context
  140. * @param \Magento\Framework\Registry $registry
  141. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  142. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  143. * @param \Magento\Config\Model\ResourceModel\Config\Data $configDataResource
  144. * @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
  145. * @param \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory
  146. * @param \Magento\Store\Model\GroupFactory $storeGroupFactory
  147. * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
  148. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  149. * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
  150. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  151. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  152. * @param array $data
  153. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  154. */
  155. public function __construct(
  156. \Magento\Framework\Model\Context $context,
  157. \Magento\Framework\Registry $registry,
  158. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  159. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  160. \Magento\Config\Model\ResourceModel\Config\Data $configDataResource,
  161. \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
  162. \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory,
  163. \Magento\Store\Model\GroupFactory $storeGroupFactory,
  164. \Magento\Store\Model\WebsiteFactory $websiteFactory,
  165. \Magento\Store\Model\StoreManagerInterface $storeManager,
  166. \Magento\Directory\Model\CurrencyFactory $currencyFactory,
  167. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  168. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  169. array $data = []
  170. ) {
  171. parent::__construct(
  172. $context,
  173. $registry,
  174. $extensionFactory,
  175. $customAttributeFactory,
  176. $resource,
  177. $resourceCollection,
  178. $data
  179. );
  180. $this->_configDataResource = $configDataResource;
  181. $this->_coreConfig = $coreConfig;
  182. $this->storeListFactory = $storeListFactory;
  183. $this->_storeGroupFactory = $storeGroupFactory;
  184. $this->_websiteFactory = $websiteFactory;
  185. $this->_storeManager = $storeManager;
  186. $this->_currencyFactory = $currencyFactory;
  187. }
  188. /**
  189. * init model
  190. *
  191. * @return void
  192. */
  193. protected function _construct()
  194. {
  195. $this->_init(\Magento\Store\Model\ResourceModel\Website::class);
  196. }
  197. /**
  198. * Custom load
  199. *
  200. * @param int|string $id
  201. * @param string $field
  202. * @return $this
  203. */
  204. public function load($id, $field = null)
  205. {
  206. if (!is_numeric($id) && $field === null) {
  207. $this->_getResource()->load($this, $id, 'code');
  208. return $this;
  209. }
  210. return parent::load($id, $field);
  211. }
  212. /**
  213. * Get website config data
  214. *
  215. * @param string $path
  216. * @return mixed
  217. */
  218. public function getConfig($path)
  219. {
  220. if (!isset($this->_configCache[$path])) {
  221. $config = $this->_coreConfig->getValue(
  222. $path,
  223. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE,
  224. $this->getCode()
  225. );
  226. if (!$config) {
  227. return false;
  228. }
  229. $this->_configCache[$path] = $config;
  230. }
  231. return $this->_configCache[$path];
  232. }
  233. /**
  234. * Load group collection and set internal data
  235. *
  236. * @return void
  237. */
  238. protected function _loadGroups()
  239. {
  240. $this->_groups = [];
  241. $this->_groupsCount = 0;
  242. foreach ($this->getGroupCollection() as $group) {
  243. $this->_groups[$group->getId()] = $group;
  244. $this->_groupIds[$group->getId()] = $group->getId();
  245. if ($this->getDefaultGroupId() == $group->getId()) {
  246. $this->_defaultGroup = $group;
  247. }
  248. $this->_groupsCount++;
  249. }
  250. }
  251. /**
  252. * Set website groups
  253. *
  254. * @param array $groups
  255. * @return $this
  256. */
  257. public function setGroups($groups)
  258. {
  259. $this->_groups = [];
  260. $this->_groupsCount = 0;
  261. foreach ($groups as $group) {
  262. $this->_groups[$group->getId()] = $group;
  263. $this->_groupIds[$group->getId()] = $group->getId();
  264. if ($this->getDefaultGroupId() == $group->getId()) {
  265. $this->_defaultGroup = $group;
  266. }
  267. $this->_groupsCount++;
  268. }
  269. return $this;
  270. }
  271. /**
  272. * Retrieve new (not loaded) Group collection object with website filter
  273. *
  274. * @return \Magento\Store\Model\ResourceModel\Group\Collection
  275. */
  276. public function getGroupCollection()
  277. {
  278. return $this->_storeGroupFactory->create()->getCollection()->addWebsiteFilter($this->getId())
  279. ->setLoadDefault(true);
  280. }
  281. /**
  282. * Retrieve website groups
  283. *
  284. * @return \Magento\Store\Model\Store[]
  285. */
  286. public function getGroups()
  287. {
  288. if ($this->_groups === null) {
  289. $this->_loadGroups();
  290. }
  291. return $this->_groups;
  292. }
  293. /**
  294. * Retrieve website group ids
  295. *
  296. * @return array
  297. */
  298. public function getGroupIds()
  299. {
  300. if ($this->_groups === null) {
  301. $this->_loadGroups();
  302. }
  303. return $this->_groupIds;
  304. }
  305. /**
  306. * Retrieve number groups in a website
  307. *
  308. * @return int
  309. */
  310. public function getGroupsCount()
  311. {
  312. if ($this->_groups === null) {
  313. $this->_loadGroups();
  314. }
  315. return $this->_groupsCount;
  316. }
  317. /**
  318. * Retrieve default group model
  319. *
  320. * @return \Magento\Store\Model\Store
  321. */
  322. public function getDefaultGroup()
  323. {
  324. if (!$this->hasDefaultGroupId()) {
  325. return false;
  326. }
  327. if ($this->_groups === null) {
  328. $this->_loadGroups();
  329. }
  330. return $this->_defaultGroup;
  331. }
  332. /**
  333. * Load store collection and set internal data
  334. *
  335. * @return void
  336. */
  337. protected function _loadStores()
  338. {
  339. $this->_stores = [];
  340. $this->_storesCount = 0;
  341. foreach ($this->getStoreCollection() as $store) {
  342. $this->_stores[$store->getId()] = $store;
  343. $this->_storeIds[$store->getId()] = $store->getId();
  344. $this->_storeCodes[$store->getId()] = $store->getCode();
  345. if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
  346. $this->_defaultStore = $store;
  347. }
  348. $this->_storesCount++;
  349. }
  350. }
  351. /**
  352. * Set website stores
  353. *
  354. * @param array $stores
  355. * @return void
  356. */
  357. public function setStores($stores)
  358. {
  359. $this->_stores = [];
  360. $this->_storesCount = 0;
  361. foreach ($stores as $store) {
  362. $this->_stores[$store->getId()] = $store;
  363. $this->_storeIds[$store->getId()] = $store->getId();
  364. $this->_storeCodes[$store->getId()] = $store->getCode();
  365. if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
  366. $this->_defaultStore = $store;
  367. }
  368. $this->_storesCount++;
  369. }
  370. }
  371. /**
  372. * Retrieve new (not loaded) Store collection object with website filter
  373. *
  374. * @return \Magento\Store\Model\ResourceModel\Store\Collection
  375. */
  376. public function getStoreCollection()
  377. {
  378. return $this->storeListFactory->create()->addWebsiteFilter($this->getId())->setLoadDefault(true);
  379. }
  380. /**
  381. * Retrieve website store objects
  382. *
  383. * @return array
  384. */
  385. public function getStores()
  386. {
  387. if ($this->_stores === null) {
  388. $this->_loadStores();
  389. }
  390. return $this->_stores;
  391. }
  392. /**
  393. * Retrieve website store ids
  394. *
  395. * @return array
  396. */
  397. public function getStoreIds()
  398. {
  399. if ($this->_stores === null) {
  400. $this->_loadStores();
  401. }
  402. return $this->_storeIds;
  403. }
  404. /**
  405. * Retrieve website store codes
  406. *
  407. * @return array
  408. */
  409. public function getStoreCodes()
  410. {
  411. if ($this->_stores === null) {
  412. $this->_loadStores();
  413. }
  414. return $this->_storeCodes;
  415. }
  416. /**
  417. * Retrieve number stores in a website
  418. *
  419. * @return int
  420. */
  421. public function getStoresCount()
  422. {
  423. if ($this->_stores === null) {
  424. $this->_loadStores();
  425. }
  426. return $this->_storesCount;
  427. }
  428. /**
  429. * Can delete website
  430. *
  431. * @return bool
  432. */
  433. public function isCanDelete()
  434. {
  435. if ($this->_isReadOnly || !$this->getId()) {
  436. return false;
  437. }
  438. if ($this->_isCanDelete === null) {
  439. $this->_isCanDelete = $this->_websiteFactory->create()->getCollection()->getSize() > 1 &&
  440. !$this->getIsDefault();
  441. }
  442. return $this->_isCanDelete;
  443. }
  444. /**
  445. * Retrieve unique website-group-store key for collection with groups and stores
  446. *
  447. * @return string
  448. */
  449. public function getWebsiteGroupStore()
  450. {
  451. return implode('-', [$this->getWebsiteId(), $this->getGroupId(), $this->getStoreId()]);
  452. }
  453. /**
  454. * @return mixed
  455. */
  456. public function getDefaultGroupId()
  457. {
  458. return $this->_getData('default_group_id');
  459. }
  460. /**
  461. * @inheritdoc
  462. */
  463. public function setDefaultGroupId($defaultGroupId)
  464. {
  465. return $this->setData('default_group_id', $defaultGroupId);
  466. }
  467. /**
  468. * @return mixed
  469. */
  470. public function getCode()
  471. {
  472. return $this->_getData('code');
  473. }
  474. /**
  475. * @inheritdoc
  476. */
  477. public function setCode($code)
  478. {
  479. return $this->setData('code', $code);
  480. }
  481. /**
  482. * @inheritdoc
  483. */
  484. public function getName()
  485. {
  486. return $this->_getData('name');
  487. }
  488. /**
  489. * @inheritdoc
  490. */
  491. public function setName($name)
  492. {
  493. return $this->setData('name', $name);
  494. }
  495. /**
  496. * @return $this
  497. */
  498. public function beforeDelete()
  499. {
  500. $this->_configDataResource->clearScopeData(
  501. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES,
  502. $this->getId()
  503. );
  504. $this->_configDataResource->clearScopeData(
  505. \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
  506. $this->getStoreIds()
  507. );
  508. return parent::beforeDelete();
  509. }
  510. /**
  511. * Rewrite in order to clear configuration cache
  512. *
  513. * @return $this
  514. */
  515. public function afterDelete()
  516. {
  517. $this->_storeManager->reinitStores();
  518. parent::afterDelete();
  519. return $this;
  520. }
  521. /**
  522. * Clear configuration cache after creation website
  523. *
  524. * @return $this
  525. * @since 100.2.0
  526. */
  527. public function afterSave()
  528. {
  529. if ($this->isObjectNew()) {
  530. $this->_storeManager->reinitStores();
  531. }
  532. return parent::afterSave();
  533. }
  534. /**
  535. * Retrieve website base currency code
  536. *
  537. * @return string
  538. */
  539. public function getBaseCurrencyCode()
  540. {
  541. if ($this->getConfig(
  542. \Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE
  543. ) == \Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL
  544. ) {
  545. $currencyCode = $this->_coreConfig->getValue(
  546. \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
  547. 'default'
  548. );
  549. } else {
  550. $currencyCode = $this->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE);
  551. }
  552. return $currencyCode;
  553. }
  554. /**
  555. * Retrieve website base currency
  556. *
  557. * @return \Magento\Directory\Model\Currency
  558. */
  559. public function getBaseCurrency()
  560. {
  561. $currency = $this->getData('base_currency');
  562. if ($currency === null) {
  563. $currency = $this->_currencyFactory->create()->load($this->getBaseCurrencyCode());
  564. $this->setData('base_currency', $currency);
  565. }
  566. return $currency;
  567. }
  568. /**
  569. * Retrieve Default Website Store or null
  570. *
  571. * @return Store
  572. */
  573. public function getDefaultStore()
  574. {
  575. // init stores if not loaded
  576. $this->getStores();
  577. return $this->_defaultStore;
  578. }
  579. /**
  580. * Retrieve default stores select object
  581. * Select fields website_id, store_id
  582. *
  583. * @param bool $withDefault include/exclude default admin website
  584. * @return \Magento\Framework\DB\Select
  585. */
  586. public function getDefaultStoresSelect($withDefault = false)
  587. {
  588. return $this->getResource()->getDefaultStoresSelect($withDefault);
  589. }
  590. /**
  591. * Get/Set isReadOnly flag
  592. *
  593. * @param bool $value
  594. * @return bool
  595. */
  596. public function isReadOnly($value = null)
  597. {
  598. if (null !== $value) {
  599. $this->_isReadOnly = (bool)$value;
  600. }
  601. return $this->_isReadOnly;
  602. }
  603. /**
  604. * Get identities
  605. *
  606. * @return array
  607. */
  608. public function getIdentities()
  609. {
  610. return [self::CACHE_TAG];
  611. }
  612. /**
  613. * {@inheritdoc}
  614. * @since 100.1.0
  615. */
  616. public function getScopeType()
  617. {
  618. return \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE;
  619. }
  620. /**
  621. * {@inheritdoc}
  622. * @since 100.1.0
  623. */
  624. public function getScopeTypeName()
  625. {
  626. return 'Website';
  627. }
  628. /**
  629. * {@inheritdoc}
  630. */
  631. public function getExtensionAttributes()
  632. {
  633. return $this->_getExtensionAttributes();
  634. }
  635. /**
  636. * {@inheritdoc}
  637. */
  638. public function setExtensionAttributes(
  639. \Magento\Store\Api\Data\WebsiteExtensionInterface $extensionAttributes
  640. ) {
  641. return $this->_setExtensionAttributes($extensionAttributes);
  642. }
  643. }