Collection.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\ResourceModel\Group;
  7. /**
  8. * Store group collection
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Define resource model
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->setFlag('load_default_store_group', false);
  23. $this->_init(\Magento\Store\Model\Group::class, \Magento\Store\Model\ResourceModel\Group::class);
  24. }
  25. /**
  26. * Set flag for load default (admin) store
  27. *
  28. * @param boolean $loadDefault
  29. * @return $this
  30. */
  31. public function setLoadDefault($loadDefault)
  32. {
  33. return $this->setFlag('load_default_store_group', (bool)$loadDefault);
  34. }
  35. /**
  36. * Is load default (admin) store
  37. *
  38. * @return boolean
  39. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  40. */
  41. public function getLoadDefault()
  42. {
  43. return $this->getFlag('load_default_store_group');
  44. }
  45. /**
  46. * Add disable default store group filter to collection
  47. *
  48. * @return $this
  49. */
  50. public function setWithoutDefaultFilter()
  51. {
  52. return $this->addFieldToFilter('main_table.group_id', ['gt' => 0]);
  53. }
  54. /**
  55. * Filter to discard stores without views
  56. *
  57. * @return $this
  58. */
  59. public function setWithoutStoreViewFilter()
  60. {
  61. return $this->addFieldToFilter('main_table.default_store_id', ['gt' => 0]);
  62. }
  63. /**
  64. * Filter to discard default group and groups with assigned category
  65. *
  66. * @return $this
  67. * @since 100.2.0
  68. */
  69. public function setWithoutAssignedCategoryFilter()
  70. {
  71. return $this->addFieldToFilter('main_table.root_category_id', ['eq' => 0])
  72. ->addFieldToFilter('main_table.group_id', ['neq' => 0]);
  73. }
  74. /**
  75. * Load collection data
  76. *
  77. * @return $this
  78. */
  79. public function _beforeLoad()
  80. {
  81. if (!$this->getLoadDefault()) {
  82. $this->setWithoutDefaultFilter();
  83. }
  84. $this->addOrder('main_table.name', self::SORT_ORDER_ASC);
  85. return parent::_beforeLoad();
  86. }
  87. /**
  88. * Convert collection items to array for select options
  89. *
  90. * @return array
  91. */
  92. public function toOptionArray()
  93. {
  94. return $this->_toOptionArray('group_id', 'name');
  95. }
  96. /**
  97. * Add filter by website to collection
  98. *
  99. * @param int|array $website
  100. * @return $this
  101. */
  102. public function addWebsiteFilter($website)
  103. {
  104. return $this->addFieldToFilter('main_table.website_id', ['in' => $website]);
  105. }
  106. }