Modernize ActivityController

This commit is contained in:
Jean-Francois Monfort 2021-05-06 10:03:49 +02:00
parent 4d8afd53ad
commit 4fd13440c6
2 changed files with 22 additions and 144 deletions

View File

@ -49,13 +49,6 @@ class ActivityController extends AbstractController
protected LoggerInterface $logger; protected LoggerInterface $logger;
/**
* ActivityController constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param AuthorizationHelper $authorizationHelper
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct( public function __construct(
EventDispatcherInterface $eventDispatcher, EventDispatcherInterface $eventDispatcher,
AuthorizationHelper $authorizationHelper, AuthorizationHelper $authorizationHelper,
@ -68,9 +61,8 @@ class ActivityController extends AbstractController
/** /**
* Lists all Activity entities. * Lists all Activity entities.
*
*/ */
public function listAction($person_id) public function listAction($person_id): Response
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
@ -121,16 +113,12 @@ class ActivityController extends AbstractController
]); ]);
} }
/** public function newAction($person_id, Request $request): Response
* Displays a form to create a new Activity entity.
*
*/
public function newAction($person_id, Request $request)
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
if ($person === NULL){ if (null === $person) {
throw $this->createNotFoundException('Person not found'); throw $this->createNotFoundException('Person not found');
} }
@ -180,47 +168,7 @@ class ActivityController extends AbstractController
)); ));
} }
public function showAction($person_id, $id): Response
/**
* 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)
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); $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. * 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(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
@ -278,7 +226,21 @@ class ActivityController extends AbstractController
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity); $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); $deleteForm = $this->createDeleteForm($id, $person);
$event = new PrivacyEvent($person, array( $event = new PrivacyEvent($person, array(
@ -290,87 +252,12 @@ class ActivityController extends AbstractController
return $this->render('ChillActivityBundle:Activity:edit.html.twig', array( return $this->render('ChillActivityBundle:Activity:edit.html.twig', array(
'entity' => $entity, 'entity' => $entity,
'edit_form' => $editForm->createView(), 'edit_form' => $form->createView(),
'delete_form' => $deleteForm->createView(), 'delete_form' => $deleteForm->createView(),
'person' => $person '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. * Deletes a Activity entity.
* *

View File

@ -15,19 +15,10 @@ chill_activity_activity_new:
controller: Chill\ActivityBundle\Controller\ActivityController::newAction controller: Chill\ActivityBundle\Controller\ActivityController::newAction
methods: [POST, GET] 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: chill_activity_activity_edit:
path: /{_locale}/person/{person_id}/activity/{id}/edit path: /{_locale}/person/{person_id}/activity/{id}/edit
controller: Chill\ActivityBundle\Controller\ActivityController::editAction controller: Chill\ActivityBundle\Controller\ActivityController::editAction
methods: [GET, POST, PUT]
chill_activity_activity_update:
path: /{_locale}/person/{person_id}/activity/{id}/update
controller: Chill\ActivityBundle\Controller\ActivityController::updateAction
methods: [POST, PUT]
chill_activity_activity_delete: chill_activity_activity_delete:
path: /{_locale}/person/{person_id}/activity/{id}/delete path: /{_locale}/person/{person_id}/activity/{id}/delete