src/Controller/DefaultController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Quotation\Quotation;
  4. use App\Form\ContactType;
  5. use App\Form\Quotation\QuotationType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\Finder\Finder;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class DefaultController extends AbstractController
  13. {
  14.     public function __construct(EntityManagerInterface $entityManagerKernelInterface $kernel)
  15.     {
  16.         $this->em $entityManager;
  17.         $this->kernel $kernel;
  18.     }
  19.     /**
  20.      * @Route("/", name="app_index")
  21.      */
  22.     public function index(Request $request)
  23.     {
  24.         return $this->redirectToRoute('app_home', ['_locale' => 'fr']);
  25.     }
  26.     /**
  27.      * @Route({
  28.      *   "fr": "/fr/",
  29.      *   "en": "/en/"
  30.      * }, name="app_home")
  31.      */
  32.     public function home(Request $request)
  33.     {
  34.         $locale $request->getLocale();
  35.         $page $this->em->getRepository('App:Page\Page')->findHomeLeftJoined($locale);
  36.         if (!$page) {
  37.             throw $this->createNotFoundException('La page n\'a pas été trouvé.');
  38.         }
  39.         $contents $this->getContents($page$locale);
  40.         return $this->render('theme/default/home.html.twig', [
  41.             'page' => $page,
  42.             'contents' => $contents,
  43.         ]);
  44.     }
  45.     /**
  46.      * @Route({
  47.      *   "fr": "/fr/demande-de-devis",
  48.      *   "en": "/en/request-a-quotation"
  49.      * }, name="app_quotation_create")
  50.      */
  51.     public function quotation(Request $request)
  52.     {
  53.         $quotation = new Quotation();
  54.         $form $this->createForm(QuotationType::class, $quotation);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted()) {
  57.             if ($form->isValid()) {
  58.                 $quotation->setReference(uniqid());
  59.                 $this->em->persist($quotation);
  60.                 $this->em->flush();
  61.                 $firstname $form->get('firstname')->getData();
  62.                 return $this->render('theme/default/quotation_end.html.twig', [
  63.                     'firstname' => $firstname
  64.                 ]);
  65.             }
  66.         }
  67.         return $this->render('theme/default/quotation.html.twig', [
  68.             'form' => $form->createView()
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route({
  73.      *   "fr": "/fr/contact",
  74.      *   "en": "/en/contact"
  75.      * }, name="app_contact")
  76.      */
  77.     public function contact(Request $request)
  78.     {
  79.         $locale $request->getLocale();
  80.         $page $this->em->getRepository('App:Page\Page')->findContactPageLeftJoined($locale);
  81.         if (!$page) {
  82.             throw $this->createNotFoundException('La page n\'a pas été trouvé.');
  83.         }
  84.         $contents $this->getContents($page$locale);
  85.         $form $this->createForm(ContactType::class);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted()) {
  88.             if ($form->isValid()) {
  89.                 $this->addFlash('success''Le formulaire de contact a bien été envoyé.');
  90.                 return $this->redirectToRoute('app_contact');
  91.             }
  92.         }
  93.         return $this->render('theme/default/contact.html.twig', [
  94.             'page' => $page,
  95.             'contents' => $contents,
  96.             'form' => $form->createView()
  97.         ]);
  98.     }
  99.     /**
  100.      * @Route({
  101.      * "fr": "/fr/mon-compte",
  102.      * "en": "/en/my-account",
  103.      * }, name="app_user_folders")
  104.      */
  105.     public function folders()
  106.     {
  107.         $user $this->getUser();
  108.         if ($this->isGranted('ROLE_ADMIN')) {
  109.             $folders $this->em->getRepository('App:Folder\Folder')->findRootFoldersForAdmin();    
  110.         } else {
  111.             $folders $this->em->getRepository('App:Folder\Folder')->findRootFoldersForUser($user);   
  112.         }
  113.         
  114.         return $this->render('theme/default/folders.html.twig', [
  115.             'user' => $user,
  116.             'folders' => $folders
  117.         ]);
  118.     }
  119.     /**
  120.      * @Route({
  121.      * "fr": "/fr/dossier/{id}",
  122.      * "en": "/en/folder/{id}",
  123.      * }, name="app_user_folder")
  124.      */
  125.     public function folder($id)
  126.     {
  127.         $user $this->getUser();
  128.         $folder $this->em->getRepository('App:Folder\Folder')->find($id);
  129.         if (!$folder) {
  130.             throw $this->createNotFoundException('Resource not found.');
  131.         }
  132.         $parent $folder;
  133.         // Security
  134.         if (!$this->isGranted('ROLE_ADMIN')) {
  135.             $acccessGranted false;
  136.     
  137.             if ($folder->getParent() != null) {
  138.                 while ($folder->getParent() != null) {
  139.                     $folder $folder->getParent();
  140.     
  141.                     if ($folder->getUsers()->contains($user)) {
  142.                         $acccessGranted true;
  143.                     }
  144.                 
  145.                     foreach($user->getGroups() as $group) {
  146.                         if ($folder->getGroups()->contains($group)) {
  147.                             $acccessGranted true;
  148.                         }
  149.                     }
  150.                 }
  151.             } else {
  152.                 if ($folder->getUsers()->contains($user)) {
  153.                     $acccessGranted true;
  154.                 }
  155.             
  156.                 foreach($user->getGroups() as $group) {
  157.                     if ($folder->getGroups()->contains($group)) {
  158.                         $acccessGranted true;
  159.                     }
  160.                 }
  161.             }
  162.             
  163.             if (!$acccessGranted) {
  164.                 throw $this->createAccessDeniedException('Vous n\'avez pas les droits pour voir ce contenu');
  165.             }
  166.         }
  167.         return $this->render('theme/default/folder.html.twig', [
  168.             'user' => $user,
  169.             'parent' => $parent
  170.         ]);
  171.     }
  172.     /**
  173.      * @Route({
  174.      *   "fr": "/fr/{slug}",
  175.      *   "en": "/en/{slug}"
  176.      * }, name="app_page")
  177.      */
  178.     public function page(Request $request$slug)
  179.     {
  180.         $locale $request->getLocale();
  181.         $page $this->em->getRepository('App:Page\Page')->findPage($slug$locale);
  182.         if (!$page) {
  183.             throw $this->createNotFoundException('La page n\'a pas été trouvé.');
  184.         }
  185.         // Check if the user has authorization to access the page
  186.         if ($page->getPrivacy() != "public") {
  187.             $authorized false;
  188.             $user $this->getUser();
  189.             if (in_array($user$page->getUsers())) {
  190.                 $authorized true;
  191.             }
  192.             foreach ($user->getGroups() as $group) {
  193.                 if (in_array($group$page->getGroups())) {
  194.                     $authorized true;
  195.                 }
  196.             }
  197.             if (!$authorized) {
  198.                 throw $this->createAccessDeniedException('Vous n\'avez pas accès à cette page.');
  199.             }
  200.         }
  201.         $contents $this->getContents($page$locale);
  202.         $theme 'theme/default/';
  203.         $template $theme $page->getTemplate()->getFilename();
  204.         return $this->render($template , [
  205.             'page' => $page,
  206.             'contents' => $contents
  207.         ]);
  208.     }
  209.     /**
  210.      * This function help to retrieve the contents of the page
  211.      */
  212.     private function getContents($page$locale)
  213.     {
  214.         $contents = [];
  215.         $filePath "";
  216.         $entityNamespaced "";
  217.         foreach($page->getContents() as $content) {
  218.             $field $content->getField();
  219.             if ($content->getLocale() == $locale) {
  220.                 if ($field) {
  221.                     $type $field->getType();
  222.                     $parent $field->getParent();
  223.     
  224.                     // If field is type of entity, get the values for that entity
  225.                     if ($type->getSlug() == "entity") {
  226.                         $entity $field->getOptions()[0]->getValue();
  227.     
  228.                         // Find namespace
  229.                         $finder = new Finder();
  230.                         $finder->files()->in($this->kernel->getProjectDir().'/src/Entity')->name($entity.'.php');
  231.     
  232.                         foreach ($finder as $file) {
  233.                             $filePath $file->getRealPath();
  234.                         }
  235.     
  236.                         $explodedFilePath explode('/'$filePath);
  237.     
  238.                         // Append namespace if exists to entity name
  239.                         if ($explodedFilePath[count($explodedFilePath) - 2] != 'Entity') {
  240.                             $entityNamespaced $explodedFilePath[count($explodedFilePath) - 2].'\\'.$entity;
  241.                         } else {
  242.                             $entityNamespaced $entity;
  243.                         }
  244.                     }
  245.     
  246.                     if ($parent) {
  247.                         $block $parent->getBlock();
  248.                         $parent $parent->getIdentifier();
  249.                         $collection $content->getCollectionId();
  250.                         $field $field->getIdentifier();
  251.                         if ($block && $parent && $field) {
  252.                             $block $block->getIdentifier();
  253.                             if ($type->getSlug() == 'entity') {
  254.                                 if ($content->getValue()) {
  255.                                     $contents[$block][$parent][$collection][$field] = $this->em->getRepository('App:'.$entityNamespaced)->find($content->getValue());
  256.                                 }
  257.                             } else {
  258.                                 $contents[$block][$parent][$collection][$field] = $content->getValue();
  259.                             }
  260.                         }
  261.                     } else {
  262.                         $block $field->getBlock();
  263.                         $field $field->getIdentifier();
  264.                         if ($block && $field) {
  265.                             $block $block->getIdentifier();
  266.                             if ($type->getSlug() == 'entity') {
  267.                                 $contents[$block][$field] = $this->em->getRepository('App:'.$entityNamespaced)->find($content->getValue());
  268.                             } else {
  269.                                 $contents[$block][$field] = $content->getValue();
  270.                             }
  271.                         }
  272.                     }
  273.                 }
  274.             }
  275.         }
  276.         return $contents;
  277.     }
  278.     public function renderWorksList($identifier)
  279.     {
  280.         $works $this->em->getRepository('App:Work\Work')->findAllLeftJoined();
  281.         return $this->render('theme/default/blocks/' $identifier '.html.twig', [
  282.             'works' => $works
  283.         ]);
  284.     }
  285.     public function renderOfficesList($identifier)
  286.     {
  287.         $stores $this->em->getRepository('App:Store\Store')->findAll();
  288.         return $this->render('theme/default/blocks/' $identifier '.html.twig', [
  289.             'stores' => $stores
  290.         ]);
  291.     }
  292. }