src/Ox/HoardBundle/Controller/Admin/AncientPlaceController.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Ox\HoardBundle\Controller\Admin;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use App\Ox\HoardBundle\Entity\AncientPlace;
  9. use App\Ox\HoardBundle\Form\AncientPlaceType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. /**
  12.  * AncientPlace controller.
  13.  *
  14.  * @Route("/ancientplace")
  15.  */
  16. class AncientPlaceController extends AbstractController
  17. {
  18.     /**
  19.      * Lists all AncientPlace entities.
  20.      *
  21.      * @Route("/", name="admin_ancientplace", methods={"GET"})
  22.      * @Template("@OxHoardBundle/admin/ancientPlace/index.html.twig")
  23.      */
  24.     public function indexAction()
  25.     {
  26.         $em $this->getDoctrine()->getManager();
  27.         $entities $em->getRepository('OxHoardBundle:AncientPlace')->findBy(array(), array('sortValue' => 'DESC''id' => 'ASC'));
  28.         return array(
  29.             'can_edit' => $this->canEditEntries(),
  30.             'entities' => $entities,
  31.         );
  32.     }
  33.     /**
  34.      * Creates a new AncientPlace entity.
  35.      *
  36.      * @Route("/", name="admin_ancientplace_create", methods={"POST"})
  37.      * @Template("@OxHoardBundle/admin/ancientPlace/new.html.twig")
  38.      */
  39.     public function createAction(Request $request)
  40.     {
  41.         $entity = new AncientPlace();
  42.         $form $this->createCreateForm($entity);
  43.         $form->handleRequest($request);
  44.         if ($form->isValid()) {
  45.             $em $this->getDoctrine()->getManager();
  46.             $em->persist($entity);
  47.             $em->flush();
  48.             return $this->redirect($this->generateUrl('admin_ancientplace_show', array('id' => $entity->getId())));
  49.         }
  50.         return array(
  51.             'entity' => $entity,
  52.             'form'   => $form->createView(),
  53.         );
  54.     }
  55.     /**
  56.      * Creates a form to create a AncientPlace entity.
  57.      *
  58.      * @param AncientPlace $entity The entity
  59.      *
  60.      * @return \Symfony\Component\Form\Form The form
  61.      */
  62.     private function createCreateForm(AncientPlace $entity)
  63.     {
  64.         $form $this->createForm(AncientPlaceType::class, $entity, array(
  65.             'action' => $this->generateUrl('admin_ancientplace_create'),
  66.             'method' => 'POST',
  67.         ));
  68.         $form->add('submit'SubmitType::class, array('label' => 'Create'));
  69.         return $form;
  70.     }
  71.     /**
  72.      * Displays a form to create a new AncientPlace entity.
  73.      *
  74.      * @Route("/new", name="admin_ancientplace_new", methods={"GET"})
  75.      * @Template("@OxHoardBundle/admin/ancientPlace/new.html.twig")
  76.      */
  77.     public function newAction()
  78.     {
  79.         $entity = new AncientPlace();
  80.         $form   $this->createCreateForm($entity);
  81.         return array(
  82.             'entity' => $entity,
  83.             'form'   => $form->createView(),
  84.         );
  85.     }
  86.     /**
  87.      * Finds and displays a AncientPlace entity.
  88.      *
  89.      * @Route("/{id}", name="admin_ancientplace_show", methods={"GET"})
  90.      * @Template("@OxHoardBundle/admin/ancientPlace/show.html.twig")
  91.      */
  92.     public function showAction($id)
  93.     {
  94.         $em $this->getDoctrine()->getManager();
  95.         $entity $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
  96.         if (!$entity) {
  97.             throw $this->createNotFoundException('Unable to find AncientPlace entity.');
  98.         }
  99.         if(!$this->getUser()) {
  100.             $isAuthenticated false;
  101.         } else {
  102.             $isAuthenticated true;
  103.         }
  104.         $hoards = [];
  105.         foreach($entity->getHoards() as $hoard) {
  106.             $hoardId $hoard->getId();
  107.             $em->getFilters()->disable('softdeleteable');
  108.             $hoard $em->getRepository('OxHoardBundle:Hoard')->find($hoardId);
  109.             if ($isAuthenticated || $hoard->getValidatedByUser()) {
  110.                 $hoards[] = $hoard;
  111.             }
  112.             usort($hoards, function ($a$b) {
  113.                 return strtolower($a) > strtolower($b);
  114.             });
  115.             $em->getFilters()->enable('softdeleteable');
  116.         }
  117.         
  118.         $deleteForm $this->createDeleteForm($id);
  119.         return array(
  120.             'isAuthenticated' => $isAuthenticated,
  121.             'can_edit' => $this->canEditEntries(),
  122.             'entity'      => $entity,
  123.             'hoards' => $hoards,
  124.             'delete_form' => $deleteForm->createView(),
  125.         );
  126.     }
  127.     /**
  128.      * Displays a form to edit an existing AncientPlace entity.
  129.      *
  130.      * @Route("/{id}/edit", name="admin_ancientplace_edit", methods={"GET"})
  131.      * @Template("@OxHoardBundle/admin/ancientPlace/edit.html.twig")
  132.      */
  133.      public function editAction(Request $request$id)
  134.     {
  135.         $em $this->getDoctrine()->getManager();
  136.         $entity $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
  137.         if (!$entity) {
  138.             throw $this->createNotFoundException('Unable to find AncientPlace entity.');
  139.         }
  140.         $editForm $this->createEditForm($entity);
  141.         $deleteForm $this->createDeleteForm($id);
  142.         $hoards $request->getSession()->get('hoards');
  143.         $request->getSession()->clear('hoards');
  144.         return array(
  145.             'hoards'      => $hoards,
  146.             'entity'      => $entity,
  147.             'edit_form'   => $editForm->createView(),
  148.             'delete_form' => $deleteForm->createView(),
  149.         );
  150.     }
  151.     /**
  152.     * Creates a form to edit a AncientPlace entity.
  153.     *
  154.     * @param AncientPlace $entity The entity
  155.     *
  156.     * @return \Symfony\Component\Form\Form The form
  157.     */
  158.     private function createEditForm(AncientPlace $entity)
  159.     {
  160.         $form $this->createForm(AncientPlaceType::class, $entity, array(
  161.             'action' => $this->generateUrl('admin_ancientplace_update', array('id' => $entity->getId())),
  162.             'method' => 'PUT',
  163.         ));
  164.         $form->add('submit'SubmitType::class, array('label' => 'Update'));
  165.         return $form;
  166.     }
  167.     /**
  168.      * Edits an existing AncientPlace entity.
  169.      *
  170.      * @Route("/{id}", name="admin_ancientplace_update", methods={"PUT"})
  171.      * @Template("@OxHoardBundle/admin/ancientPlace/edit.html.twig")
  172.      */
  173.     public function updateAction(Request $request$id)
  174.     {
  175.         $em $this->getDoctrine()->getManager();
  176.         $entity $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
  177.         if (!$entity) {
  178.             throw $this->createNotFoundException('Unable to find AncientPlace entity.');
  179.         }
  180.         $deleteForm $this->createDeleteForm($id);
  181.         $editForm $this->createEditForm($entity);
  182.         $editForm->handleRequest($request);
  183.         $hoards = [];
  184.         if ($editForm->isValid()) {
  185.             $em->flush();
  186.             $request->getSession()
  187.                 ->getFlashBag()
  188.                 ->add('success''Entry has been updated');
  189.           return $this->redirect($this->generateUrl('admin_ancientplace_show', array('id' => $id)));
  190.         }
  191.         return array(
  192.             'hoards'      => $hoards,
  193.             'entity'      => $entity,
  194.             'edit_form'   => $editForm->createView(),
  195.             'delete_form' => $deleteForm->createView(),
  196.         );
  197.     }
  198.     /**
  199.      * Deletes a AncientPlace entity.
  200.      *
  201.      * @Route("/{id}", name="admin_ancientplace_delete", methods={"DELETE"})
  202.      */
  203.     public function deleteAction(Request $request$id)
  204.     {
  205.         $form $this->createDeleteForm($id);
  206.         $form->handleRequest($request);
  207.         if ($form->isValid()) {
  208.           try {
  209.               $em $this->getDoctrine()->getManager();
  210.               $entity $em->getRepository('OxHoardBundle:AncientPlace')->find($id);
  211.               if (!$entity) {
  212.                   throw $this->createNotFoundException('Unable to find AncientPlace entity.');
  213.               }
  214.               // check whether it is in use
  215.               $query $em -> createQuery("SELECT h from OxHoardBundle:Hoard h WHERE h.ancientPlace = :query")
  216.                                  ->setParameter('query'$id);
  217.               $hoards $query->getResult();
  218.               if(count($hoards) > 0) {
  219.                 $request->getSession()->set('hoards'$hoards);
  220.                 return $this->redirect($this->generateUrl('admin_ancientplace_edit', array('id' => $entity->getId())));
  221.               } else {
  222.                 $em->remove($entity);
  223.                 $em->flush();
  224.                 return $this->redirect($this->generateUrl('admin_ancientplace'));
  225.               }
  226.           } catch (\Exception $e) {
  227.               $request->getSession()
  228.                   ->getFlashBag()
  229.                   ->add('error''Unable to delete AncientPlace. It may currently be in use.');
  230.               return $this->redirect($this->generateUrl('admin_ancientplace_edit', array('id' => $entity->getId())));
  231.           }
  232.         }
  233.     }
  234.     /**
  235.      * Creates a form to delete a AncientPlace entity by id.
  236.      *
  237.      * @param mixed $id The entity id
  238.      *
  239.      * @return \Symfony\Component\Form\Form The form
  240.      */
  241.     private function createDeleteForm($id)
  242.     {
  243.         return $this->createFormBuilder()
  244.             ->setAction($this->generateUrl('admin_ancientplace_delete', array('id' => $id)))
  245.             ->setMethod('DELETE')
  246.             ->add('submit'SubmitType::class, array(
  247.               'label' => 'Delete',
  248.               'attr' => array( 'class' => 'delete-button btn btn-danger')
  249.             ))
  250.             ->getForm()
  251.         ;
  252.     }
  253.     
  254.     private function canEditEntries() {
  255.         return $this->userIsListeditor();
  256.     }
  257.     private function userIsListeditor() {
  258.         if($this->getUser() && ($this->getUser()->hasRole('ROLE_LIST_EDITOR') || $this->getUser()->hasRole('ROLE_ADMIN') || $this->getUser()->hasRole('ROLE_SUPER_ADMIN')))
  259.         {
  260.             return true;
  261.         }
  262.         return false;
  263.     }
  264. }