Collection.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Model\ResourceModel\Rule;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\App\ObjectManager;
  9. class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\AbstractCollection
  10. {
  11. /**
  12. * Store associated with rule entities information map
  13. *
  14. * @var array
  15. */
  16. protected $_associatedEntitiesMap;
  17. /**
  18. * @var Json
  19. */
  20. protected $serializer;
  21. /**
  22. * Collection constructor.
  23. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  24. * @param \Psr\Log\LoggerInterface $logger
  25. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  26. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  27. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  28. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  29. * @param Json|null $serializer
  30. */
  31. public function __construct(
  32. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  33. \Psr\Log\LoggerInterface $logger,
  34. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  35. \Magento\Framework\Event\ManagerInterface $eventManager,
  36. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  37. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
  38. Json $serializer = null
  39. ) {
  40. parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
  41. $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
  42. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  43. }
  44. /**
  45. * Set resource model
  46. *
  47. * @return void
  48. * @codeCoverageIgnore
  49. */
  50. protected function _construct()
  51. {
  52. $this->_init(\Magento\CatalogRule\Model\Rule::class, \Magento\CatalogRule\Model\ResourceModel\Rule::class);
  53. }
  54. /**
  55. * Find product attribute in conditions or actions
  56. *
  57. * @param string $attributeCode
  58. * @return $this
  59. * @api
  60. */
  61. public function addAttributeInConditionFilter($attributeCode)
  62. {
  63. $match = sprintf('%%%s%%', substr($this->serializer->serialize(['attribute' => $attributeCode]), 1, -1));
  64. $this->addFieldToFilter('conditions_serialized', ['like' => $match]);
  65. return $this;
  66. }
  67. /**
  68. * @param string $entityType
  69. * @param string $objectField
  70. * @throws \Magento\Framework\Exception\LocalizedException
  71. * @return void
  72. */
  73. protected function mapAssociatedEntities($entityType, $objectField)
  74. {
  75. if (!$this->_items) {
  76. return;
  77. }
  78. $entityInfo = $this->_getAssociatedEntityInfo($entityType);
  79. $ruleIdField = $entityInfo['rule_id_field'];
  80. $entityIds = $this->getColumnValues($ruleIdField);
  81. $select = $this->getConnection()->select()->from(
  82. $this->getTable($entityInfo['associations_table'])
  83. )->where(
  84. $ruleIdField . ' IN (?)',
  85. $entityIds
  86. );
  87. $associatedEntities = $this->getConnection()->fetchAll($select);
  88. array_map(function ($associatedEntity) use ($entityInfo, $ruleIdField, $objectField) {
  89. $item = $this->getItemByColumnValue($ruleIdField, $associatedEntity[$ruleIdField]);
  90. $itemAssociatedValue = $item->getData($objectField) === null ? [] : $item->getData($objectField);
  91. $itemAssociatedValue[] = $associatedEntity[$entityInfo['entity_id_field']];
  92. $item->setData($objectField, $itemAssociatedValue);
  93. }, $associatedEntities);
  94. }
  95. /**
  96. * @return $this
  97. */
  98. protected function _afterLoad()
  99. {
  100. $this->mapAssociatedEntities('website', 'website_ids');
  101. $this->mapAssociatedEntities('customer_group', 'customer_group_ids');
  102. $this->setFlag('add_websites_to_result', false);
  103. return parent::_afterLoad();
  104. }
  105. /**
  106. * Limit rules collection by specific customer group
  107. *
  108. * @param int $customerGroupId
  109. * @return $this
  110. */
  111. public function addCustomerGroupFilter($customerGroupId)
  112. {
  113. $entityInfo = $this->_getAssociatedEntityInfo('customer_group');
  114. if (!$this->getFlag('is_customer_group_joined')) {
  115. $this->setFlag('is_customer_group_joined', true);
  116. $this->getSelect()->join(
  117. ['customer_group' => $this->getTable($entityInfo['associations_table'])],
  118. $this->getConnection()
  119. ->quoteInto('customer_group.' . $entityInfo['entity_id_field'] . ' = ?', $customerGroupId)
  120. . ' AND main_table.' . $entityInfo['rule_id_field'] . ' = customer_group.'
  121. . $entityInfo['rule_id_field'],
  122. []
  123. );
  124. }
  125. return $this;
  126. }
  127. /**
  128. * @return array
  129. * @deprecated 100.1.0
  130. */
  131. private function getAssociatedEntitiesMap()
  132. {
  133. if (!$this->_associatedEntitiesMap) {
  134. $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
  135. ->get(\Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap::class)
  136. ->getData();
  137. }
  138. return $this->_associatedEntitiesMap;
  139. }
  140. }