Permalink
Browse files

tweaked search page

search also by tag
  • Loading branch information...
inmarelibero committed Dec 4, 2012
1 parent 40009cf commit 8635709077b4039a1349f0452f4151c19ede50b1
@@ -87,13 +87,14 @@ public function showAction($id)
public function searchAction()
{
$em = $this->getDoctrine()->getManager();
-
- $q = $this->getRequest()->get('q');
- $entities = $em->getRepository('BricksSiteBundle:Brick')->search($q);
+ // search for bricks
+ $entities = $em->getRepository('BricksSiteBundle:Brick')->search(array(
+ 'q' => $this->getRequest()->get('q'),
+ 'tag_slug' => $this->getRequest()->get('tag')
+ ));
return array(
- 'q' => $q,
'entities' => $entities
);
}
@@ -12,7 +12,18 @@
*/
class BrickRepository extends EntityRepository
{
- public function search($q)
+ /**
+ * search Bricks
+ *
+ * $params = array(
+ * 'q' => string, search $params['q'] in Bricks' title
+ * 'tag_slug' => string, search Bricks having $params['tag_slug'] as Tag (search Tag by $params['tag_slug'], exact matching)
+ * )
+ *
+ * @param array $params array of parameters
+ * @return multitype:
+ */
+ public function search(array $params = array())
{
$em = $this->getEntityManager();
@@ -21,13 +32,34 @@ public function search($q)
->from('BricksSiteBundle:Brick', 'e')
;
- // set 'q' doctrine parameter
- $qb->setParameter('q', '%'.$q.'%');
+ /**
+ * $params['q'] filter
+ *
+ * search in Brick.title field
+ */
+ if (array_key_exists('q', $params) && '' !== trim($params['q'])) {
+ $qb->andWhere($qb->expr()->like('e.title', ':q'))
+ ->setParameter('q', '%'.$params['q'].'%')
+ ;
+ }
/**
- * searche in 'title' field
+ * $params['tag_slug'] filter
+ *
+ * search in Tag.slug field
*/
- $qb->andWhere($qb->expr()->like('e.title', ':q'));
+ if (array_key_exists('tag_slug', $params) && '' !== trim($params['tag_slug'])) {
+
+ $qb->innerJoin('e.brickHasTags', 'bht')
+ ->innerJoin('bht.tag', 't')
+
+ ->andWhere('t.slug = :tag_slug')
+ ->setParameter('tag_slug', $params['tag_slug'])
+ ;
+ }
+
+ // order by title
+ $qb->addOrderBy('e.title');
return $qb->getQuery()->getResult();
}
@@ -1,13 +1,21 @@
<?php
namespace Bricks\siteBundle\Extension;
+use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Bricks\SiteBundle\Entity\Brick;
/**
* Twig extensions related to the Brick object
*/
class BrickExtension extends \Twig_Extension
{
+ private $router;
+
+ public function __construct(Router $router)
+ {
+ $this->router = $router;
+ }
+
public function getFunctions()
{
return array(
@@ -19,7 +27,7 @@ public function getFunctions()
* print a string of tag titles separated by $separator
*
* @param Brick $brick Brick object
- * @param unknown_type $separator string to separate tag titles
+ * @param string $separator string to separate tag titles
* @return string
*/
public function brickFormattedTags(Brick $brick, $separator = ',')
@@ -34,8 +42,11 @@ public function brickFormattedTags(Brick $brick, $separator = ',')
if ($tag) {
// add tag title
- $output .= $tag->getTitle();
+ $output .= <<<EOD
+<a href="{$this->router->generate('brick_search', array('tag' => $tag->getSlug()))}">{$tag->getTitle()}</a>
+EOD;
+ // if not last iteration
if ($k < $brickHasTagsLength-1) {
// add separator
$output .= $separator.' ';
@@ -3,6 +3,7 @@ services:
# Brick Twig extenxion
bricks.twig.brick_extension:
class: Bricks\SiteBundle\Extension\BrickExtension
+ arguments: [@router]
tags:
- { name: twig.extension }
@@ -40,6 +40,22 @@ body.route-brick_show {
}
}
+/**
+ search bricks
+
+ route: brick_search
+*/
+body.route-brick_search {
+ #form-search { overflow: hidden;
+ .box {
+ float: left;
+ margin-right: 10px;
+
+ &.buttons { float: right; }
+ }
+ }
+}
+
/**
toggle-user-stars-brick element
@@ -25,7 +25,11 @@ brick:
# brick search
search:
- page_title: Results for "%q%"
+ form:
+ q: Search title
+ button_submit: Search
+ page_title: Search results
+ no_results: no bricks found
# general translations (alphabetical order)
back_to_the_list: Back to the list
@@ -25,7 +25,11 @@ brick:
# brick search
search:
- page_title: Resultados de la búsqueda "%q%"
+ form:
+ q: Busca en el título
+ button_submit: Buscar
+ page_title: Resultados de la búsqueda
+ no_results: no se encontraron resultados
# general translations (alphabetical order)
back_to_the_list: Volver a la lista
@@ -25,7 +25,11 @@ brick:
# brick search
search:
- page_title: Risultati della ricerca "%q%"
+ form:
+ q: Cerca nel titolo
+ button_submit: Cerca
+ page_title: Risultati della ricerca
+ no_results: nessun risultato trovato
# general translations (alphabetical order)
back_to_the_list: torna alla lista
@@ -2,43 +2,48 @@
{% block main %}
- <div id="brick-search-wrapper">
-
- <div class="row">
- <div class="span12">
-
- <h1>{{ 'brick.search.page_title'|trans({'%q%': q}) }}</h1>
+ <div class="row">
+ <div class="span12">
+
+ <form action="{{ path('brick_search') }}" id="form-search" class="well">
+ <h1>{{ 'brick.search.page_title'|trans }}</h1>
- <hr>
+ <div class="box">
+ <label>{{ 'brick.search.form.q'|trans }}</label>
+ <input name="q" class="span6" type="text" value="{{ app.request.get('q') }}">
+ </div>
- {% if entities|length > 0 %}
- <ul class="bricks-list">
- {% for brick in entities %}
- <li>
- <a href="{{ path('brick_show', {id: brick.id, slug: brick.slug}) }}">
- {{ brick.title }}
- </a>
- <p>
- <i class="icon-user"></i> <a href="{{ path('userprofile_show', {'username': brick.user.username}) }}">{{ brick.user }}</a>
- |
- <i class="icon-calendar"></i> {{ 'brick.last_update'|trans }}: {{ brick.updatedAt|date('d/m/Y') }}
- </p>
- </li>
- {% endfor %}
- </ul>
- {% else %}
- {% if 'it' == app.request.locale %}
- nessun risultato trovato
- {% elseif 'es' == app.request.locale %}
- no se encontraron resultados
- {% else %}
- no bricks found
- {% endif %}
- {% endif %}
+ <div class="box">
+ <label>Tag</label>
+ <input name="tag" class="span3" size="16" type="text" value="{{ app.request.get('tag') }}">
+ </div>
+
+ <div class="box buttons">
+ <label>&nbsp;</label>
+ <button class="btn btn-primary" type="submit"><i class="icon-search"></i> {{ 'brick.search.form.button_submit'|trans }}</button>
+ </div>
+ </form>
- </div>
- </div>
+ {% if entities|length > 0 %}
+ <ul class="bricks-list">
+ {% for brick in entities %}
+ <li>
+ <a href="{{ path('brick_show', {id: brick.id, slug: brick.slug}) }}">
+ {{ brick.title }}
+ </a>
+ <p>
+ <i class="icon-user"></i> <a href="{{ path('userprofile_show', {'username': brick.user.username}) }}">{{ brick.user }}</a>
+ |
+ <i class="icon-calendar"></i> {{ 'brick.last_update'|trans }}: {{ brick.updatedAt|date('d/m/Y') }}
+ </p>
+ </li>
+ {% endfor %}
+ </ul>
+ {% else %}
+ {{ 'brick.search.no_results'|trans }}
+ {% endif %}
+ </div>
</div>
-{% endblock %}
+{% endblock %}
@@ -48,7 +48,7 @@
<br>
{{ macrosBrick.toggleUserStarsBrick(brick) }}
|
- <i class="icon-tag"></i> {{ brick_formatted_tags(brick) }}
+ <i class="icon-tag"></i> {{ brick_formatted_tags(brick)|raw }}
</div>
<div class="content">
@@ -94,7 +94,7 @@
|
{{ macrosBrick.toggleUserStarsBrick(brick) }}
|
- <i class="icon-tag"></i> {{ brick_formatted_tags(brick) }}
+ <i class="icon-tag"></i> {{ brick_formatted_tags(brick)|raw }}
</p>
</li>
{% endfor %}

0 comments on commit 8635709

Please sign in to comment.