2015-08-07 18:17:23 +02:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Controller;
|
2015-08-07 18:17:23 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2021-08-03 14:35:45 +02:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
2016-04-14 15:03:22 +02:00
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
|
|
|
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
2025-03-16 15:00:28 +01:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
2015-08-19 11:46:21 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 16:59:43 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2025-03-23 13:46:38 +01:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-10-04 14:07:20 +02:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2025-03-16 15:00:28 +01:00
|
|
|
use Symfony\Component\Security\Core\Security;
|
2023-06-13 11:24:27 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Entity\Entry;
|
|
|
|
use Wallabag\Entity\Tag;
|
|
|
|
use Wallabag\Form\Type\NewTagType;
|
|
|
|
use Wallabag\Form\Type\RenameTagType;
|
|
|
|
use Wallabag\Helper\PreparePagerForEntries;
|
|
|
|
use Wallabag\Helper\Redirect;
|
|
|
|
use Wallabag\Helper\TagsAssigner;
|
|
|
|
use Wallabag\Repository\EntryRepository;
|
|
|
|
use Wallabag\Repository\TagRepository;
|
2015-08-07 18:17:23 +02:00
|
|
|
|
2022-12-19 13:23:56 +01:00
|
|
|
class TagController extends AbstractController
|
2015-08-07 18:17:23 +02:00
|
|
|
{
|
2025-04-05 13:54:27 +02:00
|
|
|
public function __construct(
|
2025-04-05 13:59:36 +02:00
|
|
|
private readonly EntityManagerInterface $entityManager,
|
|
|
|
private readonly TagsAssigner $tagsAssigner,
|
|
|
|
private readonly Redirect $redirectHelper,
|
|
|
|
private readonly Security $security,
|
2025-04-05 13:54:27 +02:00
|
|
|
) {
|
2022-12-19 10:37:22 +01:00
|
|
|
}
|
|
|
|
|
2015-08-19 11:46:21 +02:00
|
|
|
/**
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-08-19 11:46:21 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/new-tag/{entry}', name: 'new_tag', methods: ['POST'], requirements: ['entry' => '\d+'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('TAG', subject: 'entry')]
|
2023-06-13 11:24:27 +02:00
|
|
|
public function addTagFormAction(Request $request, Entry $entry, TranslatorInterface $translator)
|
2015-08-19 11:46:21 +02:00
|
|
|
{
|
2016-03-27 17:09:33 +02:00
|
|
|
$form = $this->createForm(NewTagType::class, new Tag());
|
2015-08-19 11:46:21 +02:00
|
|
|
$form->handleRequest($request);
|
|
|
|
|
2023-09-26 18:02:46 +02:00
|
|
|
$tags = $form->get('label')->getData() ?? '';
|
2025-04-05 14:01:48 +02:00
|
|
|
$tagsExploded = explode(',', (string) $tags);
|
2023-02-07 21:18:06 +01:00
|
|
|
|
|
|
|
// avoid too much tag to be added
|
2025-04-05 14:01:48 +02:00
|
|
|
if (\count($tagsExploded) >= NewTagType::MAX_TAGS || \strlen((string) $tags) >= NewTagType::MAX_LENGTH) {
|
2023-06-13 11:24:27 +02:00
|
|
|
$message = $translator->trans('flashes.tag.notice.too_much_tags', [
|
|
|
|
'%tags%' => NewTagType::MAX_TAGS,
|
|
|
|
'%characters%' => NewTagType::MAX_LENGTH,
|
|
|
|
]);
|
|
|
|
$this->addFlash('notice', $message);
|
|
|
|
|
2023-02-07 21:18:06 +01:00
|
|
|
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
|
|
|
}
|
|
|
|
|
2016-12-14 11:54:30 +01:00
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->tagsAssigner->assignTagsToEntry(
|
2016-03-27 17:09:33 +02:00
|
|
|
$entry,
|
|
|
|
$form->get('label')->getData()
|
|
|
|
);
|
2015-08-19 11:46:21 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->entityManager->persist($entry);
|
|
|
|
$this->entityManager->flush();
|
2015-08-19 11:46:21 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->addFlash(
|
2015-08-19 11:46:21 +02:00
|
|
|
'notice',
|
2016-03-11 14:48:46 +01:00
|
|
|
'flashes.tag.notice.tag_added'
|
2015-08-19 11:46:21 +02:00
|
|
|
);
|
|
|
|
|
2016-04-12 11:36:01 +02:00
|
|
|
return $this->redirect($this->generateUrl('view', ['id' => $entry->getId()]));
|
2015-08-19 11:46:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-19 00:03:14 +01:00
|
|
|
return $this->render('Tag/new_form.html.twig', [
|
2015-08-19 11:46:21 +02:00
|
|
|
'form' => $form->createView(),
|
|
|
|
'entry' => $entry,
|
2016-04-12 11:36:01 +02:00
|
|
|
]);
|
2015-08-19 11:46:21 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 17:41:28 +01:00
|
|
|
/**
|
|
|
|
* Removes tag from entry.
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2016-02-10 17:41:28 +01:00
|
|
|
*/
|
2025-04-10 01:29:49 +02:00
|
|
|
#[Route(path: '/remove-tag/{entry}/{tag}', name: 'remove_tag', methods: ['POST'], requirements: ['entry' => '\d+', 'tag' => '\d+'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('UNTAG', subject: 'entry')]
|
2016-02-10 17:41:28 +01:00
|
|
|
public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
|
|
|
|
{
|
2025-03-23 13:46:38 +01:00
|
|
|
if (!$this->isCsrfTokenValid('remove-tag', $request->request->get('token'))) {
|
|
|
|
throw new BadRequestHttpException('Bad CSRF token.');
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:41:28 +01:00
|
|
|
$entry->removeTag($tag);
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->entityManager->flush();
|
2016-10-07 23:31:53 +02:00
|
|
|
|
|
|
|
// remove orphan tag in case no entries are associated to it
|
2025-03-16 15:00:28 +01:00
|
|
|
if (0 === \count($tag->getEntries()) && $this->security->isGranted('DELETE', $tag)) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->entityManager->remove($tag);
|
|
|
|
$this->entityManager->flush();
|
2016-02-10 17:41:28 +01:00
|
|
|
}
|
|
|
|
|
2023-12-25 21:42:08 +01:00
|
|
|
$redirectUrl = $this->redirectHelper->to($request->query->get('redirect'), true);
|
2016-04-15 07:58:01 +02:00
|
|
|
|
|
|
|
return $this->redirect($redirectUrl);
|
2016-02-10 17:41:28 +01:00
|
|
|
}
|
|
|
|
|
2015-08-07 18:17:23 +02:00
|
|
|
/**
|
|
|
|
* Shows tags for current user.
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2015-08-07 18:17:23 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/tag/list', name: 'tag', methods: ['GET'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('LIST_TAGS')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function showTagAction(TagRepository $tagRepository, EntryRepository $entryRepository)
|
2015-08-07 18:17:23 +02:00
|
|
|
{
|
2025-03-16 15:00:28 +01:00
|
|
|
$allTagsWithNbEntries = $tagRepository->findAllTagsWithNbEntries($this->getUser()->getId());
|
2022-12-19 10:37:22 +01:00
|
|
|
$nbEntriesUntagged = $entryRepository->countUntaggedEntriesByUser($this->getUser()->getId());
|
2016-09-25 11:21:13 +02:00
|
|
|
|
2018-01-24 18:04:39 +01:00
|
|
|
$renameForms = [];
|
2025-03-16 15:00:28 +01:00
|
|
|
foreach ($allTagsWithNbEntries as $tagWithNbEntries) {
|
|
|
|
$renameForms[$tagWithNbEntries['tag']->getId()] = $this->createForm(RenameTagType::class, new Tag())->createView();
|
2018-01-24 18:04:39 +01:00
|
|
|
}
|
|
|
|
|
2024-02-19 00:03:14 +01:00
|
|
|
return $this->render('Tag/tags.html.twig', [
|
2025-03-16 15:00:28 +01:00
|
|
|
'allTagsWithNbEntries' => $allTagsWithNbEntries,
|
2018-01-24 18:04:39 +01:00
|
|
|
'renameForms' => $renameForms,
|
2019-06-06 13:34:20 +02:00
|
|
|
'nbEntriesUntagged' => $nbEntriesUntagged,
|
2016-09-25 11:21:13 +02:00
|
|
|
]);
|
2015-08-07 18:17:23 +02:00
|
|
|
}
|
2016-04-14 15:03:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $page
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2016-04-14 15:03:22 +02:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/tag/list/{slug}/{page}', name: 'tag_entries', methods: ['GET'], defaults: ['page' => '1'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[ParamConverter('tag', options: ['mapping' => ['slug' => 'slug']])]
|
|
|
|
#[IsGranted('LIST_ENTRIES')]
|
|
|
|
#[IsGranted('VIEW', subject: 'tag')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function showEntriesForTagAction(Tag $tag, EntryRepository $entryRepository, PreparePagerForEntries $preparePagerForEntries, $page, Request $request)
|
2016-04-14 15:03:22 +02:00
|
|
|
{
|
2022-12-19 10:37:22 +01:00
|
|
|
$entriesByTag = $entryRepository->findAllByTagId(
|
2017-06-10 12:33:58 +02:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$tag->getId()
|
|
|
|
);
|
2016-04-30 15:03:22 +02:00
|
|
|
|
|
|
|
$pagerAdapter = new ArrayAdapter($entriesByTag);
|
2016-04-14 15:03:22 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $preparePagerForEntries->prepare($pagerAdapter);
|
2016-04-14 15:03:22 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$entries->setCurrentPage($page);
|
2025-04-05 13:56:56 +02:00
|
|
|
} catch (OutOfRangeCurrentPageException) {
|
2016-04-14 15:03:22 +02:00
|
|
|
if ($page > 1) {
|
2025-01-19 02:06:54 +01:00
|
|
|
return $this->redirect($this->generateUrl($request->attributes->get('_route'), [
|
2016-04-14 15:03:22 +02:00
|
|
|
'slug' => $tag->getSlug(),
|
|
|
|
'page' => $entries->getNbPages(),
|
|
|
|
]), 302);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 00:03:14 +01:00
|
|
|
return $this->render('Entry/entries.html.twig', [
|
2016-09-25 11:21:13 +02:00
|
|
|
'form' => null,
|
|
|
|
'entries' => $entries,
|
|
|
|
'currentPage' => $page,
|
2017-06-20 18:29:46 +02:00
|
|
|
'tag' => $tag,
|
2016-09-25 11:21:13 +02:00
|
|
|
]);
|
2016-04-14 15:03:22 +02:00
|
|
|
}
|
2018-01-24 17:27:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a given tag with a new label
|
|
|
|
* Create a new tag with the new name and drop the old one.
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2018-01-24 17:27:16 +01:00
|
|
|
*/
|
2025-04-05 15:06:57 +02:00
|
|
|
#[Route(path: '/tag/rename/{slug}', name: 'tag_rename', methods: ['POST'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[ParamConverter('tag', options: ['mapping' => ['slug' => 'slug']])]
|
|
|
|
#[IsGranted('EDIT', subject: 'tag')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function renameTagAction(Tag $tag, Request $request, TagRepository $tagRepository, EntryRepository $entryRepository)
|
2018-01-24 17:27:16 +01:00
|
|
|
{
|
|
|
|
$form = $this->createForm(RenameTagType::class, new Tag());
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
2023-12-25 21:42:08 +01:00
|
|
|
$redirectUrl = $this->redirectHelper->to($request->query->get('redirect'), true);
|
2020-04-15 22:41:03 +02:00
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2020-04-04 21:03:22 +02:00
|
|
|
$newTag = new Tag();
|
2020-04-15 22:41:03 +02:00
|
|
|
$newTag->setLabel($form->get('label')->getData());
|
|
|
|
|
|
|
|
if ($newTag->getLabel() === $tag->getLabel()) {
|
|
|
|
return $this->redirect($redirectUrl);
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$tagFromRepo = $tagRepository->findOneByLabel($newTag->getLabel());
|
2020-04-15 22:41:03 +02:00
|
|
|
|
|
|
|
if (null !== $tagFromRepo) {
|
|
|
|
$newTag = $tagFromRepo;
|
|
|
|
}
|
2020-04-04 21:03:22 +02:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$entries = $entryRepository->findAllByTagId(
|
2018-01-24 17:27:16 +01:00
|
|
|
$this->getUser()->getId(),
|
|
|
|
$tag->getId()
|
|
|
|
);
|
|
|
|
foreach ($entries as $entry) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->tagsAssigner->assignTagsToEntry(
|
2018-01-24 17:27:16 +01:00
|
|
|
$entry,
|
2020-04-15 22:41:03 +02:00
|
|
|
$newTag->getLabel(),
|
2020-04-04 21:03:22 +02:00
|
|
|
[$newTag]
|
2018-01-24 17:27:16 +01:00
|
|
|
);
|
|
|
|
$entry->removeTag($tag);
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->entityManager->flush();
|
2018-01-24 17:27:16 +01:00
|
|
|
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->addFlash(
|
2020-04-04 22:08:08 +02:00
|
|
|
'notice',
|
|
|
|
'flashes.tag.notice.tag_renamed'
|
|
|
|
);
|
|
|
|
}
|
2018-01-24 17:27:16 +01:00
|
|
|
|
|
|
|
return $this->redirect($redirectUrl);
|
|
|
|
}
|
2021-08-03 14:35:45 +02:00
|
|
|
|
|
|
|
/**
|
2022-04-20 23:13:17 +02:00
|
|
|
* Tag search results with the current search term.
|
2021-08-03 14:35:45 +02:00
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2021-08-03 14:35:45 +02:00
|
|
|
*/
|
2025-04-10 01:29:49 +02:00
|
|
|
#[Route(path: '/tag/search/{filter}', name: 'tag_this_search', methods: ['POST'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[IsGranted('CREATE_TAGS')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function tagThisSearchAction($filter, Request $request, EntryRepository $entryRepository)
|
2021-08-03 14:35:45 +02:00
|
|
|
{
|
2025-03-23 14:03:25 +01:00
|
|
|
if (!$this->isCsrfTokenValid('tag-this-search', $request->request->get('token'))) {
|
|
|
|
throw new BadRequestHttpException('Bad CSRF token.');
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:13:17 +02:00
|
|
|
$currentRoute = $request->query->has('currentRoute') ? $request->query->get('currentRoute') : '';
|
2021-08-03 14:35:45 +02:00
|
|
|
|
|
|
|
/** @var QueryBuilder $qb */
|
2022-12-19 10:37:22 +01:00
|
|
|
$qb = $entryRepository->getBuilderForSearchByUser($this->getUser()->getId(), $filter, $currentRoute);
|
2021-08-03 14:35:45 +02:00
|
|
|
|
|
|
|
$entries = $qb->getQuery()->getResult();
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->tagsAssigner->assignTagsToEntry(
|
2021-08-03 14:35:45 +02:00
|
|
|
$entry,
|
|
|
|
$filter
|
|
|
|
);
|
|
|
|
|
2023-06-17 15:19:59 +02:00
|
|
|
// check to avoid duplicate tags creation
|
|
|
|
foreach ($this->entityManager->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
|
2025-04-05 14:01:48 +02:00
|
|
|
if ($entity instanceof Tag && strtolower($entity->getLabel()) === strtolower((string) $filter)) {
|
2023-06-17 15:19:59 +02:00
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
$this->entityManager->persist($entry);
|
|
|
|
}
|
|
|
|
$this->entityManager->flush();
|
2021-08-03 14:35:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-25 21:42:08 +01:00
|
|
|
return $this->redirect($this->redirectHelper->to($request->query->get('redirect'), true));
|
2021-08-03 14:35:45 +02:00
|
|
|
}
|
2022-06-14 16:45:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a given tag for the current user.
|
|
|
|
*
|
2022-08-28 16:59:43 +02:00
|
|
|
* @return Response
|
2022-06-14 16:45:02 +02:00
|
|
|
*/
|
2025-04-10 01:29:49 +02:00
|
|
|
#[Route(path: '/tag/delete/{slug}', name: 'tag_delete', methods: ['POST'])]
|
2025-04-05 15:21:29 +02:00
|
|
|
#[ParamConverter('tag', options: ['mapping' => ['slug' => 'slug']])]
|
|
|
|
#[IsGranted('DELETE', subject: 'tag')]
|
2022-12-19 10:37:22 +01:00
|
|
|
public function removeTagAction(Tag $tag, Request $request, EntryRepository $entryRepository)
|
2022-06-14 17:22:55 +02:00
|
|
|
{
|
2025-03-23 14:51:58 +01:00
|
|
|
if (!$this->isCsrfTokenValid('tag-delete', $request->request->get('token'))) {
|
|
|
|
throw new BadRequestHttpException('Bad CSRF token.');
|
|
|
|
}
|
|
|
|
|
2022-06-14 16:45:02 +02:00
|
|
|
foreach ($tag->getEntriesByUserId($this->getUser()->getId()) as $entry) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$entryRepository->removeTag($this->getUser()->getId(), $tag);
|
2022-06-14 16:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove orphan tag in case no entries are associated to it
|
|
|
|
if (0 === \count($tag->getEntries())) {
|
2022-12-19 10:37:22 +01:00
|
|
|
$this->entityManager->remove($tag);
|
|
|
|
$this->entityManager->flush();
|
2022-06-14 16:45:02 +02:00
|
|
|
}
|
2023-12-25 21:42:08 +01:00
|
|
|
$redirectUrl = $this->redirectHelper->to($request->query->get('redirect'), true);
|
2022-06-14 16:45:02 +02:00
|
|
|
|
|
|
|
return $this->redirect($redirectUrl);
|
|
|
|
}
|
2015-08-07 18:17:23 +02:00
|
|
|
}
|