Skip to content

Commit

Permalink
Add filter and extend resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Nov 29, 2024
1 parent 8624da8 commit debc081
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 11 deletions.
47 changes: 47 additions & 0 deletions src/Metadata/Attribute/Request/FilterRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Metadata\Attribute\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;

/**
* @internal
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class FilterRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: false,
content: new JsonContent(
properties: [
new Property(
property: 'filter',
type: 'string',
example: 'author',
nullable: true,
),
],
type: 'object',
),
);
}
}
2 changes: 1 addition & 1 deletion src/Metadata/Controller/Asset/GetController.php
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\Asset\Controller\Data;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Controller\Asset;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
Expand Down
14 changes: 7 additions & 7 deletions src/Metadata/Controller/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Controller;

use OpenApi\Attributes\Get;
use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Metadata\MappedParameter\MetadataParameters;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\PredefinedMetadata;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Attribute\Request\FilterRequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\FilterParameter;
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\Trait\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

Expand All @@ -47,15 +47,15 @@ public function __construct(
parent::__construct($serializer);
}

#[Route('/metadata', name: 'pimcore_studio_api_metadata', methods: ['GET'])]
#[Get(
#[Route('/metadata', name: 'pimcore_studio_api_metadata', methods: ['POST'])]
#[Post(
path: self::PREFIX . '/metadata',
operationId: 'metadata_get_collection',
description: 'metadata_get_collection_description',
summary: 'metadata_get_collection_summary',
tags: [Tags::Metadata->name]
)]
#[FilterParameter]
#[FilterRequestBody]
#[SuccessResponse(
description: 'metadata_get_collection_success_response',
content: new ItemsJson(PredefinedMetadata::class)
Expand All @@ -64,7 +64,7 @@ public function __construct(
HttpResponseCodes::UNAUTHORIZED,
])]
public function getMetadata(
#[MapQueryString] MetadataParameters $parameters = new MetadataParameters()
#[MapRequestPayload] MetadataParameters $parameters = new MetadataParameters()
): JsonResponse {
return $this->jsonResponse(['items' => $this->metadataService->getPredefinedMetadata($parameters)]);
}
Expand Down
21 changes: 19 additions & 2 deletions src/Metadata/Hydrator/MetadataHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ public function hydratePredefined(Predefined $predefined): PredefinedMetadata
$predefined->getDescription(),
$predefined->getType(),
$predefined->getTargetSubType(),
$this->resolveData(
$this->resolveDefinitionData(
$predefined->getData(),
$predefined->getType(),
),
$predefined->getConfig(),
$predefined->getLanguage(),
$predefined->getGroup()
$predefined->getGroup(),
$predefined->getCreationDate(),
$predefined->getModificationDate(),
$predefined->isWriteable()
);
}

Expand All @@ -74,4 +77,18 @@ private function resolveData(mixed $data, string $type): mixed
default => $data,
};
}

private function resolveDefinitionData(mixed $data, string $type): mixed
{
if(!$data) {
return $data;
}

return match ($type) {
'asset', 'document', 'object' => $this->referenceResolver->resolveData($type, (int)$data),
'checkbox' => (bool)$data,
default => $data
};
}

}
20 changes: 20 additions & 0 deletions src/Metadata/Repository/MetadataRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Repository;

use Pimcore\Bundle\StaticResolverBundle\Models\Metadata\Predefined\PredefinedResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Metadata\MappedParameter\MetadataParameters;
use Pimcore\Model\Metadata\Predefined;
use Pimcore\Model\Metadata\Predefined\Listing;

Expand All @@ -37,6 +38,25 @@ public function getAllPredefinedMetadata(): array
return (new Listing())->load();
}

public function getAllPredefinedMetadataDefinitions(MetadataParameters $metadataParameters): array
{
$listing = new Listing();
$filter = $metadataParameters->getFilter();
if ($filter !== null) {
$listing->setFilter(function (Predefined $predefined) use ($filter) {
foreach ($predefined->getObjectVars() as $value) {
if (stripos((string)$value, $filter) !== false) {
return true;
}
}

return false;
});
}

return $listing->getDefinitions();
}

public function getPredefinedMetadataByName(string $name): ?Predefined
{
return $this->predefinedResolver->getByName($name);
Expand Down
3 changes: 3 additions & 0 deletions src/Metadata/Repository/MetadataRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Repository;

use Pimcore\Bundle\StudioBackendBundle\Metadata\MappedParameter\MetadataParameters;
use Pimcore\Model\Metadata\Predefined;

/**
Expand All @@ -28,5 +29,7 @@ interface MetadataRepositoryInterface
*/
public function getAllPredefinedMetadata(): array;

