From 880a0e1c0ba7d0ab3320678b076402379a08c8a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 7 Oct 2015 18:08:51 +0200 Subject: [PATCH] implement bookmarklet --- .../CoreBundle/Controller/EntryController.php | 40 ++++++++++++++++--- .../Resources/views/_bookmarklet.html.twig | 1 + .../views/themes/baggy/Static/howto.html.twig | 4 +- .../views/themes/baggy/layout.html.twig | 1 + .../themes/material/Static/howto.html.twig | 3 +- .../Tests/Controller/EntryControllerTest.php | 25 ++++++++++++ 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Resources/views/_bookmarklet.html.twig diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index a9f35c36..f7b52eaf 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -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 index 00000000..c8fb5612 --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/_bookmarklet.html.twig @@ -0,0 +1 @@ +{% trans %}bag it!{% endtrans %} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Static/howto.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Static/howto.html.twig index 58cb316b..6a320692 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Static/howto.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Static/howto.html.twig @@ -22,7 +22,7 @@

{% trans %}Bookmarklet{% endtrans %}

- {% trans %}Drag & drop this link to your bookmarks bar:{% endtrans %} {% trans %}bag it!{% endtrans %} -

+ {% trans %}Drag & drop this link to your bookmarks bar:{% endtrans %} + {% include 'WallabagCoreBundle::_bookmarklet.html.twig' %} {% endblock %} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig index de4ed2e7..26de7943 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig @@ -51,6 +51,7 @@
  • {% trans %}config{% endtrans %}
  • +
  • {% trans %}howto{% endtrans %}
  • {% trans %}about{% endtrans %}
  • {% trans %}logout{% endtrans %}
  • diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/howto.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/howto.html.twig index 78591172..63b51aaa 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/howto.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/howto.html.twig @@ -42,7 +42,8 @@
    - {% trans %}Drag & drop this link to your bookmarks bar:{% endtrans %} {% trans %}bag it!{% endtrans %} + {% trans %}Drag & drop this link to your bookmarks bar:{% endtrans %} + {% include 'WallabagCoreBundle::_bookmarklet.html.twig' %}
    diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index 2862417e..5ac39d12 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -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'); -- 2.41.0