<?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\FrontMissionRepository;
use Symfony\Component\Intl\Locales;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class MissionController extends BaseController
{
protected $routeLocalizer;
private $em;
private $missionRepo;
private $pageRepo;
private $perPage;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->routeLocalizer = $routeLocalizer;
$this->missionRepo = $this->em->getRepository('Openform:Mission')->getFrontRepository();
$this->pageRepo = $this->em->getRepository('Openform:Page')->getFrontRepository();
$this->perPage = 12;
}
public function indexAction(Request $request)
{
$locale = $request->getLocale();
$pageEntity = $this->getPageByIdname('page_mission', $request->getLocale());
$parentPageEntity = $this->getPageByIdname('home', $locale);
if (!$pageEntity) {
throw new NotFoundHttpException();
}
$defaultFilters = $this->getDefaultFilters(new \stdClass());
$filters = $this->getUrlFilters($defaultFilters, $request);
$entities = $this->missionRepo->getVisibleByFilter(
$locale,
$request->query->get('page') ?? 1,
$this->perPage,
clone $filters
);
$entitiesCount = $this->missionRepo->getVisibleByFilterCount($locale, clone $filters);
return $this->render('@openform_front_templates/Mission/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->missionRepo->getVisibleByFilter(
$locale,
$requestBody->page ?? 1,
$this->perPage,
clone $filters
);
$entitiesCount = $this->missionRepo->getVisibleByFilterCount($locale, clone $filters);
$maxPages = ceil($entitiesCount / $this->perPage);
// $filtersHtml = $this->renderView('@openform_front_templates/Mission/components/_filters.html.twig', [
// 'filters' => $filters,
// 'maxPages' => $maxPages,
// ]);
$entitiesHtml = $this->renderView('@openform_front_templates/Mission/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_mission', $locale);
$repo = $this->em->getRepository('Openform:Mission')->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->generateLangLinksMission($entity),
]);
}
private function getDefaultFilters(\stdClass $filters = null): \stdClass
{
if (!$filters) {
$filters = new \stdClass();
}
$filters->sort = FrontMissionRepository::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 generateLangLinksMission( $entity)
{
$repo = $this->em->getRepository('Openform:MissionTranslation');
$translations = $repo->findBy(['translatableId' => $entity->getId()]);
$langLinks = [];
foreach ($translations as $transItem){
if (strlen($transItem->getSlug()) > 0) {
$url = $this->routeLocalizer->generate('mission_item', ['slug' => $transItem->getSlug()], $transItem->getLang());
$langLinks[$transItem->getLang()] = $url;
}
}
return $langLinks;
}
}