1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-07-12 16:58:37 +00:00

Added a simple search engine

Fix #18
This commit is contained in:
Nicolas Lœuillet 2016-11-04 23:24:43 +01:00
parent 27dce581ca
commit ee122a7528
15 changed files with 151 additions and 50 deletions

View file

@ -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,
]
);
}

View file

@ -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

View file

@ -0,0 +1,29 @@
<?php
namespace Wallabag\CoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SearchEntryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->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,
]);
}
}

View file

@ -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.
*

View file

@ -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'

View file

@ -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 %}

View file

@ -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 %}
{% include "@WallabagCore/themes/common/Entry/_title.html.twig" with {'currentTag': currentTag} %}
{% if searchTerm is not empty %}
{% set filter = searchTerm %}
{% endif %}
{% include "@WallabagCore/themes/common/Entry/_title.html.twig" with {'filter': filter} %}
{% endblock %}
{% block content %}

View file

@ -0,0 +1,14 @@
<form name="search
" method="GET" action="{{ path('search')}}">
{% if form_errors(form) %}
<span class="black-text">{{ form_errors(form) }}</span>
{% endif %}
{% if form_errors(form.term) %}
<span class="black-text">{{ form_errors(form.term) }}</span>
{% endif %}
{{ form_widget(form.term, { 'attr': {'autocomplete': 'off', 'placeholder': 'entry.search.placeholder'} }) }}
{{ form_rest(form) }}
</form>

View file

@ -89,11 +89,11 @@
<i class="material-icons">add</i>
</a>
</li>
<!--<li>
<li>
<a title="{{ 'menu.top.search'|trans }}" class="waves-effect" href="javascript: void(null);" id="nav-btn-search">
<i class="material-icons">search</i>
</a>
</li>-->
</li>
<li id="button_filters">
<a class="nav-panel-menu button-collapse-right tooltipped" data-position="bottom" data-delay="50" data-tooltip="{{ 'menu.top.filter_entries'|trans }}" href="#" data-activates="filters">
<i class="material-icons">filter_list</i>
@ -106,13 +106,11 @@
</li>
</ul>
</div>
<form method="get" action="index.php">
<div class="input-field nav-panel-search" style="display: none">
<input name="search" id="searchfield" type="search" required placeholder="{{ 'menu.search_form.input_label'|trans }}">
<label for="search"><i class="material-icons search">search</i></label>
<i class="material-icons close">clear</i>
</div>
</form>
<div class="input-field nav-panel-search" style="display: none">
{{ render(controller("WallabagCoreBundle:Entry:searchForm")) }}
<label for="search" class="active"><i class="material-icons search">search</i></label>
<i class="material-icons close">clear</i>
</div>
<div class="input-field nav-panel-add" style="display: none">
{{ render(controller("WallabagCoreBundle:Entry:addEntryForm")) }}
<label for="add" class="active"><i class="material-icons add">add</i></label>