Skip to content

Commit

Permalink
fix: Fixed back-office quick search using ExplorerItemFactory to supp…
Browse files Browse the repository at this point in the history
…ort other data than NodesSources
  • Loading branch information
ambroisemaupate committed Dec 11, 2024
1 parent 6ebbb9a commit 26f540c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 143 deletions.
6 changes: 3 additions & 3 deletions lib/RoadizRozierBundle/config/routing/ajax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ nodeAjaxEdit:
controller: Themes\Rozier\AjaxControllers\AjaxNodesController::editAction
format: json
requirements: { nodeId : "[0-9]+" }
searchNodesSourcesAjax:
path: /nodes-sources/search
searchAjax:
path: /search
methods: [GET]
defaults:
_controller: Themes\Rozier\AjaxControllers\AjaxSearchNodesSourcesController::searchAction
_controller: Themes\Rozier\AjaxControllers\AjaxSearchController::searchAction
_format: json
nodesStatusesAjax:
path: /nodes/statuses
Expand Down
91 changes: 91 additions & 0 deletions lib/Rozier/src/AjaxControllers/AjaxSearchController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Themes\Rozier\AjaxControllers;

use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
use RZ\Roadiz\CoreBundle\Explorer\ExplorerItemFactoryInterface;
use RZ\Roadiz\CoreBundle\SearchEngine\GlobalNodeSourceSearchHandler;
use RZ\Roadiz\CoreBundle\Security\Authorization\Voter\NodeVoter;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Serializer\SerializerInterface;

final class AjaxSearchController extends AbstractAjaxController
{
public const RESULT_COUNT = 10;

public function __construct(
private readonly Security $security,
private readonly ExplorerItemFactoryInterface $explorerItemFactory,
SerializerInterface $serializer,
) {
parent::__construct($serializer);
}

/**
* Handle AJAX edition requests for Node
* such as coming from node-tree widgets.
*
* @return Response JSON response
*/
public function searchAction(Request $request): Response
{
$this->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),
]);
}
}
128 changes: 0 additions & 128 deletions lib/Rozier/src/AjaxControllers/AjaxSearchNodesSourcesController.php

This file was deleted.

2 changes: 1 addition & 1 deletion lib/Rozier/src/Resources/app/api/NodesSourceSearchApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/Rozier/src/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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') }}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{isActive}}
<div id="nodes-sources-search" v-bind:class="{ 'focus-on': isFocus }">
<form id="nodes-sources-search-form"
v-on:submit.prevent action="{{ path('searchNodesSourcesAjax') }}"
v-on:submit.prevent action="{{ path('searchAjax') }}"
method="GET"
class="uk-form">
<div class="uk-form-icon">
Expand All @@ -24,21 +24,20 @@
<transition name="fade">
<ul id="nodes-sources-search-results" v-if="isFocus" v-cloak>
<li v-for="item in items">
<ajax-link class="nodes-sources-search-results-item" :href="item.url" :title="item.title" :type-color="item.typeColor">
<ajax-link class="nodes-sources-search-results-item" :href="item.editItem" :title="item.displayable" :type-color="item.color">
<span class="image-container">
<picture v-if="item.thumbnail">
<source v-if="!item.thumbnail.endsWith('svg') && !item.thumbnail.endsWith('webp')"
:srcset="item.thumbnail + '.webp'"
<picture v-if="item.thumbnail && item.thumbnail.processable">
<source v-if="!item.thumbnail.url.endsWith('svg') && !item.thumbnail.url.endsWith('webp')"
:srcset="item.thumbnail.url + '.webp'"
type="image/webp">
<img width="60" height="60" loading="lazy" :src="item.thumbnail">
<img width="60" height="60" loading="lazy" :src="item.thumbnail.url">
</picture>
</span>
<span class="texts">
<span class="texts-header">
<span class="parent">${item.parent}</span>
<span class="type">${item.typeName}</span>
<span class="texts-header" v-if="item.classname">
<span class="parent">${item.classname}</span>
</span>
<span class="title">${item.title}</span>
<span class="title">${item.displayable}</span>
</span>
</ajax-link>
</li>
Expand Down

0 comments on commit 26f540c

Please sign in to comment.