mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-24 08:33:49 +00:00
behaviour of /search path
add exception catching and message create tests refs #223 refs #377
This commit is contained in:
@@ -24,6 +24,9 @@ namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\MainBundle\Search\UnknowSearchDomainException;
|
||||
use Chill\MainBundle\Search\UnknowSearchNameException;
|
||||
use Chill\MainBundle\Search\ParsingException;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -31,16 +34,52 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
* @author julien.fastre@champs-libres.coop
|
||||
* @author marc@champs-libres.coop
|
||||
*/
|
||||
|
||||
class SearchController extends Controller
|
||||
{
|
||||
|
||||
public function searchAction(Request $request)
|
||||
{
|
||||
$results = $this->get('chill.main.search_provider')
|
||||
->getResults($request->query->get('q'));
|
||||
$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.")
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
$name = $request->query->get('name', NULL);
|
||||
|
||||
try {
|
||||
if ($name === NULL) {
|
||||
$results = $this->get('chill.main.search_provider')
|
||||
->getSearchResults($request->query->get('q'));
|
||||
} else {
|
||||
$results = [$this->get('chill.main.search_provider')
|
||||
->getResultByName($pattern, $name)];
|
||||
}
|
||||
} catch (UnknowSearchDomainException $ex) {
|
||||
return $this->render('ChillMainBundle:Search:error.html.twig',
|
||||
array(
|
||||
"message" => $this->get('translator')->trans("The domain %domain% "
|
||||
. "is unknow. Please check your search.", array('%domain%' => $ex->getDomain()))
|
||||
));
|
||||
} catch (UnknowSearchNameException $ex) {
|
||||
throw $this->createNotFoundException("The name ".$ex->getName()." is not found");
|
||||
} catch (ParsingException $ex) {
|
||||
return $this->render('ChillMainBundle:Search:error.html.twig',
|
||||
array(
|
||||
"message" => $this->get('translator')->trans('Invalid terms').
|
||||
": ".$this->get('translator')->trans($ex->getMessage())
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
return $this->render('ChillMainBundle:Search:list.html.twig',
|
||||
array('results' => $results)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user