Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Website.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Store\Model;
7 
27  \Magento\Framework\DataObject\IdentityInterface,
30 {
31  const ENTITY = 'store_website';
32 
33  const CACHE_TAG = 'website';
34 
38  protected $_cacheTag = true;
39 
43  protected $_eventPrefix = 'website';
44 
48  protected $_eventObject = 'website';
49 
55  protected $_configCache = [];
56 
62  protected $_groups;
63 
69  protected $_groupIds = [];
70 
76  protected $_groupsCount;
77 
83  protected $_stores;
84 
90  protected $_storeIds = [];
91 
97  protected $_storeCodes = [];
98 
104  protected $_storesCount = 0;
105 
111  protected $_defaultGroup;
112 
118  protected $_defaultStore;
119 
125  protected $_isCanDelete;
126 
130  private $_isReadOnly = false;
131 
136 
140  protected $storeListFactory;
141 
146 
150  protected $_websiteFactory;
151 
155  protected $_storeManager;
156 
160  protected $_currencyFactory;
161 
179  public function __construct(
180  \Magento\Framework\Model\Context $context,
181  \Magento\Framework\Registry $registry,
182  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
183  \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
184  \Magento\Config\Model\ResourceModel\Config\Data $configDataResource,
185  \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
186  \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory,
187  \Magento\Store\Model\GroupFactory $storeGroupFactory,
188  \Magento\Store\Model\WebsiteFactory $websiteFactory,
190  \Magento\Directory\Model\CurrencyFactory $currencyFactory,
191  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
192  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
193  array $data = []
194  ) {
195  parent::__construct(
196  $context,
197  $registry,
198  $extensionFactory,
200  $resource,
201  $resourceCollection,
202  $data
203  );
204  $this->_configDataResource = $configDataResource;
205  $this->_coreConfig = $coreConfig;
206  $this->storeListFactory = $storeListFactory;
207  $this->_storeGroupFactory = $storeGroupFactory;
208  $this->_websiteFactory = $websiteFactory;
209  $this->_storeManager = $storeManager;
210  $this->_currencyFactory = $currencyFactory;
211  }
212 
218  protected function _construct()
219  {
220  $this->_init(\Magento\Store\Model\ResourceModel\Website::class);
221  }
222 
230  public function load($id, $field = null)
231  {
232  if (!is_numeric($id) && $field === null) {
233  $this->_getResource()->load($this, $id, 'code');
234  return $this;
235  }
236  return parent::load($id, $field);
237  }
238 
245  public function getConfig($path)
246  {
247  if (!isset($this->_configCache[$path])) {
248  $config = $this->_coreConfig->getValue(
249  $path,
251  $this->getCode()
252  );
253  if (!$config) {
254  return false;
255  }
256  $this->_configCache[$path] = $config;
257  }
258  return $this->_configCache[$path];
259  }
260 
266  protected function _loadGroups()
267  {
268  $this->_groups = [];
269  $this->_groupsCount = 0;
270  foreach ($this->getGroupCollection() as $group) {
271  $this->_groups[$group->getId()] = $group;
272  $this->_groupIds[$group->getId()] = $group->getId();
273  if ($this->getDefaultGroupId() == $group->getId()) {
274  $this->_defaultGroup = $group;
275  }
276  $this->_groupsCount++;
277  }
278  }
279 
286  public function setGroups($groups)
287  {
288  $this->_groups = [];
289  $this->_groupsCount = 0;
290  foreach ($groups as $group) {
291  $this->_groups[$group->getId()] = $group;
292  $this->_groupIds[$group->getId()] = $group->getId();
293  if ($this->getDefaultGroupId() == $group->getId()) {
294  $this->_defaultGroup = $group;
295  }
296  $this->_groupsCount++;
297  }
298  return $this;
299  }
300 
306  public function getGroupCollection()
307  {
308  return $this->_storeGroupFactory->create()->getCollection()->addWebsiteFilter($this->getId())
309  ->setLoadDefault(true);
310  }
311 
317  public function getGroups()
318  {
319  if ($this->_groups === null) {
320  $this->_loadGroups();
321  }
322  return $this->_groups;
323  }
324 
330  public function getGroupIds()
331  {
332  if ($this->_groups === null) {
333  $this->_loadGroups();
334  }
335  return $this->_groupIds;
336  }
337 
343  public function getGroupsCount()
344  {
345  if ($this->_groups === null) {
346  $this->_loadGroups();
347  }
348  return $this->_groupsCount;
349  }
350 
356  public function getDefaultGroup()
357  {
358  if (!$this->hasDefaultGroupId()) {
359  return false;
360  }
361  if ($this->_groups === null) {
362  $this->_loadGroups();
363  }
364  return $this->_defaultGroup;
365  }
366 
372  protected function _loadStores()
373  {
374  $this->_stores = [];
375  $this->_storesCount = 0;
376  foreach ($this->getStoreCollection() as $store) {
377  $this->_stores[$store->getId()] = $store;
378  $this->_storeIds[$store->getId()] = $store->getId();
379  $this->_storeCodes[$store->getId()] = $store->getCode();
380  if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
381  $this->_defaultStore = $store;
382  }
383  $this->_storesCount++;
384  }
385  }
386 
393  public function setStores($stores)
394  {
395  $this->_stores = [];
396  $this->_storesCount = 0;
397  foreach ($stores as $store) {
398  $this->_stores[$store->getId()] = $store;
399  $this->_storeIds[$store->getId()] = $store->getId();
400  $this->_storeCodes[$store->getId()] = $store->getCode();
401  if ($this->getDefaultGroup() && $this->getDefaultGroup()->getDefaultStoreId() == $store->getId()) {
402  $this->_defaultStore = $store;
403  }
404  $this->_storesCount++;
405  }
406  }
407 
413  public function getStoreCollection()
414  {
415  return $this->storeListFactory->create()->addWebsiteFilter($this->getId())->setLoadDefault(true);
416  }
417 
423  public function getStores()
424  {
425  if ($this->_stores === null) {
426  $this->_loadStores();
427  }
428  return $this->_stores;
429  }
430 
436  public function getStoreIds()
437  {
438  if ($this->_stores === null) {
439  $this->_loadStores();
440  }
441  return $this->_storeIds;
442  }
443 
449  public function getStoreCodes()
450  {
451  if ($this->_stores === null) {
452  $this->_loadStores();
453  }
454  return $this->_storeCodes;
455  }
456 
462  public function getStoresCount()
463  {
464  if ($this->_stores === null) {
465  $this->_loadStores();
466  }
467  return $this->_storesCount;
468  }
469 
475  public function isCanDelete()
476  {
477  if ($this->_isReadOnly || !$this->getId()) {
478  return false;
479  }
480  if ($this->_isCanDelete === null) {
481  $this->_isCanDelete = $this->_websiteFactory->create()->getCollection()->getSize() > 1 &&
482  !$this->getIsDefault();
483  }
484  return $this->_isCanDelete;
485  }
486 
492  public function getWebsiteGroupStore()
493  {
494  return implode('-', [$this->getWebsiteId(), $this->getGroupId(), $this->getStoreId()]);
495  }
496 
500  public function getDefaultGroupId()
501  {
502  return $this->_getData('default_group_id');
503  }
504 
509  {
510  return $this->setData('default_group_id', $defaultGroupId);
511  }
512 
516  public function getCode()
517  {
518  return $this->_getData('code');
519  }
520 
524  public function setCode($code)
525  {
526  return $this->setData('code', $code);
527  }
528 
532  public function getName()
533  {
534  return $this->_getData('name');
535  }
536 
540  public function setName($name)
541  {
542  return $this->setData('name', $name);
543  }
544 
548  public function beforeDelete()
549  {
550  $this->_configDataResource->clearScopeData(
552  $this->getId()
553  );
554  $this->_configDataResource->clearScopeData(
556  $this->getStoreIds()
557  );
558  return parent::beforeDelete();
559  }
560 
566  public function afterDelete()
567  {
568  $this->_storeManager->reinitStores();
569  parent::afterDelete();
570  return $this;
571  }
572 
579  public function afterSave()
580  {
581  if ($this->isObjectNew()) {
582  $this->_storeManager->reinitStores();
583  }
584 
585  return parent::afterSave();
586  }
587 
593  public function getBaseCurrencyCode()
594  {
595  if ($this->getConfig(
598  ) {
599  $currencyCode = $this->_coreConfig->getValue(
600  \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
601  'default'
602  );
603  } else {
604  $currencyCode = $this->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE);
605  }
606 
607  return $currencyCode;
608  }
609 
615  public function getBaseCurrency()
616  {
617  $currency = $this->getData('base_currency');
618  if ($currency === null) {
619  $currency = $this->_currencyFactory->create()->load($this->getBaseCurrencyCode());
620  $this->setData('base_currency', $currency);
621  }
622  return $currency;
623  }
624 
630  public function getDefaultStore()
631  {
632  // init stores if not loaded
633  $this->getStores();
634  return $this->_defaultStore;
635  }
636 
644  public function getDefaultStoresSelect($withDefault = false)
645  {
646  return $this->getResource()->getDefaultStoresSelect($withDefault);
647  }
648 
655  public function isReadOnly($value = null)
656  {
657  if (null !== $value) {
658  $this->_isReadOnly = (bool)$value;
659  }
660  return $this->_isReadOnly;
661  }
662 
668  public function getIdentities()
669  {
670  return [self::CACHE_TAG];
671  }
672 
677  public function getScopeType()
678  {
679  return \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE;
680  }
681 
686  public function getScopeTypeName()
687  {
688  return 'Website';
689  }
690 
694  public function getExtensionAttributes()
695  {
696  return $this->_getExtensionAttributes();
697  }
698 
702  public function setExtensionAttributes(
703  \Magento\Store\Api\Data\WebsiteExtensionInterface $extensionAttributes
704  ) {
705  return $this->_setExtensionAttributes($extensionAttributes);
706  }
707 }
$config
Definition: fraud_order.php:17
$id
Definition: fieldset.phtml:14
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
$group
Definition: sections.phtml:16
$storeManager
setDefaultGroupId($defaultGroupId)
Definition: Website.php:508
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
getDefaultStoresSelect($withDefault=false)
Definition: Website.php:644
load($id, $field=null)
Definition: Website.php:230
setExtensionAttributes(\Magento\Store\Api\Data\WebsiteExtensionInterface $extensionAttributes)
Definition: Website.php:702
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Config\Model\ResourceModel\Config\Data $configDataResource, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory, \Magento\Store\Model\GroupFactory $storeGroupFactory, \Magento\Store\Model\WebsiteFactory $websiteFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Website.php:179
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14