Website.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * Website Resource Model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Website 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_website', 'website_id');
  23. }
  24. /**
  25. * Initialize unique fields
  26. *
  27. * @return $this
  28. */
  29. protected function _initUniqueFields()
  30. {
  31. $this->_uniqueFields = [['field' => 'code', 'title' => __('Website with the same code')]];
  32. return $this;
  33. }
  34. /**
  35. * Read all information about websites.
  36. *
  37. * Convert information to next format:
  38. * [website_code => [website_data (website_id, code, name, etc...)]]
  39. *
  40. * @return array
  41. * @since 100.1.3
  42. */
  43. public function readAllWebsites()
  44. {
  45. $websites = [];
  46. $select = $this->getConnection()
  47. ->select()
  48. ->from($this->getTable('store_website'));
  49. foreach ($this->getConnection()->fetchAll($select) as $websiteData) {
  50. $websites[$websiteData['code']] = $websiteData;
  51. }
  52. return $websites;
  53. }
  54. /**
  55. * Validate website code before object save
  56. *
  57. * @param \Magento\Framework\Model\AbstractModel $object
  58. * @return $this
  59. * @throws \Magento\Framework\Exception\LocalizedException
  60. */
  61. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  62. {
  63. if (!preg_match('/^[a-z]+[a-z0-9_]*$/i', $object->getCode())) {
  64. throw new \Magento\Framework\Exception\LocalizedException(
  65. __(
  66. 'Website code may only contain letters (a-z), numbers (0-9) or underscore (_),'
  67. . ' and the first character must be a letter.'
  68. )
  69. );
  70. }
  71. return parent::_beforeSave($object);
  72. }
  73. /**
  74. * Perform actions after object save
  75. *
  76. * @param \Magento\Framework\Model\AbstractModel $object
  77. * @return $this
  78. */
  79. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  80. {
  81. if ($object->getIsDefault()) {
  82. $this->getConnection()->update($this->getMainTable(), ['is_default' => 0]);
  83. $where = ['website_id = ?' => $object->getId()];
  84. $this->getConnection()->update($this->getMainTable(), ['is_default' => 1], $where);
  85. }
  86. return parent::_afterSave($object);
  87. }
  88. /**
  89. * Remove configuration data after delete website
  90. *
  91. * @param \Magento\Framework\Model\AbstractModel $model
  92. * @return $this
  93. */
  94. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $model)
  95. {
  96. $where = [
  97. 'scope = ?' => \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITES,
  98. 'scope_id = ?' => $model->getWebsiteId(),
  99. ];
  100. $this->getConnection()->delete($this->getTable('core_config_data'), $where);
  101. return $this;
  102. }
  103. /**
  104. * Retrieve default stores select object
  105. * Select fields website_id, store_id
  106. *
  107. * @param bool $includeDefault include/exclude default admin website
  108. * @return \Magento\Framework\DB\Select
  109. */
  110. public function getDefaultStoresSelect($includeDefault = false)
  111. {
  112. $ifNull = $this->getConnection()->getCheckSql(
  113. 'store_group_table.default_store_id IS NULL',
  114. '0',
  115. 'store_group_table.default_store_id'
  116. );
  117. $select = $this->getConnection()->select()->from(
  118. ['website_table' => $this->getTable('store_website')],
  119. ['website_id']
  120. )->joinLeft(
  121. ['store_group_table' => $this->getTable('store_group')],
  122. 'website_table.website_id=store_group_table.website_id' .
  123. ' AND website_table.default_group_id = store_group_table.group_id',
  124. ['store_id' => $ifNull]
  125. );
  126. if (!$includeDefault) {
  127. $select->where('website_table.website_id <> ?', 0);
  128. }
  129. return $select;
  130. }
  131. /**
  132. * Get total number of persistent entities in the system, excluding the admin website by default
  133. *
  134. * @param bool $includeDefault
  135. * @return int
  136. */
  137. public function countAll($includeDefault = false)
  138. {
  139. $connection = $this->getConnection();
  140. $select = $connection->select()->from($this->getMainTable(), 'COUNT(*)');
  141. if (!$includeDefault) {
  142. $select->where('website_id <> ?', 0);
  143. }
  144. return (int)$connection->fetchOne($select);
  145. }
  146. }