custom/plugins/SteincoProducts/src/Storefront/Controller/SchoettlerSearchController.php line 32

Open in your IDE?
  1. <?php
  2. namespace Schoettler\SteincoProducts\Storefront\Controller;
  3. use Shopware\Storefront\Controller\StorefrontController;
  4. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  10. use Symfony\Component\HttpFoundation\Request;
  11. /**
  12. * @RouteScope(scopes={"storefront"})
  13. */
  14. class SchoettlerSearchController extends StorefrontController
  15. {
  16.     public function redirectToFilteredPage(SalesChannelContext $context$propertygroup$propertyoption)
  17.     {
  18.         $parameters $this->getParameters($propertygroup$propertyoption);
  19.         $parameters['order'] = 'name-asc';
  20.         return $this->redirectToRoute('frontend.home.page'$parameters);
  21.     }
  22.     /**
  23.      * @RouteScope(scopes={"storefront"})
  24.      * @Route("/search/{propertygroup}/{propertyoption}", name="frontend.search", options={"seo"="false"}, methods={"GET"})
  25.      */
  26.     public function redirectToExternalFilteredPage(SalesChannelContext $contextRequest $request$propertygroup$propertyoption)
  27.     {
  28.         $propertyOptions explode('|'$propertyoption);
  29.         $propertyGroups explode('|'$propertygroup);
  30.         $groups_last_indexsizeof($propertyGroups) - 1;
  31.         $optionIds = [];
  32.         for ($i 0$i sizeof($propertyOptions); $i++) {
  33.             $groupIndex $i $groups_last_index $groups_last_index $i;
  34.             $optionIds[] = $this->getParameters($propertyGroups[$groupIndex], $propertyOptions[$i], $context->getContext());
  35.         }
  36.         foreach($optionIds as $key => $value){
  37.             if(empty($value)) {
  38.                 unset($optionIds[$key]);
  39.             }
  40.         }
  41.         if(empty($optionIds)){
  42.             for ($i 0$i sizeof($propertyOptions); $i++) {
  43.             $groupIndex $i $groups_last_index $groups_last_index $i;
  44.                 $optionIds[] = $this->getParameters(
  45.                     $propertyGroups[$groupIndex],
  46.                     $propertyOptions[$i],
  47.                     \Shopware\Core\Framework\Context::createDefaultContext()
  48.                 );
  49.             }
  50.         }
  51.         //dump($optionIds);
  52.         //die();// Um den Vorgang abzubrechen und sich nur den dump ausgeben zu lassen
  53.         $requesturl $request->attributes->get('sw-storefront-url');
  54.         $requesturl parse_url$requesturlPHP_URL_PATH);
  55.         $requesturl str_replace"/public""/catalog"$requesturl);
  56.         $externalurl $requesturl .'?p=1&properties=';
  57.         foreach($optionIds as $value){
  58.             if(array_key_exists('properties',$value)){
  59.                 $externalurl .= $value['properties'] .'|';
  60.             }
  61.         }
  62.         if($this->endsWith($externalurl'|')) {
  63.             $externalurl substr($externalurl0strlen($externalurl) -1);
  64.         } elseif($this->endsWith($externalurl'=')) {
  65.             $externalurl $requesturl;
  66.         }
  67.         return $this->redirect($externalurl);
  68.     }
  69.     
  70.     private function getParameters($propertygroup$propertyoption$myContext)
  71.     {
  72.         // get the uuid of the propertyoptions
  73.         /** @var EntityRepositoryInterface $propertyGroupRepository */
  74.         $propertyGroupRepository $this->container->get('property_group_translation.repository');
  75.         /** @var EntityCollection $propentityGroupEntities */
  76.          $propentityGroupEntities $propertyGroupRepository->search(
  77.          (new Criteria())->addFilter(new EqualsFilter('name'$propertygroup)),
  78.              $myContext
  79.         /** \Shopware\Core\Framework\Context::createDefaultContext()*/
  80.         );
  81.         $propertyElements $propentityGroupEntities->getEntities()->getElements();
  82.         $propertyElement reset($propertyElements);
  83.         // no result
  84.         $propertygroupid false;
  85.         if($propertyElement){
  86.             $propertygroupid$propertyElement->getPropertyGroupId();
  87.         }
  88.         /** @var EntityRepositoryInterface $propertyRepository */
  89.         $propertyRepository $this->container->get('property_group_option.repository');
  90.         $searchCriteria = new Criteria([]);
  91.         $searchCriteria->addFilter(new EqualsFilter('name'$propertyoption));
  92.         if($propertygroupid){
  93.             $searchCriteria->addFilter(new EqualsFilter('groupId'$propertygroupid));
  94.         }
  95.         /** @var EntityCollection $propentitiesEntities */
  96.         $propentitiesEntities $propertyRepository->search(
  97.             ($searchCriteria),
  98.             $myContext
  99.           /**\Shopware\Core\Framework\Context::createDefaultContext()*/
  100.         );
  101.         $properties $propentitiesEntities->getEntities()->getElements();
  102.         $property reset($properties);
  103.         //redirect to the filtered page
  104.         // ?order=name-asc&p=1&properties=3195595e2a3f48cf954384f802b264a8
  105.         $parameters = [];
  106.         if($property){
  107.             $parameters = [ 
  108.                         'properties' => $property->getId()
  109.                     ];
  110.         }
  111.         return $parameters;
  112.     }
  113.     
  114.     private function endsWith($haystack$needle)
  115.     {
  116.         return substr_compare($haystack$needle, -strlen($needle)) === 0;
  117.     }
  118. }