<?php
namespace App\Package\Openform\Front\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Package\Openform\Front\Controller\BaseController;
use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use App\Package\Openform\RepositoryFront\FrontProposalRepository;
use Symfony\Component\Intl\Locales;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class ProposalController extends BaseController
{
protected $routeLocalizer;
private $em;
private $proposalRepo;
private $pageRepo;
private $perPage;
private $featured = false;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->routeLocalizer = $routeLocalizer;
$this->proposalRepo = $this->em->getRepository('Openform:Proposal')->getFrontRepository();
$this->pageRepo = $this->em->getRepository('Openform:Page')->getFrontRepository();
$this->perPage = 12;
$this->featured = $this->request->get('featured');
}
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$pageEntity = $this->getPageByIdname('page_proposal', $request->getLocale());
$parentPageEntity = $this->getPageByIdname('home', $locale);
if (!$pageEntity) {
throw new NotFoundHttpException();
}
$defaultFilters = $this->getDefaultFilters(new \stdClass());
$filters = $this->getUrlFilters($defaultFilters, $request);
$entities = $this->proposalRepo->getVisibleByFilter(
$locale,
$request->query->get('page') ?? 1,
$this->perPage,
clone $filters
);
$entitiesCount = $this->proposalRepo->getVisibleByFilterCount($locale, clone $filters);
return $this->render('@openform_front_templates/Proposal/index.html.twig', [
'entity' => $pageEntity,
'parentPage' => $parentPageEntity,
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'maxPages' => ceil($entitiesCount / $this->perPage),
'filters' => $filters,
]);
}
public function filterAjaxAction(Request $request): Response
{
$locale = $request->getLocale();
$requestBody = $request->getContent() ? json_decode($request->getContent()) : null;
if ( !$requestBody) {
throw new NotFoundHttpException('No requestBody');
}
if (!$requestBody->filters) {
throw new NotFoundHttpException('No requestBody->filters');
}
$defaultFilters = $this->getDefaultFilters($requestBody ? $requestBody->filters : new \StdClass);
$filters = $this->parseFilters($defaultFilters);
$entities = $this->proposalRepo->getVisibleByFilter(
$locale,
$requestBody->page ?? 1,
$this->perPage,
clone $filters
);
$entitiesCount = $this->proposalRepo->getVisibleByFilterCount($locale, clone $filters);
$maxPages = ceil($entitiesCount / $this->perPage);
// $filtersHtml = $this->renderView('@openform_front_templates/Proposal/components/_filters.html.twig', [
// 'filters' => $filters,
// 'maxPages' => $maxPages,
// ]);
$entitiesHtml = $this->renderView('@openform_front_templates/Proposal/components/_list.html.twig', [
'entities' => $entities,
'entitiesCount' => $entitiesCount,
'maxPages' => $maxPages,
]);
return new JsonResponse(array(
// 'filtersHtml' => $filtersHtml,
'entitiesHtml' => $entitiesHtml,
'maxPages' => $maxPages,
));
}
public function itemAction(Request $request, string $slug)
{
$locale = $request->getLocale();
$parentPage = $this->getPageByIdname('page_proposal', $locale);
$repo = $this->em->getRepository('Openform:Proposal')->getFrontRepository();
$entity = $repo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
return $this->render('@openform_front_templates/Page/index.html.twig', [
'entity' => $entity,
'parentPage' => $parentPage,
// 'langLinks' => $this->generateLangLinksProposal($entity),
]);
}
private function getDefaultFilters(\stdClass $filters = null): \stdClass
{
if (!$filters) {
$filters = new \stdClass();
}
if ($this->featured) {
$filters->featured = true;
}
$filters->sort = FrontProposalRepository::ORDER_ORD_DESC;
return $filters;
}
private function getUrlFilters(\stdClass $filters = null, Request $request): \stdClass
{
if ($request->query->get('dateFrom')) {
$filters->dateFrom = $request->query->get('dateFrom');
}
return $filters;
}
private function parseFilters(\stdClass $filters): \stdClass
{
foreach ($filters as $filter => $value) {
if (!$value) {
unset($filters->$filter);
}
}
return $filters;
}
protected function generateLangLinksProposal( $entity)
{
$repo = $this->em->getRepository('Openform:ProposalTranslation');
$translations = $repo->findBy(['translatableId' => $entity->getId()]);
$langLinks = [];
foreach ($translations as $transItem){
if (strlen($transItem->getSlug()) > 0) {
$url = $this->routeLocalizer->generate('proposal_item', ['slug' => $transItem->getSlug()], $transItem->getLang());
$langLinks[$transItem->getLang()] = $url;
}
}
return $langLinks;
}
}