From 59050384257220e251cea4bcb38e2ed1b476e2c4 Mon Sep 17 00:00:00 2001 From: nobohan Date: Wed, 17 Nov 2021 11:35:54 +0100 Subject: [PATCH] user: current location edit form and page --- .../Controller/UserController.php | 96 ++++++++++++++----- .../Form/UserCurrentLocationType.php | 23 +++++ .../User/edit_current_location.html.twig | 23 +++++ .../translations/messages.fr.yml | 4 + 4 files changed, 121 insertions(+), 25 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php create mode 100644 src/Bundle/ChillMainBundle/Resources/views/User/edit_current_location.html.twig diff --git a/src/Bundle/ChillMainBundle/Controller/UserController.php b/src/Bundle/ChillMainBundle/Controller/UserController.php index bb8203011..3e6bade41 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserController.php @@ -18,6 +18,7 @@ use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\UserType; use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Form\Type\ComposedGroupCenterType; +use Chill\MainBundle\Form\UserCurrentLocationType; use Chill\MainBundle\Form\UserPasswordType; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; @@ -87,7 +88,7 @@ class UserController extends CRUDController [ 'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($entity, $request)->createView(), 'delete_groupcenter_form' => array_map( - function(\Symfony\Component\Form\Form $form) { + function (\Symfony\Component\Form\Form $form) { return $form->createView(); }, iterator_to_array($this->getDeleteLinkGroupCenterByUser($entity, $request), true) @@ -145,11 +146,10 @@ class UserController extends CRUDController private function createEditPasswordForm(User $user) { return $this->createForm(UserPasswordType::class, null, array( - 'user' => $user - )) + 'user' => $user + )) ->add('submit', SubmitType::class, array('label' => 'Change password')) - ->remove('actual_password') - ; + ->remove('actual_password'); } /** @@ -167,7 +167,7 @@ class UserController extends CRUDController } $groupCenter = $em->getRepository('ChillMainBundle:GroupCenter') - ->find($gcid); + ->find($gcid); if (!$groupCenter) { throw $this->createNotFoundException('Unable to find groupCenter entity'); @@ -184,10 +184,9 @@ class UserController extends CRUDController $em->flush(); $this->addFlash('success', $this->get('translator') - ->trans('The permissions where removed.')); + ->trans('The permissions where removed.')); return $this->redirect($this->generateUrl('chill_crud_admin_user_edit', array('id' => $uid))); - } /** @@ -209,24 +208,26 @@ class UserController extends CRUDController if ($form->isValid()) { $groupCenter = $this->getPersistedGroupCenter( - $form[self::FORM_GROUP_CENTER_COMPOSED]->getData()); + $form[self::FORM_GROUP_CENTER_COMPOSED]->getData() + ); $user->addGroupCenter($groupCenter); if ($this->validator->validate($user)->count() === 0) { $em->flush(); - $this->addFlash('success', $this->get('translator')->trans('The ' + $this->addFlash('success', $this->get('translator')->trans('The ' . 'permissions have been successfully added to the user')); - $returnPathParams = $request->query->has('returnPath') ? - ['returnPath' => $request->query->get('returnPath')] : []; - - return $this->redirect($this->generateUrl('chill_crud_admin_user_edit', - \array_merge(['id' => $uid], $returnPathParams))); + $returnPathParams = $request->query->has('returnPath') ? + ['returnPath' => $request->query->get('returnPath')] : []; + return $this->redirect($this->generateUrl( + 'chill_crud_admin_user_edit', + \array_merge(['id' => $uid], $returnPathParams) + )); } - foreach($this->validator->validate($user) as $error) { + foreach ($this->validator->validate($user) as $error) { $this->addFlash('error', $error->getMessage()); } } @@ -236,7 +237,7 @@ class UserController extends CRUDController 'edit_form' => $this->createEditForm($user)->createView(), 'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user, $request)->createView(), 'delete_groupcenter_form' => array_map( - static fn(Form $form) => $form->createView(), + static fn (Form $form) => $form->createView(), iterator_to_array($this->getDeleteLinkGroupCenterByUser($user, $request), true) ) ]); @@ -247,10 +248,10 @@ class UserController extends CRUDController $em = $this->getDoctrine()->getManager(); $groupCenterManaged = $em->getRepository('ChillMainBundle:GroupCenter') - ->findOneBy(array( - 'center' => $groupCenter->getCenter(), - 'permissionsGroup' => $groupCenter->getPermissionsGroup() - )); + ->findOneBy(array( + 'center' => $groupCenter->getCenter(), + 'permissionsGroup' => $groupCenter->getPermissionsGroup() + )); if (!$groupCenterManaged) { $em->persist($groupCenter); @@ -270,8 +271,10 @@ class UserController extends CRUDController $returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : []; return $this->createFormBuilder() - ->setAction($this->generateUrl('admin_user_delete_groupcenter', - array_merge($returnPathParams, ['uid' => $user->getId(), 'gcid' => $groupCenter->getId()]))) + ->setAction($this->generateUrl( + 'admin_user_delete_groupcenter', + array_merge($returnPathParams, ['uid' => $user->getId(), 'gcid' => $groupCenter->getId()]) + )) ->setMethod('DELETE') ->add('submit', SubmitType::class, array('label' => 'Delete')) ->getForm(); @@ -285,8 +288,10 @@ class UserController extends CRUDController $returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : []; return $this->createFormBuilder() - ->setAction($this->generateUrl('admin_user_add_groupcenter', - array_merge($returnPathParams, ['uid' => $user->getId()]))) + ->setAction($this->generateUrl( + 'admin_user_add_groupcenter', + array_merge($returnPathParams, ['uid' => $user->getId()]) + )) ->setMethod('POST') ->add(self::FORM_GROUP_CENTER_COMPOSED, ComposedGroupCenterType::class) ->add('submit', SubmitType::class, array('label' => 'Add a new groupCenter')) @@ -299,4 +304,45 @@ class UserController extends CRUDController yield $groupCenter->getId() => $this->createDeleteLinkGroupCenterForm($user, $groupCenter, $request); } } + + + /** + * @param User $user + * @return \Symfony\Component\Form\Form + */ + private function createEditLocationForm() + { + return $this->createForm(UserCurrentLocationType::class) + ->add('submit', SubmitType::class, ['label' => 'Change current location']); + } + + /** + * Displays a form to edit the user current location. + * + * @Route("/{_locale}/main/user/{id}/current-location/edit", name="chill_main_user_currentlocation_edit") + */ + public function editCurrentLocationAction(User $user, Request $request) + { + $editForm = $this->createEditLocationForm(); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + $currentLocation = $editForm->get('location')->getData(); + + $user->setCurrentLocation($currentLocation); + + $this->getDoctrine()->getManager()->flush(); + $this->addFlash('success', $this->get('translator')->trans('Current location successfully updated')); + + return $this->redirect( + $request->query->has('returnPath') ? $request->query->get('returnPath') : + $this->generateUrl('chill_main_homepage') + ); + } + + return $this->render('@ChillMain/User/edit_current_location.html.twig', [ + 'entity' => $user, + 'edit_form' => $editForm->createView() + ]); + } } diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php new file mode 100644 index 000000000..fa24d987e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -0,0 +1,23 @@ +add('location', EntityType::class, [ + 'class' => Location::class, + 'choice_label' => function (Location $entity) { + return $entity->getName(); + }, + ]); + } +} diff --git a/src/Bundle/ChillMainBundle/Resources/views/User/edit_current_location.html.twig b/src/Bundle/ChillMainBundle/Resources/views/User/edit_current_location.html.twig new file mode 100644 index 000000000..aa1f248ef --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/User/edit_current_location.html.twig @@ -0,0 +1,23 @@ +{% extends 'ChillMainBundle::layout.html.twig' %} + +{% block title %}{{ 'Edit my current location'|trans }}{% endblock %} + +{% block content -%} +

{{ 'Edit my current location'|trans }}

+ + {{ form_start(edit_form) }} + {{ form_row(edit_form.location) }} + + + + {{ form_end(edit_form) }} +{% endblock %} diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 598edaec6..c3078938d 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -176,6 +176,10 @@ Flags: Drapeaux # admin section for users jobs User jobs: Métiers +# user page for current location +Edit my current location: Éditer ma localisation actuelle +Change current location: Changer ma localisation actuelle +Current location successfully updated: Localisation actuelle mise à jour #admin section for circles (old: scopes) List circles: Cercles