custom/plugins/CbaxModulLexicon/src/Controller/FrontendController.php line 170

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulLexicon\Controller;
  3. use Shopware\Core\Framework\Context;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  8. use Cbax\ModulLexicon\Components\LexiconHelper;
  9. use Cbax\ModulLexicon\Components\LexiconSeo;
  10. use Cbax\ModulLexicon\Components\LexiconReplacer;
  11. use Shopware\Storefront\Page\GenericPageLoader;
  12. use Shopware\Core\PlatformRequest;
  13. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  14. use Shopware\Storefront\Controller\StorefrontController;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  17. /**
  18.  * @RouteScope(scopes={"storefront"})
  19.  */
  20. class FrontendController extends StorefrontController
  21. {
  22.     const CONFIG_PATH 'CbaxModulLexicon.config';
  23.     private $config null;
  24.     private $systemConfigService;
  25.     private $helperComponent;
  26.     private $lexiconSeo;
  27.     private $genericLoader;
  28.     private $lexiconReplacer;
  29.     public function __construct(
  30.         LexiconHelper $helperComponent,
  31.         LexiconSeo $lexiconSeo,
  32.         GenericPageLoader $genericLoader,
  33.         LexiconReplacer $lexiconReplacer,
  34.         SystemConfigService $systemConfigService)
  35.     {
  36.         $this->helperComponent $helperComponent;
  37.         $this->lexiconSeo $lexiconSeo;
  38.         $this->genericLoader $genericLoader;
  39.         $this->lexiconReplacer $lexiconReplacer;
  40.         $this->systemConfigService $systemConfigService;
  41.     }
  42.     /**
  43.     * @HttpCache()
  44.     * @Route("/cbax/lexicon/index", name="frontend.cbax.lexicon.index",  options={"seo"=true}, methods={"GET"})
  45.     */
  46.     public function index(Request $requestContext $context): Response
  47.     {
  48.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  49.         $salesChannelId $salesChannelContext->getSalesChannelId();
  50.         $this->config $this->config ?? $this->systemConfigService->get(self::CONFIG_PATH$salesChannelId);
  51.         if (empty($this->config['active'])) {
  52.             return $this->forwardToRoute('frontend.home.page');
  53.         }
  54.         // Standard Page Object für Breadcrumb erzeugen
  55.         $page $this->genericLoader->load($request$salesChannelContext);
  56.         $lexiconEntries $this->helperComponent->getAllLexiconEntries($context$salesChannelId$this->configtrue);
  57.         $cbaxModulLexicon['byChar'] = $this->helperComponent->getLexiconEntryByChar($context$request$lexiconEntries);
  58.         if ($this->config['newestEntriesActive'])
  59.         {
  60.             $cbaxModulLexicon['newestEntries'] = $this->helperComponent->getNewestEntries($lexiconEntries$this->config['newestEntriesCount']);
  61.         }
  62.         if ($this->config['mostreadEntriesActive'])
  63.         {
  64.             $cbaxModulLexicon['mostReadEntries'] = $this->helperComponent->getMostReadEntries($lexiconEntries$this->config['mostreadEntriesCount'], empty($this->config['newestEntriesActive']));
  65.         }
  66.         $cbaxModulLexicon['page'] = 'index';
  67.         $linkIndex $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.index''/cbax/lexicon/index'$context$request);
  68.         $breadcrumbLinkName $this->container->get('translator')->trans('cbaxLexicon.page.breadcrumb.name');
  69.         $breadcrumb[] = array(
  70.             'link' => $linkIndex,
  71.             'name' => $breadcrumbLinkName,
  72.             'active' => true,
  73.             'translated' => array(
  74.                 'name' => $breadcrumbLinkName
  75.             )
  76.         );
  77.         return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
  78.             'cbaxModulLexicon' => $cbaxModulLexicon,
  79.             'page' => $page,
  80.             'breadcrumbList' => $breadcrumb
  81.         ]);
  82.     }
  83.     /**
  84.     * @HttpCache()
  85.     * @Route("/cbax/lexicon/listing/{char?}", name="frontend.cbax.lexicon.listing",  options={"seo"=true}, methods={"GET"})
  86.     */
  87.     public function listing(Request $requestContext $context): Response
  88.     {
  89.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  90.         $salesChannelId $salesChannelContext->getSalesChannelId();
  91.         $this->config $this->config ?? $this->systemConfigService->get(self::CONFIG_PATH$salesChannelId);
  92.         if (empty($this->config['active'])) {
  93.             return $this->forwardToRoute('frontend.home.page');
  94.         }
  95.         // Standard Page Object für Breadcrumb erzeugen
  96.         $page $this->genericLoader->load($request$salesChannelContext);
  97.         $char $request->get('char');
  98.         if (empty($char)) {
  99.             return $this->forwardToRoute('frontend.cbax.lexicon.index');
  100.         }
  101.         $lexiconEntries $this->helperComponent->getAllLexiconEntries($context$salesChannelId$this->config);
  102.         $cbaxModulLexicon['byChar']     = $this->helperComponent->getLexiconEntryByChar($context$request$lexiconEntries);
  103.         $cbaxModulLexicon['char']         = $char;
  104.         $cbaxModulLexicon['entries']     = $this->helperComponent->getEntriesByChar($cbaxModulLexicon['byChar'], $char);
  105.         $cbaxModulLexicon['page']       = 'listing';
  106.         $linkIndex $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.index''/cbax/lexicon/index'$context$request);
  107.         $linkListing $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.listing' '/cbax/lexicon/listing/' $char$context$request);
  108.         $breadcrumbLinkName $this->container->get('translator')->trans('cbaxLexicon.page.breadcrumb.name');
  109.         $breadcrumb[] = array(
  110.             'link' => $linkIndex,
  111.             'name' => $breadcrumbLinkName,
  112.             'translated' => array(
  113.                 'name' => $breadcrumbLinkName
  114.             )
  115.         );
  116.         $breadcrumb[] = array(
  117.             'link' => $linkListing,
  118.             'name' => $char,
  119.             'active' => true,
  120.             'translated' => array(
  121.                 'name' => $char
  122.             )
  123.         );
  124.         return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
  125.             'cbaxModulLexicon' => $cbaxModulLexicon,
  126.             'page' => $page,
  127.             'breadcrumbList' => $breadcrumb
  128.         ]);
  129.     }
  130.     /**
  131.     * @HttpCache()
  132.     * @Route("/cbax/lexicon/detail/{id?}", name="frontend.cbax.lexicon.detail",  options={"seo"=true}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  133.     */
  134.     public function detail(Request $requestContext $context): Response
  135.     {
  136.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  137.         $salesChannelId $salesChannelContext->getSalesChannelId();
  138.         $this->config $this->config ?? $this->systemConfigService->get(self::CONFIG_PATH$salesChannelId);
  139.         if (empty($this->config['active'])) {
  140.             return $this->forwardToRoute('frontend.home.page');
  141.         }
  142.         $lexiconId $request->get('id');
  143.         if (empty($lexiconId)) {
  144.             return $this->forwardToRoute('frontend.cbax.lexicon.index');
  145.         }
  146.         $entry $this->helperComponent->getEntry($context$lexiconId);
  147.         // vormals ajax page
  148.         if ($request->isXmlHttpRequest()) {
  149.             $cbaxModulLexicon['entry'] = $entry;
  150.             $cbaxModulLexicon['modalTitle'] = $this->config['headline'];
  151.             // Impressions hochsetzen -> Muss später in Ajax Funktion
  152.             $this->helperComponent->updateImpressions($context$cbaxModulLexicon['entry']);
  153.             return $this->renderStorefront('@Storefront/storefront/cbax-lexicon/page/ajax.html.twig', [
  154.                 'cbaxModulLexicon' => $cbaxModulLexicon
  155.             ]);
  156.         }
  157.         // Standard Page Object für Breadcrumb erzeugen
  158.         $page $this->genericLoader->load($request$salesChannelContext);
  159.         if (empty($entry)) {
  160.             return $this->forwardToRoute('frontend.cbax.lexicon.index');
  161.         }
  162.         $shopUrl $request->attributes->get(RequestTransformer::STOREFRONT_URL);
  163.         if ($this->config['activeLexicon']) {
  164.             $translatedEntry $entry->getTranslated();
  165.             $translatedEntry['descriptionLong'] = $this->lexiconReplacer->getReplaceText($translatedEntry['descriptionLong'], $salesChannelId$shopUrl$context$request$this->config$lexiconId);
  166.             $translatedEntry['description'] = $this->lexiconReplacer->getReplaceText($translatedEntry['description'], $salesChannelId$shopUrl$context$request$this->config$lexiconId);
  167.             $entry->setTranslated($translatedEntry);
  168.         }
  169.         $char $this->helperComponent->getCharByEntry($entry);
  170.         // Impressions hochsetzen -> Muss später in Ajax Funktion
  171.         $this->helperComponent->updateImpressions($context$entry);
  172.         $lexiconEntries $this->helperComponent->getAllLexiconEntries($context$salesChannelId$this->config);
  173.         $cbaxModulLexicon['byChar']     = $this->helperComponent->getLexiconEntryByChar($context$request$lexiconEntries);
  174.         $cbaxModulLexicon['entry']         = $entry;
  175.         $cbaxModulLexicon['char']          = $char;
  176.         $cbaxModulLexicon['entries']     = $this->helperComponent->getEntriesByChar($cbaxModulLexicon['byChar'], $char);
  177.         $cbaxModulLexicon['products']     = $this->helperComponent->getProducts($context$entry$salesChannelContext);
  178.         $cbaxModulLexicon['pagination'] = $this->helperComponent->getPagination($cbaxModulLexicon['entries'], $entry);
  179.         $cbaxModulLexicon['page']       = 'detail';
  180.         $linkIndex $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.index''/cbax/lexicon/index'$context$request);
  181.         $linkListing $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.listing' '/cbax/lexicon/listing/' $char$context$request);
  182.         $link_detail $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.detail''/cbax/lexicon/detail/' $lexiconId$context$request);
  183.         $breadcrumbLinkName $this->container->get('translator')->trans('cbaxLexicon.page.breadcrumb.name');
  184.         $breadcrumb[] = array(
  185.             'link' => $linkIndex,
  186.             'name' => $breadcrumbLinkName,
  187.             'translated' => array(
  188.                 'name' => $breadcrumbLinkName
  189.             )
  190.         );
  191.         $breadcrumb[] = array(
  192.             'link' => $linkListing,
  193.             'name' => $char,
  194.             'translated' => array(
  195.                 'name' => $char
  196.             )
  197.         );
  198.         $breadcrumb[] = array(
  199.             'link' => $link_detail,
  200.             'name' => $entry->getTranslated()['title'],
  201.             'active' => true,
  202.             'translated' => array(
  203.                 'name' => $entry->getTranslated()['title']
  204.             )
  205.         );
  206.         return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
  207.             'cbaxModulLexicon' => $cbaxModulLexicon,
  208.             'page' => $page,
  209.             'breadcrumbList' => $breadcrumb
  210.         ]);
  211.     }
  212.     /**
  213.     * @HttpCache()
  214.     * @Route("/cbax/lexicon/ajax/{id?}", name="frontend.cbax.lexicon.ajax",  options={"seo"=true}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  215.      **/
  216.     //obsolet!
  217.     public function ajax(Request $requestContext $context): Response
  218.     {
  219.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  220.         $salesChannelId $salesChannelContext->getSalesChannelId();
  221.         $this->config $this->config ?? $this->systemConfigService->get(self::CONFIG_PATH$salesChannelId);
  222.         if (empty($this->config['active'])) {
  223.             return $this->forwardToRoute('frontend.home.page');
  224.         }
  225.         $lexiconId $request->get('id');
  226.         if (empty($lexiconId)) {
  227.             return $this->forwardToRoute('frontend.cbax.lexicon.index');
  228.         }
  229.         return $this->forwardToRoute('frontend.cbax.lexicon.detail', array('id' => $lexiconId));
  230.         /*
  231.         if (!$request->isXmlHttpRequest()) {
  232.             return $this->forwardToRoute('frontend.cbax.lexicon.detail', array('id' => $lexiconId));
  233.         }
  234.         $cbaxModulLexicon['entry'] = $this->helperComponent->getEntry($context, $lexiconId);
  235.         $cbaxModulLexicon['modalTitle'] = $this->config['headline'];
  236.         // Impressions hochsetzen -> Muss später in Ajax Funktion
  237.         $this->helperComponent->updateImpressions($context, $cbaxModulLexicon['entry']);
  238.         return $this->renderStorefront('@Storefront/storefront/cbax-lexicon/page/ajax.html.twig', [
  239.             'cbaxModulLexicon' => $cbaxModulLexicon
  240.         ]);
  241.         */
  242.     }
  243.     /**
  244.     * @HttpCache()
  245.     * @Route("/cbax/lexicon/content", name="frontend.cbax.lexicon.content",  options={"seo"=true}, methods={"GET"})
  246.     */
  247.     public function content(Request $requestContext $context): Response
  248.     {
  249.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  250.         $salesChannelId $salesChannelContext->getSalesChannelId();
  251.         $this->config $this->config ?? $this->systemConfigService->get(self::CONFIG_PATH$salesChannelId);
  252.         if (empty($this->config['active'])) {
  253.             return $this->forwardToRoute('frontend.home.page');
  254.         }
  255.         // Standard Page Object für Breadcrumb erzeugen
  256.         $page $this->genericLoader->load($request$salesChannelContext);
  257.         $lexiconEntries $this->helperComponent->getAllLexiconEntries($context$salesChannelId$this->config);
  258.         $cbaxModulLexicon['byChar']     = $this->helperComponent->getLexiconEntryByChar($context$request$lexiconEntries);
  259.         $cbaxModulLexicon['page'] = 'content';
  260.         $linkIndex $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.index''/cbax/lexicon/index'$context$request);
  261.         $link_content $this->lexiconSeo->getSeoUrl('frontend.cbax.lexicon.content' '/cbax/lexicon/content'$context$request);
  262.         $breadcrumbLinkName $this->container->get('translator')->trans('cbaxLexicon.page.breadcrumb.name');
  263.         $breadcrumbLinkContent $this->container->get('translator')->trans('cbaxLexicon.page.navigation.linkContent');
  264.         $breadcrumb[] = array(
  265.             'link' => $linkIndex,
  266.             'name' => $breadcrumbLinkName,
  267.             'translated' => array(
  268.                 'name' => $breadcrumbLinkName
  269.             )
  270.         );
  271.         $breadcrumb[] = array(
  272.             'link' => $link_content,
  273.             'name' => $breadcrumbLinkContent,
  274.             'active' => true,
  275.             'translated' => array(
  276.                 'name' => $breadcrumbLinkContent
  277.             )
  278.         );
  279.         return $this->renderStorefront('@Storefront/storefront/base.html.twig', [
  280.             'cbaxModulLexicon' => $cbaxModulLexicon,
  281.             'page' => $page,
  282.             'breadcrumbList' => $breadcrumb
  283.         ]);
  284.     }
  285. }