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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Store\Model\ResourceModel;
/**
* Store group resource model
*
* @api
* @since 100.0.2
*/
class Group extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Define main table
*
* @return void
*/
protected function _construct()
{
$this->_init('store_group', 'group_id');
}
/**
* Update default store group for website
*
* @param \Magento\Framework\Model\AbstractModel $model
* @return $this
*/
protected function _afterSave(\Magento\Framework\Model\AbstractModel $model)
{
$this->_updateStoreWebsite($model->getId(), $model->getWebsiteId());
$this->_updateWebsiteDefaultGroup($model->getWebsiteId(), $model->getId());
$this->_changeWebsite($model);
return $this;
}
/**
* Initialize unique fields
*
* @return $this
* @since 100.2.0
*/
protected function _initUniqueFields()
{
$this->_uniqueFields = [['field' => 'code', 'title' => __('Group with the same code')]];
return $this;
}
/**
* Update default store group for website
*
* @param int $websiteId
* @param int $groupId
* @return $this
*/
protected function _updateWebsiteDefaultGroup($websiteId, $groupId)
{
$select = $this->getConnection()->select()->from(
$this->getMainTable(),
'COUNT(*)'
)->where(
'website_id = :website'
);
$count = $this->getConnection()->fetchOne($select, ['website' => $websiteId]);
if ($count == 1) {
$bind = ['default_group_id' => $groupId];
$where = ['website_id = ?' => $websiteId];
$this->getConnection()->update($this->getTable('store_website'), $bind, $where);
}
return $this;
}
/**
* Change store group website
*
* @param \Magento\Framework\Model\AbstractModel $model
* @return $this
*/
protected function _changeWebsite(\Magento\Framework\Model\AbstractModel $model)
{
if ($model->getOriginalWebsiteId() && $model->getWebsiteId() != $model->getOriginalWebsiteId()) {
$select = $this->getConnection()->select()->from(
$this->getTable('store_website'),
'default_group_id'
)->where(
'website_id = :website_id'
);
$groupId = $this->getConnection()->fetchOne(
$select,
['website_id' => $model->getOriginalWebsiteId()]
);
if ($groupId == $model->getId()) {
$bind = ['default_group_id' => 0];
$where = ['website_id = ?' => $model->getOriginalWebsiteId()];
$this->getConnection()->update($this->getTable('store_website'), $bind, $where);
}
}
return $this;
}
/**
* Update website for stores that assigned to store group
*
* @param int $groupId
* @param int $websiteId
* @return $this
*/
protected function _updateStoreWebsite($groupId, $websiteId)
{
$bind = ['website_id' => $websiteId];
$where = ['group_id = ?' => $groupId];
$this->getConnection()->update($this->getTable('store'), $bind, $where);
return $this;
}
/**
* Save default store for store group
*
* @param int $groupId
* @param int $storeId
* @return $this
*/
protected function _saveDefaultStore($groupId, $storeId)
{
$bind = ['default_store_id' => $storeId];
$where = ['group_id = ?' => $groupId];
$this->getConnection()->update($this->getMainTable(), $bind, $where);
return $this;
}
/**
* Count number of all entities in the system
*
* By default won't count admin store
*
* @param bool $countAdmin
* @return int
*/
public function countAll($countAdmin = false)
{
$connection = $this->getConnection();
$select = $connection->select()->from(['main' => $this->getMainTable()], 'COUNT(*)');
if (!$countAdmin) {
$select->joinLeft(
['store_website' => $this->getTable('store_website')],
'store_website.website_id = main.website_id',
null
)->where(
sprintf('%s <> %s', $connection->quoteIdentifier('code'), $connection->quote('admin'))
);
}
return (int)$connection->fetchOne($select);
}
}