added form to edit timesheet entries for all user #45 (#48)

* added form to administrate timesheet entries for all user #45
This commit is contained in:
Kevin Papst
2018-01-07 20:42:44 +01:00
committed by GitHub
parent 4e344dbe6e
commit c5c9f37de7
10 changed files with 299 additions and 68 deletions

View File

@@ -15,11 +15,13 @@ use AppBundle\Controller\AbstractController;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Request;
use TimesheetBundle\Controller\TimesheetControllerTrait;
use TimesheetBundle\Entity\Customer;
use TimesheetBundle\Entity\Timesheet;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use TimesheetBundle\Form\TimesheetAdminForm;
/**
* Controller used for manage timesheet entries in the admin part of the site.
@@ -73,13 +75,74 @@ class TimesheetController extends AbstractController
*/
public function stopAction(Timesheet $entry)
{
try {
$this->getRepository()->stopRecording($entry);
$this->flashSuccess('timesheet.stop.success');
} catch (\Exception $ex) {
$this->flashError('timesheet.stop.error', ['%reason%' => $ex->getMessage()]);
}
return $this->stop($entry, 'admin_timesheet');
}
return $this->redirectToRoute('admin_timesheet');
/**
* The route to edit an existing entry.
*
* @Route("/{id}/edit", name="admin_timesheet_edit")
* @Method({"GET", "POST"})
* @Security("is_granted('edit', entry)")
*
* @param Timesheet $entry
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Timesheet $entry, Request $request)
{
return $this->edit($entry, $request, 'admin_timesheet_paginated', 'TimesheetBundle:admin:timesheet_edit.html.twig');
}
/**
* The route to create a new entry by form.
*
* @Route("/create", name="admin_timesheet_create")
* @Method({"GET", "POST"})
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function createAction(Request $request)
{
return $this->create($request, 'admin_timesheet', 'TimesheetBundle:admin:timesheet_edit.html.twig');
}
/**
* @param Timesheet $entry
* @return \Symfony\Component\Form\FormInterface
*/
protected function getCreateForm(Timesheet $entry)
{
return $this->createForm(
TimesheetAdminForm::class,
$entry,
[
'action' => $this->generateUrl('admin_timesheet_create'),
'method' => 'POST',
'currency' => Customer::DEFAULT_CURRENCY,
]
);
}
/**
* @param Timesheet $entry
* @param int $page
* @return \Symfony\Component\Form\FormInterface
*/
protected function getEditForm(Timesheet $entry, $page)
{
return $this->createForm(
TimesheetAdminForm::class,
$entry,
[
'action' => $this->generateUrl('admin_timesheet_edit', [
'id' => $entry->getId(),
'page' => $page
]),
'method' => 'POST',
'currency' => $entry->getActivity()->getProject()->getCustomer()->getCurrency(),
]
);
}
}

View File

@@ -87,14 +87,7 @@ class TimesheetController extends AbstractController
*/
public function stopAction(Timesheet $entry, Request $request)
{
try {
$this->getRepository()->stopRecording($entry);
$this->flashSuccess('timesheet.stop.success');
} catch (\Exception $ex) {
$this->flashError('timesheet.stop.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute('timesheet');
return $this->stop($entry, 'timesheet');
}
/**
@@ -134,26 +127,7 @@ class TimesheetController extends AbstractController
*/
public function editAction(Timesheet $entry, Request $request)
{
$editForm = $this->createEditForm($entry, $request->get('page'));
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entry);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
return $this->redirectToRoute('timesheet_paginated', ['page' => $request->get('page')]);
}
return $this->render(
'TimesheetBundle:timesheet:edit.html.twig',
[
'entry' => $entry,
'form' => $editForm->createView(),
]
);
return $this->edit($entry, $request, 'timesheet_paginated', 'TimesheetBundle:timesheet:edit.html.twig');
}
/**
@@ -167,11 +141,16 @@ class TimesheetController extends AbstractController
*/
public function createAction(Request $request)
{
$entry = new Timesheet();
$entry->setUser($this->getUser());
$entry->setBegin(new \DateTime());
return $this->create($request, 'timesheet', 'TimesheetBundle:timesheet:edit.html.twig');
}
$createForm = $this->createForm(
/**
* @param Timesheet $entry
* @return \Symfony\Component\Form\FormInterface
*/
protected function getCreateForm(Timesheet $entry)
{
return $this->createForm(
TimesheetEditForm::class,
$entry,
[
@@ -180,27 +159,6 @@ class TimesheetController extends AbstractController
'currency' => Customer::DEFAULT_CURRENCY,
]
);
$createForm->handleRequest($request);
if ($createForm->isSubmitted() && $createForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entry);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
return $this->redirectToRoute('timesheet');
}
return $this->render(
'TimesheetBundle:timesheet:edit.html.twig',
[
'entry' => $entry,
'form' => $createForm->createView(),
]
);
}
/**
@@ -208,7 +166,7 @@ class TimesheetController extends AbstractController
* @param int $page
* @return \Symfony\Component\Form\FormInterface
*/
private function createEditForm(Timesheet $entry, $page)
protected function getEditForm(Timesheet $entry, $page)
{
return $this->createForm(
TimesheetEditForm::class,

View File

@@ -105,4 +105,102 @@ trait TimesheetControllerTrait
]
);
}
/**
* @param Timesheet $entry
* @param string $route
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
protected function stop(Timesheet $entry, $route)
{
try {
$this->getRepository()->stopRecording($entry);
$this->flashSuccess('timesheet.stop.success');
} catch (\Exception $ex) {
$this->flashError('timesheet.stop.error', ['%reason%' => $ex->getMessage()]);
}
return $this->redirectToRoute($route);
}
/**
* @param Timesheet $entry
* @param Request $request
* @param string $redirectRoute
* @param string $renderTemplate
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
protected function edit(Timesheet $entry, Request $request, $redirectRoute, $renderTemplate)
{
$editForm = $this->getEditForm($entry, $request->get('page'));
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entry);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
return $this->redirectToRoute($redirectRoute, ['page' => $request->get('page')]);
}
return $this->render(
$renderTemplate,
[
'entry' => $entry,
'form' => $editForm->createView(),
]
);
}
/**
* @param Request $request
* @param string $redirectRoute
* @param string $renderTemplate
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
protected function create(Request $request, $redirectRoute, $renderTemplate)
{
$entry = new Timesheet();
$entry->setUser($this->getUser());
$entry->setBegin(new \DateTime());
$createForm = $this->getCreateForm($entry);
$createForm->handleRequest($request);
if ($createForm->isSubmitted() && $createForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entry);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');
return $this->redirectToRoute($redirectRoute);
}
return $this->render(
$renderTemplate,
[
'entry' => $entry,
'form' => $createForm->createView(),
]
);
}
/**
* @param Timesheet $entry
* @return \Symfony\Component\Form\FormInterface
*/
abstract protected function getCreateForm(Timesheet $entry);
/**
* @param Timesheet $entry
* @param int $page
* @return \Symfony\Component\Form\FormInterface
*/
abstract protected function getEditForm(Timesheet $entry, $page);
}