Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Append a generic autocomplete display framework. #12

Merged
merged 1 commit into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"smile-sa/module-elasticsuite-core": "self.version",
"smile-sa/module-elasticsuite-catalog": "self.version",
"smile-sa/module-elasticsuite-swatches": "self.version",
"smile-sa/module-elasticsuite-thesaurus": "self.version"
"smile-sa/module-elasticsuite-thesaurus": "self.version",
"smile-sa/module-elasticsuite-catalog-autocomplete": "self.version"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand All @@ -53,13 +54,15 @@
"src/module-elasticsuite-core/registration.php",
"src/module-elasticsuite-catalog/registration.php",
"src/module-elasticsuite-swatches/registration.php",
"src/module-elasticsuite-thesaurus/registration.php"
"src/module-elasticsuite-thesaurus/registration.php",
"src/module-elasticsuite-catalog-autocomplete/registration.php"
],
"psr-4" : {
"Smile\\ElasticSuiteCore\\" : "src/module-elasticsuite-core",
"Smile\\ElasticSuiteCatalog\\" : "src/module-elasticsuite-catalog",
"Smile\\ElasticSuiteSwatches\\" : "src/module-elasticsuite-swatches",
"Smile\\ElasticSuiteThesaurus\\" : "src/module-elasticsuite-thesaurus"
"Smile\\ElasticSuiteThesaurus\\" : "src/module-elasticsuite-thesaurus",
"Smile\\ElasticSuiteCatalogAutocomplete\\" : "src/module-elasticsuite-catalog-autocomplete"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile_ElasticSuiteCatalogAutocomplete
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalogAutocomplete\Block\Search\Form;

use Magento\Framework\Locale\FormatInterface;

/**
* Quick Form block for Autocomplete
*
* @category Smile
* @package Smile_ElasticSuiteCatalogAutocomplete
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class Autocomplete extends \Magento\Framework\View\Element\Template
{
/**
* @var FormatInterface
*/
private $localeFormat;

/**
* JSON Helper
*
* @var \Magento\Framework\Json\Helper\Data
*/
private $jsonHelper;

/**
* @var array
*/
private $rendererList;

/**
* Mini constructor.
*
* @param \Magento\Framework\View\Element\Template\Context $context Block context
* @param \Magento\Framework\Json\Helper\Data $jsonHelper JSON helper
* @param \Magento\Framework\Locale\FormatInterface $localeFormat Locale Format
* @param array $data The data
* @param array $rendererList The renderers used for autocomplete rendering
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Json\Helper\Data $jsonHelper,
FormatInterface $localeFormat,
array $data,
array $rendererList = []
) {
$this->jsonHelper = $jsonHelper;
$this->localeFormat = $localeFormat;
$this->rendererList = $rendererList;

parent::__construct($context, $data);
}

/**
* Retrieve templates renderers for autocomplete results
*
* @return array
*/
public function getSuggestRenderers()
{
foreach ($this->rendererList as &$renderer) {
if (isset($renderer['title'])) {
$renderer['title'] = __($renderer['title']);
}
}

return $this->rendererList;
}

/**
* Retrieve Renderers used to draw the suggest in JSON format
*
* @return string
*/
public function getJsonSuggestRenderers()
{
$templates = $this->getSuggestRenderers();

return $this->jsonHelper->jsonEncode($templates);
}

/**
* Retrieve price format configuration in Json.
*
* @return array
*/
public function getJsonPriceFormat()
{
return $this->jsonHelper->jsonEncode($this->getPriceFormat());
}

/**
* Retrieve price format configuration.
*
* @return array
*/
protected function getPriceFormat()
{
return $this->localeFormat->getPriceFormat();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile_ElasticSuiteCatalogAutocomplete
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalogAutocomplete\Model\Autocomplete\Product;

use Magento\Search\Model\Autocomplete\DataProviderInterface;
use Magento\Search\Model\Autocomplete\ItemFactory;
use Magento\Search\Model\QueryFactory;
use Smile\ElasticSuiteCatalogAutocomplete\Model\Autocomplete\Terms\DataProvider as TermDataProvider;
use Smile\ElasticSuiteCatalog\Model\ResourceModel\Product\Fulltext\CollectionFactory as ProductCollectionFactory;

/**
* Catalog product autocomplete data provider.
*
* @category Smile
* @package Smile_ElasticSuiteCatalogAutocomplete
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class DataProvider implements DataProviderInterface
{
/**
* Autocomplete result item factory
*
* @var ItemFactory
*/
protected $itemFactory;

/**
* Query factory
*
* @var QueryFactory
*/
protected $queryFactory;

/**
* @var TermDataProvider
*/
protected $termDataProvider;

/**
* @var ProductCollectionFactory
*/
protected $productCollectionFactory;

/**
* @var \Magento\Catalog\Helper\Product
*/
protected $imageHelper;

/**
* Constructor.
*
* @param ItemFactory $itemFactory Suggest item factory.
* @param QueryFactory $queryFactory Search query factory.
* @param TermDataProvider $termDataProvider Search terms suggester.
* @param ProductCollectionFactory $productCollectionFactory Product collection factory.
* @param \Magento\Catalog\Helper\Product $productHelper Catalog Image helper.
*/
public function __construct(
ItemFactory $itemFactory,
QueryFactory $queryFactory,
TermDataProvider $termDataProvider,
ProductCollectionFactory $productCollectionFactory,
\Magento\Catalog\Helper\Product $productHelper
) {
$this->itemFactory = $itemFactory;
$this->queryFactory = $queryFactory;
$this->termDataProvider = $termDataProvider;
$this->productCollectionFactory = $productCollectionFactory;
$this->imageHelper = $productHelper;
}
/**
* {@inheritDoc}
*/
public function getItems()
{
$result = [];
$productCollection = $this->getProductCollection();
if ($productCollection) {
\Magento\Catalog\Model\Product::ATTRIBUTE_SET_ID;
foreach ($productCollection as $product) {
$result[] = $this->itemFactory->create(
[
'title' => $product->getName(),
'image' => $this->imageHelper->getSmallImageUrl($product),
'url' => $product->getProductUrl(),
'price' => $product->getFinalPrice(),
'final_price' => $product->getPrice(), // The getPrice method returns always 0.
'type' => 'product',
]
);
}
}

return $result;
}

/**
* List of search terms suggested by the search terms data daprovider.
*
* @return array
*/
private function getSuggestedTerms()
{
$terms = array_map(
function (\Magento\Search\Model\Autocomplete\Item $termItem) {
return $termItem->getTitle();
},
$this->termDataProvider->getItems()
);

return $terms;
}

/**
* Suggested products collection.
* Returns null if no suggested search terms.
*
* @return \Smile\ElasticSuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection|null
*/
private function getProductCollection()
{
$productCollection = null;
$suggestedTerms = $this->getSuggestedTerms();
$terms = [$this->queryFactory->get()->getQueryText()];

if (!empty($suggestedTerms)) {
$terms = array_merge($terms, $suggestedTerms);
}

$productCollection = $this->productCollectionFactory->create();
$productCollection->addSearchFilter($terms);
$productCollection->setPageSize(5);
$productCollection
->addAttributeToSelect('name')
->addAttributeToSelect('small_image')
->addPriceData();

return $productCollection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile_ElasticSuiteCatalogAutocomplete
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalogAutocomplete\Model\Autocomplete\Terms;

use Magento\Search\Model\ResourceModel\Query\Collection;
use Magento\Search\Model\QueryFactory;
use Magento\Search\Model\Autocomplete\DataProviderInterface;
use Magento\Search\Model\Autocomplete\ItemFactory;

/**
* Popular search terms data provider.
*
* @category Smile
* @package Smile_ElasticSuiteCatalogAutocomplete
* @author Aurelien FOUCRET <aurelien.foucret@smile.fr>
*/
class DataProvider implements DataProviderInterface
{
/**
* Query factory
*
* @var QueryFactory
*/
protected $queryFactory;

/**
* Autocomplete result item factory
*
* @var ItemFactory
*/
protected $itemFactory;

/**
*
* @var \Magento\Search\Model\Autocomplete\Item[]
*/
private $items;

/**
* Constructor.
*
* @param QueryFactory $queryFactory Search query text factory.
* @param ItemFactory $itemFactory Suggest terms item facory.
*/
public function __construct(
QueryFactory $queryFactory,
ItemFactory $itemFactory
) {
$this->queryFactory = $queryFactory;
$this->itemFactory = $itemFactory;
}

/**
* {@inheritdoc}
*/
public function getItems()
{
if ($this->items === null) {
$collection = $this->getSuggestCollection();
$this->items = [];
foreach ($collection as $item) {
$resultItem = $this->itemFactory->create([
'title' => $item->getQueryText(),
'num_results' => $item->getNumResults(),
'type' => 'term',
]);
$this->items[] = $resultItem;
}
}

return $this->items;
}

/**
* Retrieve suggest collection for query
*
* @return Collection
*/
private function getSuggestCollection()
{
$queryCollection = $this->queryFactory->get()->getSuggestCollection();
$queryCollection->addFieldToFilter('is_spellchecked', 'false')->setPageSize(3);

return $queryCollection;
}
}
Loading