Skip to content

Commit d86c0e9

Browse files
authored
[Document] Add Search (#1334)
* Add Search for Documents * Apply php-cs-fixer changes * Fix Transaltion * Add Translation * Fix Style * Apply php-cs-fixer changes --------- Co-authored-by: martineiber <11687066+martineiber@users.noreply.github.com>
1 parent 01e4634 commit d86c0e9

11 files changed

Lines changed: 174 additions & 4 deletions

File tree

src/DataIndex/Grid/GridSearch.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\AssetFolder;
1717
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
1818
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchResult;
19+
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DocumentSearchResult;
1920
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQueryInterface;
2021
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\DataObjectQueryInterface;
22+
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\DocumentQueryInterface;
2123
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchIndexFilterInterface;
2224
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Service\AssetSearchServiceInterface;
2325
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Service\DataObjectSearchServiceInterface;
26+
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Service\DocumentSearchServiceInterface;
2427
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\Type\DataObjectFolder;
2528
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
2629
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
@@ -44,6 +47,7 @@ public function __construct(
4447
private FilterServiceProviderInterface $filterServiceProvider,
4548
private AssetSearchServiceInterface $assetSearchService,
4649
private DataObjectSearchServiceInterface $dataObjectSearchService,
50+
private DocumentSearchServiceInterface $documentSearchService,
4751
private SecurityServiceInterface $securityService
4852
) {
4953
$this->filterService = $this->filterServiceProvider->create(SearchIndexFilterInterface::SERVICE_TYPE);
@@ -79,11 +83,20 @@ public function searchDataObjects(GridParameter $gridParameter): DataObjectSearc
7983
);
8084
}
8185

86+
public function searchDocuments(GridParameter $gridParameter): DocumentSearchResult
87+
{
88+
return $this->searchElementsForUser(
89+
ElementTypes::TYPE_DOCUMENT,
90+
$gridParameter,
91+
$this->securityService->getCurrentUser()
92+
);
93+
}
94+
8295
public function searchElementsForUser(
8396
string $type,
8497
GridParameter $gridParameter,
8598
UserInterface $user
86-
): AssetSearchResult|DataObjectSearchResult {
99+
): AssetSearchResult|DataObjectSearchResult|DocumentSearchResult {
87100
$filter = $gridParameter->getFilters();
88101
$type = $this->getStudioElementType($type);
89102

@@ -96,6 +109,10 @@ public function searchElementsForUser(
96109
$gridParameter->getFolderId(),
97110
$user
98111
),
112+
ElementTypes::TYPE_DOCUMENT => $this->documentSearchService->getDocumentById(
113+
$gridParameter->getFolderId(),
114+
$user
115+
),
99116
default => throw new InvalidElementTypeException($type)
100117
};
101118

@@ -105,7 +122,7 @@ public function searchElementsForUser(
105122

106123
$filter->setPath($folder->getFullPath());
107124

108-
/** @var AssetQueryInterface|DataObjectQueryInterface $query */
125+
/** @var AssetQueryInterface|DataObjectQueryInterface|DocumentQueryInterface $query */
109126
$query = $this->filterService->applyFilters(
110127
$filter,
111128
$type
@@ -116,6 +133,7 @@ public function searchElementsForUser(
116133
return match($type) {
117134
ElementTypes::TYPE_ASSET => $this->assetSearchService->searchAssets($query),
118135
ElementTypes::TYPE_DATA_OBJECT => $this->dataObjectSearchService->searchDataObjects($query),
136+
ElementTypes::TYPE_DOCUMENT => $this->documentSearchService->searchDocuments($query),
119137
default => throw new InvalidElementTypeException($type)
120138
};
121139
}
@@ -130,6 +148,11 @@ private function isFolderOfType(string $type, StudioElementInterface $element):
130148
return true;
131149
}
132150

151+
// Allow all documents as folder sinc they can all have parent items.
152+
if ($type === ElementTypes::TYPE_DOCUMENT) {
153+
return true;
154+
}
155+
133156
return false;
134157
}
135158

