AbstractCollection.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Model\ResourceModel\Rule\Collection;
  7. /**
  8. * Abstract Rule entity resource collection model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  14. {
  15. /**
  16. * Store associated with rule entities information map
  17. *
  18. * Example:
  19. * array(
  20. * 'entity_type1' => array(
  21. * 'associations_table' => 'table_name',
  22. * 'rule_id_field' => 'rule_id',
  23. * 'entity_id_field' => 'entity_id'
  24. * ),
  25. * 'entity_type2' => array(
  26. * 'associations_table' => 'table_name',
  27. * 'rule_id_field' => 'rule_id',
  28. * 'entity_id_field' => 'entity_id'
  29. * )
  30. * ....
  31. * )
  32. *
  33. * @var array
  34. */
  35. protected $_associatedEntitiesMap = [];
  36. /**
  37. * Add website ids to rules data
  38. *
  39. * @return $this
  40. */
  41. protected function _afterLoad()
  42. {
  43. parent::_afterLoad();
  44. if ($this->getFlag('add_websites_to_result') && $this->_items) {
  45. /** @var \Magento\Rule\Model\AbstractModel $item */
  46. foreach ($this->_items as $item) {
  47. $item->afterLoad();
  48. }
  49. }
  50. return $this;
  51. }
  52. /**
  53. * Init flag for adding rule website ids to collection result
  54. *
  55. * @param bool|null $flag
  56. * @return $this
  57. */
  58. public function addWebsitesToResult($flag = null)
  59. {
  60. $flag = $flag === null ? true : $flag;
  61. $this->setFlag('add_websites_to_result', $flag);
  62. return $this;
  63. }
  64. /**
  65. * Limit rules collection by specific websites
  66. *
  67. * @param int|int[]|\Magento\Store\Model\Website $websiteId
  68. * @return $this
  69. */
  70. public function addWebsiteFilter($websiteId)
  71. {
  72. if (!$this->getFlag('is_website_table_joined')) {
  73. $websiteIds = is_array($websiteId) ? $websiteId : [$websiteId];
  74. $entityInfo = $this->_getAssociatedEntityInfo('website');
  75. $this->setFlag('is_website_table_joined', true);
  76. foreach ($websiteIds as $index => $website) {
  77. if ($website instanceof \Magento\Store\Model\Website) {
  78. $websiteIds[$index] = $website->getId();
  79. }
  80. }
  81. $this->getSelect()->join(
  82. ['website' => $this->getTable($entityInfo['associations_table'])],
  83. $this->getConnection()->quoteInto('website.' . $entityInfo['entity_id_field'] . ' IN (?)', $websiteIds)
  84. . ' AND main_table.' . $entityInfo['rule_id_field'] . ' = website.' . $entityInfo['rule_id_field'],
  85. []
  86. );
  87. }
  88. return $this;
  89. }
  90. /**
  91. * Provide support for website id filter
  92. *
  93. * @param string $field
  94. * @param null|string|array $condition
  95. * @return $this
  96. */
  97. public function addFieldToFilter($field, $condition = null)
  98. {
  99. if ($field == 'website_ids') {
  100. return $this->addWebsiteFilter($condition);
  101. }
  102. parent::addFieldToFilter($field, $condition);
  103. return $this;
  104. }
  105. /**
  106. * Filter collection to only active or inactive rules
  107. *
  108. * @param int $isActive
  109. * @return $this
  110. */
  111. public function addIsActiveFilter($isActive = 1)
  112. {
  113. if (!$this->getFlag('is_active_filter')) {
  114. $this->addFieldToFilter('is_active', (int)$isActive ? 1 : 0);
  115. $this->setFlag('is_active_filter', true);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Retrieve correspondent entity information (associations table name, columns names)
  121. * of rule's associated entity by specified entity type
  122. *
  123. * @param string $entityType
  124. *
  125. * @throws \Magento\Framework\Exception\LocalizedException
  126. * @return array
  127. */
  128. protected function _getAssociatedEntityInfo($entityType)
  129. {
  130. if (isset($this->_associatedEntitiesMap[$entityType])) {
  131. return $this->_associatedEntitiesMap[$entityType];
  132. }
  133. throw new \Magento\Framework\Exception\LocalizedException(
  134. __('There is no information about associated entity type "%1".', $entityType)
  135. );
  136. }
  137. }