fix: refuse deleting a document assigned to a season task
This commit is contained in:
1 parent
4aa2d7a5e6
commit
3116d23199
7 files changed
+271
-14
No files matched your search
@@ -4,8 +4,10 @@ namespace App\Controller\Administrative\System\Document;
|
||||
|
||||
use App\Entity\Upload;
|
||||
use App\Htmx\HxRedirectResponse;
|
||||
use App\Repository\SeasonTaskDocumentRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -17,14 +19,32 @@ class DeleteController extends AbstractController
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly SeasonTaskDocumentRepository $seasonTaskDocumentRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
#[Route('/administrative/system/document/delete/{uuid}', name: 'app_administrative_system_document_delete')]
|
||||
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
||||
#[IsGranted('DELETE', subject: 'document')]
|
||||
public function index(Upload $document, Request $request): Response
|
||||
{
|
||||
public function index(
|
||||
#[MapEntity(mapping: ['uuid' => 'uuid'])]
|
||||
Upload $document,
|
||||
Request $request,
|
||||
): Response {
|
||||
// A document handed out for a season task is referenced by a NOT NULL foreign key, so
|
||||
// deleting it fails in the database. It has to be replaced in that mapping first.
|
||||
$seasonTaskDocuments = $this->seasonTaskDocumentRepository->findByDocument($document);
|
||||
|
||||
if (0 < count($seasonTaskDocuments) && true === $request->isMethod('POST')) {
|
||||
$this->addFlash('error', 'Das Dokument ist als Saisonteam-Dokument hinterlegt und kann nicht gelöscht werden');
|
||||
$this->logger->info('Refuse deleting document assigned to a season task', [
|
||||
'document_id' => $document->getId(),
|
||||
'document_filename' => $document->getOriginalFilename(),
|
||||
]);
|
||||
|
||||
return new HxRedirectResponse($this->generateUrl('app_administrative_system_document_index'));
|
||||
}
|
||||
|
||||
if (true === $request->isMethod('POST')) {
|
||||
$this->entityManager->remove($document);
|
||||
$this->entityManager->flush();
|
||||
@@ -40,6 +60,7 @@ class DeleteController extends AbstractController
|
||||
|
||||
return $this->render('administrative/system/document/modal_delete.html.twig', [
|
||||
'document' => $document,
|
||||
'seasonTaskDocuments' => $seasonTaskDocuments,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controller\Administrative\System\Document;
|
||||
|
||||
use App\Repository\SeasonTaskDocumentRepository;
|
||||
use App\Repository\UploadRepository;
|
||||
use Knp\Component\Pager\PaginatorInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@@ -15,6 +16,7 @@ class IndexController extends AbstractController
|
||||
public function __construct(
|
||||
private readonly UploadRepository $uploadRepository,
|
||||
private readonly PaginatorInterface $paginator,
|
||||
private readonly SeasonTaskDocumentRepository $seasonTaskDocumentRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -36,6 +38,7 @@ class IndexController extends AbstractController
|
||||
|
||||
return $this->render('administrative/system/document/index.html.twig', [
|
||||
'pagination' => $pagination,
|
||||
'seasonTaskDocuments' => $this->seasonTaskDocumentRepository->findGroupedByDocumentId(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Config\SeasonTask;
|
||||
use App\Entity\SeasonTaskDocument;
|
||||
use App\Entity\Upload;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
@@ -43,4 +44,31 @@ class SeasonTaskDocumentRepository extends ServiceEntityRepository
|
||||
{
|
||||
return $this->findOneBy(['taskKey' => $task->value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every mapping that points at the document, including rows whose task key no longer
|
||||
* resolves: those still hold the foreign key that blocks deleting the upload.
|
||||
*
|
||||
* @return array<int, SeasonTaskDocument>
|
||||
*/
|
||||
public function findByDocument(Upload $document): array
|
||||
{
|
||||
return $this->findBy(['document' => $document]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The mappings grouped by the id of the document they point at, for the document list.
|
||||
*
|
||||
* @return array<int, array<int, SeasonTaskDocument>>
|
||||
*/
|
||||
public function findGroupedByDocumentId(): array
|
||||
{
|
||||
$grouped = [];
|
||||
|
||||
foreach ($this->findAll() as $seasonTaskDocument) {
|
||||
$grouped[$seasonTaskDocument->getDocument()->getId()][] = $seasonTaskDocument;
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user