From d4ebe5c5dcf581416ab76136908cafbde78f63bf Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 23 Sep 2015 07:55:55 +0200 Subject: Entries filter on language + updated deps --- .../CoreBundle/Controller/EntryController.php | 2 +- .../CoreBundle/DataFixtures/ORM/LoadEntryData.php | 6 +++++ src/Wallabag/CoreBundle/Filter/EntryFilterType.php | 23 +++++++++++++++++- .../CoreBundle/Repository/EntryRepository.php | 27 ++++++++++++++++++++++ .../views/themes/baggy/Entry/entries.html.twig | 8 +++++++ .../views/themes/material/Entry/entries.html.twig | 10 ++++---- .../Tests/Controller/EntryControllerTest.php | 23 ++++++++++++++++++ 7 files changed, 91 insertions(+), 8 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index b9e4e67e..a9f35c36 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -192,7 +192,7 @@ class EntryController extends Controller throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); } - $form = $this->get('form.factory')->create(new EntryFilterType()); + $form = $this->get('form.factory')->create(new EntryFilterType($repository, $this->getUser())); if ($request->query->has($form->getName())) { // manually bind values from the request diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index 6cd24d44..dd316194 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php @@ -19,6 +19,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry1->setUrl('http://0.0.0.0'); $entry1->setTitle('test title entry1'); $entry1->setContent('This is my content /o/'); + $entry1->setLanguage('en'); $manager->persist($entry1); @@ -28,6 +29,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry2->setUrl('http://0.0.0.0'); $entry2->setTitle('test title entry2'); $entry2->setContent('This is my content /o/'); + $entry2->setLanguage('fr'); $manager->persist($entry2); @@ -37,6 +39,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry3->setUrl('http://0.0.0.0'); $entry3->setTitle('test title entry3'); $entry3->setContent('This is my content /o/'); + $entry3->setLanguage('en'); $tag1 = new Tag($this->getReference('bob-user')); $tag1->setLabel('foo'); @@ -54,6 +57,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry4->setUrl('http://0.0.0.0'); $entry4->setTitle('test title entry4'); $entry4->setContent('This is my content /o/'); + $entry4->setLanguage('en'); $tag1 = new Tag($this->getReference('admin-user')); $tag1->setLabel('foo'); @@ -72,6 +76,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry5->setTitle('test title entry5'); $entry5->setContent('This is my content /o/'); $entry5->setStarred(true); + $entry5->setLanguage('fr'); $manager->persist($entry5); @@ -82,6 +87,7 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface $entry6->setTitle('test title entry6'); $entry6->setContent('This is my content /o/'); $entry6->setArchived(true); + $entry6->setLanguage('de'); $manager->persist($entry6); diff --git a/src/Wallabag/CoreBundle/Filter/EntryFilterType.php b/src/Wallabag/CoreBundle/Filter/EntryFilterType.php index 024486e6..f40c1c2d 100644 --- a/src/Wallabag/CoreBundle/Filter/EntryFilterType.php +++ b/src/Wallabag/CoreBundle/Filter/EntryFilterType.php @@ -6,9 +6,26 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface; +use Doctrine\ORM\EntityRepository; +use Wallabag\CoreBundle\Entity\User; class EntryFilterType extends AbstractType { + private $user; + private $repository; + + /** + * Repository & user are used to get a list of language entries for this user + * + * @param EntityRepository $entryRepository + * @param User $user + */ + public function __construct(EntityRepository $entryRepository, User $user) + { + $this->repository = $entryRepository; + $this->user = $user; + } + public function buildForm(FormBuilderInterface $builder, array $options) { $builder @@ -53,7 +70,11 @@ class EntryFilterType extends AbstractType return $filterQuery->createCondition($expression); }, - )); + )) + ->add('language', 'filter_choice', array( + 'choices' => $this->repository->findDistinctLanguageByUser($this->user->getId()), + )) + ; } public function getName() diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index e764e8f7..87b9befe 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -134,4 +134,31 @@ class EntryRepository extends EntityRepository return $qb->getQuery()->getResult(); } + + /** + * Find distinct language for a given user. + * Used to build the filter language list. + * + * @param int $userId User id + * + * @return array + */ + public function findDistinctLanguageByUser($userId) + { + $results = $this->createQueryBuilder('e') + ->select('e.language') + ->where('e.user = :userId')->setParameter('userId', $userId) + ->andWhere('e.language IS NOT NULL') + ->groupBy('e.language') + ->orderBy('e.language', ' ASC') + ->getQuery() + ->getResult(); + + $languages = array(); + foreach ($results as $result) { + $languages[$result['language']] = $result['language']; + } + + return $languages; + } } diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig index 176290e4..08f3fe60 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/entries.html.twig @@ -71,6 +71,14 @@ + +
+ +
+ {{ form_widget(form.language) }} +
+
+
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig index c2157db5..dcdf7c09 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/entries.html.twig @@ -121,14 +121,12 @@
-
- {{ form_widget(form.isArchived) }} - +
+
-
- {{ form_widget(form.isStarred) }} - +
+ {{ form_widget(form.language) }}
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php index 77b57884..cbd84a97 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/EntryControllerTest.php @@ -382,4 +382,27 @@ class EntryControllerTest extends WallabagCoreTestCase $crawler = $client->submit($form); $this->assertCount(1, $crawler->filter('div[class=entry]')); } + + public function testFilterOnLanguage() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/unread/list'); + $form = $crawler->filter('button[id=submit-filter]')->form(); + $data = array( + 'entry_filter[language]' => 'de', + ); + + $crawler = $client->submit($form, $data); + $this->assertCount(1, $crawler->filter('div[class=entry]')); + + $form = $crawler->filter('button[id=submit-filter]')->form(); + $data = array( + 'entry_filter[language]' => 'en', + ); + + $crawler = $client->submit($form, $data); + $this->assertCount(2, $crawler->filter('div[class=entry]')); + } } -- cgit v1.2.3