custom/plugins/CogiGlossary/src/Controller/StorefrontController.php line 125

Open in your IDE?
  1. <?php
  2. namespace CogiGlossary\Controller;
  3. use CogiGlossary\Glossary\GlossaryEntity;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\DBAL\FetchMode;
  6. use Doctrine\DBAL\ParameterType;
  7. use GuzzleHttp\Client;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  14. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Shopware\Storefront\Page\GenericPageLoader;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpFoundation\Session\Session;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  25. use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
  26. /**
  27.  * @RouteScope(scopes={"storefront"})
  28.  */
  29. class StorefrontController extends \Shopware\Storefront\Controller\StorefrontController
  30. {
  31.     /**
  32.      * @var EntityRepositoryInterface
  33.      */
  34.     private $repository;
  35.     private $genericLoader;
  36.     private $systemConfigService;
  37.     public function __construct(
  38.         GenericPageLoader $genericLoader,
  39.         SystemConfigService $systemConfigService,
  40.         EntityRepositoryInterface $repository
  41.     )
  42.     {
  43.         $this->genericLoader $genericLoader;
  44.         $this->systemConfigService $systemConfigService;
  45.         $this->repository $repository;
  46.     }
  47.     /**
  48.      * @Route("/cogi-glossary/hover/{glossaryId}", name="cogi-glossary.hover", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  49.      */
  50.     public function hover(string $glossaryIdSalesChannelContext $context): JsonResponse
  51.     {
  52.         $criteria = new Criteria();
  53.         $criteria->addAssociation('media');
  54.         $criteria->addFilter(new EqualsFilter('active'1));
  55.         $cogiGlossary $this->repository->search($criteria$context->getContext())->get($glossaryId);
  56.         if (!$glossaryId || !$cogiGlossary) {
  57.             $response[] = [
  58.                 'type' => 'danger',
  59.                 'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
  60.                     'type' => 'danger',
  61.                     'list' => [$violations[] = $this->trans('cogi-glossary.pageNotFound')],
  62.                 ]),
  63.             ];
  64.         } else {
  65.             $response[] = [
  66.                 'type' => 'success',
  67.                 'alert' => $this->renderView('plugin/cogi-glossary/page/hover.html.twig', [
  68.                     'glossaryId' => $glossaryId,
  69.                     'glossary' => $cogiGlossary
  70.                 ])
  71.             ];
  72.         }
  73.         return new JsonResponse($response);
  74.     }
  75.     /**
  76.      * @HttpCache()
  77.      * @Route("/cogi-glossary/{firstChar}", name="cogi-glossary.index", methods={"GET"}, defaults={"firstChar"="#"})
  78.      */
  79.     public function char(string $firstCharRequest $requestRequestDataBag $dataSalesChannelContext $context): Response
  80.     {
  81.         $connection $this->container->get(Connection::class);
  82.         $sql = <<<SQL
  83. SELECT
  84.     `first_char` as `firstChar`, 
  85.     count(`first_char`) as `entryCount`
  86. FROM `cogi_glossary`
  87. WHERE `active` = 1
  88. GROUP BY `first_char`
  89. SQL;
  90.         $cogiGlossaryIndex $connection->executeQuery($sql, [])->fetchAll(FetchMode::ASSOCIATIVE);
  91.         $criteria = new Criteria();
  92.         $criteria->addAssociation('media');
  93.         $criteria->addFilter(new EqualsFilter('active'1));
  94.         $criteria->addFilter(new EqualsFilter('firstChar'$firstChar));
  95.         $criteria->addSorting(new FieldSorting('name'FieldSorting::ASCENDING));
  96.         $cogiGlossary $this->repository->search($criteria$context->getContext())->getEntities();
  97.         $page $this->genericLoader->load($request$context);
  98.         $metaInformation $page->getMetaInformation();
  99.         $metaInformation->setMetaTitle($firstChar " | " $this->trans('cogi-glossary.title') . " | " $metaInformation->getMetaTitle());
  100.         $page->setMetaInformation($metaInformation);
  101.         return $this->renderStorefront('@Storefront/plugin/cogi-glossary/page/index.html.twig', [
  102.             'page' => $page,
  103.             'glossaryIndex' => $cogiGlossaryIndex,
  104.             'glossary' => $cogiGlossary,
  105.             'glossarySelected' => $firstChar
  106.         ]);
  107.     }
  108.     /**
  109.      * @HttpCache()
  110.      * @Route("/cogi-glossary/{firstChar}/{glossaryId}", name="cogi-glossary.article", methods={"GET"})
  111.      */
  112.     public function entry(string $firstCharstring $glossaryIdRequest $requestSalesChannelContext $context): Response
  113.     {
  114.         $criteria = new Criteria();
  115.         $criteria->addAssociation('media');
  116.         $criteria->addFilter(new EqualsFilter('active'1));
  117.         $criteria->addSorting(new FieldSorting('name'FieldSorting::ASCENDING));
  118.         $cogiGlossary $this->repository->search($criteria$context->getContext())->get($glossaryId);
  119.         $cogiGlossaryCollection $this->repository->search($criteria$context->getContext())->getEntities();
  120. //        /** @var GlossaryEntity $cogiGlossary */
  121. //        $cogiGlossary = $cogiGlossaryCollection->get($glossaryId);
  122.         if (!$glossaryId || !$firstChar || !$cogiGlossary) {
  123.             $this->addFlash('danger'$this->trans('cogi-glossary.pageNotFound'));
  124.             if ($firstChar) {
  125.                 return $this->redirectToRoute('cogi-glossary.index', ['firstChar' => $firstChar]);
  126.             }
  127.             return $this->redirectToRoute('cogi-glossary.index');
  128.         }
  129.         /** @var GlossaryEntity $firstEntity */
  130.         $firstEntity $cogiGlossaryCollection->first();
  131.         /** @var GlossaryEntity $firstEntity */
  132.         $lastEntity $cogiGlossaryCollection->last();
  133.         if (!$cogiGlossary->getPrev()) {
  134.             $cogiGlossary->setPrev($lastEntity);
  135.         }
  136.         if (!$cogiGlossary->getNext()) {
  137.             $cogiGlossary->setNext($firstEntity);
  138.         }
  139.         $page $this->genericLoader->load($request$context);
  140.         $metaInformation $page->getMetaInformation();
  141.         $metaInformation->setMetaTitle($cogiGlossary->getName() . " | " $this->trans('cogi-glossary.title') . " | " $metaInformation->getMetaTitle());
  142.         $page->setMetaInformation($metaInformation);
  143.         return $this->renderStorefront('@Storefront/plugin/cogi-glossary/page/article.html.twig', [
  144.             'page' => $page,
  145.             'glossaryId' => $glossaryId,
  146.             'glossary' => $cogiGlossary
  147.         ]);
  148.     }
  149. }