Collection.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\ResourceModel\Store;
  7. /**
  8. * Stores collection
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Name prefix of events that are dispatched by model
  17. *
  18. * @var string
  19. */
  20. protected $_eventPrefix = 'store_collection';
  21. /**
  22. * Name of event parameter
  23. *
  24. * @var string
  25. */
  26. protected $_eventObject = 'store_collection';
  27. /**
  28. * Define resource model
  29. *
  30. * @return void
  31. */
  32. protected function _construct()
  33. {
  34. $this->setFlag('load_default_store', false);
  35. $this->_init(\Magento\Store\Model\Store::class, \Magento\Store\Model\ResourceModel\Store::class);
  36. }
  37. /**
  38. * Set flag for load default (admin) store
  39. *
  40. * @param bool $loadDefault
  41. * @return $this
  42. */
  43. public function setLoadDefault($loadDefault)
  44. {
  45. $this->setFlag('load_default_store', (bool)$loadDefault);
  46. return $this;
  47. }
  48. /**
  49. * Is load default (admin) store
  50. *
  51. * @return bool
  52. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  53. */
  54. public function getLoadDefault()
  55. {
  56. return $this->getFlag('load_default_store');
  57. }
  58. /**
  59. * Add disable default store filter to collection
  60. *
  61. * @return $this
  62. */
  63. public function setWithoutDefaultFilter()
  64. {
  65. $this->addFieldToFilter('main_table.store_id', ['gt' => 0]);
  66. return $this;
  67. }
  68. /**
  69. * Add filter by group id.
  70. * Group id can be passed as one single value or array of values.
  71. *
  72. * @param int|array $groupId
  73. * @return $this
  74. */
  75. public function addGroupFilter($groupId)
  76. {
  77. return $this->addFieldToFilter('main_table.group_id', ['in' => $groupId]);
  78. }
  79. /**
  80. * Add filter by status to collection
  81. *
  82. * @param int $status
  83. * @return $this
  84. * @since 100.1.0
  85. */
  86. public function addStatusFilter($status)
  87. {
  88. return $this->addFieldToFilter('main_table.is_active', ['eq' => $status]);
  89. }
  90. /**
  91. * Add store id(s) filter to collection
  92. *
  93. * @param int|array $store
  94. * @return $this
  95. */
  96. public function addIdFilter($store)
  97. {
  98. return $this->addFieldToFilter('main_table.store_id', ['in' => $store]);
  99. }
  100. /**
  101. * Add filter by website to collection
  102. *
  103. * @param int|array $website
  104. * @return $this
  105. */
  106. public function addWebsiteFilter($website)
  107. {
  108. return $this->addFieldToFilter('main_table.website_id', ['in' => $website]);
  109. }
  110. /**
  111. * Add root category id filter to store collection
  112. *
  113. * @param int|array $category
  114. * @return $this
  115. */
  116. public function addCategoryFilter($category)
  117. {
  118. if (!is_array($category)) {
  119. $category = [$category];
  120. }
  121. return $this->loadByCategoryIds($category);
  122. }
  123. /**
  124. * Convert items array to array for select options
  125. *
  126. * @return array
  127. */
  128. public function toOptionArray()
  129. {
  130. return $this->_toOptionArray('store_id', 'name');
  131. }
  132. /**
  133. * Convert items array to hash for select options
  134. *
  135. * @return array
  136. */
  137. public function toOptionHash()
  138. {
  139. return $this->_toOptionHash('store_id', 'name');
  140. }
  141. /**
  142. * Load collection data
  143. *
  144. * @param bool $printQuery
  145. * @param bool $logQuery
  146. * @return $this
  147. */
  148. public function load($printQuery = false, $logQuery = false)
  149. {
  150. if (!$this->getLoadDefault()) {
  151. $this->setWithoutDefaultFilter();
  152. }
  153. if (!$this->isLoaded()) {
  154. $this->addOrder(
  155. 'CASE WHEN main_table.store_id = 0 THEN 0 ELSE 1 END',
  156. \Magento\Framework\DB\Select::SQL_ASC
  157. )->addOrder(
  158. 'main_table.sort_order',
  159. \Magento\Framework\DB\Select::SQL_ASC
  160. )->addOrder(
  161. 'main_table.name',
  162. \Magento\Framework\DB\Select::SQL_ASC
  163. );
  164. }
  165. return parent::load($printQuery, $logQuery);
  166. }
  167. /**
  168. * Add root category id filter to store collection
  169. *
  170. * @param array $categories
  171. * @return $this
  172. */
  173. public function loadByCategoryIds(array $categories)
  174. {
  175. $this->addRootCategoryIdAttribute();
  176. $this->addFieldToFilter('group_table.root_category_id', ['in' => $categories]);
  177. return $this;
  178. }
  179. /**
  180. * Add store root category data to collection
  181. *
  182. * @return $this
  183. */
  184. public function addRootCategoryIdAttribute()
  185. {
  186. if (!$this->getFlag('store_group_table_joined')) {
  187. $this->getSelect()->join(
  188. ['group_table' => $this->getTable('store_group')],
  189. 'main_table.group_id = group_table.group_id',
  190. ['root_category_id']
  191. );
  192. $this->setFlag('store_group_table_joined', true);
  193. }
  194. return $this;
  195. }
  196. }