Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Group.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Store\Model;
11 
20  \Magento\Framework\DataObject\IdentityInterface,
23 {
24  const ENTITY = 'store_group';
25 
26  const CACHE_TAG = 'store_group';
27 
31  protected $_cacheTag = true;
32 
36  protected $_eventPrefix = 'store_group';
37 
41  protected $_eventObject = 'store_group';
42 
48  protected $_stores;
49 
55  protected $_storeIds = [];
56 
62  protected $_storeCodes = [];
63 
69  protected $_storesCount = 0;
70 
76  protected $_defaultStore;
77 
81  private $_isReadOnly = false;
82 
87 
91  protected $_storeListFactory;
92 
96  protected $_storeManager;
97 
101  private $eventManager;
102 
117  public function __construct(
118  \Magento\Framework\Model\Context $context,
119  \Magento\Framework\Registry $registry,
120  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
121  \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
122  \Magento\Config\Model\ResourceModel\Config\Data $configDataResource,
123  \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory,
125  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
126  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
127  array $data = [],
128  \Magento\Framework\Event\ManagerInterface $eventManager = null
129  ) {
130  $this->_configDataResource = $configDataResource;
131  $this->_storeListFactory = $storeListFactory;
132  $this->_storeManager = $storeManager;
133  $this->eventManager = $eventManager ?: \Magento\Framework\App\ObjectManager::getInstance()
134  ->get(\Magento\Framework\Event\ManagerInterface::class);
135  parent::__construct(
136  $context,
137  $registry,
138  $extensionFactory,
140  $resource,
141  $resourceCollection,
142  $data
143  );
144  }
145 
151  protected function _construct()
152  {
153  $this->_init(\Magento\Store\Model\ResourceModel\Group::class);
154  }
155 
161  protected function _loadStores()
162  {
163  $this->_stores = [];
164  $this->_storesCount = 0;
165  foreach ($this->getStoreCollection() as $store) {
166  $this->_stores[$store->getId()] = $store;
167  $this->_storeIds[$store->getId()] = $store->getId();
168  $this->_storeCodes[$store->getId()] = $store->getCode();
169  if ($this->getDefaultStoreId() == $store->getId()) {
170  $this->_defaultStore = $store;
171  }
172  $this->_storesCount++;
173  }
174  }
175 
182  public function setStores($stores)
183  {
184  $this->_stores = [];
185  $this->_storesCount = 0;
186  foreach ($stores as $store) {
187  $this->_stores[$store->getId()] = $store;
188  $this->_storeIds[$store->getId()] = $store->getId();
189  $this->_storeCodes[$store->getId()] = $store->getCode();
190  if ($this->getDefaultStoreId() == $store->getId()) {
191  $this->_defaultStore = $store;
192  }
193  $this->_storesCount++;
194  }
195  }
196 
202  public function getStoreCollection()
203  {
204  return $this->_storeListFactory->create()->addGroupFilter($this->getId());
205  }
206 
212  public function getStores()
213  {
214  if ($this->_stores === null) {
215  $this->_loadStores();
216  }
217  return $this->_stores;
218  }
219 
225  public function getStoreIds()
226  {
227  if ($this->_stores === null) {
228  $this->_loadStores();
229  }
230  return $this->_storeIds;
231  }
232 
238  public function getStoreCodes()
239  {
240  if ($this->_stores === null) {
241  $this->_loadStores();
242  }
243  return $this->_storeCodes;
244  }
245 
249  public function getStoresCount()
250  {
251  if ($this->_stores === null) {
252  $this->_loadStores();
253  }
254  return $this->_storesCount;
255  }
256 
262  public function getDefaultStore()
263  {
264  if (!$this->hasDefaultStoreId()) {
265  return false;
266  }
267  if ($this->_stores === null) {
268  $this->_loadStores();
269  }
270  return $this->_defaultStore;
271  }
272 
281  public function getDefaultStoreByLocale($locale)
282  {
283  if ($this->getDefaultStore() && $this->getDefaultStore()->getLocaleCode() == $locale) {
284  return $this->getDefaultStore();
285  } else {
286  $stores = $this->getStoresByLocale($locale);
287  if (count($stores)) {
288  return $stores[0];
289  } else {
290  return $this->getDefaultStore() ? $this->getDefaultStore() : null;
291  }
292  }
293  }
294 
301  public function getStoresByLocale($locale)
302  {
303  $stores = [];
304  foreach ($this->getStores() as $store) {
305  /* @var $store \Magento\Store\Model\Store */
306  if ($store->getLocaleCode() == $locale) {
307  $stores[] = $store;
308  }
309  }
310  return $stores;
311  }
312 
319  public function setWebsite(Website $website)
320  {
321  $this->setWebsiteId($website->getId());
322  }
323 
329  public function getWebsite()
330  {
331  if ($this->getWebsiteId() === null) {
332  return false;
333  }
334  return $this->_storeManager->getWebsite($this->getWebsiteId());
335  }
336 
342  public function isCanDelete()
343  {
344  if (!$this->getId()) {
345  return false;
346  }
347 
348  return $this->getWebsite()->getGroupsCount() > 1;
349  }
350 
354  public function getDefaultStoreId()
355  {
356  return $this->_getData('default_store_id');
357  }
358 
362  public function setDefaultStoreId($defaultStoreId)
363  {
364  return $this->setData('default_store_id', $defaultStoreId);
365  }
366 
370  public function getRootCategoryId()
371  {
372  return $this->_getData('root_category_id');
373  }
374 
379  {
380  return $this->setData('root_category_id', $rootCategoryId);
381  }
382 
386  public function getWebsiteId()
387  {
388  return $this->_getData('website_id');
389  }
390 
394  public function setWebsiteId($websiteId)
395  {
396  return $this->setData('website_id', $websiteId);
397  }
398 
402  public function beforeDelete()
403  {
404  $this->_configDataResource->clearScopeData(
406  $this->getStoreIds()
407  );
408  return parent::beforeDelete();
409  }
410 
415  public function afterDelete()
416  {
417  $group = $this;
418  $this->getResource()->addCommitCallback(function () use ($group) {
419  $this->_storeManager->reinitStores();
420  $this->eventManager->dispatch($this->_eventPrefix . '_delete', ['group' => $group]);
421  });
422  $result = parent::afterDelete();
423 
424  if ($this->getId() === $this->getWebsite()->getDefaultGroupId()) {
425  $ids = $this->getWebsite()->getGroupIds();
426  if (!empty($ids) && count($ids) > 1) {
427  unset($ids[$this->getId()]);
428  $defaultId = current($ids);
429  } else {
430  $defaultId = null;
431  }
432  $this->getWebsite()->setDefaultGroupId($defaultId);
433  $this->getWebsite()->save();
434  }
435  return $result;
436  }
437 
442  public function afterSave()
443  {
444  $group = $this;
445  $this->getResource()->addCommitCallback(function () use ($group) {
446  $this->_storeManager->reinitStores();
447  $this->eventManager->dispatch($this->_eventPrefix . '_save', ['group' => $group]);
448  });
449  return parent::afterSave();
450  }
451 
458  public function isReadOnly($value = null)
459  {
460  if (null !== $value) {
461  $this->_isReadOnly = (bool)$value;
462  }
463  return $this->_isReadOnly;
464  }
465 
471  public function getIdentities()
472  {
473  return [self::CACHE_TAG];
474  }
475 
479  public function getName()
480  {
481  return $this->getData('name');
482  }
483 
487  public function setName($name)
488  {
489  return $this->setData('name', $name);
490  }
491 
496  public function getCode()
497  {
498  return $this->getData('code');
499  }
500 
505  public function setCode($code)
506  {
507  return $this->setData('code', $code);
508  }
509 
513  public function getExtensionAttributes()
514  {
515  return $this->_getExtensionAttributes();
516  }
517 
521  public function setExtensionAttributes(
522  \Magento\Store\Api\Data\GroupExtensionInterface $extensionAttributes
523  ) {
524  return $this->_setExtensionAttributes($extensionAttributes);
525  }
526 
531  public function getScopeType()
532  {
534  }
535 
540  public function getScopeTypeName()
541  {
542  return 'Store';
543  }
544 }
isReadOnly($value=null)
Definition: Group.php:458
setWebsite(Website $website)
Definition: Group.php:319
getStoresByLocale($locale)
Definition: Group.php:301
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
$group
Definition: sections.phtml:16
$storeManager
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
setRootCategoryId($rootCategoryId)
Definition: Group.php:378
__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\Store\Model\ResourceModel\Store\CollectionFactory $storeListFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], \Magento\Framework\Event\ManagerInterface $eventManager=null)
Definition: Group.php:117
getDefaultStoreByLocale($locale)
Definition: Group.php:281
setWebsiteId($websiteId)
Definition: Group.php:394
setExtensionAttributes(\Magento\Store\Api\Data\GroupExtensionInterface $extensionAttributes)
Definition: Group.php:521
$code
Definition: info.phtml:12
setDefaultStoreId($defaultStoreId)
Definition: Group.php:362
if(!isset($_GET['name'])) $name
Definition: log.php:14