From: Nicolas LÅ“uillet Date: Fri, 4 Nov 2016 22:24:43 +0000 (+0100) Subject: Added a simple search engine X-Git-Tag: 2.2.0~3^2~59^2~4 X-Git-Url: https://git.immae.eu/?p=github%2Fwallabag%2Fwallabag.git;a=commitdiff_plain;h=ee122a7528f55dfb5f02e351c509d00b756fedaa Added a simple search engine Fix #18 --- diff --git a/app/Resources/static/themes/material/css/main.css b/app/Resources/static/themes/material/css/main.css index df126fb4..21f948b1 100755 --- a/app/Resources/static/themes/material/css/main.css +++ b/app/Resources/static/themes/material/css/main.css @@ -322,11 +322,13 @@ nav input { color: #444; } -.input-field.nav-panel-add label { +.input-field.nav-panel-add label, +.input-field.nav-panel-search label { left: 1rem; } -.input-field.nav-panel-add .close { +.input-field.nav-panel-add .close, +.input-field.nav-panel-search .close { position: absolute; top: 0; right: 1rem; @@ -345,7 +347,9 @@ nav input { } .input-field.nav-panel-add, -.input-field.nav-panel-add form { +.input-field.nav-panel-add form, +.input-field.nav-panel-search, +.input-field.nav-panel-search form{ height: 100%; } diff --git a/app/Resources/static/themes/material/js/init.js b/app/Resources/static/themes/material/js/init.js index 9746224b..0b2832c0 100755 --- a/app/Resources/static/themes/material/js/init.js +++ b/app/Resources/static/themes/material/js/init.js @@ -55,7 +55,7 @@ $(document).ready(() => { $('.nav-panels .action').hide(100); $('.nav-panel-menu').addClass('hidden'); $('.nav-panels').css('background', 'white'); - $('#searchfield').focus(); + $('#search_entry_term').focus(); return false; }); $('.close').on('click', () => { 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 @@
  • -
    - -
    +