Skip to content

Commit

Permalink
Move 2.x version commits to correct branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilja Lapkovskis committed Sep 23, 2019
2 parents 5066e60 + 842edbf commit c00eb71
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\CatalogWidget\Model\Rule;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Api\Filter;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Exception\LocalizedException;
use Magento\Rule\Model\Condition\Sql\Builder;
use Magento\Widget\Helper\Conditions;

class ConditionsFilter implements CustomFilterInterface
{
/**
* @var Conditions
*/
protected $conditionsHelper;

/**
* @var Rule
*/
protected $rule;

/**
* @var Builder
*/
protected $sqlBuilder;
/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* ConditionsFilter constructor.
* @param Conditions $conditionsHelper
* @param Rule $rule
* @param Builder $sqlBuilder
*/
public function __construct(
Conditions $conditionsHelper,
Rule $rule,
Builder $sqlBuilder,
CollectionFactory $collectionFactory
) {
$this->conditionsHelper = $conditionsHelper;
$this->rule = $rule;
$this->sqlBuilder = $sqlBuilder;
$this->collectionFactory = $collectionFactory;
}

/**
* Get conditions
*
* @return \Magento\Rule\Model\Condition\Combine
*/
protected function getConditions($conditions)
{
$conditions = $this->conditionsHelper->decode($conditions);

foreach ($conditions as $key => $condition) {
if (!empty($condition['attribute'])
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date'])
) {
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
}
}

$this->rule->loadPost(['conditions' => $conditions]);

return $this->rule->getConditions();
}

/**
* @inheritDoc
*/
public function apply(Filter $filter, AbstractDb $collection)
{
$conditionType = $filter->getConditionType();
$rawFilterField = $filter->getField();

if ($conditionType !== 'eq') {
throw new LocalizedException(__($rawFilterField . " only supports 'eq' condition type."));
}

$conditions = base64_decode($filter->getValue());
$conditions = $this->getConditions($conditions);

$simpleSelect = clone $collection;
$conditions->collectValidatedAttributes($simpleSelect);
$this->sqlBuilder->attachConditionToCollection($simpleSelect, $conditions);

$simpleSelect->addFieldToFilter('status', Status::STATUS_ENABLED);
$simpleSelect->getSelect()
->reset(\Zend_Db_Select::COLUMNS)
->columns(['e.entity_id']);

$configurableProductCollection = $this->collectionFactory->create();
$select = $configurableProductCollection->getConnection()
->select()
->distinct()
->from(['l' => 'catalog_product_super_link'], 'l.product_id')
->join(
['k' => $configurableProductCollection->getTable('catalog_product_entity')],
`k.entity_id = l.parent_id`
)
->where($configurableProductCollection->getConnection()->prepareSqlCondition(
'l.product_id',
['in' => $simpleSelect->getSelect()]
))
->reset(\Zend_Db_Select::COLUMNS)
->columns(['l.parent_id']);

$unionCollection = $this->collectionFactory->create();
$unionSelect = $unionCollection->getConnection()
->select()
->union([$simpleSelect->getSelect(), $select]);

$collection->getSelect()
->where($collection->getConnection()->prepareSqlCondition(
'e.entity_id',
['in' => $unionSelect]
));

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function apply(Filter $filter, AbstractDb $collection)
if ($category) {
$simpleSelect->addCategoriesFilter(['in' => (int)$category->getId()]);
}


$simpleSelect->getSelect()
->reset(\Zend_Db_Select::COLUMNS)
Expand All @@ -67,14 +67,22 @@ public function apply(Filter $filter, AbstractDb $collection)
['k' => $configurableProductCollection->getTable('catalog_product_entity')],
'k.entity_id = l.parent_id'
)->where($configurableProductCollection->getConnection()->prepareSqlCondition(
'l.product_id', ['in' => $simpleSelect->getSelect()]
'l.product_id',
['in' => $simpleSelect->getSelect()]
))
->reset(\Zend_Db_Select::COLUMNS)
->columns(['l.parent_id']);

$collection->getSelect()->where($collection->getConnection()->prepareSqlCondition(
'e.entity_id', ['in' => $select]
));
$unionCollection = $this->collectionFactory->create();
$unionSelect = $unionCollection->getConnection()
->select()
->union([$simpleSelect->getSelect(), $select]);

$collection->getSelect()
->where($collection->getConnection()->prepareSqlCondition(
'e.entity_id',
['in' => $unionSelect]
));

return true;
}
Expand Down
49 changes: 49 additions & 0 deletions src/Model/Rule/Condition/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace ScandiPWA\CatalogGraphQl\Model\Rule\Condition;

use Magento\CatalogWidget\Model\Rule\Condition\Product as ProductCondition;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Catalog\Model\ResourceModel\Product\Collection;

/**
* Class Product
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Product extends ProductCondition
{
/**
* @param Attribute $attribute
* @param Collection $collection
* @return $this
*/
protected function addGlobalAttribute(
Attribute $attribute,
Collection $collection
) {
switch ($attribute->getBackendType()) {
case 'decimal':
case 'datetime':
case 'int':
$alias = 'at_' . $attribute->getAttributeCode();
$collection->addAttributeToSelect($attribute->getAttributeCode(), 'left');
break;
default:
$alias = 'at_' . sha1($this->getId()) . $attribute->getAttributeCode();

$connection = $this->_productResource->getConnection();
$storeId = $connection->getIfNullSql($alias . '.store_id', $this->storeManager->getStore()->getId());
$linkField = $attribute->getEntity()->getLinkField();

$collection->getSelect()->join(
[$alias => $collection->getTable('catalog_product_entity_varchar')],
"($alias.$linkField = e.$linkField) AND ($alias.store_id = $storeId)" .
" AND ($alias.attribute_id = {$attribute->getId()})",
[]
);
}

$this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.value';

return $this;
}
}
Loading

0 comments on commit c00eb71

Please sign in to comment.