Collection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\ResourceModel\Website;
  7. /**
  8. * Websites collection
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Map field to alias
  17. *
  18. * @var array
  19. */
  20. protected $_map = ['fields' => ['website_id' => 'main_table.website_id']];
  21. /**
  22. * Define resource model
  23. *
  24. * @return void
  25. */
  26. protected function _construct()
  27. {
  28. $this->setFlag('load_default_website', false);
  29. $this->_init(\Magento\Store\Model\Website::class, \Magento\Store\Model\ResourceModel\Website::class);
  30. }
  31. /**
  32. * Apply custom filtering
  33. *
  34. * @return void
  35. */
  36. protected function _renderFiltersBefore()
  37. {
  38. if (!$this->getLoadDefault()) {
  39. $this->getSelect()->where('main_table.website_id > ?', 0);
  40. }
  41. parent::_renderFiltersBefore();
  42. }
  43. /**
  44. * Set flag for load default (admin) website
  45. *
  46. * @param bool $loadDefault
  47. * @return $this
  48. */
  49. public function setLoadDefault($loadDefault)
  50. {
  51. $this->setFlag('load_default_website', (bool)$loadDefault);
  52. return $this;
  53. }
  54. /**
  55. * Is load default (admin) website
  56. *
  57. * @return bool
  58. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  59. */
  60. public function getLoadDefault()
  61. {
  62. return $this->getFlag('load_default_website');
  63. }
  64. /**
  65. * Convert items array to array for select options
  66. *
  67. * @return array
  68. */
  69. public function toOptionArray()
  70. {
  71. return $this->_toOptionArray('website_id', 'name');
  72. }
  73. /**
  74. * Convert items array to hash for select options
  75. *
  76. * @return array
  77. */
  78. public function toOptionHash()
  79. {
  80. return $this->_toOptionHash('website_id', 'name');
  81. }
  82. /**
  83. * Add website filter to collection
  84. *
  85. * @param int $ids|array
  86. * @return $this
  87. */
  88. public function addIdFilter($ids)
  89. {
  90. if (is_array($ids)) {
  91. if (empty($ids)) {
  92. $this->addFieldToFilter('website_id', null);
  93. } else {
  94. $this->addFieldToFilter('website_id', ['in' => $ids]);
  95. }
  96. } else {
  97. $this->addFieldToFilter('website_id', $ids);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Load collection data
  103. *
  104. * @param boolean $printQuery
  105. * @param boolean $logQuery
  106. * @return $this
  107. */
  108. public function load($printQuery = false, $logQuery = false)
  109. {
  110. $this->unshiftOrder('main_table.name', \Magento\Framework\DB\Select::SQL_ASC) // website name SECOND
  111. ->unshiftOrder('main_table.sort_order', \Magento\Framework\DB\Select::SQL_ASC); // website sort order FIRST
  112. return parent::load($printQuery, $logQuery);
  113. }
  114. /**
  115. * Join group and store info from appropriate tables.
  116. * Defines new _idFiledName as 'website_group_store' bc for
  117. * one website can be more then one row in collection.
  118. * Sets extra combined ordering by group's name, defined
  119. * sort ordering and store's name.
  120. *
  121. * @return $this
  122. */
  123. public function joinGroupAndStore()
  124. {
  125. if (!$this->getFlag('groups_and_stores_joined')) {
  126. $this->_idFieldName = 'website_group_store';
  127. $this->getSelect()->joinLeft(
  128. ['group_table' => $this->getTable('store_group')],
  129. 'main_table.website_id = group_table.website_id',
  130. ['group_id' => 'group_id', 'group_title' => 'name', 'group_code' => 'code']
  131. )->joinLeft(
  132. ['store_table' => $this->getTable('store')],
  133. 'group_table.group_id = store_table.group_id',
  134. ['store_id' => 'store_id', 'store_title' => 'name', 'store_code' => 'code']
  135. );
  136. $this->addOrder('group_table.name', \Magento\Framework\DB\Select::SQL_ASC) // store name
  137. ->addOrder(
  138. 'CASE WHEN store_table.store_id = 0 THEN 0 ELSE 1 END',
  139. \Magento\Framework\DB\Select::SQL_ASC
  140. ) // view is admin
  141. ->addOrder('store_table.sort_order', \Magento\Framework\DB\Select::SQL_ASC) // view sort order
  142. ->addOrder('store_table.name', \Magento\Framework\DB\Select::SQL_ASC) // view name
  143. ;
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Adding filter by group id or array of ids but only if
  149. * tables with appropriate information were joined before.
  150. *
  151. * @param int|array $groupIds
  152. * @return $this
  153. */
  154. public function addFilterByGroupIds($groupIds)
  155. {
  156. if ($this->getFlag('groups_and_stores_joined')) {
  157. $this->addFieldToFilter('group_table.group_id', $groupIds);
  158. }
  159. return $this;
  160. }
  161. }