mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-27 01:53:49 +00:00
add advanced search
This commit is contained in:
@@ -28,6 +28,8 @@ use Chill\MainBundle\Search\UnknowSearchDomainException;
|
||||
use Chill\MainBundle\Search\UnknowSearchNameException;
|
||||
use Chill\MainBundle\Search\ParsingException;
|
||||
use Chill\MainBundle\Search\SearchInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -104,4 +106,69 @@ class SearchController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
public function advancedSearchAction($name, Request $request)
|
||||
{
|
||||
try {
|
||||
/* @var $variable Chill\MainBundle\Search\SearchProvider */
|
||||
$searchProvider = $this->get('chill.main.search_provider');
|
||||
/* @var $variable Chill\MainBundle\Search\HasAdvancedSearchFormInterface */
|
||||
$search = $this->get('chill.main.search_provider')
|
||||
->getHasAdvancedFormByName($name);
|
||||
|
||||
} catch (\Chill\MainBundle\Search\UnknowSearchNameException $e) {
|
||||
throw $this->createNotFoundException("no advanced search for "
|
||||
. "$name");
|
||||
}
|
||||
|
||||
if ($request->query->has('q')) {
|
||||
$data = $search->convertTermsToFormData($searchProvider->parse(
|
||||
$request->query->get('q')));
|
||||
}
|
||||
|
||||
$form = $this->createAdvancedSearchForm($name, $data ?? []);
|
||||
|
||||
if ($request->isMethod(Request::METHOD_POST)) {
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()) {
|
||||
$pattern = $this->get('chill.main.search_provider')
|
||||
->getHasAdvancedFormByName($name)
|
||||
->convertFormDataToQuery($form->getData());
|
||||
|
||||
return $this->redirectToRoute('chill_main_search', [
|
||||
'q' => $pattern, 'name' => $name
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('ChillMainBundle:Search:advanced_search.html.twig',
|
||||
[
|
||||
'form' => $form->createView(),
|
||||
'name' => $name
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createAdvancedSearchForm($name, array $data = [])
|
||||
{
|
||||
$builder = $this
|
||||
->get('form.factory')
|
||||
->createNamedBuilder(
|
||||
null,
|
||||
FormType::class,
|
||||
$data,
|
||||
[ 'method' => Request::METHOD_POST ]
|
||||
);
|
||||
|
||||
$this->get('chill.main.search_provider')
|
||||
->getHasAdvancedFormByName($name)
|
||||
->buildForm($builder)
|
||||
;
|
||||
|
||||
$builder->add('submit', SubmitType::class, [
|
||||
'label' => 'Search'
|
||||
]);
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user