72 lines
2.8 KiB
PHP
72 lines
2.8 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 UploadController extends AbstractController
|
|
{
|
|
public function __construct(
|
|
private readonly UploadHandler $uploadHandler,
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
}
|
|
|
|
#[Route('/administrative/system/document/upload', name: 'app_administrative_system_document_upload')]
|
|
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
|
public function index(Request $request): Response
|
|
{
|
|
$form = $this
|
|
->createFormBuilder()
|
|
->getForm()
|
|
;
|
|
$form->handleRequest($request);
|
|
|
|
// Handle upload independently from form submission to avoid issues with failing validation
|
|
$uploadSession = $this->uploadHandler->getUploadSession();
|
|
$uploadedDocuments = [];
|
|
if (true === $request->isMethod('POST') && 0 < $uploadSession->getCount()) {
|
|
/** @var User $user */
|
|
$user = $this->getUser();
|
|
foreach ($uploadSession->getUploadsByType(Upload::TYPE_DOCUMENT) as $upload) {
|
|
$document = Upload::fromUploadDto($upload, $user, Upload::TYPE_DOCUMENT);
|
|
$uploadedDocuments[] = $document;
|
|
$this->entityManager->persist($document);
|
|
}
|
|
$this->entityManager->flush();
|
|
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_DOCUMENT, $uploadSession);
|
|
$this->uploadHandler->removeUploadSessionUploadsByType(Upload::TYPE_DOCUMENT);
|
|
}
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$count = count($uploadedDocuments);
|
|
$this->addFlash('success', 1 === $count ? 'Das Dokument wurde hochgeladen' : 'Die Dokumente wurden hochgeladen');
|
|
$loggerContext = [];
|
|
foreach ($uploadedDocuments as $document) {
|
|
$loggerContext[] = [
|
|
'document_id' => $document->getId(),
|
|
'document_filename' => $document->getOriginalFilename(),
|
|
];
|
|
}
|
|
$this->logger->info('Upload document(s)', $loggerContext);
|
|
|
|
return new HxRedirectResponse($this->generateUrl('app_administrative_system_document_index'));
|
|
}
|
|
|
|
return $this->render('administrative/system/document/modal_upload.html.twig', [
|
|
'form' => $form->createView(),
|
|
]);
|
|
}
|
|
}
|