diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php index a30e23532..37f32af28 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php @@ -49,13 +49,6 @@ class ActivityController extends AbstractController protected LoggerInterface $logger; - /** - * ActivityController constructor. - * - * @param EventDispatcherInterface $eventDispatcher - * @param AuthorizationHelper $authorizationHelper - * @param \Psr\Log\LoggerInterface $logger - */ public function __construct( EventDispatcherInterface $eventDispatcher, AuthorizationHelper $authorizationHelper, @@ -68,9 +61,8 @@ class ActivityController extends AbstractController /** * Lists all Activity entities. - * */ - public function listAction($person_id) + public function listAction($person_id): Response { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); @@ -121,16 +113,12 @@ class ActivityController extends AbstractController ]); } - /** - * Displays a form to create a new Activity entity. - * - */ - public function newAction($person_id, Request $request) + public function newAction($person_id, Request $request): Response { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); - if ($person === NULL){ + if (null === $person) { throw $this->createNotFoundException('Person not found'); } @@ -180,47 +168,7 @@ 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. - * - */ - public function showAction($person_id, $id) + public function showAction($person_id, $id): Response { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); @@ -259,7 +207,7 @@ class ActivityController extends AbstractController * Displays a form to edit an existing Activity entity. * */ - public function editAction($person_id, $id) + public function editAction($person_id, $id, Request $request): Response { $em = $this->getDoctrine()->getManager(); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); @@ -278,7 +226,21 @@ class ActivityController extends AbstractController $this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity); - $editForm = $this->createEditForm($entity); + $form = $this->createForm(ActivityType::class, $entity, [ + 'center' => $entity->getCenter(), + 'role' => new Role('CHILL_ACTIVITY_UPDATE'), + 'activityType' => $entity->getType(), + ])->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em->persist($entity); + $em->flush(); + + $this->addFlash('success', $this->get('translator')->trans('Success : activity updated!')); + + return $this->redirect($this->generateUrl('chill_activity_activity_show', array('id' => $id, 'person_id' => $person_id))); + } + $deleteForm = $this->createDeleteForm($id, $person); $event = new PrivacyEvent($person, array( @@ -290,87 +252,12 @@ class ActivityController extends AbstractController return $this->render('ChillActivityBundle:Activity:edit.html.twig', array( 'entity' => $entity, - 'edit_form' => $editForm->createView(), + 'edit_form' => $form->createView(), 'delete_form' => $deleteForm->createView(), 'person' => $person )); } - /** - * Creates a form to edit a Activity entity. - * - * @param Activity $entity The entity - */ - private function createEditForm(Activity $entity): FormInterface - { - return $this->createForm(ActivityType::class, $entity, [ - 'action' => $this->generateUrl('chill_activity_activity_update', [ - 'id' => $entity->getId(), - 'person_id' => $entity->getPerson()->getId() - ]), - 'method' => 'PUT', - 'center' => $entity->getCenter(), - 'role' => new Role('CHILL_ACTIVITY_UPDATE'), - 'activityType' => $entity->getType(), - ]); - } - - /** - * Edits an existing Activity entity. - * - */ - public function updateAction(Request $request, $person_id, $id) - { - $em = $this->getDoctrine()->getManager(); - - $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); - $entity = $em->getRepository('ChillActivityBundle:Activity')->find($id); - - if (!$entity) { - throw $this->createNotFoundException('Unable to find Activity entity.'); - } - - $this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity); - - $deleteForm = $this->createDeleteForm($id, $person); - $editForm = $this->createEditForm($entity); - $editForm->handleRequest($request); - - $event = new PrivacyEvent($person, array( - 'element_class' => Activity::class, - 'element_id' => $entity->getId(), - 'action' => 'update' - )); - $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); - - if ($editForm->isValid()) { - $em->flush(); - - $this->get('session') - ->getFlashBag() - ->add('success', - $this->get('translator') - ->trans('Success : activity updated!') - ); - - return $this->redirect($this->generateUrl('chill_activity_activity_show', array('id' => $id, 'person_id' => $person_id))); - } - - $this->get('session') - ->getFlashBag() - ->add('error', - $this->get('translator') - ->trans('This form contains errors') - ); - - return $this->render('ChillActivityBundle:Activity:edit.html.twig', array( - 'person' => $entity->getPerson(), - 'entity' => $entity, - 'edit_form' => $editForm->createView(), - 'delete_form' => $deleteForm->createView(), - )); - } - /** * Deletes a Activity entity. * diff --git a/src/Bundle/ChillActivityBundle/config/routes/activity.yaml b/src/Bundle/ChillActivityBundle/config/routes/activity.yaml index 7c7e2bb85..62459db5d 100644 --- a/src/Bundle/ChillActivityBundle/config/routes/activity.yaml +++ b/src/Bundle/ChillActivityBundle/config/routes/activity.yaml @@ -15,19 +15,10 @@ chill_activity_activity_new: controller: Chill\ActivityBundle\Controller\ActivityController::newAction methods: [POST, GET] -chill_activity_activity_create: - path: /{_locale}/person/{person_id}/activity/create - controller: Chill\ActivityBundle\Controller\ActivityController::createAction - methods: POST - chill_activity_activity_edit: path: /{_locale}/person/{person_id}/activity/{id}/edit controller: Chill\ActivityBundle\Controller\ActivityController::editAction - -chill_activity_activity_update: - path: /{_locale}/person/{person_id}/activity/{id}/update - controller: Chill\ActivityBundle\Controller\ActivityController::updateAction - methods: [POST, PUT] + methods: [GET, POST, PUT] chill_activity_activity_delete: path: /{_locale}/person/{person_id}/activity/{id}/delete