improved form handling

improved profile editing
added activity editing
This commit is contained in:
Kevin Papst
2016-11-12 23:22:47 +01:00
parent 8401797728
commit cc269d3142
17 changed files with 425 additions and 34 deletions

View File

@@ -11,13 +11,16 @@
namespace TimesheetBundle\Controller\Admin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use TimesheetBundle\Entity\Activity;
use TimesheetBundle\Entity\Timesheet;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
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\ActivityEditForm;
use TimesheetBundle\Repository\ActivityRepository;
/**
* Controller used to manage activities in the admin part of the site.
@@ -42,4 +45,67 @@ class ActivityController extends Controller
return $this->render('TimesheetBundle:admin:activity.html.twig', ['entries' => $entries]);
}
/**
* @Route("/{id}/edit", name="admin_activity_edit")
* @Method({"GET", "POST"})
*/
public function editAction($id, Request $request)
{
$activity = $this->getById($id);
$editForm = $this->createEditForm($activity);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($activity);
$entityManager->flush();
$this->addFlash('success', 'action.updated_successfully');
return $this->redirectToRoute(
'admin_activity', ['id' => $activity->getId()]
);
}
return $this->render(
'TimesheetBundle:admin:activity_edit.html.twig',
[
'activity' => $activity,
'form' => $editForm->createView()
]
);
}
/**
* @param $id
* @return null|Activity
*/
protected function getById($id)
{
/* @var $repo ActivityRepository */
$repo = $this->getDoctrine()->getRepository(Activity::class);
$activity = $repo->getById($id);
if (null === $activity) {
throw new NotFoundHttpException('Activity "'.$id.'" does not exist');
}
return $activity;
}
/**
* @param Activity $activity
* @return \Symfony\Component\Form\Form
*/
private function createEditForm(Activity $activity)
{
return $this->createForm(
ActivityEditForm::class,
$activity,
[
'action' => $this->generateUrl('admin_activity_edit', ['id' => $activity->getId()]),
'method' => 'POST'
]
);
}
}