allow json in search response and abstract interactive loading from select2

This commit is contained in:
2018-08-27 13:14:15 +02:00
parent 45946082ea
commit 1826c63251
7 changed files with 46 additions and 23 deletions

View File

@@ -30,6 +30,7 @@ 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;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
*
@@ -40,27 +41,39 @@ use Symfony\Component\Form\Extension\Core\Type\FormType;
class SearchController extends Controller
{
public function searchAction(Request $request)
public function searchAction(Request $request, $_format)
{
$pattern = $request->query->get('q', '');
if ($pattern === ''){
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
'message' => $this->get('translator')->trans("Your search is empty. "
. "Please provide search terms."),
'pattern' => $pattern
));
switch($_format) {
case 'html':
return $this->render('ChillMainBundle:Search:error.html.twig',
array(
'message' => $this->get('translator')->trans("Your search is empty. "
. "Please provide search terms."),
'pattern' => $pattern
));
case 'json':
return new JsonResponse([
'results' => [],
'pagination' => [ 'more' => false ]
]);
}
}
$name = $request->query->get('name', NULL);
try {
if ($name === NULL) {
if ($_format === 'json') {
return new JsonResponse('Currently, we still do not aggregate results '
. 'from different providers', JsonResponse::HTTP_BAD_REQUEST);
}
// no specific search selected. Rendering result in "preview" mode
$results = $this->get('chill.main.search_provider')
->getSearchResults(
->getSearchResults(
$pattern,
0,
5,
@@ -74,13 +87,18 @@ class SearchController extends Controller
$paginatorFactory = $this->get('chill_main.paginator_factory');
$results = [$this->get('chill.main.search_provider')
->getResultByName(
->getResultByName(
$pattern,
$name,
$paginatorFactory->getCurrentPageFirstItemNumber(),
$paginatorFactory->getCurrentItemsPerPage(),
array(SearchInterface::SEARCH_PREVIEW_OPTION => false)
array(SearchInterface::SEARCH_PREVIEW_OPTION => false),
$_format
)];
if ($_format === 'json') {
return new JsonResponse(\reset($results));
}
}
} catch (UnknowSearchDomainException $ex) {
return $this->render('ChillMainBundle:Search:error.html.twig',