Skip to content

Commit

Permalink
[Grid] [DataObject] Show system grid data (#457)
Browse files Browse the repository at this point in the history
* Initial.

* Fix translation.

* Apply php-cs-fixer changes

* Fix Typo.

* Apply php-cs-fixer changes

* Add return value.

---------

Co-authored-by: martineiber <[email protected]>
  • Loading branch information
martineiber and martineiber authored Oct 2, 2024
1 parent c4160ba commit 352d086
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/DataIndex/AssetSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Text;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Unknown;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Type\Video;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultItemInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultPaginationInterface;

final readonly class AssetSearchResult
final readonly class AssetSearchResult implements SearchResultItemInterface, SearchResultPaginationInterface
{
/**
* @param array<int, Asset> $items
Expand Down
5 changes: 4 additions & 1 deletion src/DataIndex/DataObjectSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex;

final readonly class DataObjectSearchResult
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultItemInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultPaginationInterface;

final readonly class DataObjectSearchResult implements SearchResultItemInterface, SearchResultPaginationInterface
{
/**
* @param array $items
Expand Down
26 changes: 23 additions & 3 deletions src/DataIndex/Grid/GridSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\OpenSearchFilterInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
Expand All @@ -32,31 +34,49 @@
*/
final readonly class GridSearch implements GridSearchInterface
{
private OpenSearchFilterInterface $filterService;

public function __construct(
private FilterServiceProviderInterface $filterServiceProvider,
private AssetSearchServiceInterface $assetSearchService,
private DataObjectSearchServiceInterface $dataObjectSearchService,
private AssetServiceInterface $assetService
) {
$this->filterService = $this->filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE);
}

/**
* @throws NotFoundException|SearchException|InvalidArgumentException
*/
public function searchAssets(GridParameter $gridParameter): AssetSearchResult
{
/** @var OpenSearchFilterInterface $filterService */
$filterService = $this->filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE);
$filter = $gridParameter->getFilters();

$asset = $this->assetService->getAssetFolder($gridParameter->getFolderId());

$filter->setPath($asset->getFullPath());

$assetQuery = $filterService->applyFilters(
$assetQuery = $this->filterService->applyFilters(
$filter,
ElementTypes::TYPE_ASSET
);

return $this->assetSearchService->searchAssets($assetQuery);
}

public function searchDataObjects(GridParameter $gridParameter): DataObjectSearchResult
{
$filter = $gridParameter->getFilters();

$folder = $this->dataObjectSearchService->getDataObjectById($gridParameter->getFolderId());

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

$query = $this->filterService->applyFilters(
$filter,
ElementTypes::TYPE_DATA_OBJECT
);

return $this->dataObjectSearchService->searchDataObjects($query);
}
}
3 changes: 3 additions & 0 deletions src/DataIndex/Grid/GridSearchInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Grid;

use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchResult;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\GridParameter;
Expand All @@ -30,4 +31,6 @@ interface GridSearchInterface
* @throws NotFoundException|SearchException
*/
public function searchAssets(GridParameter $gridParameter): AssetSearchResult;

public function searchDataObjects(GridParameter $gridParameter): DataObjectSearchResult;
}
27 changes: 27 additions & 0 deletions src/DataIndex/SearchResult/SearchResultItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult;

/**
* @internal
*/
interface SearchResultItemInterface
{
public function getTotalItems(): int;

public function getItems(): array;
}
27 changes: 27 additions & 0 deletions src/DataIndex/SearchResult/SearchResultPaginationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult;

/**
* @internal
*/
interface SearchResultPaginationInterface
{
public function getCurrentPage(): int;

public function getPageSize(): int;
}
78 changes: 78 additions & 0 deletions src/DataObject/Controller/Grid/GetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Controller\Grid;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Attribute\Property\GridCollection;
use Pimcore\Bundle\StudioBackendBundle\Grid\Attribute\Request\GridRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\GridParameter;
use Pimcore\Bundle\StudioBackendBundle\Grid\Service\GridServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\CollectionJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class GetController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly GridServiceInterface $gridService,
) {
parent::__construct($serializer);
}

