src/Ox/HoardBundle/Controller/ContainerController.php line 194

Open in your IDE?
  1. <?php
  2.     
  3. namespace App\Ox\HoardBundle\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  11. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. use Symfony\Component\Form\Extension\Core\Type\ButtonType;
  15. use App\Ox\HoardBundle\Entity\Container;
  16. use App\Ox\HoardBundle\Entity\ContainerImage;
  17. use App\Ox\HoardBundle\Entity\Image;
  18. use App\Ox\HoardBundle\Form\ContainerType;
  19. /**
  20.  * Container controller
  21.  *
  22.  * @Route("/container")
  23.  */
  24.  class ContainerController extends AbstractController
  25.  {
  26.     private $security;
  27.     public function __construct(Security $security)
  28.     {
  29.         $this->security $security;
  30.     }
  31.      /**
  32.       * @Route("/{id}/edit", name="container_edit", methods={"GET"})
  33.       * @Template()
  34.       */
  35.       public function editAction(Request $request$id)
  36.       {
  37.           $em $this->getDoctrine()->getManager();
  38.           $em->getFilters()->enable('softdeleteable');
  39.           
  40.           $container $em->getRepository('OxHoardBundle:Container')->find($id);
  41.           
  42.           if(!$container) {
  43.               throw $this->createNotFoundException('Unable to find container entity.');
  44.           }
  45.           
  46.           // $deleteForm = $this->createDeleteForm($id);
  47.           $editForm $this->createEditForm($container);
  48.           
  49.           $isAjax $request->isXmlHttpRequest();
  50.           
  51.           $template = ($isAjax '@OxHoardBundle/container/edit_form.html.twig' '@OxHoardBundle/container/edit.html.twig');  
  52.           
  53.           return $this->render($template, array(
  54.               'ajax' => $request->isXmlHttpRequest(),
  55.               'container' => $container,
  56.               'edit_form' => $editForm->createView(),
  57.               // 'delete_form' => $deleteForm->createView(),
  58.           ));
  59.       }
  60.       
  61.     /**
  62.      * Creates a form to delete an Container entity by id.
  63.      *
  64.      * @param mixed $id The entity id
  65.      *
  66.      * @return \Symfony\Component\Form\Form The form
  67.      */
  68.     private function createDeleteForm($id)
  69.     {
  70.         return $this->createFormBuilder()
  71.             ->setAction($this->generateUrl('container_delete', array('id' => $id)))
  72.             ->setMethod('DELETE')
  73.             ->add('submit'ButtonType::class, array(
  74.                 'label' => 'Delete',
  75.                 'attr' => array(
  76.                     'class' => 'delete-button btn-danger'
  77.                 )))
  78.             ->getForm()
  79.             ;
  80.     }
  81.       
  82.     /**
  83.      * Creates a form to edit an Container entity.
  84.      *
  85.      * @param Container $entity The entity
  86.      *
  87.      * @return \Symfony\Component\Form\Form The form
  88.      */
  89.     private function createEditForm(Container $entity)
  90.     {
  91.         $form $this->createForm(ContainerType::class, $entity, array(
  92.             'action' => $this->generateUrl('container_update', array('id' => $entity->getId())),
  93.             'method' => 'PUT',
  94.         ));
  95.         $form->add('submit'SubmitType::class, array('label' => 'Update'));
  96.         return $form;
  97.     }
  98.     
  99.     /**
  100.      * Edits an existing Container entity.
  101.      *
  102.      * @Route("/{id}", name="container_update", methods={"PUT"}) PUT doesn't seem to work...
  103.      * @Template("@OxHoardBundle/container/edit.html.twig")
  104.      */
  105.     public function updateAction(Request $request$id)
  106.     {
  107.         $em $this->getDoctrine()->getManager();
  108.         $em->getFilters()->enable('softdeleteable');
  109.         
  110.         $container $em->getRepository('OxHoardBundle:Container')->find($id);
  111.         if (!$container) {
  112.             throw $this->createNotFoundException('Unable to find Container entity.');
  113.         }
  114.         // $deleteForm = $this->createDeleteForm($id);
  115.         $editForm $this->createEditForm($container);
  116.         $editForm->handleRequest($request);
  117.         if ($editForm->isValid()) {
  118.             //persist the containers
  119.             foreach($container->getLayers() as $layer)
  120.             {   
  121.                 $layer->setContainer($container);
  122.                 $em->persist($layer);
  123.             }
  124.             
  125.             //mark as unvalidated since it has changed
  126.             if(!$this->userIsAdmin())
  127.             {
  128.                 $container->getHoard()->markUnvalidatedByAdmin();
  129.             }
  130.             $em->flush();
  131.             return $this->redirect($this->generateUrl('container_edit', array('id' => $id)));
  132.         }
  133.         return array(
  134.             'container'      => $container,
  135.             'edit_form'   => $editForm->createView(),
  136.             // 'delete_form' => $deleteForm->createView(),
  137.         );
  138.     }
  139.     
  140.     /**
  141.      * Container root placeholder.
  142.      *
  143.      * @Route("/", name="container", methods={"GET"})
  144.      * @Template()
  145.      */
  146.     public function indexAction(Request $request)
  147.     {
  148. //        $limit = 20;
  149. //        $em = $this->getDoctrine()->getManager();
  150. //        $em->getFilters()->enable('softdeleteable');
  151. //
  152. //        $dql = 'SELECT c FROM OxHoardBundle:Container c';
  153. //        $query = $em->createQuery($dql)
  154. //            ->setFirstResult(0)
  155. //            ->setMaxResults($limit);
  156. //
  157. //        $paginator = $this->get('knp_paginator');
  158. //
  159. //        $pagination = $paginator->paginate(
  160. //            $query,
  161. //            $request->query->getInt('page', 1)/*page number*/,
  162. //            $limit/*limit per page*/
  163. //        );
  164.         // parameters to template
  165. //        return $this->render('@OxHoardBundle/container/index.html.twig', array('pagination' => $pagination));
  166.         return $this->render('@OxHoardBundle/container/index.html.twig');
  167.     } 
  168.      
  169.     /**
  170.      * Finds and displays a container entity.
  171.      *
  172.      * @Route("/{id}", name="container_show", methods={"GET"})
  173.      * @Template()
  174.      */
  175.     public function showAction($id)
  176.     {
  177.         $em $this->getDoctrine()->getManager();
  178.         $container $em->getRepository('OxHoardBundle:Container')->find($id);
  179.         $this->checkAccess($container'view');
  180.         if (!$container) {
  181.             throw $this->createNotFoundException('Unable to find container entity.');
  182.         }
  183.         $objects $em->getRepository('OxHoardBundle:HObject')->findBy(array(
  184.             'container' => $id,
  185.         ));
  186.         return array(
  187.             'container'     => $container,
  188.             'objects'       => $objects,
  189.         );
  190.     }
  191.      /**
  192.       * Adds a new image file, creating an Image entity, and a ContainerImage entity
  193.       *
  194.       * @Route("/{id}/ajax_add_image", methods={"POST"})
  195.       */
  196.      public function ajaxAddImage(Request $request$id)
  197.      {
  198.          $em $this->getDoctrine()->getManager();
  199.          $file $request->files->get('image');
  200.          $container $em->getRepository('OxHoardBundle:Container')->find($id);
  201.          //validate the file - TODO
  202.          $this->checkAccess($container'edit');
  203.          //move to desired location/name
  204.          $count $container->getContainerImages()->count();
  205.          $fileName $id.$count.'.'.$file->guessExtension();
  206.          $file $file->move($this->getPermanentContainerImageUploadDir(), $fileName);
  207.          //create Image entity
  208.          $image = new Image();
  209.          $image->setFileName($fileName);
  210.          $em->persist($image);
  211.          //create ContainerImage entity
  212.          $containerImage = new ContainerImage();
  213.          $containerImage->setContainer($container);
  214.          $containerImage->setImage($image);
  215.          $em->persist($containerImage);
  216.          //mark as unvalidated since it has changed
  217.          if(!$this->userIsAdmin())
  218.          {
  219.              if($container->getHoard())
  220.              {
  221.                  $container->getHoard()->markUnvalidatedByAdmin();
  222.              }
  223.          }
  224.          $em->persist($container);
  225.          $em->flush();
  226.          return new JsonResponse(array(
  227.              'fileName'=>$fileName,
  228.              'container_image_id'=>$containerImage->getId()
  229.          ));
  230.      }
  231.      private function removeImageFailed($reason)
  232.      {
  233.          return new JsonResponse( array(
  234.              'removedImage'=>null,
  235.              'error'=> $reason
  236.          ));
  237.      }
  238.     /**
  239.      * checks permission of user's current request
  240.      *
  241.      * @param mixed $entity The entity being validated
  242.      *
  243.      * @param string $attribute - 'view' or 'edit' or 'delete'
  244.      * @return boolean
  245.      *
  246.      * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  247.      */
  248.     private function checkAccess($entity$attribute) {
  249.         // call security voter(s)
  250.         if (false === $this->security->isGranted($attribute$entity->getHoard())) {
  251.             throw new AccessDeniedException('Unauthorised access!');
  252.         }
  253.         return true;
  254.     }
  255.     
  256.     private function userIsAdmin() {
  257.         if($this->getUser() && ($this->getUser()->hasRole('ROLE_ADMIN') || $this->getUser()->hasRole('ROLE_SUPER_ADMIN')))
  258.         {
  259.             return true;
  260.         }
  261.         return false;
  262.     }
  263.     private function getPermanentContainerImageUploadDir() {
  264.         return '/srv/hoards_container_images';
  265.     }
  266.     
  267.  }