public function getAllPredefinedMetadataDefinitions(MetadataParameters $metadataParameters): array;

public function getPredefinedMetadataByName(string $name): ?Predefined;
}
21 changes: 21 additions & 0 deletions src/Metadata/Schema/PredefinedMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public function __construct(
private ?string $language,
#[Property(description: 'Group', type: 'string', example: 'group')]
private ?string $group,
#[Property(description: 'Creation Date', type: 'integer', example: 1634025600)]
private int $creationDate,
#[Property(description: 'Modfication Date', type: 'integer', example: 1634025600)]
private int $modificationDate,
#[Property(description: 'Writable', type: 'bool', example: false)]
private bool $isWriteable = false,
) {
}

Expand Down Expand Up @@ -96,4 +102,19 @@ public function getGroup(): ?string
{
return $this->group;
}

public function getCreationDate(): int
{
return $this->creationDate;
}

public function getModificationDate(): int
{
return $this->modificationDate;
}

public function isWriteable(): bool
{
return $this->isWriteable;
}
}
3 changes: 2 additions & 1 deletion src/Metadata/Service/MetadataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ public function getCustomMetadata(int $id): array

public function getPredefinedMetadata(MetadataParameters $parameters): array
{
$originalPredefinedMetadata = $this->metadataRepository->getAllPredefinedMetadata();
$originalPredefinedMetadata = $this->metadataRepository->getAllPredefinedMetadataDefinitions($parameters);

$predefinedMetadata = [];

foreach ($originalPredefinedMetadata as $predefined) {
$metadata = $this->hydrator->hydratePredefined($predefined);

Expand Down
15 changes: 15 additions & 0 deletions src/Resolver/Element/ReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

namespace Pimcore\Bundle\StudioBackendBundle\Resolver\Element;

use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Model\Element\AbstractElement;
use Pimcore\Model\Element\ElementInterface;

final class ReferenceResolver implements ReferenceResolverInterface
{
use ElementProviderTrait;

private const ALLOWED_MODEL_PROPERTIES = [
'key',
'filename',
Expand All @@ -33,6 +37,10 @@ final class ReferenceResolver implements ReferenceResolverInterface
*/
private array $cache = [];

public function __construct(private ServiceResolverInterface $serviceResolver)
{
}

public function resolve(ElementInterface $element): array
{
if (isset($this->cache[$element->getId()])) {
Expand All @@ -53,4 +61,11 @@ public function resolve(ElementInterface $element): array

return $data;
}

public function resolveData(string $type, int $id): mixed
{
$element = $this->getElement($this->serviceResolver, $type, $id);

return $this->resolve($element);
}
}
2 changes: 2 additions & 0 deletions src/Resolver/Element/ReferenceResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
interface ReferenceResolverInterface
{
public function resolve(ElementInterface $element): array;

public function resolveData(string $type, int $id): mixed;
}
4 changes: 4 additions & 0 deletions translations/studio_api_docs.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ logout_summary: Logout and invalidate current session for active user.
mercure_create_cookie_description: Retrieve JWT token for Mercure hub as cookie
mercure_create_cookie_success_response: Retrieve JWT token for Mercure hub as cookie
mercure_create_cookie_summary: Retrieve JWT token for Mercure hub as cookie
metadata_get_collection_description: Get predefined metadata collection with basic filter options
metadata_get_collection_success_response: Predefined metadata collection
metadata_get_collection_summary: Get predefined metadata collection
note_delete_by_id_description: |
Delete the note with the given <strong>{id}</strong>
note_delete_by_id_success_response: Successfully deleted note
Expand Down Expand Up @@ -453,6 +456,7 @@ tag_get_collection_for_element_by_type_and_id_summary: Get tags for an element
tag_get_collection_success_response: All tags for a parent filtered based on type and query parameters
tag_get_collection_summary: Get all tags for a parent
tag_mercure_description: Retrieve JWT token for Mercure hub as cookie
tag_metadata_description: Metadata operations to get/update/create/delete metadata
tag_notes_description: Note operations to list/delete notes
tag_notifications_description: Notification operations to get/delete/send notifications
tag_properties_description: Property operations to get/update/create/delete properties
Expand Down

0 comments on commit debc081

Please sign in to comment.