Group.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\ResourceModel;
  7. /**
  8. * Store group resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Group extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Define main table
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('store_group', 'group_id');
  23. }
  24. /**
  25. * Update default store group for website
  26. *
  27. * @param \Magento\Framework\Model\AbstractModel $model
  28. * @return $this
  29. */
  30. protected function _afterSave(\Magento\Framework\Model\AbstractModel $model)
  31. {
  32. $this->_updateStoreWebsite($model->getId(), $model->getWebsiteId());
  33. $this->_updateWebsiteDefaultGroup($model->getWebsiteId(), $model->getId());
  34. $this->_changeWebsite($model);
  35. return $this;
  36. }
  37. /**
  38. * Initialize unique fields
  39. *
  40. * @return $this
  41. * @since 100.2.0
  42. */
  43. protected function _initUniqueFields()
  44. {
  45. $this->_uniqueFields = [['field' => 'code', 'title' => __('Group with the same code')]];
  46. return $this;
  47. }
  48. /**
  49. * Update default store group for website
  50. *
  51. * @param int $websiteId
  52. * @param int $groupId
  53. * @return $this
  54. */
  55. protected function _updateWebsiteDefaultGroup($websiteId, $groupId)
  56. {
  57. $select = $this->getConnection()->select()->from(
  58. $this->getMainTable(),
  59. 'COUNT(*)'
  60. )->where(
  61. 'website_id = :website'
  62. );
  63. $count = $this->getConnection()->fetchOne($select, ['website' => $websiteId]);
  64. if ($count == 1) {
  65. $bind = ['default_group_id' => $groupId];
  66. $where = ['website_id = ?' => $websiteId];
  67. $this->getConnection()->update($this->getTable('store_website'), $bind, $where);
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Change store group website
  73. *
  74. * @param \Magento\Framework\Model\AbstractModel $model
  75. * @return $this
  76. */
  77. protected function _changeWebsite(\Magento\Framework\Model\AbstractModel $model)
  78. {
  79. if ($model->getOriginalWebsiteId() && $model->getWebsiteId() != $model->getOriginalWebsiteId()) {
  80. $select = $this->getConnection()->select()->from(
  81. $this->getTable('store_website'),
  82. 'default_group_id'
  83. )->where(
  84. 'website_id = :website_id'
  85. );
  86. $groupId = $this->getConnection()->fetchOne(
  87. $select,
  88. ['website_id' => $model->getOriginalWebsiteId()]
  89. );
  90. if ($groupId == $model->getId()) {
  91. $bind = ['default_group_id' => 0];
  92. $where = ['website_id = ?' => $model->getOriginalWebsiteId()];
  93. $this->getConnection()->update($this->getTable('store_website'), $bind, $where);
  94. }
  95. }
  96. return $this;
  97. }
  98. /**
  99. * Update website for stores that assigned to store group
  100. *
  101. * @param int $groupId
  102. * @param int $websiteId
  103. * @return $this
  104. */
  105. protected function _updateStoreWebsite($groupId, $websiteId)
  106. {
  107. $bind = ['website_id' => $websiteId];
  108. $where = ['group_id = ?' => $groupId];
  109. $this->getConnection()->update($this->getTable('store'), $bind, $where);
  110. return $this;
  111. }
  112. /**
  113. * Save default store for store group
  114. *
  115. * @param int $groupId
  116. * @param int $storeId
  117. * @return $this
  118. */
  119. protected function _saveDefaultStore($groupId, $storeId)
  120. {
  121. $bind = ['default_store_id' => $storeId];
  122. $where = ['group_id = ?' => $groupId];
  123. $this->getConnection()->update($this->getMainTable(), $bind, $where);
  124. return $this;
  125. }
  126. /**
  127. * Count number of all entities in the system
  128. *
  129. * By default won't count admin store
  130. *
  131. * @param bool $countAdmin
  132. * @return int
  133. */
  134. public function countAll($countAdmin = false)
  135. {
  136. $connection = $this->getConnection();
  137. $select = $connection->select()->from(['main' => $this->getMainTable()], 'COUNT(*)');
  138. if (!$countAdmin) {
  139. $select->joinLeft(
  140. ['store_website' => $this->getTable('store_website')],
  141. 'store_website.website_id = main.website_id',
  142. null
  143. )->where(
  144. sprintf('%s <> %s', $connection->quoteIdentifier('code'), $connection->quote('admin'))
  145. );
  146. }
  147. return (int)$connection->fetchOne($select);
  148. }
  149. }