123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\CatalogRule\Model\ResourceModel\Rule;
- use Magento\Framework\Serialize\Serializer\Json;
- use Magento\Framework\App\ObjectManager;
- class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\AbstractCollection
- {
- /**
- * Store associated with rule entities information map
- *
- * @var array
- */
- protected $_associatedEntitiesMap;
- /**
- * @var Json
- */
- protected $serializer;
- /**
- * Collection constructor.
- * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
- * @param \Magento\Framework\Event\ManagerInterface $eventManager
- * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
- * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
- * @param Json|null $serializer
- */
- public function __construct(
- \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
- \Psr\Log\LoggerInterface $logger,
- \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
- \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
- Json $serializer = null
- ) {
- parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
- $this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
- $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
- }
- /**
- * Set resource model
- *
- * @return void
- * @codeCoverageIgnore
- */
- protected function _construct()
- {
- $this->_init(\Magento\CatalogRule\Model\Rule::class, \Magento\CatalogRule\Model\ResourceModel\Rule::class);
- }
- /**
- * Find product attribute in conditions or actions
- *
- * @param string $attributeCode
- * @return $this
- * @api
- */
- public function addAttributeInConditionFilter($attributeCode)
- {
- $match = sprintf('%%%s%%', substr($this->serializer->serialize(['attribute' => $attributeCode]), 1, -1));
- $this->addFieldToFilter('conditions_serialized', ['like' => $match]);
- return $this;
- }
- /**
- * @param string $entityType
- * @param string $objectField
- * @throws \Magento\Framework\Exception\LocalizedException
- * @return void
- */
- protected function mapAssociatedEntities($entityType, $objectField)
- {
- if (!$this->_items) {
- return;
- }
- $entityInfo = $this->_getAssociatedEntityInfo($entityType);
- $ruleIdField = $entityInfo['rule_id_field'];
- $entityIds = $this->getColumnValues($ruleIdField);
- $select = $this->getConnection()->select()->from(
- $this->getTable($entityInfo['associations_table'])
- )->where(
- $ruleIdField . ' IN (?)',
- $entityIds
- );
- $associatedEntities = $this->getConnection()->fetchAll($select);
- array_map(function ($associatedEntity) use ($entityInfo, $ruleIdField, $objectField) {
- $item = $this->getItemByColumnValue($ruleIdField, $associatedEntity[$ruleIdField]);
- $itemAssociatedValue = $item->getData($objectField) === null ? [] : $item->getData($objectField);
- $itemAssociatedValue[] = $associatedEntity[$entityInfo['entity_id_field']];
- $item->setData($objectField, $itemAssociatedValue);
- }, $associatedEntities);
- }
- /**
- * @return $this
- */
- protected function _afterLoad()
- {
- $this->mapAssociatedEntities('website', 'website_ids');
- $this->mapAssociatedEntities('customer_group', 'customer_group_ids');
- $this->setFlag('add_websites_to_result', false);
- return parent::_afterLoad();
- }
- /**
- * Limit rules collection by specific customer group
- *
- * @param int $customerGroupId
- * @return $this
- */
- public function addCustomerGroupFilter($customerGroupId)
- {
- $entityInfo = $this->_getAssociatedEntityInfo('customer_group');
- if (!$this->getFlag('is_customer_group_joined')) {
- $this->setFlag('is_customer_group_joined', true);
- $this->getSelect()->join(
- ['customer_group' => $this->getTable($entityInfo['associations_table'])],
- $this->getConnection()
- ->quoteInto('customer_group.' . $entityInfo['entity_id_field'] . ' = ?', $customerGroupId)
- . ' AND main_table.' . $entityInfo['rule_id_field'] . ' = customer_group.'
- . $entityInfo['rule_id_field'],
- []
- );
- }
- return $this;
- }
- /**
- * @return array
- * @deprecated 100.1.0
- */
- private function getAssociatedEntitiesMap()
- {
- if (!$this->_associatedEntitiesMap) {
- $this->_associatedEntitiesMap = \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\CatalogRule\Model\ResourceModel\Rule\AssociatedEntityMap::class)
- ->getData();
- }
- return $this->_associatedEntitiesMap;
- }
- }
|