MunicipioController.php 4.43 KB
<?php

namespace UBV\SurUbvBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use UBV\SurUbvBundle\Entity\Estado;
use UBV\SurUbvBundle\Entity\Municipio;
use UBV\SurUbvBundle\Form\MunicipioType;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * Municipio controller.
 *
 * @Route("/municipio")
 */
class MunicipioController extends Controller {

  /**
   * Lists all Municipio entities.
   *
   * @Route("/", name="municipio_index")
   * @Method("GET")
   */
  public function indexAction() {
    $em = $this->getDoctrine()->getManager();

    $municipios = $em->getRepository('UBVSurUbvBundle:Municipio')->findAll();

    return $this->render('municipio/index.html.twig', array(
                'municipios' => $municipios,
    ));
  }
  
  /**
   * 
   * @Route("/municipios", name="select_municipios")
   * @Method("POST")
   */
  public function municipiosAction(Request $request) 
  {
    $estado_id = $request->request->get('estado_id');
    $em = $this->getDoctrine()->getManager();
    $municipios = $em->getRepository('UBVSurUbvBundle:Municipio')->findMunicipiosByEstadoId($estado_id);

    return new JsonResponse($municipios);
  }

  /**
   * Creates a new Municipio entity.
   *
   * @Route("/new", name="municipio_new")
   * @Method({"GET", "POST"})
   */
  public function newAction(Request $request) {
    $municipio = new Municipio();
    $form = $this->createForm('UBV\SurUbvBundle\Form\MunicipioType', $municipio);
    $form->handleRequest($request);
    //die(dump($request));
    if ($form->isSubmitted() && $form->isValid()) {
      $em = $this->getDoctrine()->getManager();
      $estado = $form->get('estado')->getData();
      $municipio->setEstado($estado);
      $em->persist($municipio);
      $em->flush();

      
//            die(dump($form->get('estado')->getData()));
      return $this->redirectToRoute('municipio_show', array('id' => $municipio->getId()));
    }

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

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

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

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

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

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

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

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

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

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

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

}