src/Controller/CMS/WebsiteController.php line 140

Open in your IDE?
  1. <?php
  2. namespace App\Controller\CMS;
  3. use App\Entity\CMS\Website;
  4. use App\Form\CMS\WebsiteType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use App\Service\Kernel\DMSService;
  10. /**
  11.  * @Route("/{_locale}/website")
  12.  */
  13. class WebsiteController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/", name="website_index", methods={"GET"})
  17.      */
  18.     public function index(): Response
  19.     {
  20.         $classname =  explode("\\", get_class($this));
  21.         $arraysize = count($classname);
  22.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  23.         $this->denyAccessUnlessGranted($attribute);
  24.     
  25.         $websites = $this->getDoctrine()
  26.             ->getRepository(Website::class)
  27.             ->findBy(array('state' => 0));
  28.         return $this->render('cms/website/index.html.twig', [
  29.             'websites' => $websites,
  30.         ]);
  31.     }
  32.     
  33.     /**
  34.      * @Route("/showarchived", name="website_showarchived", methods={"GET"})
  35.      */
  36.     public function showarchived(): Response
  37.     {
  38.         $classname =  explode("\\", get_class($this));
  39.         $arraysize = count($classname);
  40.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  41.         $this->denyAccessUnlessGranted($attribute);
  42.     
  43.         $websites = $this->getDoctrine()
  44.             ->getRepository(Website::class)
  45.             ->findBy(array('state' => 1));
  46.         return $this->render('cms/website/archived.html.twig', [
  47.             'websites' => $websites,
  48.         ]);
  49.     }
  50.     
  51.     /**
  52.      * @Route("/showdeleted", name="website_showdeleted", methods={"GET"})
  53.      */
  54.     public function showdeleted(): Response
  55.     {
  56.         $classname =  explode("\\", get_class($this));
  57.         $arraysize = count($classname);
  58.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  59.         $this->denyAccessUnlessGranted($attribute);
  60.     
  61.         $websites = $this->getDoctrine()
  62.             ->getRepository(Website::class)
  63.             ->findBy(array('state' => 2));
  64.         return $this->render('cms/website/deleted.html.twig', [
  65.             'websites' => $websites,
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/new", name="website_new", methods={"GET","POST"})
  70.      */
  71.     public function new(Request $request): Response
  72.     {
  73.         $classname =  explode("\\", get_class($this));
  74.         $arraysize = count($classname);
  75.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  76.         $this->denyAccessUnlessGranted($attribute);
  77.     
  78.         $website = new Website();
  79.         $form = $this->createForm(WebsiteType::class, $website, 
  80.                 ['action' => $this->generateUrl('website_new'), 'attr' => ['id' => $this->generateUrl('website_new')]]);
  81.         if ($this->getUser()->getOrganization() == null) {
  82.             $form->add('organization', null, array('label' => 'Organization',
  83.                 'query_builder' => function ($repository) {
  84.                     return $repository->createQueryBuilder('s')->where("s.state = 0");
  85.                 },
  86.                 'attr' => ['class' => 'select2']));
  87.         }
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             $entityManager = $this->getDoctrine()->getManager();
  91.             if ($this->getUser()->getOrganization() != null) {
  92.                 $website->setOrganization($this->getUser()->getOrganization());
  93.             }
  94.             $website->setState(0);
  95.             $entityManager->persist($website);
  96.             $entityManager->flush();
  97.             
  98.             $this->denyAccessUnlessGranted($attribute, $website);
  99.             return $this->redirectToRoute('website_index');
  100.         }
  101.         return $this->render('cms/website/new.html.twig', [
  102.             'website' => $website,
  103.             'form' => $form->createView(),
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/{id}", name="website_show", methods={"GET"})
  108.      */
  109.     public function show(Website $website, DMSService $dmsservice): Response
  110.     {
  111.         $classname =  explode("\\", get_class($this));
  112.         $arraysize = count($classname);
  113.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  114.         $this->denyAccessUnlessGranted($attribute, $website); 
  115.         
  116.         $applicationfolder = $dmsservice->createFolderIfNeeded("System");
  117.         $websitefolder = $dmsservice->createFolderIfNeeded("Website", $applicationfolder);
  118.         $folder = $dmsservice->createFolderIfNeeded("".$website->getId(), $websitefolder);
  119.         
  120.         return $this->render('cms/website/show.html.twig', [
  121.             'website' => $website,
  122.             'folder' => $folder,
  123.         ]);
  124.     }
  125.     
  126.     /**
  127.      * @Route("/{id}/layout/{state?0}", name="website_layout", methods={"GET"})
  128.      */
  129.     public function layout(Website $website, int $state = 0,DMSService $dmsservice): Response
  130.     {
  131.         $classname =  explode("\\", get_class($this));
  132.         $arraysize = count($classname);
  133.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  134.         $this->denyAccessUnlessGranted($attribute, $website); 
  135.         
  136.         $entityManager = $this->getDoctrine()->getManager();
  137.         $rows = $entityManager->getRepository('App\Entity\CMS\CMSRow')->findBy(array('website'=>$website->getId()), array('position' => 'ASC'));
  138.         
  139.         
  140.         return $this->render('cms/website/layout.html.twig', [
  141.             'website' => $website,
  142.             'state' => $state,
  143.             'rows' => $rows
  144.         ]);
  145.     }
  146.     
  147.     /**
  148.      * @Route("/{id}/audit", name="website_showaudit", methods={"GET"})
  149.      */
  150.     public function showaudit(Website $website): Response
  151.     {
  152.         $classname =  explode("\\", get_class($this));
  153.         $arraysize = count($classname);
  154.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  155.         $this->denyAccessUnlessGranted($attribute, $website);  
  156.         
  157.         return $this->render('cms/website/showaudit.html.twig', [
  158.             'website' => $website,
  159.         ]);
  160.     }
  161.     
  162.     /**
  163.      * @Route("/{id}/archive", name="website_archive", methods={"GET"})
  164.      */
  165.     public function archive(Website $website): Response
  166.     {
  167.         $classname =  explode("\\", get_class($this));
  168.         $arraysize = count($classname);
  169.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  170.         $this->denyAccessUnlessGranted($attribute, $website); 
  171.         
  172.                 
  173.         $entityManager = $this->getDoctrine()->getManager();
  174.         $website->setState(1);
  175.         $entityManager->persist($website);
  176.         $entityManager->flush();
  177.         
  178.         return $this->redirectToRoute('website_show', array('id' => $website->getId()));
  179.     }
  180.     
  181.     /**
  182.      * @Route("/{id}/restore", name="website_restore", methods={"GET"})
  183.      */
  184.     public function restore(Website $website): Response
  185.     {
  186.         $classname =  explode("\\", get_class($this));
  187.         $arraysize = count($classname);
  188.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  189.         $this->denyAccessUnlessGranted($attribute, $website); 
  190.         
  191.                 
  192.         $entityManager = $this->getDoctrine()->getManager();
  193.         $website->setState(0);
  194.         $entityManager->persist($website);
  195.         $entityManager->flush();
  196.         
  197.         return $this->redirectToRoute('website_show', array('id' => $website->getId()));
  198.     }
  199.     
  200.     /**
  201.      * @Route("/{id}/delete", name="website_logicaldelete", methods={"GET"})
  202.      */
  203.     public function logicaldelete(Website $website): Response
  204.     {
  205.         $classname =  explode("\\", get_class($this));
  206.         $arraysize = count($classname);
  207.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  208.         $this->denyAccessUnlessGranted($attribute, $website); 
  209.         
  210.                 
  211.         $entityManager = $this->getDoctrine()->getManager();
  212.         $website->setState(2);
  213.         $entityManager->persist($website);
  214.         $entityManager->flush();
  215.         
  216.         return $this->redirectToRoute('website_index');
  217.     }
  218.     /**
  219.      * @Route("/{id}/edit", name="website_edit", methods={"GET","POST"})
  220.      */
  221.     public function edit(Request $request, Website $website, DMSService $dmsservice): Response
  222.     {
  223.         $classname =  explode("\\", get_class($this));
  224.         $arraysize = count($classname);
  225.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  226.         $this->denyAccessUnlessGranted($attribute, $website); 
  227.         
  228.         $applicationfolder = $dmsservice->createFolderIfNeeded("System");
  229.         $websitefolder = $dmsservice->createFolderIfNeeded("Website", $applicationfolder);
  230.         $folder = $dmsservice->createFolderIfNeeded("".$website->getId(), $websitefolder);
  231.     
  232.         $form = $this->createForm(WebsiteType::class, $website, 
  233.                 ['folder'=>$folder, 'action' => $this->generateUrl('website_edit', array('id' => $website->getId())), 'attr' => ['id' => $this->generateUrl('website_edit', array('id' => $website->getId()))]]);
  234.         $form->handleRequest($request);
  235.         if ($form->isSubmitted() && $form->isValid()) {
  236.             $this->getDoctrine()->getManager()->flush();
  237.             return $this->redirectToRoute('website_show', array('id' => $website->getId()));
  238.         }
  239.         return $this->render('cms/website/edit.html.twig', [
  240.             'website' => $website,
  241.             'form' => $form->createView(),
  242.         ]);
  243.     }
  244.     /**
  245.      * @Route("/{id}", name="website_delete", methods={"DELETE"})
  246.      */
  247.     public function delete(Request $request, Website $website): Response
  248.     {
  249.         $classname =  explode("\\", get_class($this));
  250.         $arraysize = count($classname);
  251.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  252.         $this->denyAccessUnlessGranted($attribute, $website); 
  253.     
  254.         if ($this->isCsrfTokenValid('delete'.$website->getId(), $request->request->get('_token'))) {
  255.             $entityManager = $this->getDoctrine()->getManager();
  256.             $entityManager->remove($website);
  257.             $entityManager->flush();
  258.         }
  259.         return $this->redirectToRoute('website_index');
  260.     }
  261. }