request->get('qry');
$json = [];
$options = SearchEngineOptions::fromRequest($this->app, $request);
$form = $options->serialize();
$perPage = (int) $this->getSettings()->getUserSetting($this->getAuthenticatedUser(), 'images_per_page');
$page = (int) $request->request->get('pag');
$firstPage = $page < 1;
$engine = $this->getSearchEngine();
if ($page < 1) {
$engine->resetCache();
$page = 1;
}
/** @var SearchEngineResult $result */
$result = $engine->query($query, (($page - 1) * $perPage), $perPage, $options);
$user = $this->getAuthenticatedUser();
$userManipulator = $this->getUserManipulator();
$userManipulator->logQuery($user, $result->getQuery());
if ($this->getSettings()->getUserSetting($user, 'start_page') === 'LAST_QUERY') {
$userManipulator->setUserSetting($user, 'start_page_query', $query);
}
foreach ($options->getDataboxes() as $databox) {
$collections = array_map(function (\collection $collection) {
return $collection->get_coll_id();
}, array_filter($options->getCollections(), function (\collection $collection) use ($databox) {
return $collection->get_databox()->get_sbas_id() == $databox->get_sbas_id();
}));
$this->getSearchEngineLogger()->log($databox, $result->getQuery(), $result->getTotal(), $collections);
}
$proposals = $firstPage ? $result->getProposals() : false;
$npages = $result->getTotalPages($perPage);
$page = $result->getCurrentPage($perPage);
$string = '';
if ($npages > 1) {
$d2top = ($npages - $page);
$d2bottom = $page;
if (min($d2top, $d2bottom) < 4) {
if ($d2bottom < 4) {
if($page != 1){
$string .= "";
}
for ($i = 1; ($i <= 4 && (($i <= $npages) === true)); $i++) {
if ($i == $page)
$string .= '';
else
$string .= "" . $i . "";
}
if ($npages > 4)
$string .= "";
$string .= "";
} else {
$start = $npages - 4;
if (($start) > 0){
$string .= "";
$string .= "";
}else
$start = 1;
for ($i = ($start); $i <= $npages; $i++) {
if ($i == $page)
$string .= '';
else
$string .= "" . $i . "";
}
if($page < $npages){
$string .= "";
}
}
} else {
$string .= "";
for ($i = ($page - 2); $i <= ($page + 2); $i++) {
if ($i == $page)
$string .= '';
else
$string .= "" . $i . "";
}
$string .= "";
}
}
$string .= '
';
$explain = "";
$explain .= "
";
if ($result->getTotal() != $result->getAvailable()) {
$explain .= $this->app->trans('reponses:: %available% Resultats rappatries sur un total de %total% trouves', ['available' => $result->getAvailable(), '%total%' => $result->getTotal()]);
} else {
$explain .= $this->app->trans('reponses:: %total% Resultats', ['%total%' => $result->getTotal()]);
}
$explain .= " ";
$explain .= '
' . $result->getDuration() . ' s
dans index ' . $result->getIndexes();
$explain .= "
";
$infoResult = ''
. $this->app->trans('%number% documents
selectionnes', ['%number%' => ''])
. '
'
. $this->app->trans('%total% reponses', ['%total%' => ''.$result->getTotal().'']) . '';
$json['infos'] = $infoResult;
$json['navigation'] = $string;
$prop = null;
if ($firstPage) {
$propals = $result->getSuggestions();
if (count($propals) > 0) {
foreach ($propals as $prop_array) {
if ($prop_array->getSuggestion() !== $query && $prop_array->getHits() > $result->getTotal()) {
$prop = $prop_array->getSuggestion();
break;
}
}
}
}
if ($result->getTotal() === 0) {
$template = 'prod/results/help.html.twig';
} else {
$template = 'prod/results/records.html.twig';
}
$json['results'] = $this->render($template, ['results'=> $result]);
$json['query'] = $query;
/** Debug */
$json['parsed_query'] = $result->getQuery();
/** End debug */
$json['facets'] = $result->getFacets();
$json['phrasea_props'] = $proposals;
$json['total_answers'] = (int) $result->getAvailable();
$json['next_page'] = ($page < $npages && $result->getAvailable() > 0) ? ($page + 1) : false;
$json['prev_page'] = ($page > 1 && $result->getAvailable() > 0) ? ($page - 1) : false;
$json['form'] = $form;
return $this->app->json($json);
}
/**
* Get a preview answer train
*
* @param Request $request
* @return Response
*/
public function queryAnswerTrain(Request $request)
{
if (null === $optionsSerial = $request->get('options_serial')) {
$this->app->abort(400, 'Search engine options are missing');
}
try {
$options = SearchEngineOptions::hydrate($this->app, $optionsSerial);
} catch (\Exception $e) {
$this->app->abort(400, 'Provided search engine options are not valid');
}
$pos = (int) $request->request->get('pos', 0);
$query = $request->request->get('query', '');
$record = new \record_preview($this->app, 'RESULT', $pos, '', $this->getSearchEngine(), $query, $options);
$index = ($pos - 3) < 0 ? 0 : ($pos - 3);
return $this->app->json([
'current' => $this->render('prod/preview/result_train.html.twig', [
'records' => $record->get_train(),
'index' => $index,
'selected' => $pos,
])
]);
}
/**
* @return DisplaySettingService
*/
private function getSettings()
{
return $this->app['settings'];
}
/**
* @return mixed
*/
private function getUserManipulator()
{
return $this->app['manipulator.user'];
}
}