65 lines
2.5 KiB
PHP
65 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\Administrative\System\Document;
|
|
|
|
use App\Entity\Upload;
|
|
use App\Entity\User;
|
|
use App\Htmx\HxRedirectResponse;
|
|
use App\Service\Upload\UploadHandler;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
|
|
class ReplaceController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
#[Route('/administrative/system/document/replace/{uuid}', name: 'app_administrative_system_document_replace')]
|
|
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
|
public function index(Upload $upload, Request $request): Response
|
|
{
|
|
// Handle upload independently from form submission to avoid issues with failing validation
|
|
$uploadSession = $this->uploadHandler->getUploadSession();
|
|
if (true === $request->isMethod('POST') && 0 < $uploadSession->getCount()) {
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
Upload::fromUploadDto($uploadSession->getUploads()->first(), $user, Upload::TYPE_DOCUMENT, $upload);
|
|
$this->entityManager->flush();
|
|
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_DOCUMENT, $uploadSession);
|
|
$this->uploadHandler->destroyUploadSession();
|
|
}
|
|
|
|
$filenameBefore = $upload->getOriginalFilename();
|
|
|
|
$form = $this
|
|
->createFormBuilder()
|
|
->getForm()
|
|
;
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$this->addFlash('success', 'Das Dokument wurden ersetzt');
|
|
$this->logger->info('Replace document', [
|
|
'document_id' => $upload->getId(),
|
|
'document_filename_before' => $filenameBefore,
|
|
'document_filename_after' => $upload->getOriginalFilename(),
|
|
]);
|
|
|
|
return new HxRedirectResponse($this->generateUrl('app_administrative_system_document_index'));
|
|
}
|
|
|
|
return $this->render('administrative/system/document/modal_replace.html.twig', [
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
}
|