Group.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
  7. use Magento\Eav\Model\Entity\Attribute\AttributeGroupAlreadyExistsException;
  8. use Magento\Eav\Model\ResourceModel\Entity\Attribute;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\DB\Adapter\DuplicateException;
  11. use Magento\Framework\Model\AbstractModel;
  12. /**
  13. * Eav Resource Entity Attribute Group
  14. */
  15. class Group extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  16. {
  17. /**
  18. * @var Attribute
  19. */
  20. private $attributeResource;
  21. /**
  22. * @inheritDoc
  23. */
  24. public function __construct(
  25. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  26. $connectionName = null,
  27. Attribute $attributeResource = null
  28. ) {
  29. parent::__construct($context, $connectionName);
  30. $this->attributeResource = $attributeResource ?: ObjectManager::getInstance()->get(Attribute::class);
  31. }
  32. /**
  33. * Resource initialization
  34. *
  35. * @return void
  36. * @codeCoverageIgnore
  37. */
  38. protected function _construct()
  39. {
  40. $this->_init('eav_attribute_group', 'attribute_group_id');
  41. }
  42. /**
  43. * Checks if attribute group exists
  44. *
  45. * @param \Magento\Eav\Model\Entity\Attribute\Group $object
  46. * @return bool
  47. */
  48. public function itemExists($object)
  49. {
  50. $connection = $this->getConnection();
  51. $bind = [
  52. 'attribute_set_id' => $object->getAttributeSetId(),
  53. 'attribute_group_name' => $object->getAttributeGroupName(),
  54. ];
  55. $select = $connection->select()->from(
  56. $this->getMainTable()
  57. )->where(
  58. 'attribute_set_id = :attribute_set_id'
  59. )->where(
  60. 'attribute_group_name = :attribute_group_name'
  61. );
  62. return $connection->fetchRow($select, $bind) > 0;
  63. }
  64. /**
  65. * Perform actions before object save
  66. *
  67. * @param AbstractModel $object
  68. * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  69. */
  70. protected function _beforeSave(AbstractModel $object)
  71. {
  72. if (!$object->getSortOrder()) {
  73. $object->setSortOrder($this->_getMaxSortOrder($object) + 1);
  74. }
  75. return parent::_beforeSave($object);
  76. }
  77. /**
  78. * Perform actions after object save
  79. *
  80. * @param AbstractModel $object
  81. * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  82. */
  83. protected function _afterSave(AbstractModel $object)
  84. {
  85. if ($object->getAttributes()) {
  86. foreach ($object->getAttributes() as $attribute) {
  87. /** @var $attribute \Magento\Eav\Api\Data\AttributeInterface */
  88. $attribute->setAttributeGroupId($object->getId());
  89. $this->attributeResource->saveInSetIncluding(
  90. $attribute
  91. );
  92. }
  93. }
  94. return parent::_afterSave($object);
  95. }
  96. /**
  97. * Retrieve max sort order
  98. *
  99. * @param AbstractModel $object
  100. * @return int
  101. */
  102. protected function _getMaxSortOrder($object)
  103. {
  104. $connection = $this->getConnection();
  105. $bind = [':attribute_set_id' => $object->getAttributeSetId()];
  106. $select = $connection->select()->from(
  107. $this->getMainTable(),
  108. new \Zend_Db_Expr("MAX(sort_order)")
  109. )->where(
  110. 'attribute_set_id = :attribute_set_id'
  111. );
  112. return $connection->fetchOne($select, $bind);
  113. }
  114. /**
  115. * Set any group default if old one was removed
  116. *
  117. * @param integer $attributeSetId
  118. * @return $this
  119. */
  120. public function updateDefaultGroup($attributeSetId)
  121. {
  122. $connection = $this->getConnection();
  123. $bind = [':attribute_set_id' => $attributeSetId];
  124. $select = $connection->select()->from(
  125. $this->getMainTable(),
  126. $this->getIdFieldName()
  127. )->where(
  128. 'attribute_set_id = :attribute_set_id'
  129. )->order(
  130. 'default_id ' . \Magento\Framework\Data\Collection::SORT_ORDER_DESC
  131. )->limit(
  132. 1
  133. );
  134. $groupId = $connection->fetchOne($select, $bind);
  135. if ($groupId) {
  136. $data = ['default_id' => 1];
  137. $where = ['attribute_group_id =?' => $groupId];
  138. $connection->update($this->getMainTable(), $data, $where);
  139. }
  140. return $this;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. protected function saveNewObject(AbstractModel $object)
  146. {
  147. try {
  148. return parent::saveNewObject($object);
  149. } catch (DuplicateException $e) {
  150. throw new AttributeGroupAlreadyExistsException(
  151. __(
  152. 'Attribute group with same code already exist. Please rename "%1" group',
  153. $object->getAttributeGroupName()
  154. )
  155. );
  156. }
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. protected function updateObject(AbstractModel $object)
  162. {
  163. try {
  164. return parent::updateObject($object);
  165. } catch (DuplicateException $e) {
  166. throw new AttributeGroupAlreadyExistsException(
  167. __(
  168. 'Attribute group with same code already exist. Please rename "%1" group',
  169. $object->getAttributeGroupName()
  170. )
  171. );
  172. }
  173. }
  174. }