Group.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel;
  7. use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
  8. use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
  9. /**
  10. * Customer group resource model
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class Group extends \Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb
  15. {
  16. /**
  17. * Group Management
  18. *
  19. * @var \Magento\Customer\Api\GroupManagementInterface
  20. */
  21. protected $_groupManagement;
  22. /**
  23. * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
  24. */
  25. protected $_customersFactory;
  26. /**
  27. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  28. * @param Snapshot $entitySnapshot
  29. * @param RelationComposite $entityRelationComposite
  30. * @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
  31. * @param Customer\CollectionFactory $customersFactory
  32. * @param string $connectionName
  33. */
  34. public function __construct(
  35. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  36. Snapshot $entitySnapshot,
  37. RelationComposite $entityRelationComposite,
  38. \Magento\Customer\Api\GroupManagementInterface $groupManagement,
  39. \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customersFactory,
  40. $connectionName = null
  41. ) {
  42. $this->_groupManagement = $groupManagement;
  43. $this->_customersFactory = $customersFactory;
  44. parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
  45. }
  46. /**
  47. * Resource initialization
  48. *
  49. * @return void
  50. */
  51. protected function _construct()
  52. {
  53. $this->_init('customer_group', 'customer_group_id');
  54. }
  55. /**
  56. * Initialize unique fields
  57. *
  58. * @return $this
  59. */
  60. protected function _initUniqueFields()
  61. {
  62. $this->_uniqueFields = [['field' => 'customer_group_code', 'title' => __('Customer Group')]];
  63. return $this;
  64. }
  65. /**
  66. * Check if group uses as default
  67. *
  68. * @param \Magento\Framework\Model\AbstractModel $group
  69. * @return $this
  70. * @throws \Magento\Framework\Exception\LocalizedException
  71. */
  72. protected function _beforeDelete(\Magento\Framework\Model\AbstractModel $group)
  73. {
  74. if ($group->usesAsDefault()) {
  75. throw new \Magento\Framework\Exception\LocalizedException(
  76. __('You can\'t delete group "%1".', $group->getCode())
  77. );
  78. }
  79. return parent::_beforeDelete($group);
  80. }
  81. /**
  82. * Method set default group id to the customers collection
  83. *
  84. * @param \Magento\Framework\Model\AbstractModel $group
  85. * @return $this
  86. */
  87. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $group)
  88. {
  89. $customerCollection = $this->_createCustomersCollection()->addAttributeToFilter(
  90. 'group_id',
  91. $group->getId()
  92. )->load();
  93. foreach ($customerCollection as $customer) {
  94. /** @var $customer \Magento\Customer\Model\Customer */
  95. $customer->load($customer->getId());
  96. $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStoreId())->getId();
  97. $customer->setGroupId($defaultGroupId);
  98. $customer->save();
  99. }
  100. return parent::_afterDelete($group);
  101. }
  102. /**
  103. * Create customers collection.
  104. *
  105. * @return \Magento\Customer\Model\ResourceModel\Customer\Collection
  106. */
  107. protected function _createCustomersCollection()
  108. {
  109. return $this->_customersFactory->create();
  110. }
  111. /**
  112. * Prepare data before save
  113. *
  114. * @param \Magento\Framework\Model\AbstractModel $group
  115. * @return $this
  116. */
  117. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $group)
  118. {
  119. /** @var \Magento\Customer\Model\Group $group */
  120. $group->setCode(substr($group->getCode(), 0, $group::GROUP_CODE_MAX_LENGTH));
  121. return parent::_beforeSave($group);
  122. }
  123. /**
  124. * @inheritdoc
  125. */
  126. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  127. {
  128. if ($object->getId() == \Magento\Customer\Model\Group::CUST_GROUP_ALL) {
  129. $this->skipReservedId($object);
  130. }
  131. return $this;
  132. }
  133. /**
  134. * Here we do not allow to save systems reserved ID.
  135. *
  136. * @param \Magento\Framework\Model\AbstractModel $object
  137. * @throws \Magento\Framework\Exception\LocalizedException
  138. * @return void
  139. */
  140. private function skipReservedId(\Magento\Framework\Model\AbstractModel $object)
  141. {
  142. $tableFieldsWithoutIdField = $this->getTableFieldsWithoutIdField();
  143. $select = $this->getConnection()->select();
  144. $select->from(
  145. [$this->getMainTable()],
  146. $tableFieldsWithoutIdField
  147. )
  148. ->where('customer_group_id = ?', \Magento\Customer\Model\Group::CUST_GROUP_ALL);
  149. $query = $this->getConnection()->insertFromSelect(
  150. $select,
  151. $this->getMainTable(),
  152. $tableFieldsWithoutIdField
  153. );
  154. $this->getConnection()->query($query);
  155. $lastInsertId = $this->getConnection()->lastInsertId();
  156. $query = $this->getConnection()->deleteFromSelect(
  157. $select,
  158. $this->getMainTable()
  159. );
  160. $this->getConnection()->query($query);
  161. $object->setId($lastInsertId);
  162. }
  163. /**
  164. * Get main table fields except of ID field.
  165. *
  166. * @return array
  167. */
  168. private function getTableFieldsWithoutIdField()
  169. {
  170. $fields = $this->getConnection()->describeTable($this->getMainTable());
  171. if (isset($fields['customer_group_id'])) {
  172. unset($fields['customer_group_id']);
  173. }
  174. return array_keys($fields);
  175. }
  176. }