Skip to content

Commit

Permalink
[Data Object Editor] Get Select Options Endpoint (#631)
Browse files Browse the repository at this point in the history
* get select Options for Dynamic select

* Apply php-cs-fixer changes

* Fix style.

* Apply php-cs-fixer changes

* Fix style.

* Fix Exception Handling.

* Apply php-cs-fixer changes

---------

Co-authored-by: martineiber <[email protected]>
  • Loading branch information
martineiber and martineiber authored Dec 11, 2024
1 parent 75dd36a commit 7cbcc42
Show file tree
Hide file tree
Showing 13 changed files with 822 additions and 1 deletion.
17 changes: 17 additions & 0 deletions config/data_objects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ services:
Pimcore\Bundle\StudioBackendBundle\DataObject\Service\LayoutServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\DataObject\Service\LayoutService

Pimcore\Bundle\StudioBackendBundle\DataObject\Service\SelectOptionsServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\DataObject\Service\SelectOptionsService

#
# Legacy Services
#
Pimcore\Bundle\StudioBackendBundle\DataObject\Legacy\ApplyChangesHelperInterface:
class: Pimcore\Bundle\StudioBackendBundle\DataObject\Legacy\ApplyChangesHelper


#
# Hydrator
#
Pimcore\Bundle\StudioBackendBundle\DataObject\Hydrator\SelectOptionHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\DataObject\Hydrator\SelectOptionHydrator


#
# Data Adapters
#
Expand Down
52 changes: 52 additions & 0 deletions src/DataObject/Attribute/Request/SelectOptionRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\Attribute\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\SingleInteger;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\SingleString;

#[Attribute(Attribute::TARGET_METHOD)]
final class SelectOptionRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new JsonContent(
required: ['objectId', 'fieldName', 'context'],
properties: [
new SingleInteger('objectId'),
new SingleString('fieldName'),
new Property(
property: 'changedData',
type: 'object',
example: '{"Input":"new value"}'
),
new Property(
property: 'context',
type: 'object',
example: '{"containerType":"object","fieldname":"select","objectId":40,"layoutId":"0"}'
),
]
)
);
}
}
95 changes: 95 additions & 0 deletions src/DataObject/Controller/SelectOptionsController.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;

use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Attribute\Request\SelectOptionRequestBody;
use Pimcore\Bundle\StudioBackendBundle\DataObject\MappedParameter\SelectOptionsParameter;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\SelectOption;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Service\SelectOptionsServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Property\GenericCollection;
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 Pimcore\Bundle\StudioBackendBundle\Util\Trait\PaginatedResponseTrait;
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;
use function count;

/**
* @internal
*/
final class SelectOptionsController extends AbstractApiController
{
use PaginatedResponseTrait;

public function __construct(
SerializerInterface $serializer,
private readonly SelectOptionsServiceInterface $selectOptionsService,
) {
parent::__construct($serializer);
}

/**
* @throws DatabaseException
* @throws NotFoundException
* @throws InvalidArgumentException
*/
#[Route(
path: '/data-objects/select-options',
name: 'pimcore_studio_api_get_data_object_get_select_options',
methods: ['POST']
)]
#[IsGranted(UserPermissions::DATA_OBJECTS->value)]
#[Post(
path: self::PREFIX . '/data-objects/select-options',
operationId: 'data_object_get_select_options',
description: 'data_object_get_select_options_description',
summary: 'data_object_get_select_options_summary',
tags: [Tags::DataObjects->value]
)]
#[SelectOptionRequestBody]
#[SuccessResponse(
description: 'data_object_get_select_options_success_response',
content: new CollectionJson(new GenericCollection(SelectOption::class))
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
HttpResponseCodes::BAD_REQUEST,
])]
public function getSelectOptions(#[MapRequestPayload] SelectOptionsParameter $selectOptionsParameter): JsonResponse
{
$selectOptions = $this->selectOptionsService->getSelectOptions($selectOptionsParameter);

return $this->getPaginatedCollection(
$this->serializer,
$selectOptions,
count($selectOptions),
);
}
}
39 changes: 39 additions & 0 deletions src/DataObject/Event/PreResponse/DynamicSelectOptionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Event\PreResponse;

use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\SelectOption;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class DynamicSelectOptionEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.data_object.dynamic_select_option';

public function __construct(
private readonly SelectOption $selectOption
) {
parent::__construct($this->selectOption);
}

/**
* Use this to get additional infos out of the response object
*/
public function getSelectOption(): SelectOption
{
return $this->selectOption;
}
}
33 changes: 33 additions & 0 deletions src/DataObject/Hydrator/SelectOptionHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\SelectOption;

/**
* @internal
*/
final readonly class SelectOptionHydrator implements SelectOptionHydratorInterface
{
public function hydrate(array $data): SelectOption
{
return new SelectOption(
key: $data['key'],
value: $data['value']
);
}
}
27 changes: 27 additions & 0 deletions src/DataObject/Hydrator/SelectOptionHydratorInterface.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\DataObject\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\SelectOption;

/**
* @internal
*/
interface SelectOptionHydratorInterface
{
public function hydrate(array $data): SelectOption;
}
Loading

0 comments on commit 7cbcc42

Please sign in to comment.