/**
* @throws InvalidArgumentException
*/
#[Route('/data-objects/grid', name: 'pimcore_studio_api_get_data_object_grid', methods: ['POST'])]
#[IsGranted(UserPermissions::ASSETS->value)]
#[Post(
path: self::API_PATH . '/data-objects/grid',
operationId: 'data_object_get_grid',
description: 'data_object_get_grid_description',
summary: 'data_object_get_grid_summary',
tags: [Tags::DataObjectsGrid->value]
)]
#[GridRequestBody]
#[SuccessResponse(
description: 'data_object_get_grid_success_response',
content: new CollectionJson(
collection: new GridCollection()
)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::BAD_REQUEST,
])]
public function getDataObjectGrid(#[MapRequestPayload] GridParameter $gridParameter): JsonResponse
{
return $this->jsonResponse($this->gridService->getDataObjectGrid($gridParameter));
}
}
5 changes: 5 additions & 0 deletions src/DataObject/Schema/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ public function getIndex(): int
{
return $this->index;
}

public function getFilename(): string
{
return $this->key;
}
}
48 changes: 32 additions & 16 deletions src/Grid/Service/GridService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Exception;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Grid\GridSearchInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultItemInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnCollectorInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnDefinitionInterface;
Expand Down Expand Up @@ -73,25 +74,15 @@ public function __construct(
public function getAssetGrid(GridParameter $gridParameter): Collection
{
$result = $this->gridSearch->searchAssets($gridParameter);
$items = $result->getItems();

if (empty($items)) {
return new Collection(totalItems: 0, items: []);
}
return $this->getCollectionFromSearchResult($result, $gridParameter);
}

$data = [];
foreach ($items as $item) {
$data[] = $this->getGridDataForElement(
$this->getConfigurationFromArray($gridParameter->getColumns()),
$item,
ElementTypes::TYPE_ASSET
);
}
public function getDataObjectGrid(GridParameter $gridParameter): Collection
{
$result = $this->gridSearch->searchDataObjects($gridParameter);

return new Collection(
totalItems: $result->getTotalItems(),
items: $data
);
return $this->getCollectionFromSearchResult($result, $gridParameter);
}

/**
Expand Down Expand Up @@ -249,4 +240,29 @@ private function isExportable(string $type): bool

return $this->getColumnDefinitions()[$type]->isExportable();
}

private function getCollectionFromSearchResult(
SearchResultItemInterface $searchResultItem,
GridParameter $gridParameter
): Collection {
$items = $searchResultItem->getItems();

if (empty($items)) {
return new Collection(totalItems: 0, items: []);
}

$data = [];
foreach ($items as $item) {
$data[] = $this->getGridDataForElement(
$this->getConfigurationFromArray($gridParameter->getColumns()),
$item,
ElementTypes::TYPE_ASSET
);
}

return new Collection(
totalItems: $searchResultItem->getTotalItems(),
items: $data
);
}
}
2 changes: 2 additions & 0 deletions src/Grid/Service/GridServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public function getConfigurationFromArray(array $config, bool $isExport = false)

public function getAssetGrid(GridParameter $gridParameter): Collection;

public function getDataObjectGrid(GridParameter $gridParameter): Collection;

public function getColumnKeys(ColumnCollection $columnCollection, bool $withGroup = false): array;

/**
Expand Down
14 changes: 12 additions & 2 deletions translations/studio_api_docs.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ system_settings_get_success_response: System settings data
system_settings_get_summary: Get system settings
tag_asset_thumbnail_description: List thumbnails for assets like videos and images
tag_assets_description: Asset operations to get/update/create/delete assets
data: Asset Grid operations
tag_asset_grid_description: Asset Grid operations
tag_assign_to_element_description: |
Assign a specific tag based on the given <strong>{tagId}</strong> to an element based on <strong>{elementType}</strong> and <strong>{id}</strong>. <br>
The <strong>{id}</strong> must be an ID of an existing element of the provided <strong>{elementType}</strong>.
Expand Down Expand Up @@ -555,4 +555,14 @@ user_get_collection_success_response: List of users
data_object_get_available_grid_columns_description: |
Get all available grid columns for data objects
data_object_get_available_grid_columns_summary: Get all available grid columns for data objects
data_object_get_available_grid_columns_success_response: List of available grid columns for data objects
data_object_get_available_grid_columns_success_response: List of available grid columns for data objects
data_object_get_grid_description: |
Retrieve data objects data for grid. <br>
You can use different query parameters to filter the data objects data: <br>
<ul>
<li><strong>folderId</strong>: ID of parent folder</li>
<li><strong>columns</strong>: To see the full description of request fields see the schema <strong>Grid Column Request</strong></li>
<li><strong>filters</strong>: To see the full description of request fields see the schema <strong>Grid Filter</strong></li>
</ul>
data_object_get_grid_success_response: Data object grid data
data_object_get_grid_summary: Get data object data for grid

0 comments on commit 352d086

Please sign in to comment.