AmbitoController.php 3.74 KB
<?php

namespace UBV\SurUbvBundle\Controller;

use UBV\SurUbvBundle\Entity\Ambito;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;

/**
 * Ambito controller.
 *
 * @Route("ambito")
 */
class AmbitoController extends Controller
{
    /**
     * Lists all ambito entities.
     *
     * @Route("/", name="ambito_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $ambitos = $em->getRepository('UBVSurUbvBundle:Ambito')->findAll();

        return $this->render('ambito/index.html.twig', array(
            'ambitos' => $ambitos,
        ));
    }

    /**
     * Creates a new ambito entity.
     *
     * @Route("/new", name="ambito_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $ambito = new Ambito();
        $form = $this->createForm('UBV\SurUbvBundle\Form\AmbitoType', $ambito);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($ambito);
            $em->flush();

            return $this->redirectToRoute('ambito_show', array('id' => $ambito->getId()));
        }

        return $this->render('ambito/new.html.twig', array(
            'ambito' => $ambito,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a ambito entity.
     *
     * @Route("/{id}", name="ambito_show")
     * @Method("GET")
     */
    public function showAction(Ambito $ambito)
    {
        $deleteForm = $this->createDeleteForm($ambito);

        return $this->render('ambito/show.html.twig', array(
            'ambito' => $ambito,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing ambito entity.
     *
     * @Route("/{id}/edit", name="ambito_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Ambito $ambito)
    {
        $deleteForm = $this->createDeleteForm($ambito);
        $editForm = $this->createForm('UBV\SurUbvBundle\Form\AmbitoType', $ambito);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('ambito_edit', array('id' => $ambito->getId()));
        }

        return $this->render('ambito/edit.html.twig', array(
            'ambito' => $ambito,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a ambito entity.
     *
     * @Route("/{id}", name="ambito_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Ambito $ambito)
    {
        $form = $this->createDeleteForm($ambito);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($ambito);
            $em->flush();
        }

        return $this->redirectToRoute('ambito_index');
    }

    /**
     * Creates a form to delete a ambito entity.
     *
     * @param Ambito $ambito The ambito entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Ambito $ambito)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('ambito_delete', array('id' => $ambito->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}