WIP: Implement document handling

This commit is contained in:
Björn Fromme
2023-10-12 10:41:31 +02:00
parent 3e7ca7e166
commit 46915955b0
18 changed files with 529 additions and 49 deletions
@@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class BpnDateController extends AbstractController
class DestinationController extends AbstractController
{
public function __construct(private readonly DestinationRepository $bpnDateRepository)
{}
@@ -0,0 +1,32 @@
<?php
namespace App\Controller\Admin\Autocomplete;
use App\Repository\UploadRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class DocumentController extends AbstractController
{
public function __construct(private readonly UploadRepository $uploadRepository)
{}
#[Route('/admin/autocomplete/document', name: 'app_admin_autocomplete_document')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): JsonResponse
{
try {
$query = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$queryString = $query['search'];
} catch (\JsonException $e) {
throw $this->createNotFoundException();
}
$dates = $this->uploadRepository->getAutocompletionData($queryString);
return $this->json($dates);
}
}
@@ -0,0 +1,36 @@
<?php
namespace App\Controller\Admin\Document;
use App\Entity\Upload;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class DeleteController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger
) {
}
#[Route('/admin/document/delete/{uuid}', name: 'app_admin_document_delete')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
#[IsGranted('DELETE', subject: 'document')]
public function index(Upload $document): Response
{
$this->entityManager->remove($document);
$this->entityManager->flush();
$this->addFlash('success', 'Das Dokument wurde gelöscht');
$this->logger->info('Delete document', [
'document' => $document->getOriginalFilename(),
]);
return $this->redirectToRoute('app_admin_document_index');
}
}
@@ -2,17 +2,40 @@
namespace App\Controller\Admin\Document;
use App\Repository\UploadRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class IndexController extends AbstractController
{
public function __construct(
private readonly UploadRepository $uploadRepository,
private readonly PaginatorInterface $paginator
) {
}
#[Route('/admin/document', name: 'app_admin_document_index')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(): Response
public function index(Request $request): Response
{
return $this->render('admin/document/index.html.twig');
$query = $this->uploadRepository->getDocumentListQuery();
$pagination = $this->paginator->paginate(
$query,
$request->query->getInt('page', 1),
10,
[
'defaultSortFieldName' => 'upload.originalFilename',
'defaultSortDirection' => 'asc',
]
);
return $this->render('admin/document/index.html.twig', [
'pagination' => $pagination,
]);
}
}
@@ -0,0 +1,63 @@
<?php
namespace App\Controller\Admin\Document;
use App\Entity\Upload;
use App\Entity\User;
use App\Model\AjaxModalResponseDto;
use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\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('/admin/document/replace/{uuid}', name: 'app_admin_document_replace')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Upload $upload, Request $request): JsonResponse
{
// 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();
}
$response = new AjaxModalResponseDto();
$formAction = $this->generateUrl('app_admin_document_replace', ['uuid' => $upload->getUuid()]);
$form = $this
->createFormBuilder(null, ['action' => $formAction, 'ajax_submit' => true])
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success', 'Das Dokument wurden ersetzt');
$this->logger->info('Replace document');
$response->setCloseAndRedirect($this->generateUrl('app_admin_document_index'));
} else {
$response->setContent($this->renderView('admin/document/replace.html.twig', [
'form' => $form,
]));
}
return $this->json($response);
}
}
@@ -0,0 +1,66 @@
<?php
namespace App\Controller\Admin\Document;
use App\Entity\Upload;
use App\Entity\User;
use App\Model\AjaxModalResponseDto;
use App\Service\Upload\UploadHandler;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\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('/admin/document/upload', name: 'app_admin_document_upload')]
#[IsGranted('ROLE_ADMINISTRATIVE')]
public function index(Request $request): JsonResponse
{
// 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();
foreach ($uploadSession->getUploads() as $upload) {
$document = Upload::fromUploadDto($upload, $user, Upload::TYPE_DOCUMENT);
$this->entityManager->persist($document);
}
$this->entityManager->flush();
$this->uploadHandler->moveUploadSessionFilesFromOrphanage(Upload::TYPE_DOCUMENT, $uploadSession);
$this->uploadHandler->destroyUploadSession();
}
$response = new AjaxModalResponseDto();
$formAction = $this->generateUrl('app_admin_document_upload');
$form = $this
->createFormBuilder(null, ['action' => $formAction, 'ajax_submit' => true])
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash('success', 'Das/die Dokument/e wurden hochgeladen');
$this->logger->info('Upload document');
$response->setCloseAndRedirect($this->generateUrl('app_admin_document_index'));
} else {
$response->setContent($this->renderView('admin/document/upload.html.twig', [
'form' => $form,
]));
}
return $this->json($response);
}
}