add tag assign to documents batch

This commit is contained in:
team 1
2026-02-25 15:37:29 +01:00
parent 4b5ad9b338
commit 3e264cbff4
4 changed files with 269 additions and 3 deletions

View File

@@ -4,9 +4,12 @@ declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\Document;
use App\Entity\DocumentTag;
use App\Entity\Tag;
use App\Entity\TagRebuildJob;
use App\Tag\TagService;
use App\Service\TagRebuildJobService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -20,10 +23,10 @@ final class TagController extends AbstractController
#[Route('', name: 'admin_tags_index', methods: ['GET'])]
public function index(
EntityManagerInterface $em,
\App\Service\TagRebuildJobService $jobs
TagRebuildJobService $jobs
): Response {
$tags = $em->getRepository(\App\Entity\Tag::class)
$tags = $em->getRepository(Tag::class)
->findBy([], ['label' => 'ASC']);
return $this->render('admin/tag/index.html.twig', [
@@ -89,5 +92,71 @@ final class TagController extends AbstractController
return $this->redirectToRoute('admin_tags_index');
}
#[Route('/{id}/assign', name: 'admin_tags_assign', methods: ['GET', 'POST'])]
public function assign(
string $id,
EntityManagerInterface $em,
Request $request,
TagService $tagService,
TagRebuildJobService $jobs
): Response {
$tag = $em->getRepository(Tag::class)->find($id);
if (!$tag instanceof Tag) {
throw $this->createNotFoundException('Tag nicht gefunden.');
}
// Alle Dokumente laden
$documents = $em->getRepository(Document::class)->findAll();
$documentsData = array_map(
fn(Document $d) => [
'id' => (string) $d->getId(),
'title' => $d->getTitle(),
],
$documents
);
// Aktuell zugewiesene Dokumente ermitteln
$existingRelations = $em
->getRepository(DocumentTag::class)
->findBy(['tag' => $tag]);
$assignedDocIds = array_map(
fn(DocumentTag $dt) => (string) $dt->getDocument()->getId(),
$existingRelations
);
if ($request->isMethod('POST')) {
if (!$this->isCsrfTokenValid(
'assign_tag_' . $tag->getId(),
$request->request->get('_token')
)) {
throw $this->createAccessDeniedException();
}
$selectedIds = $request->request->all('documents') ?? [];
$tagService->syncTagDocuments($tag, $selectedIds);
$this->addFlash('success', 'Zuweisungen aktualisiert.');
return $this->redirectToRoute('admin_tags_assign', [
'id' => $tag->getId()
]);
}
return $this->render('admin/tag/assign.html.twig', [
'tag' => $tag,
'documents' => $documentsData,
'assignedDocIds' => $assignedDocIds,
'latestJob' => $jobs->getLatestJob(),
'hasActiveJob' => $jobs->hasActiveJob(),
'statusRunning' => TagRebuildJob::STATUS_RUNNING,
'statusQueued' => TagRebuildJob::STATUS_QUEUED,
'statusCompleted' => TagRebuildJob::STATUS_COMPLETED,
'statusFailed' => TagRebuildJob::STATUS_FAILED,
]);
}
}