Skip to content

Commit

Permalink
allow user to choose which index to search from
Browse files Browse the repository at this point in the history
  • Loading branch information
joonsp committed Aug 14, 2012
1 parent 06d69bd commit 942a993
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 20 deletions.
28 changes: 28 additions & 0 deletions Form/ChoosableSearchType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Xi\Bundle\SearchBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ChoosableSearchType extends SearchType
{

private $choices = array();

public function __construct($choices)
{
$this->choices = $choices;
}

public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('options', 'hidden')
->add('index', 'choice', array('choices' => $this->choices, 'expanded' => true, 'label' => 'xi_search.choose-index'))
->add('searchType', 'hidden')
->add('term', 'text', array('label' => 'search.form.term.label'));
}

}
13 changes: 13 additions & 0 deletions Resources/views/SearchForm/searchform_choosable.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<form action="{{ path('XiSearchBundle_search') }}" class="ajax-form search-form" method="post" {{ form_enctype(form) }}>
{{ form_widget(form.options) }}
{{ form_widget(form.searchType) }}
{{ form_label(form.index) }}
{{ form_widget(form.index) }}
{{ form_widget(form.term) }}
{{ form_rest(form) }}

<div id="search-form">
<button type="submit">{{'search.form.submit_button'|trans}}</button>
</div>

</form>
16 changes: 14 additions & 2 deletions Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\Form\FormFactory,
Symfony\Component\Form\Form,
Xi\Bundle\SearchBundle\Form\SearchType,
Xi\Bundle\SearchBundle\Form\ChoosableSearchType,
Xi\Bundle\SearchBundle\Service\Search\Search,
Xi\Bundle\SearchBundle\Service\Search\Result\SearchResultSet;

Expand Down Expand Up @@ -41,8 +42,19 @@ public function getSearchForm()
return $this->formFactory->create(
new SearchType()
);
}

}

/**
* @param array $choices array of choosable indices
* @return Form
*/
public function getChoosableSearchForm(array $choices)
{
return $this->formFactory->create(
new ChoosableSearchType($choices)
);
}

/**
* @param string $index
* @param string $term
Expand Down
14 changes: 12 additions & 2 deletions Tests/Service/SearchServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ public function getSearchForm()
$this->formfactory->expects($this->once())->method('create')->will($this->returnValue('form'));
$form = $this->service->getSearchForm();
$this->assertEquals('form', $form);
}

}

/**
* @test
*/
public function getChoosableSearchForm()
{
$this->formfactory->expects($this->once())->method('create')->will($this->returnValue('form'));
$form = $this->service->getChoosableSearchForm(array('index1', 'index2'));
$this->assertEquals('form', $form);
}

/**
* @test
*/
Expand Down
90 changes: 74 additions & 16 deletions Twig/Extensions/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,104 @@ public function getFunctions()
{
return array(
'xi_search_form' => new \Twig_Function_Method(
$this, 'searchForm', array('is_safe' => array('html'))
$this, 'searchForm', array('is_safe' => array('html'))
),
'xi_search_form_choosable' => new \Twig_Function_Method(
$this, 'searchFormChoosable', array('is_safe' => array('html'))
),
'xi_find_form' => new \Twig_Function_Method(
$this, 'findForm', array('is_safe' => array('html'))
),
$this, 'findForm', array('is_safe' => array('html'))
),
'xi_find_form_choosable' => new \Twig_Function_Method(
$this, 'findFormChoosable', array('is_safe' => array('html'))
),
'xi_search_result' => new \Twig_Function_Method(
$this, 'searchResult', array('is_safe' => array('html'))
$this, 'searchResult', array('is_safe' => array('html'))
)
);
}

/**
* For default search
* @param string $index
* @param array e $options
* @param array $options
*/
public function searchForm($index, $options = array())
{
{
return $this->renderForm($index, $options, 'search');
}


/**
* @param string $index currently selected index
* @param array $choosableFiels the choosable indices
* @param array $options
*/
public function searchFormChoosable($index, array $choiceFields, $options = array())
{
return $this->renderFormWithChoosableIndices($index, $options, 'search', $choiceFields);
}

/**
* if you like to have search results converted to entities
* @param string $index
* @param array e $options
* @param string $index
* @param array $options
* @return string
*/
*/
public function findForm($index, $options = array())
{
{
return $this->renderForm($index, $options, 'find');
}


/**
* @param string $index
* @param array $choosableFiels the choosable indices
* @param array $options
* @return string
*/
public function findFormChoosable($index, array $choiceFields, $options = array())
{
return $this->renderFormWithChoosableIndices($index, $options, 'find', $choiceFields);
}

/**
* @param int $index
* @param array $options
* @param string $searchType
* @return string
*/
private function renderForm($index, $options, $searchType)
{
$form = $this->searchService->getSearchForm();
$form->get('options')->setData(json_encode($options));

$form->get('options')->setData($options);
$form->get('index')->setData($index);
$form->get('searchType')->setData($searchType);

return $this->twig->render(
'XiSearchBundle:SearchForm:searchform.html.twig',
array('form' => $form->createView())
);
}

/**
* @param int $index
* @param array $options
* @param string $searchType
* @param array $choiceFields
* @return string
*/
private function renderFormWithChoosableIndices($index, $options, $searchType, $choiceFields)
{
$form = $this->searchService->getChoosableSearchForm($choiceFields);

$form->get('options')->setData($options);
$form->get('index')->setData($index);
$form->get('searchType')->setData($searchType);
return $this->twig->render('XiSearchBundle:SearchForm:searchform.html.twig',
array('form' => $form->createView()));

return $this->twig->render(
'XiSearchBundle:SearchForm:searchform_choosable.html.twig',
array('form' => $form->createView())
);
}

/**
Expand Down

0 comments on commit 942a993

Please sign in to comment.