]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
implement bookmarklet 1471/head
authorNicolas Lœuillet <nicolas.loeuillet@smile.fr>
Wed, 7 Oct 2015 16:08:51 +0000 (18:08 +0200)
committerNicolas Lœuillet <nicolas.loeuillet@smile.fr>
Thu, 8 Oct 2015 11:29:41 +0000 (13:29 +0200)
src/Wallabag/CoreBundle/Controller/EntryController.php
src/Wallabag/CoreBundle/Resources/views/_bookmarklet.html.twig [new file with mode: 0644]
src/Wallabag/CoreBundle/Resources/views/themes/baggy/Static/howto.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig
src/Wallabag/CoreBundle/Resources/views/themes/material/Static/howto.html.twig
src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php

index a9f35c36dc51dd5349bca30d0effb9034508d139..f7b52eaf71b4de811dedc7173d78eae51935d181 100644 (file)
@@ -14,6 +14,23 @@ use Pagerfanta\Pagerfanta;
 
 class EntryController extends Controller
 {
+    /**
+     * @param Entry $entry
+     */
+    private function updateEntry(Entry $entry)
+    {
+        try {
+            $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
+            $em = $this->getDoctrine()->getManager();
+            $em->persist($entry);
+            $em->flush();
+        } catch (\Exception $e) {
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * @param Request $request
      *
@@ -30,12 +47,7 @@ class EntryController extends Controller
         $form->handleRequest($request);
 
         if ($form->isValid()) {
-            $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
-
-            $em = $this->getDoctrine()->getManager();
-            $em->persist($entry);
-            $em->flush();
-
+            $this->updateEntry($entry);
             $this->get('session')->getFlashBag()->add(
                 'notice',
                 'Entry saved'
@@ -49,6 +61,22 @@ class EntryController extends Controller
         ));
     }
 
+    /**
+     * @param Request $request
+     *
+     * @Route("/bookmarklet", name="bookmarklet")
+     *
+     * @return \Symfony\Component\HttpFoundation\Response
+     */
+    public function addEntryViaBookmarklet(Request $request)
+    {
+        $entry = new Entry($this->getUser());
+        $entry->setUrl($request->get('url'));
+        $this->updateEntry($entry);
+
+        return $this->redirect($this->generateUrl('homepage'));
+    }
+
     /**
      * @param Request $request
      *
diff --git a/src/Wallabag/CoreBundle/Resources/views/_bookmarklet.html.twig b/src/Wallabag/CoreBundle/Resources/views/_bookmarklet.html.twig
new file mode 100644 (file)
index 0000000..c8fb561
--- /dev/null
@@ -0,0 +1 @@
+<a id="bookmarklet" ondragend="this.click();" href="javascript:var url=location.href||url;var wllbg=window.open('{{ url('bookmarklet') }}?url=' + encodeURI(url),'_blank');wllbg.close();void(0);">{% trans %}bag it!{% endtrans %}</a>
index 58cb316b16958ec5eef2ac592050c6ac4c331627..6a32069296fb29d0516f58c5cfe4790700bf92f7 100644 (file)
@@ -22,7 +22,7 @@
     </ul>
     <h3>{% trans %}Bookmarklet{% endtrans %}</h3>
     <p>
-    {% trans %}Drag &amp; drop this link to your bookmarks bar:{% endtrans %} {% trans %}bag it!{% endtrans %}
-    </p>
+    {% trans %}Drag &amp; drop this link to your bookmarks bar:{% endtrans %}
+    {% include 'WallabagCoreBundle::_bookmarklet.html.twig' %}
 
 {% endblock %}
index de4ed2e78e199527986a5a9c27d6a966ab1fcbcb..26de7943c87d7f8856faf63829227790549d7a3b 100644 (file)
@@ -51,6 +51,7 @@
             </div>
         </li>
         <li><a href="{{ path('config') }}">{% trans %}config{% endtrans %}</a></li>
+        <li><a href="{{ path('howto') }}">{% trans %}howto{% endtrans %}</a></li>
         <li><a href="{{ path('about') }}">{% trans %}about{% endtrans %}</a></li>
         <li><a class="icon icon-power" href="{{ path('fos_user_security_logout') }}" title="{% trans %}logout{% endtrans %}">{% trans %}logout{% endtrans %}</a></li>
     </ul>
index 7859117203c6dbacadc3fcaba2280c497a4f02af..63b51aaa04fbb0d49ce38af8574fb50b8a535b0e 100644 (file)
@@ -42,7 +42,8 @@
 
 
                     <div id="set4" class="col s12">
-                        {% trans %}Drag &amp; drop this link to your bookmarks bar:{% endtrans %} {% trans %}bag it!{% endtrans %}
+                        {% trans %}Drag &amp; drop this link to your bookmarks bar:{% endtrans %}
+                        {% include 'WallabagCoreBundle::_bookmarklet.html.twig' %}
                     </div>
 
                 </div>
index 2862417e304627dc7dc690183484f54d370d76f5..5ac39d1212276a210ba3b6471d6ee815aa41ff26 100644 (file)
@@ -31,6 +31,31 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->assertCount(1, $crawler->filter('button[type=submit]'));
     }
 
+    public function testPostNewViaBookmarklet()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $crawler = $client->request('GET', '/');
+
+        $this->assertCount(4, $crawler->filter('div[class=entry]'));
+
+        // Good URL
+        $crawler = $client->request('GET', '/bookmarklet', array('url' => $this->url));
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+        $crawler = $client->followRedirect();
+        $crawler = $client->request('GET', '/');
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
+
+        $em = $client->getContainer()
+            ->get('doctrine.orm.entity_manager');
+        $entry = $em
+            ->getRepository('WallabagCoreBundle:Entry')
+            ->findOneByUrl($this->url);
+        $em->remove($entry);
+        $em->flush();
+    }
+
     public function testPostNewEmpty()
     {
         $this->logInAs('admin');