diff --git a/src/AjaxControllers/AjaxSearchController.php b/src/AjaxControllers/AjaxSearchController.php new file mode 100644 index 00000000..69695faa --- /dev/null +++ b/src/AjaxControllers/AjaxSearchController.php @@ -0,0 +1,91 @@ +denyAccessUnlessGranted(NodeVoter::SEARCH); + + if (!$request->query->has('searchTerms') || '' == $request->query->get('searchTerms')) { + throw new BadRequestHttpException('searchTerms parameter is missing.'); + } + + $searchHandler = new GlobalNodeSourceSearchHandler($this->em()); + $searchHandler->setDisplayNonPublishedNodes(true); + + /** @var array $nodesSources */ + $nodesSources = $searchHandler->getNodeSourcesBySearchTerm( + $request->get('searchTerms'), + self::RESULT_COUNT + ); + + if (0 === count($nodesSources)) { + return new JsonResponse([ + 'statusCode' => Response::HTTP_OK, + 'status' => 'success', + 'data' => [], + 'responseText' => 'No results found.', + ]); + } + + $data = []; + + foreach ($nodesSources as $source) { + $uniqueKey = null; + if ($source instanceof NodesSources) { + $uniqueKey = 'n_' . $source->getNode()->getId(); + if (!$this->security->isGranted(NodeVoter::READ, $source)) { + continue; + } + } elseif ($source instanceof PersistableInterface) { + $uniqueKey = 'p_' . $source->getId(); + } + if (key_exists($uniqueKey, $data)) { + continue; + } + + $data[$uniqueKey] = $this->explorerItemFactory->createForEntity($source)->toArray(); + } + + $data = array_values($data); + + return $this->createSerializedResponse([ + 'status' => 'confirm', + 'statusCode' => 200, + 'data' => $data, + 'count' => count($data), + ]); + } +} diff --git a/src/AjaxControllers/AjaxSearchNodesSourcesController.php b/src/AjaxControllers/AjaxSearchNodesSourcesController.php deleted file mode 100644 index 7441437a..00000000 --- a/src/AjaxControllers/AjaxSearchNodesSourcesController.php +++ /dev/null @@ -1,128 +0,0 @@ -denyAccessUnlessGranted(NodeVoter::SEARCH); - - if (!$request->query->has('searchTerms') || '' == $request->query->get('searchTerms')) { - throw new BadRequestHttpException('searchTerms parameter is missing.'); - } - - $searchHandler = new GlobalNodeSourceSearchHandler($this->em()); - $searchHandler->setDisplayNonPublishedNodes(true); - - /** @var array $nodesSources */ - $nodesSources = $searchHandler->getNodeSourcesBySearchTerm( - $request->get('searchTerms'), - self::RESULT_COUNT - ); - - if (0 === count($nodesSources)) { - return new JsonResponse([ - 'statusCode' => Response::HTTP_OK, - 'status' => 'success', - 'data' => [], - 'responseText' => 'No results found.', - ]); - } - - $responseArray = [ - 'statusCode' => Response::HTTP_OK, - 'status' => 'success', - 'data' => [], - 'responseText' => count($nodesSources).' results found.', - ]; - - foreach ($nodesSources as $source) { - if ( - $source instanceof NodesSources - && $this->security->isGranted(NodeVoter::READ, $source) - && !key_exists($source->getNode()->getId(), $responseArray['data']) - ) { - $responseArray['data'][$source->getNode()->getId()] = $this->getNodeSourceData($source); - } - } - /* - * Only display one nodeSource - */ - $responseArray['data'] = array_values($responseArray['data']); - - return $this->createSerializedResponse( - $responseArray - ); - } - - protected function getNodeSourceData(NodesSources $source): array - { - $thumbnail = null; - /** @var Translation $translation */ - $translation = $source->getTranslation(); - $displayableNSDoc = $source->getDocumentsByFields()->filter(function (NodesSourcesDocuments $nsDoc) { - $doc = $nsDoc->getDocument(); - - return !$doc->isPrivate() && ($doc->isImage() || $doc->isSvg()); - })->first(); - if (false !== $displayableNSDoc) { - $thumbnail = $displayableNSDoc->getDocument(); - $this->documentUrlGenerator->setDocument($thumbnail); - $this->documentUrlGenerator->setOptions([ - 'fit' => '60x60', - 'quality' => 80, - ]); - } - - return [ - 'title' => $source->getTitle() ?? $source->getNode()->getNodeName(), - 'parent' => $source->getParent() ? - $source->getParent()->getTitle() ?? $source->getParent()->getNode()->getNodeName() : - null, - 'thumbnail' => $thumbnail ? $this->documentUrlGenerator->getUrl() : null, - 'nodeId' => $source->getNode()->getId(), - 'translationId' => $translation->getId(), - 'typeName' => $source->getNode()->getNodeType()->getLabel(), - 'typeColor' => $source->getNode()->getNodeType()->getColor(), - 'url' => $this->generateUrl( - 'nodesEditSourcePage', - [ - 'nodeId' => $source->getNode()->getId(), - 'translationId' => $translation->getId(), - ] - ), - ]; - } -} diff --git a/src/Resources/app/api/NodesSourceSearchApi.js b/src/Resources/app/api/NodesSourceSearchApi.js index 65e57004..734a3e87 100644 --- a/src/Resources/app/api/NodesSourceSearchApi.js +++ b/src/Resources/app/api/NodesSourceSearchApi.js @@ -15,7 +15,7 @@ export function getNodesSourceFromSearch(searchTerms) { return request({ method: 'GET', - url: window.RozierRoot.routes.searchNodesSourcesAjax, + url: window.RozierRoot.routes.searchAjax, params: postData, }) .then((response) => { diff --git a/src/Resources/views/base.html.twig b/src/Resources/views/base.html.twig index 39d732c5..24d86def 100644 --- a/src/Resources/views/base.html.twig +++ b/src/Resources/views/base.html.twig @@ -203,7 +203,7 @@ 'providerAjaxExplorer' : '{{ path('providerAjaxExplorerPage') }}', 'providerAjaxByArray' : '{{ path('providerAjaxByArray') }}', 'customFormsAjaxExplorer' : '{{ path('customFormsAjaxExplorerPage') }}', - 'searchNodesSourcesAjax': '{{ path('searchNodesSourcesAjax') }}', + 'searchAjax': '{{ path('searchAjax') }}', 'nodesStatusesAjax' : '{{ path('nodesStatusesAjax') }}', 'nodesTreeAjax' : '{{ path('nodesTreeAjax') }}', 'tagsTreeAjax' : '{{ path('tagsTreeAjax') }}', diff --git a/src/Resources/views/partials/css-inject.html.twig b/src/Resources/views/partials/css-inject.html.twig index dc4f1a24..58ce0a36 100644 --- a/src/Resources/views/partials/css-inject.html.twig +++ b/src/Resources/views/partials/css-inject.html.twig @@ -1,7 +1,7 @@ - + diff --git a/src/Resources/views/partials/js-inject.html.twig b/src/Resources/views/partials/js-inject.html.twig index b08b8011..14bdaed9 100644 --- a/src/Resources/views/partials/js-inject.html.twig +++ b/src/Resources/views/partials/js-inject.html.twig @@ -1,5 +1,5 @@ - + - + diff --git a/src/Resources/views/partials/simple-js-inject.html.twig b/src/Resources/views/partials/simple-js-inject.html.twig index f5de5f8e..a65ceeee 100644 --- a/src/Resources/views/partials/simple-js-inject.html.twig +++ b/src/Resources/views/partials/simple-js-inject.html.twig @@ -1,3 +1,3 @@ - + diff --git a/src/Resources/views/widgets/nodesSourcesSearch.html.twig b/src/Resources/views/widgets/nodesSourcesSearch.html.twig index 407d0157..2952a08b 100644 --- a/src/Resources/views/widgets/nodesSourcesSearch.html.twig +++ b/src/Resources/views/widgets/nodesSourcesSearch.html.twig @@ -2,7 +2,7 @@ {{isActive}}