<?php

namespace AppBundle\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 AppBundle\Entity\ProductHistory;
use AppBundle\Form\ProductHistoryType;

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

        $productHistories = $em->getRepository('AppBundle:ProductHistory')->findAll();

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

    /**
     * Creates a new ProductHistory entity.
     *
     * @Route("/new", name="producthistory_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $productHistory = new ProductHistory();
        $form = $this->createForm('AppBundle\Form\ProductHistoryType', $productHistory);
        $form->handleRequest($request);

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

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

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

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

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

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

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

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

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

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

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

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

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