custom/plugins/CbaxModulManufacturers/src/Controller/FrontendController.php line 280

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulManufacturers\Controller;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Storefront\Controller\StorefrontController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Cbax\ModulManufacturers\Components\ManufacturersHelper;
  12. use Cbax\ModulManufacturers\Components\ManufacturerSeo;
  13. use Cbax\ModulManufacturers\Components\ManufacturersPage;
  14. use Shopware\Storefront\Page\GenericPageLoader;
  15. use Shopware\Core\PlatformRequest;
  16. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  17. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  18. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  21. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  22. /**
  23.  * @RouteScope(scopes={"storefront"})
  24.  */
  25. class FrontendController extends StorefrontController
  26. {
  27.     private $helperComponent;
  28.     private $manufacturerSeo;
  29.     private $manufacturerRepository;
  30.     private $genericLoader;
  31.     private $mediaRepository;
  32.     private $systemConfigService;
  33.     public function __construct(
  34.         ManufacturersHelper $helperComponent,
  35.         ManufacturerSeo $manufacturerSeo,
  36.         EntityRepositoryInterface $manufacturerRepository,
  37.         GenericPageLoader $genericLoader,
  38.         EntityRepositoryInterface $mediaRepository,
  39.         SystemConfigService $systemConfigService
  40.     )
  41.     {
  42.         $this->helperComponent $helperComponent;
  43.         $this->manufacturerSeo $manufacturerSeo;
  44.         $this->manufacturerRepository $manufacturerRepository;
  45.         $this->genericLoader $genericLoader;
  46.         $this->mediaRepository $mediaRepository;
  47.         $this->systemConfigService $systemConfigService;
  48.     }
  49.     /**
  50.      * @HttpCache()
  51.      * alle Daten für die Herstellerübersicht holen und aufbereiten
  52.      * @param $request Request aktueller Request
  53.      * @param $context Context Standardcontext Object
  54.      * @return Object View mit Herstellerübersicht
  55.      * @Route("/cbax/manufacturer/index", name="frontend.cbax.manufacturer.index",  options={"seo"=true}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  56.      */
  57.     public function getManufacturers(Request $requestContext $context): Response
  58.     {
  59.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  60.         $salesChannelId     $salesChannelContext->getSalesChannel()->getId();
  61.         $config $this->systemConfigService->get('CbaxModulManufacturers'$salesChannelId)['config'];
  62.         if ($config['active'] !== true || empty($config['cmsPageIndex'])) {
  63.             return $this->forwardToRoute('frontend.home.page');
  64.         }
  65.         // Standard Page Object für Breadcrumb erzeugen
  66.         $newPage $this->genericLoader->load($request$salesChannelContext);
  67.         $page ManufacturersPage::createFrom($newPage);
  68.         $cmsPage $this->helperComponent->getCmsPage($config['cmsPageIndex'], $context);
  69.         $showProductCount false;
  70.         if ($cmsPage)
  71.         {
  72.             if ($cmsPage->getSections())
  73.             {
  74.                 $cmsPage->getSections()->sort(function (CmsSectionEntity $aCmsSectionEntity $b) {
  75.                     return $a->getPosition() <=> $b->getPosition();
  76.                 });
  77.                 $resolverContext = new ResolverContext($salesChannelContext$request);
  78.                 // sort blocks into sectionPositions
  79.                 foreach ($cmsPage->getSections() as $section) {
  80.                     $section->getBlocks()->sort(function (CmsBlockEntity $aCmsBlockEntity $b) {
  81.                         return $a->getPosition() <=> $b->getPosition();
  82.                     });
  83.                     // suche nach sidebar filter
  84.                     foreach ($section->getBlocks() as $block) {
  85.                         if (!empty($block) && $block->getType() === 'cbax-manufacturers-filter-button') {
  86.                             $filterElement = !empty($block->getSlots()) ? $block->getSlots()->first() : null;
  87.                             if (!empty($filterElement) && $filterElement->getType() === 'cbax-manufacturers-filter-button') {
  88.                                 $showProductCount $showProductCount || $filterElement->getTranslated()['config']['showArticleCountInSidebar']['value'];
  89.                                 if ($showProductCount) break;
  90.                             }
  91.                         }
  92.                         if (!empty($block) && $block->getType() === 'cbax-manufacturers-listing') {
  93.                             $listingElement = !empty($block->getSlots()) ? $block->getSlots()->first() : null;
  94.                             if (!empty($listingElement) && $listingElement->getType() === 'cbax-manufacturers-listing') {
  95.                                 $showProductCount $showProductCount || $listingElement->getTranslated()['config']['showArticleCountInOverview']['value'];
  96.                                 if ($showProductCount) break;
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.                 // find config overwrite
  102.                 //$overwrite = $config[$page->getId()] ?? $config;
  103.                 // overwrite slot config
  104.                 //$this->overwriteSlotConfig($page, $overwrite);
  105.                 // resolve slot data
  106.                 $this->helperComponent->loadSlotData($cmsPage$resolverContext);
  107.             }
  108.             $page->setCmsPage($cmsPage);
  109.         }
  110.         // Herstellerdaten aufbereiten
  111.         $pageData $this->helperComponent->getManufacturerByChar($showProductCount$config$salesChannelId$contexttrue);
  112.         $cbaxModulManufacturers['data'] = $pageData['sortedData'];
  113.         // weitere allgemeine Templatedaten ermitteln, aktuell Top Hersteller
  114.         $result2 $this->helperComponent->getTemplateData($pageData['unsortedData'], $config);
  115.         $cbaxModulManufacturers['templateData'] = $result2;
  116.         $cbaxModulManufacturers['page'] = 'index';
  117.         $link_index $this->manufacturerSeo->getSeoUrl('frontend.cbax.manufacturer.index''/cbax/manufacturer/index'$context);
  118.         $breadcrumbLinkName $this->container->get('translator')->trans('cbaxManufacturer.page.breadcrumb.allManufacturers');
  119.         $breadcrumb[] = array(
  120.             'link' => $link_index,
  121.             'name' => $breadcrumbLinkName,
  122.             'active' => true,
  123.             'translated' => array(
  124.                 'name' => $breadcrumbLinkName
  125.             )
  126.         );
  127.         //Blöcke mit leeren Elementen nicht laden
  128.         $page $this->helperComponent->filterEmptyCmsBlocksIndex($page$cbaxModulManufacturers);
  129.         return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
  130.             'cbaxModulManufacturers' => $cbaxModulManufacturers,
  131.             'page' => $page,
  132.             'breadcrumbList' => $breadcrumb
  133.         ]);
  134.     }
  135.     /**
  136.      * @HttpCache()
  137.      * alle Daten für die Herstellerdetailseite holen und aufbereiten
  138.      * @param $request Request aktueller Request
  139.      * @param $context Context Standardcontext Object
  140.      * @return Object View mit Herstellerdetailseite
  141.     * @Route("/cbax/manufacturer/detail/{id?}", name="frontend.cbax.manufacturer.detail", options={"seo"=true}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  142.     */
  143.     public function getManufacturer(Request $requestContext $context): Response
  144.     {
  145.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  146.         $salesChannelId     $salesChannelContext->getSalesChannel()->getId();
  147.         $config $this->systemConfigService->get('CbaxModulManufacturers'$salesChannelId)['config'];
  148.         $manufacturer_id $request->get('id');
  149.         $pageNumber $request->get('p');
  150.         // Checken, ob der Shopware Cache anfragt
  151.         if (empty($manufacturer_id)) {
  152.             $manufacturer_id $request->get('cbaxManufacturerId');
  153.             
  154.             if(empty($manufacturer_id)) {
  155.                 return $this->forwardToRoute('frontend.home.page');
  156.             }
  157.         }
  158.         // einzelnen Hersteller mit Bildern und Produkten holen
  159.         $criteria = new Criteria();
  160.         $criteria->addAssociation('media');
  161.         $criteria->addFilter(new EqualsFilter('id'$manufacturer_id));
  162.         $data $this->manufacturerRepository->search($criteria$context)->first();
  163.         if(empty($data)) {
  164.             return $this->forwardToRoute('frontend.home.page');
  165.         }
  166.         $cmsConfigPageId = !empty($config['cmsPageDetail']) ? $config['cmsPageDetail'] : null;
  167.         $cmsAltPageId = (!empty($data) && !empty($data->getCustomFields()) && !empty($data->getCustomFields()['cbaxManufacturerAltCmsPage'])) ? $data->getCustomFields()['cbaxManufacturerAltCmsPage'] : null;
  168.         $cmsPageId = !empty($cmsAltPageId) ? $cmsAltPageId $cmsConfigPageId;
  169.         if ($config['active'] !== true || empty($cmsPageId)) {
  170.             return $this->forwardToRoute('frontend.home.page');
  171.         }
  172.         if (!empty($data) && !empty($data->getCustomFields()) && !empty($data->getCustomFields()['cbaxManufacturerBannerMedia'])) {
  173.             $mediaCriteria = new Criteria();
  174.             $mediaCriteria->addFilter(new EqualsFilter('id'$data->getCustomFields()['cbaxManufacturerBannerMedia']));
  175.             $bannerMedia $this->mediaRepository->search($mediaCriteria$context)->first();
  176.         }
  177.         // Standard Page Object für Breadcrumb erzeugen
  178.         $newPage $this->genericLoader->load($request$salesChannelContext);
  179.         $page ManufacturersPage::createFrom($newPage);
  180.         $cmsPage $this->helperComponent->getCmsPage($cmsPageId$context);
  181.         $hasSidebarFilterBlock false;
  182.         $showProductCount false;
  183.         if ($cmsPage)
  184.         {
  185.             if ($cmsPage->getSections())
  186.             {
  187.                 $cmsPage->getSections()->sort(function (CmsSectionEntity $aCmsSectionEntity $b) {
  188.                     return $a->getPosition() <=> $b->getPosition();
  189.                 });
  190.                 $resolverContext = new ResolverContext($salesChannelContext$request);
  191.                 // sort blocks into sectionPositions
  192.                 foreach ($cmsPage->getSections() as $section) {
  193.                     $section->getBlocks()->sort(function (CmsBlockEntity $aCmsBlockEntity $b) {
  194.                         return $a->getPosition() <=> $b->getPosition();
  195.                     });
  196.                     // suche nach sidebar filter
  197.                     foreach ($section->getBlocks() as $block) {
  198.                         if (!empty($block) && $block->getType() === 'cbax-manufacturers-filter-button') {
  199.                             $filterElement = !empty($block->getSlots()) ? $block->getSlots()->first() : null;
  200.                             if (!empty($filterElement) && $filterElement->getType() === 'cbax-manufacturers-filter-button') {
  201.                                 $hasSidebarFilterBlock true;
  202.                                 $showProductCount $filterElement->getTranslated()['config']['showArticleCountInSidebar']['value'];
  203.                                 break;
  204.                             }
  205.                         }
  206.                     }
  207.                 }
  208.                 // find config overwrite
  209.                 //$overwrite = $config[$page->getId()] ?? $config;
  210.                 // overwrite slot config
  211.                 //$this->overwriteSlotConfig($page, $overwrite);
  212.                 // resolve slot data
  213.                 $this->helperComponent->loadSlotData($cmsPage$resolverContext);
  214.             }
  215.             $page->setCmsPage($cmsPage);
  216.         }
  217.         if ($hasSidebarFilterBlock) {
  218.             // alle Hersteller für Sidebar laden
  219.             $result $this->helperComponent->getManufacturerByChar($showProductCount$config$salesChannelId$context);
  220.             $cbaxModulManufacturers['data'] = $result;
  221.         }
  222.         // Produktlistendaten ermitteln
  223.         $data2 $this->helperComponent->getProductsListing($manufacturer_id$pageNumber$salesChannelContext);
  224.         if ($pageNumber 0) {
  225.             $limit $this->systemConfigService->get("core.listing.productsPerPage"$salesChannelId);
  226.             $limit = !empty($limit) ? (int)$limit 20;
  227.             $offset $limit * ($pageNumber -1);
  228.             $cbaxModulManufacturers['offset'] = $offset;
  229.         }
  230.         $cbaxModulManufacturers['manufacturer'] = $data;
  231.         if (!empty($bannerMedia) && !empty($bannerMedia->getUrl())) {
  232.             $cbaxModulManufacturers['bannerMedia'] = $bannerMedia;
  233.         }
  234.         $cbaxModulManufacturers['products'] = $data2;
  235.         $cbaxModulManufacturers['p'] = $pageNumber;
  236.         $cbaxModulManufacturers['page'] = 'detail';
  237.         $link_index $this->manufacturerSeo->getSeoUrl('frontend.cbax.manufacturer.index''/cbax/manufacturer/index'$context);
  238.         $link_detail $this->manufacturerSeo->getSeoUrl('frontend.cbax.manufacturer.detail''/cbax/manufacturer/detail/' $manufacturer_id$context);
  239.         $breadcrumbLinkName $this->container->get('translator')->trans('cbaxManufacturer.page.breadcrumb.allManufacturers');
  240.         $breadcrumb[] = array(
  241.             'link' => $link_index,
  242.             'name' => $breadcrumbLinkName,
  243.             'translated' => array(
  244.                 'name' => $breadcrumbLinkName
  245.             )
  246.         );
  247.         $breadcrumb[] = array(
  248.             'link' => $link_detail,
  249.             'name' => $cbaxModulManufacturers['manufacturer']->getName(),
  250.             'active' => true,
  251.             'translated' => array(
  252.                 'name' => $cbaxModulManufacturers['manufacturer']->getTranslated()['name']
  253.             )
  254.         );
  255.         //Blöcke mit leeren Elementen nicht laden
  256.         $page $this->helperComponent->filterEmptyCmsBlocksDetail($page$cbaxModulManufacturers);
  257.         return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
  258.             'cbaxModulManufacturers' => $cbaxModulManufacturers,
  259.             'page' => $page,
  260.             'breadcrumbList' => $breadcrumb
  261.         ]);
  262.     }
  263. }