src/Package/Openform/Front/Controller/MenuPageController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Package\Openform\Front\Controller;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use App\Package\Openform\Front\Controller\BaseController;
  10. use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use App\Package\Openform\RepositoryFront\FrontAboutUsRepository;
  13. use Symfony\Component\Intl\Locales;
  14. use App\Package\Openform\Entity\MenuPage;
  15. /**
  16.  * PageController
  17.  *
  18.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  19.  * @copyright  2019 Openform
  20.  * @since      07.2019
  21.  */
  22. class MenuPageController extends BaseController
  23. {
  24.     protected $routeLocalizer
  25.     private $em
  26.     private $menuPageRepo
  27.     
  28.     public function __construct(RequestStack $requestRouteLocalizer $routeLocalizerEntityManagerInterface $em)
  29.     {
  30.         parent::__construct($request$routeLocalizer$em);
  31.         
  32.         $this->em $em;
  33.         $this->routeLocalizer $routeLocalizer;
  34.         $this->menuPageRepo $this->em->getRepository('Openform:MenuPage')->getFrontRepository();
  35.     }    
  36.     public function indexAction(Request $request$makSlug$rootParentSlug$slug
  37.     {
  38.         $locale $request->getLocale();
  39.         $entity $this->menuPageRepo->getVisibleBySlug($slug$locale);
  40.         if (!$entity) {
  41.             throw new NotFoundHttpException();
  42.         }
  43.         
  44.         $rootParentReal MenuPage::getRootParent($entity);
  45.         
  46.         if (! $this->checkRootParent($rootParentReal$rootParentSlug$locale) ) {
  47.             throw new NotFoundHttpException();
  48.         } 
  49.         return $this->render('@openform_front_templates/MenuPage/index.html.twig', [
  50.             'entity' => $entity,
  51.             'rootParent' =>  MenuPage::getRootParent($entity),
  52.         ]);
  53.     }
  54.     private function checkRootParent($rootParentReal$slug$locale
  55.     {
  56.         $rootParentRepo $this->getRootParentRepo($rootParentReal);
  57.         
  58.         $rootParentFromSlug $rootParentRepo->getFrontRepository()->getVisiblebySlug($slug$locale);
  59.         if (! $rootParentFromSlug) {
  60.             return false;
  61.         }
  62.         
  63.         if ($rootParentFromSlug->getId() != $rootParentReal->getId()) {
  64.             return false
  65.         }
  66.         
  67.         return true;
  68.     }
  69.     
  70.     private function getRootParentRepo($entity
  71.     {
  72.         $className 'Openform:'.$this->getClassName($entity);
  73.         
  74.         return $this->em->getRepository$className );
  75.     }
  76.     
  77.     private function getClassName($entity)
  78.     {
  79.         return pathinfo(str_replace('\\''/'get_class($entity)), PATHINFO_FILENAME);
  80.     }
  81. }