src/DataIndex/Grid/GridSearchInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
1717
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchResult;
18+
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DocumentSearchResult;
1819
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
1920
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
2021
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\GridParameter;
@@ -34,9 +35,11 @@ public function searchAssetsForUser(GridParameter $gridParameter, UserInterface
3435

3536
public function searchDataObjects(GridParameter $gridParameter): DataObjectSearchResult;
3637

38+
public function searchDocuments(GridParameter $gridParameter): DocumentSearchResult;
39+
3740
public function searchElementsForUser(
3841
string $type,
3942
GridParameter $gridParameter,
4043
UserInterface $user
41-
): AssetSearchResult|DataObjectSearchResult;
44+
): AssetSearchResult|DataObjectSearchResult|DocumentSearchResult;
4245
}

src/DataObject/Schema/DataObject.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ public function isPublished(): bool
134134
return $this->published;
135135
}
136136

137+
public function getPublished(): bool
138+
{
139+
return $this->isPublished();
140+
}
141+
137142
public function getHasChildren(): bool
138143
{
139144
return $this->hasChildren;

src/Document/Schema/Document.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public function getFullPath(): string
9494
return $this->fullPath;
9595
}
9696

97+
public function getPublished(): bool
98+
{
99+
return $this->isPublished();
100+
}
101+
97102
public function isPublished(): bool
98103
{
99104
return $this->published;

src/Grid/Column/Resolver/System/BooleanResolver.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515

1616
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnResolverInterface;
1717
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\CoreElementColumnResolverInterface;
18+
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\StudioElementColumnResolverInterface;
1819
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\Column;
1920
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\ColumnData;
2021
use Pimcore\Bundle\StudioBackendBundle\Grid\Util\Trait\ColumnDataTrait;
2122
use Pimcore\Bundle\StudioBackendBundle\Grid\Util\Trait\SimpleGetterTrait;
23+
use Pimcore\Bundle\StudioBackendBundle\Response\StudioElementInterface;
2224
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
2325
use Pimcore\Model\Element\ElementInterface;
2426

2527
/**
2628
* @internal
2729
*/
28-
final class BooleanResolver implements ColumnResolverInterface, CoreElementColumnResolverInterface
30+
final class BooleanResolver implements
31+
ColumnResolverInterface,
32+
CoreElementColumnResolverInterface,
33+
StudioElementColumnResolverInterface
2934
{
3035
use SimpleGetterTrait;
3136
use ColumnDataTrait;
@@ -39,6 +44,15 @@ public function resolveForCoreElement(Column $column, ElementInterface $element)
3944
);
4045
}
4146

47+
public function resolveForStudioElement(Column $column, StudioElementInterface $element): ColumnData
48+
{
49+
return $this->getColumnData(
50+
$column,
51+
$this->getValue($column, $element),
52+
$this->getType()
53+
);
54+
}
55+
4256
public function getType(): string
4357
{
4458
return 'system.boolean';
@@ -48,6 +62,7 @@ public function supportedElementTypes(): array
4862
{
4963
return [
5064
ElementTypes::TYPE_OBJECT,
65+
ElementTypes::TYPE_DOCUMENT,
5166
];
5267
}
5368
}

src/Grid/Service/GridSearchService.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,21 @@ public function getDataObjectSearchGrid(SearchGridParameter $searchParameter, ?s
5959

6060
return $this->gridService->getDataObjectGrid($gridParameter, $classId);
6161
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function getDocumentSearchGrid(SearchGridParameter $searchParameter): Collection
67+
{
68+
$filter = $searchParameter->getFilters();
69+
$filter->setExcludeFolders(false);
70+
71+
$gridParameter = new GridParameter(
72+
folderId: 1,
73+
columns: $searchParameter->getColumns(),
74+
filters: $filter
75+
);
76+
77+
return $this->gridService->getDocumentGrid($gridParameter);
78+
}
6279
}

