From 7244d6cb61912b80341595c56cc22d9bb56e6251 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 19 Aug 2015 11:46:21 +0200 Subject: [PATCH] assign tags to an entry --- .../CoreBundle/Controller/TagController.php | 50 +++++++++++++++++++ src/Wallabag/CoreBundle/Entity/Tag.php | 5 ++ .../CoreBundle/Form/Type/NewTagType.php | 30 +++++++++++ .../CoreBundle/Repository/TagRepository.php | 17 +++++++ .../Resources/views/Entry/entry.html.twig | 3 +- .../Resources/views/Tag/new_form.html.twig | 15 ++++++ .../themes/material/Entry/entry.html.twig | 3 +- .../themes/material/Tag/new_form.html.twig | 15 ++++++ .../Tests/Controller/TagControllerTest.php | 50 +++++++++++++++++++ 9 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Form/Type/NewTagType.php create mode 100644 src/Wallabag/CoreBundle/Resources/views/Tag/new_form.html.twig create mode 100644 src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/new_form.html.twig diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index a342ec0b..fd2069e0 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php @@ -4,9 +4,59 @@ namespace Wallabag\CoreBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\HttpFoundation\Request; +use Wallabag\CoreBundle\Form\Type\NewTagType; +use Wallabag\CoreBundle\Entity\Tag; +use Wallabag\CoreBundle\Entity\Entry; class TagController extends Controller { + /** + * @param Request $request + * + * @Route("/new-tag/{entry}", requirements={"entry" = "\d+"}, name="new_tag") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function addTagFormAction(Request $request, Entry $entry) + { + $tag = new Tag($this->getUser()); + $form = $this->createForm(new NewTagType(), $tag); + $form->handleRequest($request); + + if ($form->isValid()) { + $existingTag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabelAndUserId($tag->getLabel(), $this->getUser()->getId()); + + $em = $this->getDoctrine()->getManager(); + + if (is_null($existingTag)) { + $entry->addTag($tag); + $em->persist($tag); + } else { + if (!$existingTag->hasEntry($entry)) { + $entry->addTag($existingTag); + $em->persist($existingTag); + } + } + + $em->flush(); + + $this->get('session')->getFlashBag()->add( + 'notice', + 'Tag added' + ); + + return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); + } + + return $this->render('WallabagCoreBundle:Tag:new_form.html.twig', array( + 'form' => $form->createView(), + 'entry' => $entry, + )); + } + /** * Shows tags for current user. * diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 6f005314..97c4579f 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -96,6 +96,11 @@ class Tag $this->entries[] = $entry; } + public function hasEntry($entry) + { + return $this->entries->contains($entry); + } + /** * @return User */ diff --git a/src/Wallabag/CoreBundle/Form/Type/NewTagType.php b/src/Wallabag/CoreBundle/Form/Type/NewTagType.php new file mode 100644 index 00000000..8e4ab649 --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/NewTagType.php @@ -0,0 +1,30 @@ +add('label', 'text', array('required' => true)) + ->add('save', 'submit') + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Wallabag\CoreBundle\Entity\Tag', + )); + } + + public function getName() + { + return 'tag'; + } +} diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index 9c409607..ac3145a1 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php @@ -24,4 +24,21 @@ class TagRepository extends EntityRepository return new Pagerfanta($pagerAdapter); } + + /** + * Find a tag by its label and its owner. + * + * @param string $label + * @param int $userId + * + * @return Tag|null + */ + public function findOneByLabelAndUserId($label, $userId) + { + return $this->createQueryBuilder('t') + ->where('t.label = :label')->setParameter('label', $label) + ->andWhere('t.user = :user_id')->setParameter('user_id', $userId) + ->getQuery() + ->getOneOrNullResult(); + } } diff --git a/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig b/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig index 00480d1a..18cfd59d 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig @@ -28,7 +28,8 @@

{{ entry.title|raw }} ✎

{{ entry.content | raw }} diff --git a/src/Wallabag/CoreBundle/Resources/views/Tag/new_form.html.twig b/src/Wallabag/CoreBundle/Resources/views/Tag/new_form.html.twig new file mode 100644 index 00000000..0b5a530d --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/Tag/new_form.html.twig @@ -0,0 +1,15 @@ +
+ + {% if form_errors(form) %} + {{ form_errors(form) }} + {% endif %} + + {% if form_errors(form.label) %} + {{ form_errors(form.label) }} + {% endif %} + + {{ form_widget(form.label, { 'attr': {'autocomplete': 'off'} }) }} + {{ form_widget(form.save, { 'attr': {'class': 'btn waves-effect waves-light'}, 'label': 'add tag' }) }} + + +
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig index b92c41b6..31b2c664 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig @@ -137,7 +137,8 @@ main { {{ entry.domainName }}
{{ entry.content | raw }} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/new_form.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/new_form.html.twig new file mode 100644 index 00000000..0b5a530d --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/new_form.html.twig @@ -0,0 +1,15 @@ +
+ + {% if form_errors(form) %} + {{ form_errors(form) }} + {% endif %} + + {% if form_errors(form.label) %} + {{ form_errors(form.label) }} + {% endif %} + + {{ form_widget(form.label, { 'attr': {'autocomplete': 'off'} }) }} + {{ form_widget(form.save, { 'attr': {'class': 'btn waves-effect waves-light'}, 'label': 'add tag' }) }} + + +
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php index 4a43e049..af39d6ce 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php @@ -15,4 +15,54 @@ class TagControllerTest extends WallabagCoreTestCase $this->assertEquals(200, $client->getResponse()->getStatusCode()); } + + public function testAddTagToEntry() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $entry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByIsArchived(false); + + $crawler = $client->request('GET', '/view/'.$entry->getId()); + + $form = $crawler->filter('button[id=tag_save]')->form(); + + $data = array( + 'tag[label]' => 'opensource', + ); + + $client->submit($form, $data); + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $this->assertEquals(1, count($entry->getTags())); + + # tag already exists and already assigned + $client->submit($form, $data); + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $newEntry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneById($entry->getId()); + + $this->assertEquals(1, count($newEntry->getTags())); + + # tag already exists but still not assigned to this entry + $data = array( + 'tag[label]' => 'foo', + ); + + $client->submit($form, $data); + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $newEntry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneById($entry->getId()); + + $this->assertEquals(2, count($newEntry->getTags())); + } } -- 2.41.0