diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php b/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php index c770c03bc..8fadf73c8 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php @@ -15,51 +15,161 @@ use Chill\PersonBundle\Entity\Person\PersonResource; use Chill\PersonBundle\Form\PersonResourceType; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Repository\PersonResourceRepository; +use Chill\PersonBundle\Security\Authorization\PersonVoter; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\Translation\TranslatorInterface; final class PersonResourceController extends AbstractController { private PersonResourceRepository $personResourceRepository; private PersonRepository $personRepository; private EntityManagerInterface $em; + private TranslatorInterface $translator; public function __construct( PersonResourceRepository $personResourceRepository, PersonRepository $personRepository, - EntityManagerInterface $em + EntityManagerInterface $em, + TranslatorInterface $translator ) { $this->personResourceRepository = $personResourceRepository; $this->personRepository = $personRepository; $this->em = $em; + $this->translator = $translator; } public function listAction(Request $request, $person_id) { - $person = $this->personRepository->find($person_id); - $personResource = new PersonResource(); + $personOwner = $this->personRepository->find($person_id); + $this->denyAccessUnlessGranted(PersonVoter::SEE, $personOwner); + + $personResources = []; + $personResources = $this->personResourceRepository->findBy(['personOwner' => $personOwner->getId()]); $form = $this->createForm(PersonResourceType::class); $form->handleRequest($request); if ($request->getMethod() === Request::METHOD_POST && $form->isValid()) { - dump($personResource); + + $this->denyAccessUnlessGranted(PersonVoter::CREATE, $personOwner); + + $personResource = new PersonResource(); + + $person = $form['person']->getData(); + $thirdparty = $form['thirdparty']->getData(); + $freetext = $form['freetext']->getData(); + $comment = $form['comment']->getData(); + $kind = $form['kind']->getData(); + + $personResource->setKind($kind); + $personResource->setPerson($person); + $personResource->setThirdParty($thirdparty); + $personResource->setFreeText($freetext); + $personResource->setComment($comment); + + $personResource->setPersonOwner($personOwner); + $this->em->persist($personResource); $this->em->flush(); - return $this->redirectToRoute( - 'chill_person_resource', - ['person_id' => $person->getId()] - ); + return $this->redirectToRoute('chill_person_resource_list', [ + 'person_id' => $personOwner->getId(), + ]); + } return $this->render( 'ChillPersonBundle:PersonResource:list.html.twig', [ - 'person' => $person, + 'person' => $personOwner, + 'personResources' => $personResources, + 'form' => $form->createView() + ] + ); + } + + public function editAction(Request $request, $resource_id, $person_id): Response + { + $resource = $this->personResourceRepository->find($resource_id); + $personOwner = $this->personRepository->find($person_id); + + $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $personOwner); + + if (null === $resource) { + throw $this->createNotFoundException('Unable to find Resource entity.'); + } + + $form = $this->createForm(PersonResourceType::class, $resource); + $form->handleRequest($request); + + if($form->isSubmitted() && $form->isValid()) { + $this->em->persist($resource); + $this->em->flush(); + + return $this->redirectToRoute('chill_person_resource_list', [ + 'person_id' => $personOwner->getId(), + ]); + } + + return $this->render( + 'ChillPersonBundle:PersonResource:edit.html.twig', + [ + 'person' => $personOwner, + 'resource' => $resource, + 'form' => $form->createView(), + 'action' => 'edit' + ] + ); + } + + public function deleteAction(Request $request, $person_id, $resource_id): Response + { + + $personOwner = $this->personRepository->find($person_id); + $resource = $this->personResourceRepository->find($resource_id); + + $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $personOwner); + + if (null === $resource) { + throw $this->createNotFoundException('Unable to find Resource entity.'); + } + + $form = $this->createFormBuilder() + ->setAction($this->generateUrl('chill_person_resource_delete', [ + 'resource_id' => $resource_id, + 'person_id' => $person_id, + ])) + ->setMethod('DELETE') + ->add('submit', SubmitType::class, ['label' => 'Delete']) + ->getForm(); + + if($request->getMethod() === Request::METHOD_DELETE) { + $form->handleRequest($request); + + if ($form->isValid()) { + $this->em->remove($resource); + $this->em->flush(); + + $this->addFlash('success', $this->translator + ->trans('The resource has been successfully removed.')); + + return $this->redirectToRoute('chill_person_resource_list', [ + 'person_id' => $personOwner->getId(), + ]); + } + } + + return $this->render( + 'ChillPersonBundle:PersonResource:delete.html.twig', + [ + 'person' => $personOwner, + 'resource' => $resource, 'form' => $form->createView() ] ); diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/create.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/create.html.twig index 86c4ef9d1..6b9389f3b 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/create.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/create.html.twig @@ -1,7 +1,5 @@
-

{{ 'Add a person resource'|trans }}

- {{ form_start(form, {'attr' : {'id' : 'create-form'}}) }} {{ form_row(form.kind) }} @@ -9,19 +7,19 @@
- Associer un + Associer un
- +
- +
- +
@@ -41,14 +39,26 @@
{{ form_row(form.comment) }} -
    -
  • - -
  • -
