Skip to content

Commit

Permalink
[Grid] [DataObject] List available system columns (#439)
Browse files Browse the repository at this point in the history
* Move Collector for assets to asset Namespace

* Get system colums for data objects.

* add translation.

* Apply php-cs-fixer changes

---------

Co-authored-by: martineiber <martineiber@users.noreply.github.com>
  • Loading branch information
martineiber and martineiber authored Sep 25, 2024
1 parent 675fecc commit 6a1ceb9
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 9 deletions.
7 changes: 5 additions & 2 deletions config/grid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ services:
# Column Collector
#

Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\SystemFieldCollector:
Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\Asset\SystemFieldCollector:
tags: [ 'pimcore.studio_backend.grid_column_collector' ]

Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\MetadataCollector:
Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\DataObject\SystemFieldCollector:
tags: [ 'pimcore.studio_backend.grid_column_collector' ]

Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\Asset\MetadataCollector:
tags: [ 'pimcore.studio_backend.grid_column_collector' ]
95 changes: 95 additions & 0 deletions src/DataObject/Controller/Grid/GetAvailableColumnsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\Get;
use OpenApi\Attributes\Items;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\ColumnConfiguration;
use Pimcore\Bundle\StudioBackendBundle\Grid\Service\ColumnConfigurationServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\StringParameter;
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\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;

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

/**
* @throws NotFoundException|SearchException
*/
#[Route(
'/data-object/grid/available-columns/{className}',
name: 'pimcore_studio_api_get_data_objects_grid_available_columns',
methods: ['GET']
)]
#[IsGranted(UserPermissions::DATA_OBJECTS->value)]
#[Get(
path: self::API_PATH . '/data-object/grid/available-columns/{className}',
operationId: 'data_object_get_available_grid_columns',
description: 'data_object_get_available_grid_columns_description',
summary: 'data_object_get_available_grid_columns_summary',
tags: [Tags::DataObjectsGrid->value]
)]
#[SuccessResponse(
description: 'data_object_get_available_grid_columns_success_response',
content: new JsonContent(
properties: [
new Property(
property: 'columns',
type: 'array',
items: new Items(ref: ColumnConfiguration::class),
)],
)
)]
#[StringParameter(
name: 'className',
example: 'MyClass',
description: 'Itentifies the class name for which the columns should be retrieved.',
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function getDataObjectAvailableGridColumns(string $className): JsonResponse
{
$columns = $this->columnConfigurationService->getAvailableDataObjectColumnConfiguration();

return $this->jsonResponse([
'columns' => $columns,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector;
namespace Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\Asset;

use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnCollectorInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnDefinitionInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector;
namespace Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\Asset;

use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnCollectorInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnDefinitionInterface;
Expand Down
85 changes: 85 additions & 0 deletions src/Grid/Column/Collector/DataObject/SystemFieldCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\Grid\Column\Collector\DataObject;

use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnCollectorInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnDefinitionInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\ColumnConfiguration;
use Pimcore\Bundle\StudioBackendBundle\Grid\Service\SystemColumnServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use function array_key_exists;

/**
* @internal
*/
final class SystemFieldCollector implements ColumnCollectorInterface
{
public function __construct(
private SystemColumnServiceInterface $systemColumnService,
) {
}

public function getCollectorName(): string
{
return 'system';
}

/**
* @param ColumnDefinitionInterface[] $availableColumnDefinitions
*
* @return ColumnConfiguration[]
*/
public function getColumnConfigurations(array $availableColumnDefinitions): array
{
$systemColumns = $this->systemColumnService->getSystemColumnsForDataObjects();
$columns = [];
foreach ($systemColumns as $columnKey => $type) {
$type = $this->concatType($type);
if (!array_key_exists($type, $availableColumnDefinitions)) {
continue;
}

$column = new ColumnConfiguration(
key: $columnKey,
group: $this->getCollectorName(),
sortable: $availableColumnDefinitions[$type]->isSortable(),
editable: false,
localizable: false,
locale: null,
type: $availableColumnDefinitions[$type]->getType(),
frontendType: $availableColumnDefinitions[$type]->getFrontendType(),
config: []
);

$columns[] = $column;
}

return $columns;
}

public function supportedElementTypes(): array
{
return [
ElementTypes::TYPE_DATA_OBJECT,
];
}

private function concatType(string $type): string
{
return $this->getCollectorName() . '.' . $type;
}
}
7 changes: 6 additions & 1 deletion src/Grid/Mapper/ColumnMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use function array_key_exists;
use function sprintf;

/**
* @internal
Expand All @@ -33,12 +34,16 @@
'creationDate' => 'datetime',
'modificationDate' => 'datetime',
'size' => 'fileSize',
'key' => 'string',
'published' => 'boolean',
'classname' => 'string',
'index' => 'integer',
];

public function getType(string $column): string
{
if (!array_key_exists($column, self::COLUMN_MAPPING)) {
throw new InvalidArgumentException('Column not found');
throw new InvalidArgumentException(sprintf('Column "%s" not supported.', $column));
}

return self::COLUMN_MAPPING[$column];
Expand Down
34 changes: 32 additions & 2 deletions src/Grid/Service/ColumnConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,43 @@ public function getAvailableAssetColumnConfiguration(): array
];
}

$this->dispatchEventForAllColumns($columns);

return $columns;
}

public function getAvailableDataObjectColumnConfiguration(): array
{
$columns = [];
foreach ($this->gridService->getColumnCollectors() as $collector) {
// Only collect supported data object collectors
if (!in_array(ElementTypes::TYPE_DATA_OBJECT, $collector->supportedElementTypes(), true)) {
continue;
}

// rather use the spread operator instead of array_merge in a loop
$columns = [
...$columns,
...$collector->getColumnConfigurations($this->gridService->getColumnDefinitions()),
];
}

$this->dispatchEventForAllColumns($columns);

return $columns;

}

/**
* @param ColumnConfiguration[] $columns
*/
private function dispatchEventForAllColumns(array $columns): void
{
foreach ($columns as $column) {
$this->eventDispatcher->dispatch(
new GridColumnConfigurationEvent($column),
GridColumnConfigurationEvent::EVENT_NAME
);
}

return $columns;
}
}
5 changes: 5 additions & 0 deletions src/Grid/Service/ColumnConfigurationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ interface ColumnConfigurationServiceInterface
* @return ColumnConfiguration[]
*/
public function getAvailableAssetColumnConfiguration(): array;

/**
* @return ColumnConfiguration[]
*/
public function getAvailableDataObjectColumnConfiguration(): array;
}
12 changes: 12 additions & 0 deletions src/Grid/Service/SystemColumnService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use Pimcore\Bundle\StudioBackendBundle\Grid\Mapper\ColumnMapperInterface;
use Pimcore\Model\Asset\Service;
use Pimcore\Model\DataObject\Concrete;

/**
* @internal
Expand All @@ -39,4 +40,15 @@ public function getSystemColumnsForAssets(): array

return $columns;
}

public function getSystemColumnsForDataObjects(): array
{
$systemColumns = Concrete::SYSTEM_COLUMN_NAMES;
$columns = [];
foreach ($systemColumns as $column) {
$columns[$column] = $this->columnMapper->getType($column);
}

return $columns;
}
}
2 changes: 2 additions & 0 deletions src/Grid/Service/SystemColumnServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@
interface SystemColumnServiceInterface
{
public function getSystemColumnsForAssets(): array;

public function getSystemColumnsForDataObjects(): array;
}
40 changes: 40 additions & 0 deletions src/OpenApi/Attribute/Parameter/Path/StringParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\OpenApi\Attribute\Parameter\Path;

use Attribute;
use OpenApi\Attributes\PathParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class StringParameter extends PathParameter
{
public function __construct(
string $name,
string $example,
string $description,
bool $required = true,
) {
parent::__construct(
name: $name,
description: $description,
in: 'path',
required: $required,
schema: new Schema(type: 'string', example: $example),
);
}
}
5 changes: 5 additions & 0 deletions src/OpenApi/Config/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
name: Tags::DataObjects->value,
description: 'tag_dataobjects_description'
)]
#[Tag(
name: Tags::DataObjectsGrid->value,
description: 'tag_dataobject_grid_description'
)]
#[Tag(
name: Tags::Dependencies->value,
description: 'tag_dependencies_description'
Expand Down Expand Up @@ -116,6 +120,7 @@ enum Tags: string
case AssetThumbnails = 'Asset Thumbnails';
case Authorization = 'Authorization';
case DataObjects = 'Data Objects';
case DataObjectsGrid = 'Data Object Grid';
case Dependencies = 'Dependencies';
case Elements = 'Elements';
case ExecutionEngine = 'Execution Engine';
Expand Down
Loading

0 comments on commit 6a1ceb9

Please sign in to comment.