<?php
namespace CogiGlossary\Controller;
use CogiGlossary\Glossary\GlossaryEntity;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use GuzzleHttp\Client;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\GenericPageLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
/**
* @RouteScope(scopes={"storefront"})
*/
class StorefrontController extends \Shopware\Storefront\Controller\StorefrontController
{
/**
* @var EntityRepositoryInterface
*/
private $repository;
private $genericLoader;
private $systemConfigService;
public function __construct(
GenericPageLoader $genericLoader,
SystemConfigService $systemConfigService,
EntityRepositoryInterface $repository
)
{
$this->genericLoader = $genericLoader;
$this->systemConfigService = $systemConfigService;
$this->repository = $repository;
}
/**
* @Route("/cogi-glossary/hover/{glossaryId}", name="cogi-glossary.hover", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function hover(string $glossaryId, SalesChannelContext $context): JsonResponse
{
$criteria = new Criteria();
$criteria->addAssociation('media');
$criteria->addFilter(new EqualsFilter('active', 1));
$cogiGlossary = $this->repository->search($criteria, $context->getContext())->get($glossaryId);
if (!$glossaryId || !$cogiGlossary) {
$response[] = [
'type' => 'danger',
'alert' => $this->renderView('@Storefront/storefront/utilities/alert.html.twig', [
'type' => 'danger',
'list' => [$violations[] = $this->trans('cogi-glossary.pageNotFound')],
]),
];
} else {
$response[] = [
'type' => 'success',
'alert' => $this->renderView('plugin/cogi-glossary/page/hover.html.twig', [
'glossaryId' => $glossaryId,
'glossary' => $cogiGlossary
])
];
}
return new JsonResponse($response);
}
/**
* @HttpCache()
* @Route("/cogi-glossary/{firstChar}", name="cogi-glossary.index", methods={"GET"}, defaults={"firstChar"="#"})
*/
public function char(string $firstChar, Request $request, RequestDataBag $data, SalesChannelContext $context): Response
{
$connection = $this->container->get(Connection::class);
$sql = <<<SQL
SELECT
`first_char` as `firstChar`,
count(`first_char`) as `entryCount`
FROM `cogi_glossary`
WHERE `active` = 1
GROUP BY `first_char`
SQL;
$cogiGlossaryIndex = $connection->executeQuery($sql, [])->fetchAll(FetchMode::ASSOCIATIVE);
$criteria = new Criteria();
$criteria->addAssociation('media');
$criteria->addFilter(new EqualsFilter('active', 1));
$criteria->addFilter(new EqualsFilter('firstChar', $firstChar));
$criteria->addSorting(new FieldSorting('name', FieldSorting::ASCENDING));
$cogiGlossary = $this->repository->search($criteria, $context->getContext())->getEntities();
$page = $this->genericLoader->load($request, $context);
$metaInformation = $page->getMetaInformation();
$metaInformation->setMetaTitle($firstChar . " | " . $this->trans('cogi-glossary.title') . " | " . $metaInformation->getMetaTitle());
$page->setMetaInformation($metaInformation);
return $this->renderStorefront('@Storefront/plugin/cogi-glossary/page/index.html.twig', [
'page' => $page,
'glossaryIndex' => $cogiGlossaryIndex,
'glossary' => $cogiGlossary,
'glossarySelected' => $firstChar
]);
}
/**
* @HttpCache()
* @Route("/cogi-glossary/{firstChar}/{glossaryId}", name="cogi-glossary.article", methods={"GET"})
*/
public function entry(string $firstChar, string $glossaryId, Request $request, SalesChannelContext $context): Response
{
$criteria = new Criteria();
$criteria->addAssociation('media');
$criteria->addFilter(new EqualsFilter('active', 1));
$criteria->addSorting(new FieldSorting('name', FieldSorting::ASCENDING));
$cogiGlossary = $this->repository->search($criteria, $context->getContext())->get($glossaryId);
$cogiGlossaryCollection = $this->repository->search($criteria, $context->getContext())->getEntities();
// /** @var GlossaryEntity $cogiGlossary */
// $cogiGlossary = $cogiGlossaryCollection->get($glossaryId);
if (!$glossaryId || !$firstChar || !$cogiGlossary) {
$this->addFlash('danger', $this->trans('cogi-glossary.pageNotFound'));
if ($firstChar) {
return $this->redirectToRoute('cogi-glossary.index', ['firstChar' => $firstChar]);
}
return $this->redirectToRoute('cogi-glossary.index');
}
/** @var GlossaryEntity $firstEntity */
$firstEntity = $cogiGlossaryCollection->first();
/** @var GlossaryEntity $firstEntity */
$lastEntity = $cogiGlossaryCollection->last();
if (!$cogiGlossary->getPrev()) {
$cogiGlossary->setPrev($lastEntity);
}
if (!$cogiGlossary->getNext()) {
$cogiGlossary->setNext($firstEntity);
}
$page = $this->genericLoader->load($request, $context);
$metaInformation = $page->getMetaInformation();
$metaInformation->setMetaTitle($cogiGlossary->getName() . " | " . $this->trans('cogi-glossary.title') . " | " . $metaInformation->getMetaTitle());
$page->setMetaInformation($metaInformation);
return $this->renderStorefront('@Storefront/plugin/cogi-glossary/page/article.html.twig', [
'page' => $page,
'glossaryId' => $glossaryId,
'glossary' => $cogiGlossary
]);
}
}