-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathGroup.php
191 lines (174 loc) · 5.29 KB
/
Group.php
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
<?php
/**
* Copyright 2015 Adobe
* All Rights Reserved.
*/
namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
use Magento\Eav\Model\Entity\Attribute\AttributeGroupAlreadyExistsException;
use Magento\Eav\Model\ResourceModel\Entity\Attribute;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DB\Adapter\DuplicateException;
use Magento\Framework\Model\AbstractModel;
/**
* Eav Resource Entity Attribute Group
*/
class Group extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* @var Attribute
*/
private $attributeResource;
/**
* @inheritDoc
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
$connectionName = null,
?Attribute $attributeResource = null
) {
parent::__construct($context, $connectionName);
$this->attributeResource = $attributeResource ?: ObjectManager::getInstance()->get(Attribute::class);
}
/**
* Resource initialization
*
* @return void
* @codeCoverageIgnore
*/
protected function _construct()
{
$this->_init('eav_attribute_group', 'attribute_group_id');
}
/**
* Checks if attribute group exists
*
* @param \Magento\Eav\Model\Entity\Attribute\Group $object
* @return bool
*/
public function itemExists($object)
{
$connection = $this->getConnection();
$bind = [
'attribute_set_id' => $object->getAttributeSetId(),
'attribute_group_name' => $object->getAttributeGroupName(),
];
$select = $connection->select()->from(
$this->getMainTable()
)->where(
'attribute_set_id = :attribute_set_id'
)->where(
'attribute_group_name = :attribute_group_name'
);
return !empty($connection->fetchRow($select, $bind));
}
/**
* Perform actions before object save
*
* @param AbstractModel $object
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
protected function _beforeSave(AbstractModel $object)
{
if (!$object->getSortOrder()) {
$object->setSortOrder($this->_getMaxSortOrder($object) + 1);
}
return parent::_beforeSave($object);
}
/**
* Perform actions after object save
*
* @param AbstractModel $object
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
protected function _afterSave(AbstractModel $object)
{
if ($object->getAttributes()) {
foreach ($object->getAttributes() as $attribute) {
/** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
$attribute->setAttributeGroupId($object->getId());
$this->attributeResource->saveInSetIncluding(
$attribute
);
}
}
return parent::_afterSave($object);
}
/**
* Retrieve max sort order
*
* @param AbstractModel $object
* @return int
*/
protected function _getMaxSortOrder($object)
{
$connection = $this->getConnection();
$bind = [':attribute_set_id' => $object->getAttributeSetId()];
$select = $connection->select()->from(
$this->getMainTable(),
new \Zend_Db_Expr("MAX(sort_order)")
)->where(
'attribute_set_id = :attribute_set_id'
);
return $connection->fetchOne($select, $bind);
}
/**
* Set any group default if old one was removed
*
* @param integer $attributeSetId
* @return $this
*/
public function updateDefaultGroup($attributeSetId)
{
$connection = $this->getConnection();
$bind = [':attribute_set_id' => $attributeSetId];
$select = $connection->select()->from(
$this->getMainTable(),
$this->getIdFieldName()
)->where(
'attribute_set_id = :attribute_set_id'
)->order(
'default_id ' . \Magento\Framework\Data\Collection::SORT_ORDER_DESC
)->limit(
1
);
$groupId = $connection->fetchOne($select, $bind);
if ($groupId) {
$data = ['default_id' => 1];
$where = ['attribute_group_id =?' => $groupId];
$connection->update($this->getMainTable(), $data, $where);
}
return $this;
}
/**
* @inheritdoc
*/
protected function saveNewObject(AbstractModel $object)
{
try {
parent::saveNewObject($object);
} catch (DuplicateException $e) {
throw new AttributeGroupAlreadyExistsException(
__(
'Attribute group with same code already exist. Please rename "%1" group',
$object->getAttributeGroupName()
)
);
}
}
/**
* @inheritdoc
*/
protected function updateObject(AbstractModel $object)
{
try {
parent::updateObject($object);
} catch (DuplicateException $e) {
throw new AttributeGroupAlreadyExistsException(
__(
'Attribute group with same code already exist. Please rename "%1" group',
$object->getAttributeGroupName()
)
);
}
}
}