Skip to content

Commit

Permalink
Merge pull request #1848 from magento-engcom/2.2-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - 2.2-develop
 - MAGETWO-85583: #12378: Regions list in Directory module for India #1007
 - MAGETWO-85580: Fixes #12660 invalid parameter configuration provided for argument #12661
 - MAGETWO-85534: #8647: [GitHub] Order of how arguments are merged in multiple di.xml-… #995
 - MAGETWO-84370: 11946: Layer navigation showing wrong product count #12063
Fixed issues:
 - #12378 Regions list in Directory module for India
 - #12452 ACL permissions issue
 - #12660 Invalid parameter configuration provided for $block argument upon no ACL permissions to the block
 - #8647 Order of how arguments are merged in multiple di.xml-files causes unexpected results
 - #11946 Layer navigation showing wrong product count
  • Loading branch information
ishakhsuvarov authored Dec 13, 2017
2 parents 7d00d28 + b0ce3b6 commit 3839c0f
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation;

use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Model\Stock;
use Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider\QueryBuilder;
use Magento\Customer\Model\Session;
use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;
Expand All @@ -19,7 +20,7 @@
use Magento\Framework\App\ObjectManager;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* DataProvider for Catalog search Mysql.
*/
class DataProvider implements DataProviderInterface
{
Expand Down Expand Up @@ -48,23 +49,31 @@ class DataProvider implements DataProviderInterface
*/
private $connection;

/**
* @var QueryBuilder;
*/
private $queryBuilder;

/**
* @param Config $eavConfig
* @param ResourceConnection $resource
* @param ScopeResolverInterface $scopeResolver
* @param Session $customerSession
* @param QueryBuilder|null $queryBuilder
*/
public function __construct(
Config $eavConfig,
ResourceConnection $resource,
ScopeResolverInterface $scopeResolver,
Session $customerSession
Session $customerSession,
QueryBuilder $queryBuilder = null
) {
$this->eavConfig = $eavConfig;
$this->resource = $resource;
$this->connection = $resource->getConnection();
$this->scopeResolver = $scopeResolver;
$this->customerSession = $customerSession;
$this->queryBuilder = $queryBuilder ?: ObjectManager::getInstance()->get(QueryBuilder::class);
}

/**
Expand All @@ -79,47 +88,13 @@ public function getDataSet(

$attribute = $this->eavConfig->getAttribute(Product::ENTITY, $bucket->getField());

$select = $this->getSelect();

$select->joinInner(
['entities' => $entityIdsTable->getName()],
'main_table.entity_id = entities.entity_id',
[]
$select = $this->queryBuilder->build(
$attribute,
$entityIdsTable->getName(),
$currentScope,
$this->customerSession->getCustomerGroupId()
);

if ($attribute->getAttributeCode() === 'price') {
/** @var \Magento\Store\Model\Store $store */
$store = $this->scopeResolver->getScope($currentScope);
if (!$store instanceof \Magento\Store\Model\Store) {
throw new \RuntimeException('Illegal scope resolved');
}
$table = $this->resource->getTableName('catalog_product_index_price');
$select->from(['main_table' => $table], null)
->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])
->where('main_table.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
->where('main_table.website_id = ?', $store->getWebsiteId());
} else {
$currentScopeId = $this->scopeResolver->getScope($currentScope)
->getId();
$table = $this->resource->getTableName(
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
);
$subSelect = $select;
$subSelect->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value'])
->distinct()
->joinLeft(
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')],
'main_table.source_id = stock_index.product_id',
[]
)
->where('main_table.attribute_id = ?', $attribute->getAttributeId())
->where('main_table.store_id = ? ', $currentScopeId)
->where('stock_index.stock_status = ?', Stock::STOCK_IN_STOCK);
$parentSelect = $this->getSelect();
$parentSelect->from(['main_table' => $subSelect], ['main_table.value']);
$select = $parentSelect;
}

return $select;
}

Expand All @@ -130,12 +105,4 @@ public function execute(Select $select)
{
return $this->connection->fetchAssoc($select);
}

/**
* @return Select
*/
private function getSelect()
{
return $this->connection->select();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider;

use Magento\CatalogInventory\Model\Configuration as CatalogInventoryConfiguration;
use Magento\CatalogInventory\Model\Stock;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\Search\Request\BucketInterface;

/**
* Attribute query builder
*/
class QueryBuilder
{
/**
* @var Resource
*/
private $resource;

/**
* @var ScopeResolverInterface
*/
private $scopeResolver;

/**
* @var CatalogInventoryConfiguration
*/
private $inventoryConfig;

/**
* @param ResourceConnection $resource
* @param ScopeResolverInterface $scopeResolver
* @param CatalogInventoryConfiguration $inventoryConfig
*/
public function __construct(
ResourceConnection $resource,
ScopeResolverInterface $scopeResolver,
CatalogInventoryConfiguration $inventoryConfig
) {
$this->resource = $resource;
$this->scopeResolver = $scopeResolver;
$this->inventoryConfig = $inventoryConfig;
}

/**
* Build select.
*
* @param AbstractAttribute $attribute
* @param string $tableName
* @param int $currentScope
* @param int $customerGroupId
*
* @return Select
*/
public function build(
AbstractAttribute $attribute,
string $tableName,
int $currentScope,
int $customerGroupId
) : Select {
$select = $this->resource->getConnection()->select();
$select->joinInner(
['entities' => $tableName],
'main_table.entity_id = entities.entity_id',
[]
);

if ($attribute->getAttributeCode() === 'price') {
return $this->buildQueryForPriceAttribute($currentScope, $customerGroupId, $select);
}

return $this->buildQueryForAttribute($currentScope, $attribute, $select);
}

/**
* Build select for price attribute.
*
* @param int $currentScope
* @param int $customerGroupId
* @param Select $select
*
* @return Select
*/
private function buildQueryForPriceAttribute(
int $currentScope,
int $customerGroupId,
Select $select
) : Select {
/** @var \Magento\Store\Model\Store $store */
$store = $this->scopeResolver->getScope($currentScope);
if (!$store instanceof \Magento\Store\Model\Store) {
throw new \RuntimeException('Illegal scope resolved');
}

$table = $this->resource->getTableName('catalog_product_index_price');
$select->from(['main_table' => $table], null)
->columns([BucketInterface::FIELD_VALUE => 'main_table.min_price'])
->where('main_table.customer_group_id = ?', $customerGroupId)
->where('main_table.website_id = ?', $store->getWebsiteId());

return $select;
}

/**
* Build select for attribute.
*
* @param int $currentScope
* @param AbstractAttribute $attribute
* @param Select $select
*
* @return Select
*/
private function buildQueryForAttribute(
int $currentScope,
AbstractAttribute $attribute,
Select $select
) : Select {
$currentScopeId = $this->scopeResolver->getScope($currentScope)->getId();
$table = $this->resource->getTableName(
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
);
$select->from(['main_table' => $table], ['main_table.entity_id', 'main_table.value'])
->distinct()
->joinLeft(
['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')],
'main_table.source_id = stock_index.product_id',
[]
)
->where('main_table.attribute_id = ?', $attribute->getAttributeId())
->where('main_table.store_id = ? ', $currentScopeId);

if (!$this->inventoryConfig->isShowOutOfStock($currentScopeId)) {
$select->where('stock_index.stock_status = ?', Stock::STOCK_IN_STOCK);
}

$parentSelect = $this->resource->getConnection()->select();
$parentSelect->from(['main_table' => $select], ['main_table.value']);
return $parentSelect;
}
}
Loading

0 comments on commit 3839c0f

Please sign in to comment.