<?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\FrontAboutUsRepository;
use Symfony\Component\Intl\Locales;
use App\Package\Openform\Entity\MenuPage;
/**
* PageController
*
* @author Daniel Balowski <d.balowski@openform.pl> (_creator)
* @copyright 2019 Openform
* @since 07.2019
*/
class MenuPageController extends BaseController
{
protected $routeLocalizer;
private $em;
private $menuPageRepo;
public function __construct(RequestStack $request, RouteLocalizer $routeLocalizer, EntityManagerInterface $em)
{
parent::__construct($request, $routeLocalizer, $em);
$this->em = $em;
$this->routeLocalizer = $routeLocalizer;
$this->menuPageRepo = $this->em->getRepository('Openform:MenuPage')->getFrontRepository();
}
public function indexAction(Request $request, $makSlug, $rootParentSlug, $slug)
{
$locale = $request->getLocale();
$entity = $this->menuPageRepo->getVisibleBySlug($slug, $locale);
if (!$entity) {
throw new NotFoundHttpException();
}
$rootParentReal = MenuPage::getRootParent($entity);
if (! $this->checkRootParent($rootParentReal, $rootParentSlug, $locale) ) {
throw new NotFoundHttpException();
}
return $this->render('@openform_front_templates/MenuPage/index.html.twig', [
'entity' => $entity,
'rootParent' => MenuPage::getRootParent($entity),
]);
}
private function checkRootParent($rootParentReal, $slug, $locale)
{
$rootParentRepo = $this->getRootParentRepo($rootParentReal);
$rootParentFromSlug = $rootParentRepo->getFrontRepository()->getVisiblebySlug($slug, $locale);
if (! $rootParentFromSlug) {
return false;
}
if ($rootParentFromSlug->getId() != $rootParentReal->getId()) {
return false;
}
return true;
}
private function getRootParentRepo($entity)
{
$className = 'Openform:'.$this->getClassName($entity);
return $this->em->getRepository( $className );
}
private function getClassName($entity)
{
return pathinfo(str_replace('\\', '/', get_class($entity)), PATHINFO_FILENAME);
}
}