chill-bundles/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkDuplicateController.php

79 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\PersonBundle\Controller;
use Chill\PersonBundle\Form\FindAccompanyingPeriodWorkType;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class AccompanyingPeriodWorkDuplicateController extends AbstractController
{
public function __construct(private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository, private readonly TranslatorInterface $translator) {}
#[Route(path: '{_locale}/person/accompanying-period/work/{id}/assign-duplicate', name: 'chill_person_accompanying_period_work_assign_duplicate', methods: ['GET'])]
public function assignDuplicate(int $id, Request $request)
{
$acpw1= $this->accompanyingPeriodWorkRepository->find($id);
$accompanyingPeriod = $acpw1->getAccompanyingPeriod();
if (null === $acpw1) {
throw $this->createNotFoundException("Accompanying period work with id {$id} not".' found on this server');
}
$this->denyAccessUnlessGranted(
'CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_UPDATE',
$acpw1,
'You are not allowed to merge this accompanying period work'
);
$form = $this->createForm(FindAccompanyingPeriodWorkType::class, null, ['accompanyingPeriod' => $accompanyingPeriod]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$acpw2 = $form->get('person')->getData();
// Validation: Ensure person1 is not the same as person2
if ($acpw1->getId() === $acpw2->getId()) {
$this->addFlash('error', $this->translator->trans('The entities you are trying to merge cannot be the same'));
return $this->redirectToRoute('chill_person_accompanying_period_work_show', ['id' => $acpw1->getId()]);
}
$direction = $form->get('direction')->getData();
if ('starting' === $direction) {
$params = [
'acpw1_id' => $acpw1->getId(),
'acpw2_id' => $acpw2->getId(),
];
} else {
$params = [
'acpw1_id' => $acpw2->getId(),
'acpw2_id' => $acpw1->getId(),
];
}
// return $this->redirectToRoute('chill_person_acpw_duplicate_confirm', $params);
}
return $this->render('@ChillPerson/AccompanyingPeriodWorkDuplicate/assign_acpw_duplicate.html.twig', [
'accompanyingCourse' => $acpw1->getAccompanyingPeriod(),
'acpw' => $acpw1,
'form' => $form->createView(),
]);
}
}