<?php
namespace App\Service;
use Exception;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
use App\Service\Contract\SearchServiceContract;
use Symfony\Component\HttpFoundation\Request;
class SearchService implements SearchServiceContract
{
protected $currentTenant;
public function __construct($tenant)
{
$this->currentTenant = $tenant;
}
/**
* @param Request $request
*
* @return ProductListInterface
* @throws Exception
*/
public function getCurrentSearchListing(Request $request)
{
$methodName = 'get' . ucfirst($this->currentTenant) . 'SearchListing';
if (method_exists($this, $methodName)) {
return $this->$methodName($request);
}
throw new Exception($methodName . ' method is not implemented');
}
/**
* @param Request $request
*
* @return ProductListInterface
*/
public function getDefaultSearchListing(Request $request)
{
$ecommerceFactory = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance();
$environment = $ecommerceFactory->getEnvironment();
$environment->setCurrentAssortmentTenant("default");
$productListing = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getIndexService()->getProductListForCurrentTenant();
$productListing->addCondition("o_type = 'object'", 'o_type');
$productListing->addCondition("product_type = 'grouped'", 'product_type');
$productListing->setOrderKey('LTRIM(name)');
$productListing->setOrder('asc');
return $productListing;
}
/**
* @param Request $request
*
* @return ProductListInterface
*/
public function getEsTenantSearchListing(Request $request)
{
$ecommerceFactory = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance();
$environment = $ecommerceFactory->getEnvironment();
$environment->setCurrentAssortmentTenant("EsTenant");
$productListing = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getIndexService()->getProductListForCurrentTenant();
$productListing->setDefaultFilters($request->get('division'));
$productListing->setOrder('asc');
$productListing->setOrderKey('name');
return $productListing;
}
public function escapeElasticReservedChars($string) {
$regex = "/[\\+\\-\\=\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\<\\>\\?\\:\\\\\\/]/";
return preg_replace($regex, addslashes('\\$0'), trim($string));
}
/**
* @param Request $request
*
* @return ProductListInterface
* @throws Exception
*/
public function applySearchForCurrentTenant(ProductListInterface $productListing, $term)
{
$methodName = 'apply' . ucfirst($this->currentTenant) . 'Search';
if (method_exists($this, $methodName)) {
return $this->$methodName($productListing, $term ?? '');
}
throw new Exception($methodName . ' method is not implemented');
}
/**
* @param ProductListInterface $productListing
* @param string $term
*
* @return ProductListInterface
*/
public function applyDefaultSearch(ProductListInterface $productListing, string $term)
{
$term = preg_replace('/\s+/', ' ', trim(ltrim($term), " "));
if (ctype_space($term) || $term == '') {
$productListing->addCondition("o_id = 'Nonexistent id'");
} else {
$productListing->addCondition(
"name LIKE '%" . $term . "%'
OR sku LIKE '%" . $term . "%' OR
o_id IN (
SELECT o_id FROM object_relations_Product WHERE src_id = o_id
AND fieldname = 'Products'
AND dest_id
IN (
SELECT oo_id
FROM object_store_Product
WHERE Name LIKE '%" . $term . "%'
OR SKU LIKE '%" . $term . "%'
)
)"
);
}
$productListing->setOrderKey(null);
$productListing->setOrder(null);
return $productListing;
}
/**
* @param ProductListInterface $productListing
* @param string $term
*
* @return ProductListInterface
*/
public function applyEsTenantSearch(ProductListInterface $productListing, string $term)
{
$term = $this->escapeElasticReservedChars($term);
if (ctype_space($term) || $term == '') {
$productListing->addCondition([
'term' => [
'_id' => 'Nonexistent id'
]
]);
} else {
$productListing->search(strtolower($term));
}
$productListing->setOrder(null);
$productListing->setOrderKey(null);
return $productListing;
}
}