Skip to content

Commit

Permalink
Initial.
Browse files Browse the repository at this point in the history
  • Loading branch information
martineiber committed Oct 2, 2024
1 parent 747fd44 commit 89c5fb0
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 22 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\SearchResultIPaginationInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultItemInterface;

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

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex;

final readonly class DataObjectSearchResult

use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultIPaginationInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult\SearchResultItemInterface;

final readonly class DataObjectSearchResult implements SearchResultItemInterface, SearchResultIPaginationInterface
{
/**
* @param array $items
Expand Down
29 changes: 26 additions & 3 deletions src/DataIndex/Grid/GridSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
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\DataObject\Service\DataObjectServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
Expand All @@ -32,31 +35,51 @@
*/
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;
}
25 changes: 25 additions & 0 deletions src/DataIndex/SearchResult/SearchResultIPaginationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/


namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult;

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

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

/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/


namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchResult;

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

public function getItems(): array;
}
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()
{
return $this->key;
}
}
50 changes: 34 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,27 +74,18 @@ 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);
}


/**
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -249,4 +241,30 @@ 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
12 changes: 11 additions & 1 deletion translations/studio_api_docs.en.yaml
Original file line number Diff line number Diff line change
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 89c5fb0

Please sign in to comment.