diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index 7df73e8c7..cce241663 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -222,4 +222,29 @@ class TagController extends Controller return $this->redirect($this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true)); } + + /** + * Delete a given tag for the current user. + * + * @Route("/tag/delete/{slug}", name="tag_delete") + * @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function removeTagAction(Tag $tag, Request $request) { + foreach ($tag->getEntriesByUserId($this->getUser()->getId()) as $entry) { + $this->get('wallabag_core.entry_repository')->removeTag($this->getUser()->getId(), $tag); + } + + // remove orphan tag in case no entries are associated to it + if (0 === \count($tag->getEntries())) { + $em = $this->getDoctrine()->getManager(); + $em->remove($tag); + $em->flush(); + } + + $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true); + + return $this->redirect($redirectUrl); + } } diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig index 50afa6d5d..009abbc77 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/tags.html.twig @@ -26,6 +26,9 @@ mode_edit {% endif %} + + mode_delete + {% if app.user.config.feedToken %} rss_feed {% endif %} diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php index e3d3ff5e0..c9e706bba 100644 --- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php @@ -141,6 +141,53 @@ class TagControllerTest extends WallabagCoreTestCase $this->assertNull($tag, $this->tagName . ' was removed because it begun an orphan tag'); } + public function testRemoveTag() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $tag = new Tag(); + $tag->setLabel($this->tagName); + + $entry = new Entry($this->getLoggedInUser()); + $entry->setUrl('http://0.0.0.0/foo'); + $entry->addTag($tag); + $this->getEntityManager()->persist($entry); + + $entry2 = new Entry($this->getLoggedInUser()); + $entry2->setUrl('http://0.0.0.0/bar'); + $entry2->addTag($tag); + $this->getEntityManager()->persist($entry2); + $this->getEntityManager()->flush(); + $this->getEntityManager()->clear(); + + $client->request('GET', '/tag/delete/' . $tag->getSlug()); + + $tag = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($this->tagName); + + $this->assertNull($tag, $this->tagName . ' was removed because it begun an orphan tag'); + + $user = $this->getEntityManager() + ->getRepository('WallabagUserBundle:User') + ->findOneByUserName('admin'); + + $entry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId('http://0.0.0.0/foo', $user->getId()); + + $entry2 = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId('http://0.0.0.0/bar', $user->getId()); + + $this->assertEmpty($entry->getTagsLabel()); + $this->assertEmpty($entry2->getTagsLabel()); + } + public function testShowEntriesForTagAction() { $this->logInAs('admin');