<?php
namespace Schoettler\SteincoProducts\Storefront\Controller;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
use Symfony\Component\HttpFoundation\Request;
/**
* @RouteScope(scopes={"storefront"})
*/
class SchoettlerSearchController extends StorefrontController
{
public function redirectToFilteredPage(SalesChannelContext $context, $propertygroup, $propertyoption)
{
$parameters = $this->getParameters($propertygroup, $propertyoption);
$parameters['order'] = 'name-asc';
return $this->redirectToRoute('frontend.home.page', $parameters);
}
/**
* @RouteScope(scopes={"storefront"})
* @Route("/search/{propertygroup}/{propertyoption}", name="frontend.search", options={"seo"="false"}, methods={"GET"})
*/
public function redirectToExternalFilteredPage(SalesChannelContext $context, Request $request, $propertygroup, $propertyoption)
{
$propertyOptions = explode('|', $propertyoption);
$propertyGroups = explode('|', $propertygroup);
$groups_last_index= sizeof($propertyGroups) - 1;
$optionIds = [];
for ($i = 0; $i < sizeof($propertyOptions); $i++) {
$groupIndex = $i > $groups_last_index ? $groups_last_index : $i;
$optionIds[] = $this->getParameters($propertyGroups[$groupIndex], $propertyOptions[$i], $context->getContext());
}
foreach($optionIds as $key => $value){
if(empty($value)) {
unset($optionIds[$key]);
}
}
if(empty($optionIds)){
for ($i = 0; $i < sizeof($propertyOptions); $i++) {
$groupIndex = $i > $groups_last_index ? $groups_last_index : $i;
$optionIds[] = $this->getParameters(
$propertyGroups[$groupIndex],
$propertyOptions[$i],
\Shopware\Core\Framework\Context::createDefaultContext()
);
}
}
//dump($optionIds);
//die();// Um den Vorgang abzubrechen und sich nur den dump ausgeben zu lassen
$requesturl = $request->attributes->get('sw-storefront-url');
$requesturl = parse_url( $requesturl, PHP_URL_PATH);
$requesturl = str_replace( "/public", "/catalog", $requesturl);
$externalurl = $requesturl .'?p=1&properties=';
foreach($optionIds as $value){
if(array_key_exists('properties',$value)){
$externalurl .= $value['properties'] .'|';
}
}
if($this->endsWith($externalurl, '|')) {
$externalurl = substr($externalurl, 0, strlen($externalurl) -1);
} elseif($this->endsWith($externalurl, '=')) {
$externalurl = $requesturl;
}
return $this->redirect($externalurl);
}
private function getParameters($propertygroup, $propertyoption, $myContext)
{
// get the uuid of the propertyoptions
/** @var EntityRepositoryInterface $propertyGroupRepository */
$propertyGroupRepository = $this->container->get('property_group_translation.repository');
/** @var EntityCollection $propentityGroupEntities */
$propentityGroupEntities = $propertyGroupRepository->search(
(new Criteria())->addFilter(new EqualsFilter('name', $propertygroup)),
$myContext
/** \Shopware\Core\Framework\Context::createDefaultContext()*/
);
$propertyElements = $propentityGroupEntities->getEntities()->getElements();
$propertyElement = reset($propertyElements);
// no result
$propertygroupid = false;
if($propertyElement){
$propertygroupid= $propertyElement->getPropertyGroupId();
}
/** @var EntityRepositoryInterface $propertyRepository */
$propertyRepository = $this->container->get('property_group_option.repository');
$searchCriteria = new Criteria([]);
$searchCriteria->addFilter(new EqualsFilter('name', $propertyoption));
if($propertygroupid){
$searchCriteria->addFilter(new EqualsFilter('groupId', $propertygroupid));
}
/** @var EntityCollection $propentitiesEntities */
$propentitiesEntities = $propertyRepository->search(
($searchCriteria),
$myContext
/**\Shopware\Core\Framework\Context::createDefaultContext()*/
);
$properties = $propentitiesEntities->getEntities()->getElements();
$property = reset($properties);
//redirect to the filtered page
// ?order=name-asc&p=1&properties=3195595e2a3f48cf954384f802b264a8
$parameters = [];
if($property){
$parameters = [
'properties' => $property->getId()
];
}
return $parameters;
}
private function endsWith($haystack, $needle)
{
return substr_compare($haystack, $needle, -strlen($needle)) === 0;
}
}