<?php

namespace VGM\WOBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use VGM\WOBundle\Entity\WorkType;
use VGM\WOBundle\Form\WorkTypeType;

/**
 * WorkType controller.
 *
 */
class WorkTypeController extends Controller
{

    /**
     * Lists all WorkType entities.
     *
     */
    public function indexAction($page)
    {
        $em = $this->getDoctrine()->getManager();

        $cant_worktypes = $em->getRepository('VGMWOBundle:WorkType')->countWorkTypes();
        $worktypes_per_page = $this->container->getParameter('worktypes_per_page');
        $last_page = ceil($cant_worktypes / $worktypes_per_page);
        $previous_page = $page > 1 ? $page - 1 : 1;
        $next_page = $page < $last_page ? $page + 1 : $last_page;

        $offset = ($page - 1) > -1? ($page - 1) * $worktypes_per_page: null;
        $entities = $em->getRepository('VGMWOBundle:WorkType')
            ->getWorkTypes($worktypes_per_page, $offset);

        return $this->render('VGMWOBundle:WorkType:index.html.twig', array(
                'entities' => $entities,
                'last_page' => $last_page,
                'previous_page' => $previous_page,
                'current_page' => $page,
                'next_page' => $next_page,
                'cant_worktypes' => $cant_worktypes
            ));
    }
    /**
     * Creates a new WorkType entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new WorkType();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

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

            return $this->redirect($this->generateUrl('worktypes'));
        }

        return $this->render('VGMWOBundle:WorkType:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Creates a form to create a WorkType entity.
     *
     * @param WorkType $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(WorkType $entity)
    {
        $form = $this->createForm(new WorkTypeType(), $entity, array(
            'action' => $this->generateUrl('worktypes_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new WorkType entity.
     *
     */
    public function newAction()
    {
        $entity = new WorkType();
        $form   = $this->createCreateForm($entity);

        return $this->render('VGMWOBundle:WorkType:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a WorkType entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('VGMWOBundle:WorkType')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find WorkType entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('VGMWOBundle:WorkType:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing WorkType entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('VGMWOBundle:WorkType')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find WorkType entity.');
        }

        $editForm = $this->createForm(new WorkTypeType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('VGMWOBundle:WorkType:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
    * Creates a form to edit a WorkType entity.
    *
    * @param WorkType $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(WorkType $entity)
    {
        $form = $this->createForm(new WorkTypeType(), $entity, array(
            'action' => $this->generateUrl('worktypes_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing WorkType entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('VGMWOBundle:WorkType')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find WorkType entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new WorkTypeType(), $entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('worktypes'));
        }

        return $this->render('VGMWOBundle:WorkType:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Deletes a WorkType entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('VGMWOBundle:WorkType')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find WorkType entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('worktypes'));
    }

    /**
     * Creates a form to delete a WorkType entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('worktypes_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}
