From 82d6d9cb06a1486e2e3b05fa6ce857b3b8655180 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 2 Jun 2015 18:54:34 +0200 Subject: [PATCH] Add basic title edition Fix #218 I mean basic, because there is no javascript at all. It could be a nice edit-in-place. But for the moment, it is simple. --- .../CoreBundle/Controller/EntryController.php | 43 +++++++++++++++-- src/Wallabag/CoreBundle/Entity/Entry.php | 2 +- src/Wallabag/CoreBundle/Entity/Tag.php | 6 +++ .../CoreBundle/Form/Type/EditEntryType.php | 36 ++++++++++++++ .../Type/{EntryType.php => NewEntryType.php} | 2 +- .../Resources/views/Entry/edit.html.twig | 7 +++ .../Resources/views/Entry/entry.html.twig | 16 +++---- .../Tests/Controller/EntryControllerTest.php | 48 +++++++++++++++++++ 8 files changed, 147 insertions(+), 13 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Form/Type/EditEntryType.php rename src/Wallabag/CoreBundle/Form/Type/{EntryType.php => NewEntryType.php} (94%) create mode 100644 src/Wallabag/CoreBundle/Resources/views/Entry/edit.html.twig diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 7fd982c9..4a7a0644 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -7,7 +7,8 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Service\Extractor; -use Wallabag\CoreBundle\Form\Type\EntryType; +use Wallabag\CoreBundle\Form\Type\NewEntryType; +use Wallabag\CoreBundle\Form\Type\EditEntryType; class EntryController extends Controller { @@ -22,7 +23,7 @@ class EntryController extends Controller { $entry = new Entry($this->getUser()); - $form = $this->createForm(new EntryType(), $entry); + $form = $this->createForm(new NewEntryType(), $entry); $form->handleRequest($request); @@ -49,6 +50,42 @@ class EntryController extends Controller )); } + /** + * Edit an entry content. + * + * @param Request $request + * @param Entry $entry + * + * @Route("/edit/{id}", requirements={"id" = "\d+"}, name="edit") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function editEntryAction(Request $request, Entry $entry) + { + $this->checkUserAction($entry); + + $form = $this->createForm(new EditEntryType(), $entry); + + $form->handleRequest($request); + + if ($form->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $this->get('session')->getFlashBag()->add( + 'notice', + 'Entry updated' + ); + + return $this->redirect($this->generateUrl('view', array('id' => $entry->getId()))); + } + + return $this->render('WallabagCoreBundle:Entry:edit.html.twig', array( + 'form' => $form->createView(), + )); + } + /** * Shows unread entries for current user. * @@ -212,7 +249,7 @@ class EntryController extends Controller private function checkUserAction(Entry $entry) { if ($this->getUser()->getId() != $entry->getUser()->getId()) { - throw $this->createAccessDeniedException('You can not use this entry.'); + throw $this->createAccessDeniedException('You can not access this entry.'); } } } diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index b1998ab6..f139bbac 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -390,7 +390,7 @@ class Entry /** * @param bool $isPublic */ - public function setPublic($isPublic) + public function setIsPublic($isPublic) { $this->isPublic = $isPublic; } diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index afe9e1b9..92442df6 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php @@ -51,6 +51,12 @@ class Tag $this->user = $user; $this->entries = new ArrayCollection(); } + + public function __toString() + { + return $this->label; + } + /** * Get id. * diff --git a/src/Wallabag/CoreBundle/Form/Type/EditEntryType.php b/src/Wallabag/CoreBundle/Form/Type/EditEntryType.php new file mode 100644 index 00000000..0fa4b71f --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/EditEntryType.php @@ -0,0 +1,36 @@ +add('title', 'text', array('required' => true)) + ->add('is_public', 'checkbox', array('required' => false)) + // @todo: add autocomplete + // ->add('tags', 'entity', array( + // 'class' => 'Wallabag\CoreBundle\Entity\Tag', + // 'choice_translation_domain' => true, + // )) + ->add('save', 'submit') + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Wallabag\CoreBundle\Entity\Entry', + )); + } + + public function getName() + { + return 'entry'; + } +} diff --git a/src/Wallabag/CoreBundle/Form/Type/EntryType.php b/src/Wallabag/CoreBundle/Form/Type/NewEntryType.php similarity index 94% rename from src/Wallabag/CoreBundle/Form/Type/EntryType.php rename to src/Wallabag/CoreBundle/Form/Type/NewEntryType.php index 0532bf10..a2d203ea 100644 --- a/src/Wallabag/CoreBundle/Form/Type/EntryType.php +++ b/src/Wallabag/CoreBundle/Form/Type/NewEntryType.php @@ -6,7 +6,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; -class EntryType extends AbstractType +class NewEntryType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { diff --git a/src/Wallabag/CoreBundle/Resources/views/Entry/edit.html.twig b/src/Wallabag/CoreBundle/Resources/views/Entry/edit.html.twig new file mode 100644 index 00000000..0d4d5ad2 --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/Entry/edit.html.twig @@ -0,0 +1,7 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} + +{% block title %}{% trans %}Edit an entry{% endtrans %}{% endblock %} + +{% block content %} + {{ form(form) }} +{% endblock %} diff --git a/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig b/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig index f23ab968..f5c19ea3 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Entry/entry.html.twig @@ -17,18 +17,18 @@ {# {% if flattr %}{% if flattr.status == flattrable %}
  • {% trans %}flattr{% endtrans %}
  • {% elseif flattr.status == flattred %}
  • Carrot
  • {% endif %} {% if show_printlink %}
  • {% trans %}Print{% endtrans %}
  • {% endif %} - {% if export_epub %}
  • EPUB
  • {% endif %} - {% if export_mobi %}
  • MOBI
  • {% endif %} - {% if export_pdf %}
  • PDF
  • {% endif %} + {% if export_epub %}
  • EPUB
  • {% endif %} + {% if export_mobi %}
  • MOBI
  • {% endif %} + {% if export_pdf %}
  • PDF
  • {% endif %}
  • {% trans %}Does this article appear wrong?{% endtrans %}
  • -

    {{ entry.title|raw }}

    +

    {{ entry.title|raw }} ✎

    {{ entry.content | raw }} @@ -87,13 +87,13 @@ var docHeight = $(document).height(); var scrollPercent = (scrollTop) / (docHeight); var scrollPercentRounded = Math.round(scrollPercent*100)/100; - savePercent({{ entry.id|e }}, scrollPercentRounded); + savePercent({{ entry.id }}, scrollPercentRounded); }); - retrievePercent({{ entry.id|e }}); + retrievePercent({{ entry.id }}); $(window).resize(function(){ - retrievePercent({{ entry.id|e }}); + retrievePercent({{ entry.id }}); }); }); diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index 8a7fdda2..904e2a5c 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -109,6 +109,54 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertContains($content->getTitle(), $client->getResponse()->getContent()); } + public function testEdit() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByIsArchived(false); + + $crawler = $client->request('GET', '/edit/'.$content->getId()); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $this->assertCount(1, $crawler->filter('input[id=entry_title]')); + $this->assertCount(1, $crawler->filter('button[id=entry_save]')); + } + + public function testEditUpdate() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByIsArchived(false); + + $crawler = $client->request('GET', '/edit/'.$content->getId()); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('button[type=submit]')->form(); + + $data = array( + 'entry[title]' => 'My updated title hehe :)', + ); + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertGreaterThan(1, $alert = $crawler->filter('div[id=article] h1')->extract(array('_text'))); + $this->assertContains('My updated title hehe :)', $alert[0]); + } + public function testToggleArchive() { $this->logInAs('admin'); -- 2.41.0