mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
189 lines
6.2 KiB
PHP
189 lines
6.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Controller;
|
|
|
|
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 EntityManagerInterface $em;
|
|
|
|
private PersonRepository $personRepository;
|
|
|
|
private PersonResourceRepository $personResourceRepository;
|
|
|
|
private TranslatorInterface $translator;
|
|
|
|
public function __construct(
|
|
PersonResourceRepository $personResourceRepository,
|
|
PersonRepository $personRepository,
|
|
EntityManagerInterface $em,
|
|
TranslatorInterface $translator
|
|
) {
|
|
$this->personResourceRepository = $personResourceRepository;
|
|
$this->personRepository = $personRepository;
|
|
$this->em = $em;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
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(),
|
|
]
|
|
);
|
|
}
|
|
|
|
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 listAction(Request $request, $person_id)
|
|
{
|
|
$personOwner = $this->personRepository->find($person_id);
|
|
$this->denyAccessUnlessGranted(PersonVoter::SEE, $personOwner);
|
|
|
|
$personResources = [];
|
|
$personResources = $this->personResourceRepository->findBy(['personOwner' => $personOwner->getId()]);
|
|
|
|
return $this->render(
|
|
'ChillPersonBundle:PersonResource:list.html.twig',
|
|
[
|
|
'person' => $personOwner,
|
|
'personResources' => $personResources,
|
|
]
|
|
);
|
|
}
|
|
|
|
public function newAction(Request $request, $person_id)
|
|
{
|
|
$personOwner = $this->personRepository->find($person_id);
|
|
|
|
$form = $this->createForm(PersonResourceType::class);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$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_list', [
|
|
'person_id' => $personOwner->getId(),
|
|
]);
|
|
}
|
|
|
|
return $this->render(
|
|
'ChillPersonBundle:PersonResource:create.html.twig',
|
|
[
|
|
'form' => $form->createView(),
|
|
'person' => $personOwner,
|
|
]
|
|
);
|
|
}
|
|
}
|