Advertisement
Guest User

Untitled

a guest
Nov 20th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.81 KB | None | 0 0
  1. <?php
  2.  
  3. namespace D\UserBundle\Controller;
  4.  
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7.  
  8. use D\UserBundle\Entity\User;
  9. use D\UserBundle\Form\UserType;
  10.  
  11. /**
  12.  * User controller.
  13.  *
  14.  */
  15. class UserController extends Controller
  16. {
  17.     /**
  18.      * Lists all User entities.
  19.      *
  20.      */
  21.     public function indexAction()
  22.     {
  23.         $em = $this->getDoctrine()->getManager();
  24.  
  25.         $entities = $em->getRepository('DUserBundle:User')->findAll();
  26.  
  27.         return $this->render('DUserBundle:User:index.html.twig', array(
  28.             'entities' => $entities,
  29.         ));
  30.     }
  31.  
  32.     /**
  33.      * Finds and displays a User entity.
  34.      *
  35.      */
  36.     public function showAction($id)
  37.     {
  38.         var_dump($_SESSION);
  39.         $em = $this->getDoctrine()->getManager();
  40.  
  41.         $entity = $this->get('security.context')->getToken()->getUser();
  42.  
  43.         if (!$entity) {
  44.             throw $this->createNotFoundException('Unable to find User entity.');
  45.         }
  46.  
  47.         $deleteForm = $this->createDeleteForm($id);
  48.  
  49.         return $this->render('DUserBundle:User:show.html.twig', array(
  50.             'entity'      => $entity,
  51.             'delete_form' => $deleteForm->createView(),        ));
  52.     }
  53.  
  54.     /**
  55.      * Displays a form to create a new User entity.
  56.      *
  57.      */
  58.     public function newAction()
  59.     {
  60.         $entity = new User();
  61.         $form   = $this->createForm(new UserType(), $entity);
  62.  
  63.         return $this->render('DUserBundle:User:new.html.twig', array(
  64.             'entity' => $entity,
  65.             'form'   => $form->createView(),
  66.         ));
  67.     }
  68.  
  69.     /**
  70.      * Creates a new User entity.
  71.      *
  72.      */
  73.     public function createAction(Request $request)
  74.     {
  75.         $entity  = new User();
  76.         $form = $this->createForm(new UserType(), $entity);
  77.         $form->bind($request);
  78.  
  79.         if ($form->isValid()) {
  80.             $em = $this->getDoctrine()->getManager();
  81.             $em->persist($entity);
  82.             $em->flush();
  83.  
  84.             return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
  85.         }
  86.  
  87.         return $this->render('DUserBundle:User:new.html.twig', array(
  88.             'entity' => $entity,
  89.             'form'   => $form->createView(),
  90.         ));
  91.     }
  92.  
  93.     /**
  94.      * Displays a form to edit an existing User entity.
  95.      *
  96.      */
  97.     public function editAction($id)
  98.     {
  99.         $em = $this->getDoctrine()->getManager();
  100.  
  101.         $entity = $em->getRepository('DUserBundle:User')->find($id);
  102.  
  103.         if (!$entity) {
  104.             throw $this->createNotFoundException('Unable to find User entity.');
  105.         }
  106.  
  107.         $editForm = $this->createForm(new UserType(), $entity);
  108.         $deleteForm = $this->createDeleteForm($id);
  109.  
  110.         return $this->render('DUserBundle:User:edit.html.twig', array(
  111.             'entity'      => $entity,
  112.             'edit_form'   => $editForm->createView(),
  113.             'delete_form' => $deleteForm->createView(),
  114.         ));
  115.     }
  116.  
  117.     /**
  118.      * Edits an existing User entity.
  119.      *
  120.      */
  121.     public function updateAction(Request $request, $id)
  122.     {
  123.         $em = $this->getDoctrine()->getManager();
  124.  
  125.         $entity = $em->getRepository('DUserBundle:User')->find($id);
  126.  
  127.         if (!$entity) {
  128.             throw $this->createNotFoundException('Unable to find User entity.');
  129.         }
  130.  
  131.         $deleteForm = $this->createDeleteForm($id);
  132.         $editForm = $this->createForm(new UserType(), $entity);
  133.         $editForm->bind($request);
  134.  
  135.         if ($editForm->isValid()) {
  136.             $em->persist($entity);
  137.             $em->flush();
  138.  
  139.             return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
  140.         }
  141.  
  142.         return $this->render('DUserBundle:User:edit.html.twig', array(
  143.             'entity'      => $entity,
  144.             'edit_form'   => $editForm->createView(),
  145.             'delete_form' => $deleteForm->createView(),
  146.         ));
  147.     }
  148.  
  149.     /**
  150.      * Deletes a User entity.
  151.      *
  152.      */
  153.     public function deleteAction(Request $request, $id)
  154.     {
  155.         $form = $this->createDeleteForm($id);
  156.         $form->bind($request);
  157.  
  158.         if ($form->isValid()) {
  159.             $em = $this->getDoctrine()->getManager();
  160.             $entity = $em->getRepository('DUserBundle:User')->find($id);
  161.  
  162.             if (!$entity) {
  163.                 throw $this->createNotFoundException('Unable to find User entity.');
  164.             }
  165.  
  166.             $em->remove($entity);
  167.             $em->flush();
  168.         }
  169.  
  170.         return $this->redirect($this->generateUrl('user'));
  171.     }
  172.  
  173.     private function createDeleteForm($id)
  174.     {
  175.         return $this->createFormBuilder(array('id' => $id))
  176.             ->add('id', 'hidden')
  177.             ->getForm()
  178.         ;
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement