-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
3,441 additions
and
444 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ dev/frontend/node_modules/ | |
dev/bin/auth.json | ||
composer.lock | ||
vendor/ | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Adapter\Aggregation; | ||
|
||
use Magento\Catalog\Model\ProductFactory; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Ddl\Table; | ||
use Magento\Framework\Search\Adapter\Aggregation\AggregationResolverInterface; | ||
use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder\Container as AggregationContainer; | ||
use Magento\Framework\Search\Adapter\Mysql\Aggregation\DataProviderContainer; | ||
use Magento\Framework\Search\Adapter\Mysql\TemporaryStorage; | ||
use Magento\Framework\Search\RequestInterface; | ||
|
||
class Builder | ||
{ | ||
/** @var DataProviderContainer */ | ||
private $dataProviderContainer; | ||
|
||
/** @var AggregationContainer */ | ||
private $aggregationContainer; | ||
|
||
/** @var ResourceConnection */ | ||
private $resource; | ||
|
||
/** @var AggregationResolverInterface */ | ||
private $aggregationResolver; | ||
|
||
/** @var ProductFactory */ | ||
private $productFactory; | ||
|
||
/** | ||
* @param ResourceConnection $resource | ||
* @param DataProviderContainer $dataProviderContainer | ||
* @param AggregationContainer $aggregationContainer | ||
* @param AggregationResolverInterface $aggregationResolver | ||
* @param ProductFactory $productFactory | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resource, | ||
DataProviderContainer $dataProviderContainer, | ||
AggregationContainer $aggregationContainer, | ||
AggregationResolverInterface $aggregationResolver, | ||
ProductFactory $productFactory | ||
) { | ||
$this->dataProviderContainer = $dataProviderContainer; | ||
$this->aggregationContainer = $aggregationContainer; | ||
$this->resource = $resource; | ||
$this->aggregationResolver = $aggregationResolver; | ||
$this->productFactory = $productFactory; | ||
} | ||
|
||
public function build(RequestInterface $request, Table $documentsTable, array $documents, array $facets) | ||
{ | ||
return $this->processAggregations($request, $documentsTable, $documents, $facets); | ||
} | ||
|
||
private function processAggregations(RequestInterface $request, Table $documentsTable, $documents, $facets) | ||
{ | ||
$aggregations = []; | ||
$documentIds = $documents ? $this->extractDocumentIds($documents) : $this->getDocumentIds($documentsTable); | ||
$buckets = $this->aggregationResolver->resolve($request, $documentIds); | ||
$dataProvider = $this->dataProviderContainer->get($request->getIndex()); | ||
|
||
foreach ($buckets as $bucket) { | ||
if (isset($facets[$bucket->getField()])) { | ||
$aggregations[$bucket->getName()] = | ||
$this->formatAggregation($bucket->getField(), $facets[$bucket->getField()]); | ||
} else { | ||
$aggregationBuilder = $this->aggregationContainer->get($bucket->getType()); | ||
$aggregations[$bucket->getName()] = $aggregationBuilder->build( | ||
$dataProvider, | ||
$request->getDimensions(), | ||
$bucket, | ||
$documentsTable | ||
); | ||
} | ||
} | ||
|
||
return $aggregations; | ||
} | ||
|
||
private function formatAggregation($attribute, $facetData) | ||
{ | ||
$aggregation = []; | ||
|
||
foreach ($facetData as $value => $count) { | ||
$optionId = $this->getOptionIdByLabel($attribute, $value); | ||
$aggregation[$optionId] = [ | ||
'value' => (string) $optionId, | ||
'count' => (string) $count, | ||
]; | ||
} | ||
|
||
return $aggregation; | ||
} | ||
|
||
private function getOptionIdByLabel($attributeCode, $optionLabel) | ||
{ | ||
$product = $this->productFactory->create(); | ||
$isAttributeExist = $product->getResource()->getAttribute($attributeCode); | ||
$optionId = ''; | ||
if ($isAttributeExist && $isAttributeExist->usesSource()) { | ||
$optionId = $isAttributeExist->getSource()->getOptionId($optionLabel); | ||
} | ||
|
||
return $optionId; | ||
} | ||
|
||
private function extractDocumentIds(array $documents) | ||
{ | ||
return $documents ? array_keys($documents) : []; | ||
} | ||
|
||
private function getDocumentIds(Table $documentsTable) | ||
{ | ||
$select = $this->getConnection() | ||
->select() | ||
->from($documentsTable->getName(), TemporaryStorage::FIELD_ENTITY_ID); | ||
|
||
return $this->getConnection()->fetchCol($select); | ||
} | ||
|
||
private function getConnection() | ||
{ | ||
return $this->resource->getConnection(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Block; | ||
|
||
use Algolia\AlgoliaSearch\Helper\ConfigHelper; | ||
|
||
class Navigation extends \Magento\LayeredNavigation\Block\Navigation | ||
{ | ||
/** @var ConfigHelper */ | ||
private $configHelper; | ||
|
||
/** | ||
* Navigation constructor. | ||
* | ||
* @param \Magento\Framework\View\Element\Template\Context $context | ||
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver | ||
* @param \Magento\Catalog\Model\Layer\FilterList $filterList | ||
* @param \Magento\Catalog\Model\Layer\AvailabilityFlagInterface $visibilityFlag | ||
* @param ConfigHelper $configHelper | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\View\Element\Template\Context $context, | ||
\Magento\Catalog\Model\Layer\Resolver $layerResolver, | ||
\Magento\Catalog\Model\Layer\FilterList $filterList, | ||
\Magento\Catalog\Model\Layer\AvailabilityFlagInterface $visibilityFlag, | ||
ConfigHelper $configHelper, | ||
array $data | ||
) { | ||
parent::__construct($context, $layerResolver, $filterList, $visibilityFlag, $data); | ||
$this->configHelper = $configHelper; | ||
|
||
if ($this->configHelper->isBackendRenderingEnabled()) { | ||
$this->getLayout()->unsetElement('catalog.compare.sidebar'); | ||
$this->getLayout()->unsetElement('wishlist_sidebar'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Block\Navigation\Renderer; | ||
|
||
use Magento\Catalog\Model\Layer\Filter\FilterInterface; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\LayeredNavigation\Block\Navigation\FilterRendererInterface; | ||
|
||
class CategoryRenderer extends Template implements FilterRendererInterface | ||
{ | ||
/** @var string */ | ||
protected $_template = 'Algolia_AlgoliaSearch::layer/filter/category.phtml'; | ||
|
||
public function isMultipleSelectEnabled() | ||
{ | ||
return false; | ||
} | ||
|
||
public function render(FilterInterface $filter) | ||
{ | ||
$html = ''; | ||
$this->filter = $filter; | ||
|
||
if ($this->canRenderFilter()) { | ||
$this->assign('filterItems', $filter->getItems()); | ||
$html = $this->_toHtml(); | ||
$this->assign('filterItems', []); | ||
} | ||
|
||
return $html; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function canRenderFilter() | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.