Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function initializeListBuilder(ListBuilderInterface $listBuilder, array $fieldDescriptors)
 {
     $listBuilder->limit($this->listRestHelper->getLimit())->setCurrentPage($this->listRestHelper->getPage());
     $listBuilder->setFieldDescriptors($fieldDescriptors);
     $fields = $this->listRestHelper->getFields();
     if ($fields != null) {
         foreach ($fields as $field) {
             if (!array_key_exists($field, $fieldDescriptors)) {
                 continue;
             }
             $listBuilder->addSelectField($fieldDescriptors[$field]);
         }
     } else {
         $listBuilder->setSelectFields($fieldDescriptors);
     }
     $searchFields = $this->listRestHelper->getSearchFields();
     if ($searchFields != null) {
         foreach ($searchFields as $searchField) {
             $listBuilder->addSearchField($fieldDescriptors[$searchField]);
         }
         $listBuilder->search($this->listRestHelper->getSearchPattern());
     }
     $sortBy = $this->listRestHelper->getSortColumn();
     if ($sortBy != null) {
         $listBuilder->sort($fieldDescriptors[$sortBy], $this->listRestHelper->getSortOrder());
     }
 }
Пример #2
0
 /**
  * Perform a search and return a JSON response.
  *
  * @param Request $request
  *
  * @return Response
  */
 public function searchAction(Request $request)
 {
     $queryString = $request->query->get('q');
     $index = $request->query->get('index', null);
     $locale = $request->query->get('locale', null);
     $page = $this->listRestHelper->getPage();
     $limit = $this->listRestHelper->getLimit();
     $startTime = microtime(true);
     $indexes = $index ? [$index] : $this->getAllowedIndexes();
     $query = $this->searchManager->createSearch($queryString);
     if ($locale) {
         $query->locale($locale);
     }
     $query->indexes($indexes);
     $query->setLimit($limit);
     $time = microtime(true) - $startTime;
     $adapter = new ArrayAdapter(iterator_to_array($query->execute()));
     $pager = new Pagerfanta($adapter);
     $pager->setMaxPerPage($limit);
     $pager->setCurrentPage($page);
     $representation = new SearchResultRepresentation(new CollectionRepresentation($pager->getCurrentPageResults(), 'result'), 'sulu_search_search', ['locale' => $locale, 'query' => $query, 'index' => $index], (int) $page, (int) $limit, $pager->getNbPages(), 'page', 'limit', false, $adapter->getNbResults(), $this->getIndexTotals($adapter->getArray()), number_format($time, 8));
     $view = View::create($representation);
     $context = SerializationContext::create();
     $context->enableMaxDepthChecks();
     $context->setSerializeNull(true);
     $view->setSerializationContext($context);
     return $this->viewHandler->handle($view);
 }