diff --git a/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php b/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php index f88ac0c8a..df291b5bd 100644 --- a/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php +++ b/src/Bundle/ChillMainBundle/Routing/ChillUrlGenerator.php @@ -40,4 +40,15 @@ final readonly class ChillUrlGenerator implements ChillUrlGeneratorInterface return $this->urlGenerator->generate($name, $parameters, $referenceType); } + + public function forwardReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string + { + $request = $this->requestStack->getCurrentRequest(); + + if ($request->query->has('returnPath')) { + return $this->urlGenerator->generate($name, [...$parameters, 'returnPath' => $request->query->get('returnPath')], $referenceType); + } + + return $this->urlGenerator->generate($name, $parameters, $referenceType); + } } diff --git a/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php b/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php index 5c00fa665..26b7fe7a7 100644 --- a/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php +++ b/src/Bundle/ChillMainBundle/Routing/ChillUrlGeneratorInterface.php @@ -32,4 +32,9 @@ interface ChillUrlGeneratorInterface * Get the return path or, if any, generate an url. */ public function returnPathOr(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string; + + /** + * Return a new URL, with the same return path as the existing one. If any, no return path is forwarded. + */ + public function forwardReturnPath(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string; } diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationDocumentDuplicateController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationDocumentDuplicateController.php index ec48b5b24..033533b05 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationDocumentDuplicateController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationDocumentDuplicateController.php @@ -11,11 +11,13 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; +use Chill\MainBundle\Routing\ChillUrlGeneratorInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodWorkVoter; use Chill\PersonBundle\Service\AccompanyingPeriodWorkEvaluationDocument\AccompanyingPeriodWorkEvaluationDocumentDuplicator; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\Routing\Annotation\Route; @@ -30,10 +32,11 @@ class AccompanyingPeriodWorkEvaluationDocumentDuplicateController private readonly Security $security, private readonly SerializerInterface $serializer, private readonly EntityManagerInterface $entityManager, + private readonly ChillUrlGeneratorInterface $urlGenerator, ) {} #[Route('/api/1.0/person/accompanying-course-work-evaluation-document/{id}/duplicate', methods: ['POST'])] - public function __invoke(AccompanyingPeriodWorkEvaluationDocument $document): Response + public function duplicateApi(AccompanyingPeriodWorkEvaluationDocument $document): Response { $work = $document->getAccompanyingPeriodWorkEvaluation()->getAccompanyingPeriodWork(); @@ -52,4 +55,27 @@ class AccompanyingPeriodWorkEvaluationDocumentDuplicateController json: true ); } + + #[Route('/{_locale}/person/accompanying-course-work-evaluation-document/{id}/duplicate', name: 'chill_person_accompanying_period_work_evaluation_document_duplicate', methods: ['POST'])] + public function duplicate(AccompanyingPeriodWorkEvaluationDocument $document): Response + { + $work = $document->getAccompanyingPeriodWorkEvaluation()->getAccompanyingPeriodWork(); + + if (!$this->security->isGranted(AccompanyingPeriodWorkVoter::UPDATE, $work)) { + throw new AccessDeniedHttpException('not allowed to edit this accompanying period work'); + } + + $duplicatedDocument = $this->duplicator->duplicate($document); + + $this->entityManager->persist($duplicatedDocument); + $this->entityManager->persist($duplicatedDocument->getStoredObject()); + $this->entityManager->flush(); + + return new RedirectResponse( + $this->urlGenerator->forwardReturnPath( + 'chill_person_accompanying_period_work_edit', + ['id' => $work->getId(), 'doc_id' => $duplicatedDocument->getId()] + ) + ); + } } diff --git a/src/Bundle/ChillPersonBundle/Resources/views/GenericDoc/evaluation_document.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/GenericDoc/evaluation_document.html.twig index ae54d077b..70ff82b88 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/GenericDoc/evaluation_document.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/GenericDoc/evaluation_document.html.twig @@ -60,9 +60,11 @@ {{ document.storedObject|chill_document_button_group(document.title, is_granted('CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_UPDATE', document.accompanyingPeriodWorkEvaluation.accompanyingPeriodWork)) }} {% endif %} - {% if is_granted('CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_SEE', w)%} + {% if is_granted('CHILL_MAIN_ACCOMPANYING_PERIOD_WORK_UPDATE', w) %}