Skip to content

Commit

Permalink
[Assets][Tree] Add new attributes to tree endpoint (#266)
Browse files Browse the repository at this point in the history
* add new attributes to tree endpoint

* Apply php-cs-fixer changes

* fix: sonar

* rename the tree action

* update getCustomTreeAttributes based on the feedback

* Apply php-cs-fixer changes

* fix: sonar

---------

Co-authored-by: lukmzig <lukmzig@users.noreply.github.com>
  • Loading branch information
lukmzig and lukmzig authored Jul 24, 2024
1 parent d888df2 commit e1b04df
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Asset/Controller/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
/**
* @throws NotFoundException|SearchException
*/
#[Route('/assets/{id}', name: 'pimcore_studio_api_get_asset', methods: ['GET'])]
#[Route('/assets/{id}', name: 'pimcore_studio_api_get_asset', requirements: ['id' => '\d+'], methods: ['GET'])]
#[IsGranted(UserPermissions::ASSETS->value)]
#[Get(
path: self::API_PATH . '/assets/{id}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/**
* @internal
*/
final class CollectionController extends AbstractApiController
final class TreeController extends AbstractApiController
{
use PaginatedResponseTrait;

Expand All @@ -63,13 +63,13 @@ public function __construct(
/**
* @throws InvalidFilterServiceTypeException|SearchException|InvalidQueryTypeException|InvalidFilterTypeException
*/
#[Route('/assets', name: 'pimcore_studio_api_assets', methods: ['GET'])]
#[Route('/assets/tree', name: 'pimcore_studio_api_assets_tree', methods: ['GET'])]
#[IsGranted(UserPermissions::ASSETS->value)]
#[Get(
path: self::API_PATH . '/assets',
operationId: 'getAssets',
path: self::API_PATH . '/assets/tree',
operationId: 'getAssetTree',
description: 'Get paginated assets',
summary: 'Get all assets',
summary: 'Get all assets for the tree',
tags: [Tags::Assets->name]
)]
#[PageParameter]
Expand All @@ -91,7 +91,7 @@ public function __construct(
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function getAssets(#[MapQueryString] ElementParameters $parameters): JsonResponse
public function getAssetTree(#[MapQueryString] ElementParameters $parameters): JsonResponse
{
$collection = $this->assetService->getAssets($parameters);

Expand Down
11 changes: 11 additions & 0 deletions src/Asset/Event/PreResponse/AssetEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\Asset\Event\PreResponse;

use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\Asset;
use Pimcore\Bundle\StudioBackendBundle\Element\Schema\CustomTreeAttributes;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class AssetEvent extends AbstractPreResponseEvent
Expand All @@ -36,4 +37,14 @@ public function getAsset(): Asset
{
return $this->asset;
}

public function getCustomTreeAttributes(): ?CustomTreeAttributes
{
return $this->asset->getCustomTreeAttributes();
}

public function setCustomTreeAttributes(CustomTreeAttributes $customTreeAttributes): void
{
$this->asset->setCustomTreeAttributes($customTreeAttributes);
}
}
5 changes: 4 additions & 1 deletion src/Asset/Schema/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Pimcore\Bundle\StudioBackendBundle\Response\Element;
use Pimcore\Bundle\StudioBackendBundle\Util\Schema\AdditionalAttributesInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\AdditionalAttributesTrait;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\CustomTreeAttributesTrait;

#[Schema(
title: 'Asset',
Expand All @@ -31,15 +32,17 @@
'type',
'filename',
'mimeType',
'metaData',
'hasMetaData',
'hasWorkflowWithPermissions',
'fullPath',
'customTreeAttributes',
],
type: 'object'
)]
class Asset extends Element implements AdditionalAttributesInterface
{
use AdditionalAttributesTrait;
use CustomTreeAttributesTrait;

public function __construct(
#[Property(description: 'IconName', type: 'string', example: 'pimcore_icon_pdf')]
Expand Down
108 changes: 108 additions & 0 deletions src/Element/Schema/CustomTreeAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\Element\Schema;

use OpenApi\Attributes\Items;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;

#[Schema(
title: 'CustomTreeAttributes',
description: 'Custom attributes for the tree',
required: ['icon', 'tooltip', 'additionalIcons', 'key', 'additionalCssClasses'],
type: 'object'
)]
final class CustomTreeAttributes
{
public function __construct(
#[Property(description: 'Custom Icon', type: 'string', example: 'my_custom_icon')]
private ?string $icon = null,
#[Property(description: 'Custom Tooltip', type: 'string', example: '<b>My Tooltip</b>')]
private ?string $tooltip = null,
#[Property(
description: 'AdditionalIcons',
type: 'array',
items: new Items(type: 'string', example: 'some_other_icon'),
)]
private array $additionalIcons = [],
#[Property(description: 'Custom Key/Filename', type: 'string', example: 'my_custom_key')]
private ?string $key = null,
#[Property(
description: 'Additional Css Classes',
type: 'array',
items: new Items(type: 'string', example: 'my_custom_class'),
)]
private array $additionalCssClasses = [],
) {

}

public function getIcon(): ?string
{
return $this->icon;
}

public function setIcon(string $icon): void
{
$this->icon = $icon;
}

public function getTooltip(): ?string
{
return $this->tooltip;
}

public function setTooltip(string $tooltip): void
{
$this->tooltip = $tooltip;
}

public function getAdditionalIcons(): array
{
return $this->additionalIcons;
}

public function setAdditionalIcons(array $icons): void
{
$this->additionalIcons = $icons;
}

public function addAdditionalIcon(string $value): void
{
$this->additionalIcons[] = $value;
}

public function getKey(): ?string
{
return $this->key;
}

public function setKey(string $key): void
{
$this->key = $key;
}

public function getAdditionalCssClasses(): array
{
return $this->additionalCssClasses;
}

public function setAdditionalCssClasses(array $classes): void
{
$this->additionalCssClasses = $classes;
}
}
43 changes: 43 additions & 0 deletions src/Util/Traits/CustomTreeAttributesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Util\Traits;

use OpenApi\Attributes\Property;
use Pimcore\Bundle\StudioBackendBundle\Element\Schema\CustomTreeAttributes;

/**
* @internal
*/
trait CustomTreeAttributesTrait
{
#[Property(description: 'Custom attributes for the tree', type: CustomTreeAttributes::class)]
private ?CustomTreeAttributes $customTreeAttributes = null;

public function getCustomTreeAttributes(): CustomTreeAttributes
{
if ($this->customTreeAttributes === null) {
$this->customTreeAttributes = new CustomTreeAttributes();
}

return $this->customTreeAttributes;
}

public function setCustomTreeAttributes(CustomTreeAttributes $customTreeAttributes): void
{
$this->customTreeAttributes = $customTreeAttributes;
}
}

0 comments on commit e1b04df

Please sign in to comment.