From ee122a7528f55dfb5f02e351c509d00b756fedaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 4 Nov 2016 23:24:43 +0100 Subject: Added a simple search engine Fix #18 --- .../CoreBundle/Controller/EntryController.php | 33 ++++++++++++++++++++-- .../CoreBundle/Controller/ExportController.php | 2 +- .../CoreBundle/Form/Type/SearchEntryType.php | 29 +++++++++++++++++++ .../CoreBundle/Repository/EntryRepository.php | 19 +++++++++++++ .../Resources/translations/messages.en.yml | 3 ++ .../views/themes/common/Entry/_title.html.twig | 4 ++- .../views/themes/material/Entry/entries.html.twig | 9 ++++-- .../themes/material/Entry/search_form.html.twig | 14 +++++++++ .../views/themes/material/layout.html.twig | 16 +++++------ 9 files changed, 113 insertions(+), 16 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Form/Type/SearchEntryType.php create mode 100644 src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/search_form.html.twig (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 3f4eb17d..fc1697be 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -15,9 +15,33 @@ use Wallabag\CoreBundle\Form\Type\NewEntryType; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Wallabag\CoreBundle\Event\EntrySavedEvent; use Wallabag\CoreBundle\Event\EntryDeletedEvent; +use Wallabag\CoreBundle\Form\Type\SearchEntryType; class EntryController extends Controller { + /** + * @param Request $request + * @param int $page + * + * @Route("/search/{page}", name="search", defaults={"page" = "1"}) + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function searchFormAction(Request $request, $page) + { + $form = $this->createForm(SearchEntryType::class); + + $form->handleRequest($request); + + if ($form->isValid()) { + return $this->showEntries('search', $request, $page); + } + + return $this->render('WallabagCoreBundle:Entry:search_form.html.twig', [ + 'form' => $form->createView(), + ]); + } + /** * Fetch content and update entry. * In case it fails, entry will return to avod loosing the data. @@ -244,8 +268,13 @@ class EntryController extends Controller private function showEntries($type, Request $request, $page) { $repository = $this->get('wallabag_core.entry_repository'); + $searchTerm = (isset($request->get('search_entry')['term']) ? $request->get('search_entry')['term'] : ''); switch ($type) { + case 'search': + $qb = $repository->getBuilderForSearchByUser($this->getUser()->getId(), $searchTerm); + + break; case 'untagged': $qb = $repository->getBuilderForUntaggedByUser($this->getUser()->getId()); @@ -294,11 +323,11 @@ class EntryController extends Controller } return $this->render( - 'WallabagCoreBundle:Entry:entries.html.twig', - [ + 'WallabagCoreBundle:Entry:entries.html.twig', [ 'form' => $form->createView(), 'entries' => $entries, 'currentPage' => $page, + 'searchTerm' => $searchTerm, ] ); } diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php index 79653cfe..abc3336a 100644 --- a/src/Wallabag/CoreBundle/Controller/ExportController.php +++ b/src/Wallabag/CoreBundle/Controller/ExportController.php @@ -48,7 +48,7 @@ class ExportController extends Controller * * @Route("/export/{category}.{format}", name="export_entries", requirements={ * "format": "epub|mobi|pdf|json|xml|txt|csv", - * "category": "all|unread|starred|archive|tag_entries|untagged" + * "category": "all|unread|starred|archive|tag_entries|untagged|search" * }) * * @return \Symfony\Component\HttpFoundation\Response diff --git a/src/Wallabag/CoreBundle/Form/Type/SearchEntryType.php b/src/Wallabag/CoreBundle/Form/Type/SearchEntryType.php new file mode 100644 index 00000000..b56cae8e --- /dev/null +++ b/src/Wallabag/CoreBundle/Form/Type/SearchEntryType.php @@ -0,0 +1,29 @@ +setMethod('GET') + ->add('term', TextType::class, [ + 'required' => true, + 'label' => 'entry.new.form_search.term_label', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'csrf_protection' => false, + ]); + } +} diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 61be5220..8f23164f 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -85,6 +85,25 @@ class EntryRepository extends EntityRepository ; } + /** + * Retrieves entries filtered with a search term for a user. + * + * @param int $userId + * @param string $term + * + * @return QueryBuilder + */ + public function getBuilderForSearchByUser($userId, $term) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.content LIKE :term')->setParameter('term', '%'.$term.'%') + ->orWhere('e.title LIKE :term')->setParameter('term', '%'.$term.'%') + ->leftJoin('e.tags', 't') + ->groupBy('e.id') + ->having('count(t.id) = 0'); + } + /** * Retrieves untagged entries for a user. * diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml index 20e83a07..63216a0f 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml @@ -162,6 +162,7 @@ entry: archived: 'Archived entries' filtered: 'Filtered entries' filtered_tags: 'Filtered by tags:' + filtered_search: 'Filtered by search:' untagged: 'Untagged entries' list: number_on_the_page: '{0} There are no entries.|{1} There is one entry.|]1,Inf[ There are %count% entries.' @@ -227,6 +228,8 @@ entry: placeholder: 'http://website.com' form_new: url_label: Url + search: + placeholder: 'What are you looking for?' edit: page_title: 'Edit an entry' title_label: 'Title' diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/common/Entry/_title.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/common/Entry/_title.html.twig index 92cabdd9..654c1d2d 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/common/Entry/_title.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/common/Entry/_title.html.twig @@ -6,8 +6,10 @@ {{ 'entry.page_titles.archived'|trans }} {% elseif currentRoute == 'all' %} {{ 'entry.page_titles.filtered'|trans }} +{% elseif currentRoute == 'search' %} + {{ 'entry.page_titles.filtered_search'|trans }} {{ filter }} {% elseif currentRoute == 'tag_entries' %} - {{ 'entry.page_titles.filtered_tags'|trans }} {{ currentTag }} + {{ 'entry.page_titles.filtered_tags'|trans }} {{ filter }} {% elseif currentRoute == 'untagged' %} {{ 'entry.page_titles.untagged'|trans }} {% else %} 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 160dbf3d..ff555055 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 @@ -1,11 +1,14 @@ {% extends "WallabagCoreBundle::layout.html.twig" %} {% block title %} - {% set currentTag = '' %} + {% set filter = '' %} {% if tag is defined %} - {% set currentTag = tag %} + {% set filter = tag %} + {% endif %} + {% if searchTerm is not empty %} + {% set filter = searchTerm %} {% endif %} - {% include "@WallabagCore/themes/common/Entry/_title.html.twig" with {'currentTag': currentTag} %} + {% include "@WallabagCore/themes/common/Entry/_title.html.twig" with {'filter': filter} %} {% endblock %} {% block content %} diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/search_form.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/search_form.html.twig new file mode 100644 index 00000000..9b8ac86d --- /dev/null +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Entry/search_form.html.twig @@ -0,0 +1,14 @@ +
+ {% if form_errors(form) %} + {{ form_errors(form) }} + {% endif %} + + {% if form_errors(form.term) %} + {{ form_errors(form.term) }} + {% endif %} + + {{ form_widget(form.term, { 'attr': {'autocomplete': 'off', 'placeholder': 'entry.search.placeholder'} }) }} + + {{ form_rest(form) }} +
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig index f1ef01df..9437a2a8 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig @@ -89,11 +89,11 @@ add - +
  • filter_list @@ -106,13 +106,11 @@
  • -
    - -
    +
    -- cgit v1.2.3 From 32f455c1317bf536aea63147d2c5074ae7425d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Fri, 18 Nov 2016 17:36:19 +0100 Subject: Added tests --- src/Wallabag/CoreBundle/Repository/EntryRepository.php | 3 +-- src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml | 2 +- .../Resources/views/themes/baggy/Entry/search_form.html.twig | 2 ++ src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 51cfe4d1..47e24d6b 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository $qb ->andWhere('e.content LIKE :term OR e.title LIKE :term')->setParameter('term', '%'.$term.'%') ->leftJoin('e.tags', 't') - ->groupBy('e.id') - ->having('count(t.id) = 0'); + ->groupBy('e.id'); return $qb; } diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml index d5c00e52..d97eab26 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml @@ -162,7 +162,7 @@ entry: archived: "Articles lus" filtered: "Articles filtrés" filtered_tags: "Articles filtrés par tags :" - filtered_search: 'Articles filtrés par une recherche :' + filtered_search: 'Articles filtrés par recherche :' untagged: "Article sans tag" list: number_on_the_page: "{0} Il n’y a pas d’articles.|{1} Il y a un article.|]1,Inf[ Il y a %count% articles." diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/search_form.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/search_form.html.twig index 818513f3..20821b6d 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/search_form.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/Entry/search_form.html.twig @@ -9,6 +9,8 @@ {{ form_errors(form.term) }} {% endif %} + + {{ form_widget(form.term, { 'attr': {'autocomplete': 'off', 'placeholder': 'entry.search.placeholder'} }) }} {{ form_rest(form) }} 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 fc024aff..07ff8e14 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/layout.html.twig @@ -33,7 +33,7 @@
  • {{ 'menu.left.search'|trans }}
  • -- cgit v1.2.3