]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #1392 from wallabag/v2-fix-redirect
authorNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 24 Aug 2015 08:44:14 +0000 (10:44 +0200)
committerNicolas Lœuillet <nicolas@loeuillet.org>
Mon, 24 Aug 2015 08:44:14 +0000 (10:44 +0200)
Fix redirect after deletion

15 files changed:
app/config/services.yml
src/Wallabag/CoreBundle/Controller/TagController.php
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Event/Subscriber/CustomDoctrineORMSubscriber.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Filter/EntryFilterType.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/Config/index.html.twig
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/Resources/views/themes/material/public/css/main.css
src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php
src/Wallabag/CoreBundle/Tests/Controller/TagControllerTest.php

index af22d3818cd7880c02c7ac8f67cbd610992b9242..b3ec7c513317e690f0899ab613e975fa4c2bd4c6 100644 (file)
@@ -4,6 +4,7 @@ parameters:
     security.authentication.provider.dao.class: Wallabag\CoreBundle\Security\Authentication\Provider\WallabagAuthenticationProvider
     security.encoder.digest.class: Wallabag\CoreBundle\Security\Authentication\Encoder\WallabagPasswordEncoder
     security.validator.user_password.class: Wallabag\CoreBundle\Security\Validator\WallabagUserPasswordValidator
+    lexik_form_filter.get_filter.doctrine_orm.class: Wallabag\CoreBundle\Event\Subscriber\CustomDoctrineORMSubscriber
 
 services:
     # used for tests
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/Event/Subscriber/CustomDoctrineORMSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/CustomDoctrineORMSubscriber.php
new file mode 100644 (file)
index 0000000..cfdbfe9
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace Wallabag\CoreBundle\Event\Subscriber;
+
+use Lexik\Bundle\FormFilterBundle\Event\Subscriber\DoctrineORMSubscriber;
+use Lexik\Bundle\FormFilterBundle\Event\GetFilterConditionEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * This custom class override the default behavior of LexikFilterBundle on `filter_date_range`
+ * It converts a date_range to date_time_range to add hour to be able to grab a whole day (from 00:00:00 to 23:59:59).
+ */
+class CustomDoctrineORMSubscriber extends DoctrineORMSubscriber implements EventSubscriberInterface
+{
+    /**
+     * @param GetFilterConditionEvent $event
+     */
+    public function filterDateRange(GetFilterConditionEvent $event)
+    {
+        $expr = $event->getFilterQuery()->getExpressionBuilder();
+        $values = $event->getValues();
+        $value = $values['value'];
+
+        // left date should start at midnight
+        if (isset($value['left_date'][0]) && $value['left_date'][0] instanceof \DateTime) {
+            $value['left_date'][0]->setTime(0, 0, 0);
+        }
+
+        // right adte should end one second before midnight
+        if (isset($value['right_date'][0]) && $value['right_date'][0] instanceof \DateTime) {
+            $value['right_date'][0]->setTime(23, 59, 59);
+        }
+
+        if (isset($value['left_date'][0]) || isset($value['right_date'][0])) {
+            $event->setCondition($expr->dateTimeInRange($event->getField(), $value['left_date'][0], $value['right_date'][0]));
+        }
+    }
+}
index ff51785b8411ca733def9bdc0cdfe12b84fd902e..85d1a0610c1e576fc40947bbf336c44695c0a922 100644 (file)
@@ -27,8 +27,9 @@ class EntryFilterType extends AbstractType
                         ),
                         'format' => 'dd/MM/yyyy',
                         'widget' => 'single_text',
-                ),
-            ))
+                    ),
+                )
+            )
             ->add('domainName', 'filter_text', array(
                 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
                         $value = $values['value'];
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 287c5fbb41efdb11b2432743ca7c8c35b0c667c2..3ec31a02ca202606a27b64eccc26303351ea0692 100644 (file)
                             <div class="row">
                                 <div class="input-field col s12">
                                     <label>Rss token</label>
-                                    {% if rss.token %}
-                                        {{ rss.token }}
-                                    {% else %}
-                                        <em>No token</em>
-                                    {% endif %}
-                                    –
-                                    <a href="{{ path('generate_token') }}">Regenerate ?</a>
+                                    <div>
+                                        {% if rss.token %}
+                                            {{ rss.token }}
+                                        {% else %}
+                                            <em>No token</em>
+                                        {% endif %}
+                                        –
+                                        <a href="{{ path('generate_token') }}">Regenerate ?</a>
+                                    </div>
                                 </div>
                             </div>
 
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 52a2be809bab7a2d8054a8bcb32c17e38eecf42f..a976da100f65b1140548593941d1326ff8afd39a 100755 (executable)
@@ -4,13 +4,11 @@
    0 = Common
    1 = Nav
    2 = Side-nav
-
-   2 = Layout
-   3 = Pictos
-   4 = Messages
+   3 = Filters slider
+   4 = Cards
    5 = Article
-
    6 = Media queries
+   7 = Others
 
    ========================================================================== */
 
@@ -212,7 +210,7 @@ nav input {
 }
 
 /* ==========================================================================
-   3 = Cards
+   4 = Cards
    ========================================================================== */
 
 main #content {
@@ -261,7 +259,7 @@ main ul.row {
 }
 
 /* ==========================================================================
-   4 = Article
+   5 = Article
    ========================================================================== */
 
 #article {
@@ -365,3 +363,10 @@ main ul.row {
         display: none;
     }
 }
+/* ==========================================================================
+   7 = Others
+   ========================================================================== */
+
+div.input-field div, div.input-field ul {
+    margin-top: 40px;
+}
index 5f0a6076388535821460c9c5eb83f0561c11d032..a096628582cc4a6209e48ecc3f1f7e5a2dc57775 100644 (file)
@@ -278,6 +278,15 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $this->assertCount(5, $crawler->filter('div[class=entry]'));
 
+        $data = array(
+            'entry_filter[createdAt][left_date]' => date('d/m/Y'),
+            'entry_filter[createdAt][right_date]' => date('d/m/Y'),
+        );
+
+        $crawler = $client->submit($form, $data);
+
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
+
         $data = array(
             'entry_filter[createdAt][left_date]' => '01/01/1970',
             'entry_filter[createdAt][right_date]' => '01/01/1970',
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()));
+    }
 }