add tagging
This commit is contained in:
93
src/Controller/Admin/DocumentTagController.php
Normal file
93
src/Controller/Admin/DocumentTagController.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Entity\Document;
|
||||
use App\Entity\Tag;
|
||||
use App\Service\TagRebuildJobService;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[Route('/admin/documents')]
|
||||
final class DocumentTagController extends AbstractController
|
||||
{
|
||||
#[Route('/{id}/tags', name: 'admin_document_tags_edit', methods: ['GET'])]
|
||||
public function edit(string $id, EntityManagerInterface $em): Response
|
||||
{
|
||||
$document = $em->getRepository(Document::class)->find($id);
|
||||
if (!$document instanceof Document) {
|
||||
throw $this->createNotFoundException('Document not found');
|
||||
}
|
||||
|
||||
$allTags = $em->createQueryBuilder()
|
||||
->select('t')
|
||||
->from(Tag::class, 't')
|
||||
->orderBy('t.label', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$assigned = [];
|
||||
foreach ($document->getTags() as $tag) {
|
||||
$assigned[(string)$tag->getId()] = true;
|
||||
}
|
||||
|
||||
return $this->render('admin/document_tags/edit.html.twig', [
|
||||
'document' => $document,
|
||||
'allTags' => $allTags,
|
||||
'assigned' => $assigned,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}/tags/save', name: 'admin_document_tags_save', methods: ['POST'])]
|
||||
public function save(
|
||||
string $id,
|
||||
Request $request,
|
||||
EntityManagerInterface $em,
|
||||
TagRebuildJobService $jobs
|
||||
): RedirectResponse {
|
||||
|
||||
$document = $em->getRepository(Document::class)->find($id);
|
||||
if (!$document instanceof Document) {
|
||||
return $this->redirectToRoute('admin_documents');
|
||||
}
|
||||
|
||||
$selected = $request->request->all('tag_ids') ?? [];
|
||||
|
||||
$uuidObjects = [];
|
||||
foreach ($selected as $value) {
|
||||
try {
|
||||
$uuidObjects[] = \Symfony\Component\Uid\Uuid::fromString($value);
|
||||
} catch (\Throwable) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove
|
||||
foreach ($document->getTags() as $tag) {
|
||||
if (!in_array($tag->getId(), $uuidObjects, false)) {
|
||||
$document->removeTag($tag);
|
||||
}
|
||||
}
|
||||
|
||||
// Add
|
||||
foreach ($uuidObjects as $uuid) {
|
||||
$tag = $em->find(\App\Entity\Tag::class, $uuid);
|
||||
if ($tag && !$document->hasTag($tag)) {
|
||||
$document->addTag($tag);
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
$jobs->enqueueAndStartAsync();
|
||||
|
||||
return $this->redirectToRoute('admin_document_tags_edit', ['id' => $id]);
|
||||
}
|
||||
}
|
||||
101
src/Controller/Admin/TagController.php
Normal file
101
src/Controller/Admin/TagController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Entity\Tag;
|
||||
use App\Service\TagRebuildJobService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/admin/tags')]
|
||||
final class TagController extends AbstractController
|
||||
{
|
||||
#[Route('', name: 'admin_tags_index', methods: ['GET'])]
|
||||
public function index(EntityManagerInterface $em): Response
|
||||
{
|
||||
$tags = $em->createQueryBuilder()
|
||||
->select('t')
|
||||
->from(Tag::class, 't')
|
||||
->orderBy('t.label', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
return $this->render('admin/tag/index.html.twig', [
|
||||
'tags' => $tags,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/create', name: 'admin_tags_create', methods: ['POST'])]
|
||||
public function create(Request $request, EntityManagerInterface $em, TagRebuildJobService $jobs): RedirectResponse
|
||||
{
|
||||
$token = (string)$request->request->get('_token', '');
|
||||
if (!$this->isCsrfTokenValid('admin_tag_create', $token)) {
|
||||
$this->addFlash('danger', 'Ungültiges CSRF Token.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
|
||||
$label = trim((string)$request->request->get('label', ''));
|
||||
$slug = trim((string)$request->request->get('slug', ''));
|
||||
$desc = trim((string)$request->request->get('description', ''));
|
||||
|
||||
if ($label === '' || $slug === '') {
|
||||
$this->addFlash('danger', 'Label und Slug sind Pflichtfelder.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
|
||||
$exists = (int)$em->createQueryBuilder()
|
||||
->select('COUNT(t.id)')
|
||||
->from(Tag::class, 't')
|
||||
->where('t.slug = :slug')
|
||||
->setParameter('slug', $slug)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
|
||||
if ($exists > 0) {
|
||||
$this->addFlash('danger', 'Slug existiert bereits.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
|
||||
$tag = new Tag($slug, $label, $desc !== '' ? $desc : null);
|
||||
|
||||
$em->persist($tag);
|
||||
$em->flush();
|
||||
|
||||
// enqueue async rebuild
|
||||
$jobs->enqueueAndStartAsync();
|
||||
|
||||
$this->addFlash('success', 'Tag wurde erstellt. Rebuild läuft im Hintergrund.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
|
||||
#[Route('/{id}/delete', name: 'admin_tags_delete', methods: ['POST'])]
|
||||
public function delete(string $id, Request $request, EntityManagerInterface $em, TagRebuildJobService $jobs): RedirectResponse
|
||||
{
|
||||
$token = (string)$request->request->get('_token', '');
|
||||
if (!$this->isCsrfTokenValid('admin_tag_delete_' . $id, $token)) {
|
||||
$this->addFlash('danger', 'Ungültiges CSRF Token.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
|
||||
$tag = $em->getRepository(Tag::class)->find($id);
|
||||
if (!$tag instanceof Tag) {
|
||||
$this->addFlash('danger', 'Tag nicht gefunden.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
|
||||
$em->remove($tag);
|
||||
$em->flush();
|
||||
|
||||
// enqueue async rebuild
|
||||
$jobs->enqueueAndStartAsync();
|
||||
|
||||
$this->addFlash('success', 'Tag wurde gelöscht. Rebuild läuft im Hintergrund.');
|
||||
return $this->redirectToRoute('admin_tags_index');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user