src/Grid/Service/GridSearchServiceInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public function getAssetSearchGrid(SearchGridParameter $gridParameter): Collecti
3535
* @throws Exception
3636
*/
3737
public function getDataObjectSearchGrid(SearchGridParameter $searchParameter, ?string $classId): Collection;
38+
39+
/**
40+
* @throws InvalidArgumentException
41+
*/
42+
public function getDocumentSearchGrid(SearchGridParameter $searchParameter): Collection;
3843
}

src/Grid/Service/GridService.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ public function getAssetGrid(GridParameter $gridParameter): Collection
9090
);
9191
}
9292

93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function getDocumentGrid(GridParameter $gridParameter): Collection
97+
{
98+
$result = $this->gridSearch->searchDocuments($gridParameter);
99+
100+
return $this->getCollectionFromSearchResult(
101+
$result,
102+
$gridParameter,
103+
ElementTypes::TYPE_DOCUMENT
104+
);
105+
}
106+
93107
/**
94108
* @throws NotFoundException
95109
* @throws Exception

src/Grid/Service/GridServiceInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function getDataObjectGrid(
7878
?string $classId
7979
): Collection;
8080

81+
/**
82+
* @throws InvalidArgumentException
83+
*/
84+
public function getDocumentGrid(GridParameter $gridParameter): Collection;
85+
8186
/**
8287
* @throws Exception
8388
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* This source file is available under the terms of the
6+
* Pimcore Open Core License (POCL)
7+
* Full copyright and license information is available in
8+
* LICENSE.md which is distributed with this source code.
9+
*
10+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
11+
* @license Pimcore Open Core License (POCL)
12+
*/
13+
14+
namespace Pimcore\Bundle\StudioBackendBundle\Search\Controller\Document;
15+
16+
use OpenApi\Attributes\Post;
17+
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
18+
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
19+
use Pimcore\Bundle\StudioBackendBundle\Grid\Attribute\Property\GridCollection;
20+
use Pimcore\Bundle\StudioBackendBundle\Grid\Attribute\Request\SearchGridRequestBody;
21+
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\SearchGridParameter;
22+
use Pimcore\Bundle\StudioBackendBundle\Grid\Service\GridSearchServiceInterface;
23+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\CollectionJson;
24+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
25+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
26+
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
27+
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
28+
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
29+
use Symfony\Component\HttpFoundation\JsonResponse;
30+
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
31+
use Symfony\Component\Routing\Attribute\Route;
32+
use Symfony\Component\Security\Http\Attribute\IsGranted;
33+
use Symfony\Component\Serializer\SerializerInterface;
34+
35+
/**
36+
* @internal
37+
*/
38+
final class GetSearchResultController extends AbstractApiController
39+
{
40+
public function __construct(
41+
SerializerInterface $serializer,
42+
private readonly GridSearchServiceInterface $searchService
43+
) {
44+
parent::__construct($serializer);
45+
}
46+
47+
/**
48+
* @throws InvalidArgumentException
49+
*/
50+
#[Route('/search/documents', name: 'pimcore_studio_api_get_document_search', methods: ['POST'])]
51+
#[IsGranted(UserPermissions::DOCUMENTS->value)]
52+
#[Post(
53+
path: self::PREFIX . '/search/documents',
54+
operationId: 'document_get_search',
55+
description: 'document_get_search_description',
56+
summary: 'document_get_search_summary',
57+
tags: [Tags::Search->value]
58+
)]
59+
#[SearchGridRequestBody]
60+
#[SuccessResponse(
61+
description: 'dcocument_get_search_success_response',
62+
content: new CollectionJson(
63+
collection: new GridCollection()
64+
)
65+
)]
66+
#[DefaultResponses([
67+
HttpResponseCodes::UNAUTHORIZED,
68+
HttpResponseCodes::NOT_FOUND,
69+
HttpResponseCodes::BAD_REQUEST,
70+
])]
71+
public function getDocumentSearchGrid(#[MapRequestPayload] SearchGridParameter $searchGridParameter): JsonResponse
72+
{
73+
return $this->jsonResponse($this->searchService->getDocumentSearchGrid($searchGridParameter));
74+
}
75+
}

0 commit comments

Comments
 (0)