diff --git a/Form/ChoosableSearchType.php b/Form/ChoosableSearchType.php new file mode 100644 index 0000000..072ba99 --- /dev/null +++ b/Form/ChoosableSearchType.php @@ -0,0 +1,28 @@ +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')); + } + +} diff --git a/Resources/views/SearchForm/searchform_choosable.html.twig b/Resources/views/SearchForm/searchform_choosable.html.twig new file mode 100644 index 0000000..0aed5c8 --- /dev/null +++ b/Resources/views/SearchForm/searchform_choosable.html.twig @@ -0,0 +1,13 @@ +
+ {{ form_widget(form.options) }} + {{ form_widget(form.searchType) }} + {{ form_label(form.index) }} + {{ form_widget(form.index) }} + {{ form_widget(form.term) }} + {{ form_rest(form) }} + +
+ +
+ +
\ No newline at end of file diff --git a/Service/SearchService.php b/Service/SearchService.php index cb56269..9b38e18 100644 --- a/Service/SearchService.php +++ b/Service/SearchService.php @@ -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; @@ -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 diff --git a/Tests/Service/SearchServiceTest.php b/Tests/Service/SearchServiceTest.php index 598bf66..d6d2829 100644 --- a/Tests/Service/SearchServiceTest.php +++ b/Tests/Service/SearchServiceTest.php @@ -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 */ diff --git a/Twig/Extensions/SearchForm.php b/Twig/Extensions/SearchForm.php index 3c1ae11..beabcd5 100644 --- a/Twig/Extensions/SearchForm.php +++ b/Twig/Extensions/SearchForm.php @@ -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()) + ); } /**