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 @@