+ + {% if action is defined %} +
    +
  • + +
  • +
+ {% else %} +
    +
  • + +
  • +
+ {% endif %} {{ form_end(form) }} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/delete.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/delete.html.twig new file mode 100644 index 000000000..57acaa634 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/delete.html.twig @@ -0,0 +1,17 @@ +{% extends "@ChillPerson/Person/layout.html.twig" %} + +{% set activeRouteKey = 'chill_person_resource_list' %} +{# {% set person = person %} #} + +{% block title 'Remove resource'|trans %} + +{% block personcontent %} +{{ include('@ChillMain/Util/confirmation_template.html.twig', + { + 'title' : 'Remove resource'|trans, + 'confirm_question' : 'Are you sure you want to remove the resource for "%name%" ?'|trans({ '%name%' : person.firstname ~ ' ' ~ person.lastname } ), + 'cancel_route' : 'chill_person_resource_list', + 'cancel_parameters' : { 'person_id' : person.id }, + 'form' : form + } ) }} +{% endblock %} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/edit.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/edit.html.twig new file mode 100644 index 000000000..6aa47228d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/edit.html.twig @@ -0,0 +1,17 @@ +{% extends "@ChillPerson/Person/layout.html.twig" %} + +{% set activeRouteKey = 'chill_person_resource_edit' %} + +{% block title %}{{ 'edit resource'|trans|capitalize }}{% endblock %} + +{% block personcontent %} + +

{{ 'edit resource'|trans }}

+ +{% include "@ChillPerson/PersonResource/create.html.twig" %} + +{% endblock %} + +{% block js %} + {{ encore_entry_script_tags('page_person_resource') }} +{% endblock %} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/list.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/list.html.twig index 844f39d41..1ed726477 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/list.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/PersonResource/list.html.twig @@ -6,33 +6,69 @@ {% block personcontent %} -
-
-
-
Name of resource
-
kind of resource
+

{{ 'List of resources'|trans }}

+ +{% if personResources is not null %} + {% for resource in personResources %} +
+
+
+
+
+
+ {% if resource.person is not null %} +

{{ resource.person }}

+ {% elseif resource.thirdparty is not null %} +

{{ resource.thirdparty }}

+ {% else %} +

{{ resource.freetext }}

+ {% endif %} +
+ {{ resource.kind.title.fr }} +
+
+
+
+
+
+
+
+
{{ resource.comment.comment }}
+
+
+
+
+
+
    +
  • + +
  • +
  • + +
  • +
+
+
+
- {#
-
How
-
are
-
#} -
-
-
-
comment zone
-
....
-
-
- {#
today
#} -
Buttons
-
-
-
+ {% endfor %} + +{% else %} +

{{ 'There are no available resources'|trans }}

+{% endif %} + +

{{ 'Add a person resource'|trans }}

{% include "@ChillPerson/PersonResource/create.html.twig" %} {% endblock %} {% block js %} - {# {{ encore_entry_script_tags('page_person_resource') }} #} + {{ encore_entry_script_tags('page_person_resource') }} {% endblock %} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/config/routes.yaml b/src/Bundle/ChillPersonBundle/config/routes.yaml index 3fbf38c0d..13d1c1227 100644 --- a/src/Bundle/ChillPersonBundle/config/routes.yaml +++ b/src/Bundle/ChillPersonBundle/config/routes.yaml @@ -56,9 +56,17 @@ chill_person_accompanying_period_re_open: controller: Chill\PersonBundle\Controller\AccompanyingPeriodController::reOpenAction chill_person_resource_list: - path: /{_locale}/person/resource/{person_id}/general + path: /{_locale}/person/{person_id}/resources/list controller: Chill\PersonBundle\Controller\PersonResourceController::listAction +chill_person_resource_edit: + path: /{_locale}/person/{person_id}/resources/{resource_id}/edit + controller: Chill\PersonBundle\Controller\PersonResourceController::editAction + +chill_person_resource_delete: + path: /{_locale}/person/{person_id}/resources/{resource_id}/delete + controller: Chill\PersonBundle\Controller\PersonResourceController::deleteAction + chill_person_address_list: path: /{_locale}/person/{person_id}/address/list controller: Chill\PersonBundle\Controller\PersonAddressController::listAction diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 7165d7084..d28b216d6 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -225,6 +225,12 @@ The accompanying course has been successfully removed.: La période d'accompagne person_resources_menu: "Ressources" Person resources: "Ressources de la personne" Add a person resource: "Ajouter une ressource" +edit resource: "Modifier ressource" +Remove resource: "Supprimer ressource" +Are you sure you want to remove the resource for "%name%" ?: Étes-vous sûr de vouloir supprimer cette ressource de %name%? +The resource has been successfully removed.: "La ressource a été supprimée." +List of resources: "Liste des ressources" +There are no available resources: "Il y aucun ressource" # pickAPersonType Pick a person: Choisir une personne