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