<?php

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\ProductGroup;
use AppBundle\Form\ProductGroupType;

/**
 * ProductGroup controller.
 *
 * @Route("/7073a00e724bbda643cb7e2a85110640/home/productgroup")
 */
class ProductGroupController extends Controller
{
    /**
     * Lists all ProductGroup entities.
     *
     * @Route("/", name="productgroup_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $productGroups = $em->getRepository('AppBundle:ProductGroup')->findAll();

        return $this->render('productgroup/index.html.twig', array(
            'productGroups' => $productGroups,
            'token' => $this->get('app.crsfutil')->getToken()
        ));
    }
    /**
     * Lists all ProductGroup entities.
     *
     * @Route("/all", name="product_groups_all")
     * @Method("GET")
     * return JsonResponse
     */
    public function getProductGroups()
    {
        $em = $this->getDoctrine()->getManager();
        return new JsonResponse($em->getRepository('AppBundle:ProductGroup')->findAll());
    }
    /**
     * Creates a new ProductGroup entity.
     *
     * @Route("/new", name="productgroup_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $productGroup = new ProductGroup();
        $form = $this->createForm('AppBundle\Form\ProductGroupType', $productGroup);
        $form->handleRequest($request);

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

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

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

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

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

    /**
     * Displays a form to edit an existing ProductGroup entity.
     *
     * @Route("/{id}/edit", name="productgroup_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, ProductGroup $productGroup)
    {
        $deleteForm = $this->createDeleteForm($productGroup);
        $editForm = $this->createForm('AppBundle\Form\ProductGroupType', $productGroup);
        $editForm->handleRequest($request);

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

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

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

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

        if (($form->isSubmitted() && $form->isValid()) || $this->get('app.crsfutil')->checkCsrfValid($request)) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($productGroup);
            $em->flush();
        }

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

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