Form & save into DB

This commit is contained in:
Jean-Francois Monfort
2021-05-06 09:36:03 +02:00
parent 82d8556f24
commit 4d8afd53ad
7 changed files with 109 additions and 134 deletions

View File

@@ -121,80 +121,6 @@ class ActivityController extends AbstractController
]);
}
/**
* Creates a new Activity entity.
*
*/
public function createAction($person_id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException('person not found');
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$entity = new Activity();
$entity->setPerson($person);
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity,
'creation of this activity not allowed');
$em->persist($entity);
$em->flush();
$this->get('session')
->getFlashBag()
->add('success',
$this->get('translator')
->trans('Success : activity created!')
);
return $this->redirect(
$this->generateUrl('chill_activity_activity_show',
array('id' => $entity->getId(), 'person_id' => $person_id)));
}
$this->get('session')
->getFlashBag()->add('danger',
$this->get('translator')
->trans('The form is not valid. The activity has not been created !')
);
return $this->render('ChillActivityBundle:Activity:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
'person' => $person
));
}
/**
* Creates a form to create a Activity entity.
*
* @param Activity $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Activity $entity): FormInterface
{
return $this->createForm(ActivityType::class, $entity, [
'action' => $this->generateUrl('chill_activity_activity_create', [
'person_id' => $entity->getPerson()->getId(),
]),
'method' => 'POST',
'center' => $entity->getCenter(),
'role' => new Role('CHILL_ACTIVITY_CREATE'),
'activityType' => $entity->getType(),
]);
}
/**
* Displays a form to create a new Activity entity.
*
@@ -229,7 +155,23 @@ class ActivityController extends AbstractController
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity);
$form = $this->createCreateForm($entity);
$form = $this->createForm(ActivityType::class, $entity, [
'center' => $entity->getCenter(),
'role' => new Role('CHILL_ACTIVITY_CREATE'),
'activityType' => $entity->getType(),
])->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($entity);
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('Success : activity created!'));
return $this->redirectToRoute('chill_activity_activity_show', [
'id' => $entity->getId(),
'person_id' => $person_id
]);
}
return $this->render('ChillActivityBundle:Activity:new.html.twig', array(
'person' => $person,
@@ -238,6 +180,42 @@ class ActivityController extends AbstractController
));
}
/**
* Creates a new Activity entity.
*
*/
public function createAction($person_id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException('person not found');
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$entity = new Activity();
$entity->setPerson($person);
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
$this->get('session')
->getFlashBag()->add('danger',
$this->get('translator')
->trans('The form is not valid. The activity has not been created !')
);
return $this->render('ChillActivityBundle:Activity:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
'person' => $person
));
}
/**
* Finds and displays a Activity entity.
*