]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
assign tags to an entry 1372/head
authorNicolas Lœuillet <nicolas@loeuillet.org>
Wed, 19 Aug 2015 09:46:21 +0000 (11:46 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sat, 22 Aug 2015 10:40:48 +0000 (12:40 +0200)
src/Wallabag/CoreBundle/Controller/TagController.php
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Form/Type/NewTagType.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Repository/TagRepository.php
src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig
src/Wallabag/CoreBundle/Resources/views/Tag/new_form.html.twig [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entry.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Tag/new_form.html.twig [new file with mode: 0644]
src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php

index a342ec0b91e21a44fa6c8ad7d3aa13a1c5c2817c..fd2069e0315982f7eb42e7ef6a769899dd5804c2 100644 (file)
@@ -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.
      *
index 6f005314f69fd8e33a2e253a0d135bf74913171c..97c4579f70e8c1a607817a28ca5985912a2c8b3d 100644 (file)
@@ -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 (file)
index 0000000..8e4ab64
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+namespace Wallabag\CoreBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class NewTagType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder
+            ->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';
+    }
+}
index 9c4096072626273dd98372a69f95f85a07c61262..ac3145a1dc8896d5ec8a980f3fee4769d37817ed 100644 (file)
@@ -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();
+    }
 }
index 00480d1aa6d6fc6fe1e12527548919aefa909d07..18cfd59d2d036e586980fe14e8439dd161fe8acf 100644 (file)
@@ -28,7 +28,8 @@
             <h1>{{ entry.title|raw }} <a href="{{ path('edit', { 'id': entry.id }) }}" title="{% trans %}Edit tags{% endtrans %}">✎</a></h1>
         </header>
         <aside class="tags">
-            tags: {% for tag in entry.tags %}<a href="./?view=tag&amp;id={{ tag.id }}">{{ tag.label }}</a> {% endfor %}<a href="./?view=edit-tags&amp;id={{ entry.id }}" title="{% trans %}Edit tags{% endtrans %}">✎</a>
+            {% for tag in entry.tags %}<span class="mdi-action-label-outline">{{ tag.label }}</span>{% endfor %}
+            {{ render(controller( "WallabagCoreBundle:Tag:addTagForm", { 'id': entry.id } )) }}
         </aside>
         <article>
             {{ 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 (file)
index 0000000..0b5a530
--- /dev/null
@@ -0,0 +1,15 @@
+<form name="tag" method="post" action="{{ path('new_tag', { 'entry': entry.id })}}">
+
+        {% if form_errors(form) %}
+            <span class="black-text">{{ form_errors(form) }}</span>
+        {% endif %}
+
+        {% if form_errors(form.label) %}
+            <span class="black-text">{{ form_errors(form.label) }}</span>
+        {% endif %}
+
+        {{ form_widget(form.label, { 'attr': {'autocomplete': 'off'} }) }}
+        {{ form_widget(form.save, { 'attr': {'class': 'btn waves-effect waves-light'}, 'label': 'add tag' }) }}
+
+    <div class="hidden">{{ form_rest(form) }}</div>
+</form>
index b92c41b60b565808d820da1af886aac1af7680f1..31b2c664c17be5439b1f38f7f6014d4d0e6c9573 100644 (file)
@@ -137,7 +137,8 @@ main {
             <a href="{{ entry.url|e }}" target="_blank" title="{% trans %}original{% endtrans %} : {{ entry.title|e }}" class="tool link"><span>{{ entry.domainName }}</span></a>
         </header>
         <aside class="tags">
-            tags: {% for tag in entry.tags %}<a href="./?view=tag&amp;id={{ tag.id }}">{{ tag.label }}</a> {% endfor %}<a href="./?view=edit-tags&amp;id={{ entry.id }}" title="{% trans %}Edit tags{% endtrans %}">✎</a>
+            {% for tag in entry.tags %}<span class="mdi-action-label-outline">{{ tag.label }}</span>{% endfor %}
+            {{ render(controller( "WallabagCoreBundle:Tag:addTagForm", { 'id': entry.id } )) }}
         </aside>
         <article>
             {{ 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 (file)
index 0000000..0b5a530
--- /dev/null
@@ -0,0 +1,15 @@
+<form name="tag" method="post" action="{{ path('new_tag', { 'entry': entry.id })}}">
+
+        {% if form_errors(form) %}
+            <span class="black-text">{{ form_errors(form) }}</span>
+        {% endif %}
+
+        {% if form_errors(form.label) %}
+            <span class="black-text">{{ form_errors(form.label) }}</span>
+        {% endif %}
+
+        {{ form_widget(form.label, { 'attr': {'autocomplete': 'off'} }) }}
+        {{ form_widget(form.save, { 'attr': {'class': 'btn waves-effect waves-light'}, 'label': 'add tag' }) }}
+
+    <div class="hidden">{{ form_rest(form) }}</div>
+</form>
index 4a43e04928124d1718673936d1322019e9d5932c..af39d6ce11ea28386cc00bef3a121c2fbaaa49ca 100644 (file)
@@ -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()));
+    }
 }