mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,62 +1,47 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Search\ParsingException;
|
||||
use Chill\MainBundle\Search\SearchApi;
|
||||
use Chill\MainBundle\Search\SearchApiNoQueryException;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Chill\MainBundle\Search\SearchInterface;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
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\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Chill\MainBundle\Search\SearchProvider;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Search\SearchApi;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
use function key;
|
||||
use function reset;
|
||||
|
||||
/**
|
||||
* Class SearchController
|
||||
*
|
||||
* @package Chill\MainBundle\Controller
|
||||
* Class SearchController.
|
||||
*/
|
||||
class SearchController extends AbstractController
|
||||
{
|
||||
protected SearchProvider $searchProvider;
|
||||
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
protected PaginatorFactory $paginatorFactory;
|
||||
|
||||
protected SearchApi $searchApi;
|
||||
|
||||
function __construct(
|
||||
protected SearchProvider $searchProvider;
|
||||
|
||||
protected TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
SearchProvider $searchProvider,
|
||||
TranslatorInterface $translator,
|
||||
PaginatorFactory $paginatorFactory,
|
||||
@@ -68,127 +53,6 @@ class SearchController extends AbstractController
|
||||
$this->searchApi = $searchApi;
|
||||
}
|
||||
|
||||
|
||||
public function searchAction(Request $request, $_format)
|
||||
{
|
||||
$pattern = $request->query->get('q', '');
|
||||
|
||||
if ($pattern === ''){
|
||||
switch($_format) {
|
||||
case 'html':
|
||||
return $this->render('@ChillMain/Search/error.html.twig',
|
||||
array(
|
||||
'message' => $this->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->searchProvider
|
||||
->getSearchResults(
|
||||
$pattern,
|
||||
0,
|
||||
5,
|
||||
array(SearchInterface::SEARCH_PREVIEW_OPTION => true)
|
||||
);
|
||||
} else {
|
||||
// we want search on a specific search provider. Display full results.
|
||||
$results = [$this->searchProvider
|
||||
->getResultByName(
|
||||
$pattern,
|
||||
$name,
|
||||
$this->paginatorFactory->getCurrentPageFirstItemNumber(),
|
||||
$this->paginatorFactory->getCurrentItemsPerPage(),
|
||||
array(
|
||||
SearchInterface::SEARCH_PREVIEW_OPTION => false,
|
||||
SearchInterface::REQUEST_QUERY_PARAMETERS => $request
|
||||
->get(SearchInterface::REQUEST_QUERY_KEY_ADD_PARAMETERS, [])
|
||||
),
|
||||
$_format
|
||||
)];
|
||||
|
||||
if ($_format === 'json') {
|
||||
return new JsonResponse(\reset($results));
|
||||
}
|
||||
}
|
||||
} catch (UnknowSearchDomainException $ex) {
|
||||
return $this->render('@ChillMain/Search/error.html.twig',
|
||||
array(
|
||||
"message" => $this->translator->trans("The domain %domain% "
|
||||
. "is unknow. Please check your search.", array('%domain%' => $ex->getDomain())),
|
||||
'pattern' => $pattern
|
||||
));
|
||||
} catch (UnknowSearchNameException $ex) {
|
||||
throw $this->createNotFoundException("The name ".$ex->getName()." is not found");
|
||||
} catch (ParsingException $ex) {
|
||||
return $this->render('@ChillMain/Search/error.html.twig',
|
||||
array(
|
||||
"message" => $this->translator->trans('Invalid terms').
|
||||
": ".$this->translator->trans($ex->getMessage()),
|
||||
'pattern' => $pattern
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
return $this->render('@ChillMain/Search/list.html.twig',
|
||||
array('results' => $results, 'pattern' => $pattern)
|
||||
);
|
||||
}
|
||||
|
||||
public function searchApi(Request $request, $_format): JsonResponse
|
||||
{
|
||||
//TODO this is an incomplete implementation
|
||||
$query = $request->query->get('q', '');
|
||||
$types = $request->query->get('type', []);
|
||||
|
||||
if (count($types) === 0) {
|
||||
throw new BadRequestException("The request must contains at "
|
||||
." one type");
|
||||
}
|
||||
|
||||
try {
|
||||
$collection = $this->searchApi->getResults($query, $types, []);
|
||||
} catch (SearchApiNoQueryException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage(), $e);
|
||||
}
|
||||
|
||||
return $this->json($collection, \Symfony\Component\HttpFoundation\Response::HTTP_OK, [], [ "groups" => ["read"]]);
|
||||
}
|
||||
|
||||
public function advancedSearchListAction(Request $request)
|
||||
{
|
||||
/* @var $variable Chill\MainBundle\Search\SearchProvider */
|
||||
$searchProvider = $this->searchProvider;
|
||||
$advancedSearchProviders = $searchProvider
|
||||
->getHasAdvancedFormSearchServices();
|
||||
|
||||
if(\count($advancedSearchProviders) === 1) {
|
||||
\reset($advancedSearchProviders);
|
||||
|
||||
return $this->redirectToRoute('chill_main_advanced_search', [
|
||||
'name' => \key($advancedSearchProviders)
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/Search/choose_list.html.twig');
|
||||
}
|
||||
|
||||
public function advancedSearchAction($name, Request $request)
|
||||
{
|
||||
try {
|
||||
@@ -196,16 +60,16 @@ class SearchController extends AbstractController
|
||||
$searchProvider = $this->searchProvider;
|
||||
/* @var $variable Chill\MainBundle\Search\HasAdvancedSearchFormInterface */
|
||||
$search = $this->searchProvider
|
||||
->getHasAdvancedFormByName($name);
|
||||
|
||||
->getHasAdvancedFormByName($name);
|
||||
} catch (\Chill\MainBundle\Search\UnknowSearchNameException $e) {
|
||||
throw $this->createNotFoundException("no advanced search for "
|
||||
. "$name");
|
||||
throw $this->createNotFoundException('no advanced search for '
|
||||
. "{$name}");
|
||||
}
|
||||
|
||||
if ($request->query->has('q')) {
|
||||
$data = $search->convertTermsToFormData($searchProvider->parse(
|
||||
$request->query->get('q')));
|
||||
$request->query->get('q')
|
||||
));
|
||||
}
|
||||
|
||||
$form = $this->createAdvancedSearchForm($name, $data ?? []);
|
||||
@@ -219,17 +83,146 @@ class SearchController extends AbstractController
|
||||
->convertFormDataToQuery($form->getData());
|
||||
|
||||
return $this->redirectToRoute('chill_main_search', [
|
||||
'q' => $pattern, 'name' => $name
|
||||
'q' => $pattern, 'name' => $name,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/Search/advanced_search.html.twig',
|
||||
return $this->render(
|
||||
'@ChillMain/Search/advanced_search.html.twig',
|
||||
[
|
||||
'form' => $form->createView(),
|
||||
'name' => $name,
|
||||
'title' => $search->getAdvancedSearchTitle()
|
||||
'title' => $search->getAdvancedSearchTitle(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function advancedSearchListAction(Request $request)
|
||||
{
|
||||
/* @var $variable Chill\MainBundle\Search\SearchProvider */
|
||||
$searchProvider = $this->searchProvider;
|
||||
$advancedSearchProviders = $searchProvider
|
||||
->getHasAdvancedFormSearchServices();
|
||||
|
||||
if (\count($advancedSearchProviders) === 1) {
|
||||
reset($advancedSearchProviders);
|
||||
|
||||
return $this->redirectToRoute('chill_main_advanced_search', [
|
||||
'name' => key($advancedSearchProviders),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/Search/choose_list.html.twig');
|
||||
}
|
||||
|
||||
public function searchAction(Request $request, $_format)
|
||||
{
|
||||
$pattern = $request->query->get('q', '');
|
||||
|
||||
if ('' === $pattern) {
|
||||
switch ($_format) {
|
||||
case 'html':
|
||||
return $this->render(
|
||||
'@ChillMain/Search/error.html.twig',
|
||||
[
|
||||
'message' => $this->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 (null === $name) {
|
||||
if ('json' === $_format) {
|
||||
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->searchProvider
|
||||
->getSearchResults(
|
||||
$pattern,
|
||||
0,
|
||||
5,
|
||||
[SearchInterface::SEARCH_PREVIEW_OPTION => true]
|
||||
);
|
||||
} else {
|
||||
// we want search on a specific search provider. Display full results.
|
||||
$results = [$this->searchProvider
|
||||
->getResultByName(
|
||||
$pattern,
|
||||
$name,
|
||||
$this->paginatorFactory->getCurrentPageFirstItemNumber(),
|
||||
$this->paginatorFactory->getCurrentItemsPerPage(),
|
||||
[
|
||||
SearchInterface::SEARCH_PREVIEW_OPTION => false,
|
||||
SearchInterface::REQUEST_QUERY_PARAMETERS => $request
|
||||
->get(SearchInterface::REQUEST_QUERY_KEY_ADD_PARAMETERS, []),
|
||||
],
|
||||
$_format
|
||||
), ];
|
||||
|
||||
if ('json' === $_format) {
|
||||
return new JsonResponse(reset($results));
|
||||
}
|
||||
}
|
||||
} catch (UnknowSearchDomainException $ex) {
|
||||
return $this->render(
|
||||
'@ChillMain/Search/error.html.twig',
|
||||
[
|
||||
'message' => $this->translator->trans('The domain %domain% '
|
||||
. 'is unknow. Please check your search.', ['%domain%' => $ex->getDomain()]),
|
||||
'pattern' => $pattern,
|
||||
]
|
||||
);
|
||||
} catch (UnknowSearchNameException $ex) {
|
||||
throw $this->createNotFoundException('The name ' . $ex->getName() . ' is not found');
|
||||
} catch (ParsingException $ex) {
|
||||
return $this->render(
|
||||
'@ChillMain/Search/error.html.twig',
|
||||
[
|
||||
'message' => $this->translator->trans('Invalid terms') .
|
||||
': ' . $this->translator->trans($ex->getMessage()),
|
||||
'pattern' => $pattern,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return $this->render(
|
||||
'@ChillMain/Search/list.html.twig',
|
||||
['results' => $results, 'pattern' => $pattern]
|
||||
);
|
||||
}
|
||||
|
||||
public function searchApi(Request $request, $_format): JsonResponse
|
||||
{
|
||||
//TODO this is an incomplete implementation
|
||||
$query = $request->query->get('q', '');
|
||||
$types = $request->query->get('type', []);
|
||||
|
||||
if (count($types) === 0) {
|
||||
throw new BadRequestException('The request must contains at '
|
||||
. ' one type');
|
||||
}
|
||||
|
||||
try {
|
||||
$collection = $this->searchApi->getResults($query, $types, []);
|
||||
} catch (SearchApiNoQueryException $e) {
|
||||
throw new BadRequestHttpException($e->getMessage(), $e);
|
||||
}
|
||||
|
||||
return $this->json($collection, \Symfony\Component\HttpFoundation\Response::HTTP_OK, [], ['groups' => ['read']]);
|
||||
}
|
||||
|
||||
protected function createAdvancedSearchForm($name, array $data = [])
|
||||
@@ -240,19 +233,17 @@ class SearchController extends AbstractController
|
||||
null,
|
||||
FormType::class,
|
||||
$data,
|
||||
[ 'method' => Request::METHOD_POST ]
|
||||
['method' => Request::METHOD_POST]
|
||||
);
|
||||
|
||||
$this->searchProvider
|
||||
->getHasAdvancedFormByName($name)
|
||||
->buildForm($builder)
|
||||
;
|
||||
->buildForm($builder);
|
||||
|
||||
$builder->add('submit', SubmitType::class, [
|
||||
'label' => 'Search'
|
||||
'label' => 'Search',
|
||||
]);
|
||||
|
||||
return $builder->getForm();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user