AldeaController.php 4.84 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\Aldea;
use UBV\SurUbvBundle\Form\AldeaType;
use Symfony\Component\HttpFoundation\JsonResponse;

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

        $aldeas = $em->getRepository('UBVSurUbvBundle:Aldea')->findAll();

        return $this->render('aldea/index.html.twig', array(
            'aldeas' => $aldeas,
        ));
    }
    
    /**
     * 
     * @Route("/aldeas", name="select_aldeas")
     * @Method("POST")
     */
    public function aldeasAction(Request $request) {
      $parroquia_id = $request->request->get('parroquia_id');
      $em = $this->getDoctrine()->getManager();
      $aldea = $em->getRepository('UBVSurUbvBundle:Aldea')->findAldeaByParroquiaId($parroquia_id);

      return new JsonResponse($aldea);
    }
    
    /**
     * 
     * @Route("/programas", name="select_programas")
     * @Method("POST")
     */
    public function programasAction(Request $request) {
      $session = $request->getSession();
      
      $datosFormulario = $session->get('datosForm');
      //die(dump($datosFormulario));
      $programaNivel = $datosFormulario["programaNivel"];

      $aldea_id = $request->request->get('aldea_id');
      $em = $this->getDoctrine()->getManager();
      $programa = $em->getRepository('UBVSurUbvBundle:Programa')->findProgramaByAldeaId($aldea_id, $programaNivel);

      return new JsonResponse($programa);
    }
    
    /**
     * Creates a new Aldea entity.
     *
     * @Route("/new", name="aldea_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $aldea = new Aldea();
        $form = $this->createForm('UBV\SurUbvBundle\Form\AldeaType', $aldea);
        $form->handleRequest($request);

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

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

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

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

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

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

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

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

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

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

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

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

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