request->get('fake_qry'); $selStart = (int) $request->request->get('_selectionStart'); $selEnd = (int) $request->request->get('_selectionEnd'); // move the selection back to find the begining of the "word" for(;;) { $c = ''; if($selStart>0) { $c = mb_substr($query, $selStart-1, 1); } if(in_array($c, ['', ' ', '"'])) { break; } $selStart--; } // move the selection up to find the end of the "word" for(;;) { $c = mb_substr($query, $selEnd, 1); if(in_array($c, ['', ' ', '"'])) { break; } $selEnd++; } $before = mb_substr($query, 0, $selStart); $word = mb_substr($query, $selStart, $selEnd-$selStart); $after = mb_substr($query, $selEnd); // since the query comes from a submited form, normalize crlf,cr,lf ... $word = StringHelper::crlfNormalize($word); $options = SearchEngineOptions::fromRequest($this->app, $request); $search_engine_structure = GlobalStructure::createFromDataboxes( $this->app->getDataboxes(), Structure::WITH_EVERYTHING & ~(Structure::STRUCTURE_WITH_FLAGS | Structure::FIELD_WITH_FACETS | Structure::FIELD_WITH_THESAURUS) ); $query_context_factory = new QueryContextFactory( $search_engine_structure, array_keys($this->app['locales.available']), $this->app['locale'] ); $engine = new ElasticSearchEngine( $this->app, $search_engine_structure, $this->app['elasticsearch.client'], $query_context_factory, $this->app['elasticsearch.facets_response.factory'], $this->app['elasticsearch.options'] ); $autocomplete = $engine->autocomplete($word, $options); $completions = []; foreach($autocomplete['text'] as $text) { $completions[] = [ 'label' => $text, 'value' => [ 'before' => $before, 'word' => $word, 'after' => $after, 'completion' => $text, 'completed' => $before . $text . $after ] ]; } foreach($autocomplete['byField'] as $fieldName=>$values) { foreach($values as $value) { $completions[] = [ 'label' => $value['query'], 'value' => [ 'before' => $before, 'word' => $word, 'after' => $after, 'completion' => $value['query'], 'completed' => $before . $value['query'] . $after ] ]; } } return $this->app->json($completions); } /** * Query Phraseanet to fetch records * * @param Request $request * @return Response */ public function query(Request $request) { $query = (string) $request->request->get('qry'); // since the query comes from a submited form, normalize crlf,cr,lf ... $query = StringHelper::crlfNormalize($query); $json = [ 'query' => $query ]; $options = SearchEngineOptions::fromRequest($this->app, $request); $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; } $options->setFirstResult(($page - 1) * $perPage); $options->setMaxResults($perPage); $user = $this->getAuthenticatedUser(); $userManipulator = $this->getUserManipulator(); $userManipulator->logQuery($user, $query); try { $result = $engine->query($query, $options); if ($this->getSettings()->getUserSetting($user, 'start_page') === 'LAST_QUERY') { $userManipulator->setUserSetting($user, 'start_page_query', $query); } // log array of collectionIds (from $options) for each databox $collectionsReferencesByDatabox = $options->getCollectionsReferencesByDatabox(); foreach ($collectionsReferencesByDatabox as $sbid => $references) { $databox = $this->findDataboxById($sbid); $collectionsIds = array_map(function(CollectionReference $ref){return $ref->getCollectionId();}, $references); $this->getSearchEngineLogger()->log($databox, $result->getQueryText(), $result->getTotal(), $collectionsIds); } $proposals = $firstPage ? $result->getProposals() : false; $npages = $result->getTotalPages($perPage); $page = $result->getCurrentPage($perPage); $queryESLib = $result->getQueryESLib(); $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 = $this->render( "prod/results/infos.html.twig", [ 'results'=> $result, 'esquery' => $this->getAclForUser()->is_admin() ? json_encode($queryESLib['body'], JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES) : null ] ); $infoResult = '