src/Service/SearchService.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Exception;
  4. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  5. use App\Service\Contract\SearchServiceContract;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class SearchService implements SearchServiceContract
  8. {
  9.     protected $currentTenant;
  10.     public function __construct($tenant)
  11.     {
  12.         $this->currentTenant $tenant;
  13.     }
  14.     /**
  15.      * @param Request $request
  16.      *
  17.      * @return ProductListInterface
  18.      * @throws Exception
  19.      */
  20.     public function getCurrentSearchListing(Request $request)
  21.     {
  22.         $methodName 'get' ucfirst($this->currentTenant) . 'SearchListing';
  23.         if (method_exists($this$methodName)) {
  24.             return $this->$methodName($request);
  25.         }
  26.         throw new Exception($methodName ' method is not implemented');
  27.     }
  28.     /**
  29.      * @param Request $request
  30.      *
  31.      * @return ProductListInterface
  32.      */
  33.     public function getDefaultSearchListing(Request $request)
  34.     {
  35.         $ecommerceFactory \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance();
  36.         $environment $ecommerceFactory->getEnvironment();
  37.         $environment->setCurrentAssortmentTenant("default");
  38.         $productListing \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getIndexService()->getProductListForCurrentTenant();
  39.         $productListing->addCondition("o_type = 'object'"'o_type');
  40.         $productListing->addCondition("product_type = 'grouped'"'product_type');
  41.         $productListing->setOrderKey('LTRIM(name)');
  42.         $productListing->setOrder('asc');
  43.         return $productListing;
  44.     }
  45.     /**
  46.      * @param Request $request
  47.      *
  48.      * @return ProductListInterface
  49.      */
  50.     public function getEsTenantSearchListing(Request $request)
  51.     {
  52.         $ecommerceFactory \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance();
  53.         $environment $ecommerceFactory->getEnvironment();
  54.         $environment->setCurrentAssortmentTenant("EsTenant");
  55.         $productListing \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getIndexService()->getProductListForCurrentTenant();
  56.         $productListing->setDefaultFilters($request->get('division'));
  57.         $productListing->setOrder('asc');
  58.         $productListing->setOrderKey('name');
  59.         return $productListing;
  60.     }
  61.     public function escapeElasticReservedChars($string) {
  62.         $regex "/[\\+\\-\\=\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\<\\>\\?\\:\\\\\\/]/";
  63.         return preg_replace($regexaddslashes('\\$0'), trim($string));
  64.     }
  65.     /**
  66.      * @param Request $request
  67.      *
  68.      * @return ProductListInterface
  69.      * @throws Exception
  70.      */
  71.     public function applySearchForCurrentTenant(ProductListInterface $productListing$term)
  72.     {
  73.         $methodName 'apply' ucfirst($this->currentTenant) . 'Search';
  74.         if (method_exists($this$methodName)) {
  75.             return $this->$methodName($productListing$term ?? '');
  76.         }
  77.         throw new Exception($methodName ' method is not implemented');
  78.     }
  79.     /**
  80.      * @param ProductListInterface $productListing
  81.      * @param string               $term
  82.      *
  83.      * @return ProductListInterface
  84.      */
  85.     public function applyDefaultSearch(ProductListInterface $productListingstring $term)
  86.     {
  87.         $term preg_replace('/\s+/'' 'trim(ltrim($term), " "));
  88.         if (ctype_space($term) || $term == '') {
  89.             $productListing->addCondition("o_id = 'Nonexistent id'");
  90.         } else {
  91.             $productListing->addCondition(
  92.                 "name LIKE '%" $term "%'
  93.                     OR sku LIKE '%" $term "%' OR
  94.                     o_id IN (
  95.                     SELECT o_id FROM object_relations_Product WHERE src_id = o_id
  96.                     AND fieldname = 'Products'
  97.                     AND dest_id
  98.                     IN (
  99.                     SELECT oo_id
  100.                     FROM object_store_Product
  101.                     WHERE Name LIKE '%" $term "%'
  102.                     OR SKU LIKE '%" $term "%'
  103.                     )
  104.                 )"
  105.             );
  106.         }
  107.         $productListing->setOrderKey(null);
  108.         $productListing->setOrder(null);
  109.         return $productListing;
  110.     }
  111.     /**
  112.      * @param ProductListInterface $productListing
  113.      * @param string               $term
  114.      *
  115.      * @return ProductListInterface
  116.      */
  117.     public function applyEsTenantSearch(ProductListInterface $productListingstring $term)
  118.     {
  119.         $term $this->escapeElasticReservedChars($term);
  120.         if (ctype_space($term) || $term == '') {
  121.             $productListing->addCondition([
  122.                 'term' => [
  123.                     '_id' => 'Nonexistent id'
  124.                 ]
  125.             ]);
  126.         } else {
  127.             $productListing->search(strtolower($term));
  128.         }
  129.         $productListing->setOrder(null);
  130.         $productListing->setOrderKey(null);
  131.         return $productListing;
  132.     }
  133. }