Skip to content

Commit

Permalink
WIP: get versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ibousfiha committed Apr 19, 2024
1 parent 785cf31 commit 5f75c81
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/Attributes/Parameters/Query/ElementTypeParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\StudioApiBundle\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class ElementTypeParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'type',
description: 'Element type.',
in: 'query',
required: true,
schema: new Schema(type: 'string', enum: [
FilterServiceInterface::TYPE_ASSET,
FilterServiceInterface::TYPE_DATA_OBJECT,
FilterServiceInterface::TYPE_DOCUMENT,
], example: 'asset'),
);
}
}
36 changes: 36 additions & 0 deletions src/Attributes/Parameters/Query/IdParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\StudioApiBundle\Attributes\Parameters\Query;

use Attribute;
use OpenApi\Attributes\QueryParameter;
use OpenApi\Attributes\Schema;

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
final class IdParameter extends QueryParameter
{
public function __construct()
{
parent::__construct(
name: 'id',
description: 'Element id.',
in: 'query',
required: true,
schema: new Schema(type: 'integer', example: 1),
);
}
}
2 changes: 1 addition & 1 deletion src/Attributes/Response/Content/OneOfAssetJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()
{
parent::__construct(
type: 'object',
oneOf: array_map(static function ($class) {
oneOf: array_map(static function (string $class) {
return new Schema(ref: $class);
}, Schemas::Assets),
);
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
#[Tag(name: Tags::Authorization->name, description: 'Login via username and password to get a token or refresh the token')]
#[Tag(name: Tags::DataObjects->name, description: 'DataObject operations to get/update/create/delete data objects')]
#[Tag(name: Tags::Translation->name, description: 'Get translations either for a single key or multiple keys')]
#[Tag(name: Tags::Versions->name, description: 'Get versions of an element')]
enum Tags
{
case Assets;
case Authorization;
case DataObjects;
case Translation;
case Versions;
}
110 changes: 110 additions & 0 deletions src/Controller/Api/Versions/CollectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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\StudioApiBundle\Controller\Api\Versions;

use Exception;
use OpenApi\Attributes\Get;
use OpenApi\Attributes\JsonContent;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\ElementTypeParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Parameters\Query\IdParameter;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioApiBundle\Attributes\Response\UnauthorizedResponse;
use Pimcore\Bundle\StudioApiBundle\Config\Tags;
use Pimcore\Bundle\StudioApiBundle\Controller\Api\AbstractApiController;
use Pimcore\Bundle\StudioApiBundle\Controller\Trait\PaginatedResponseTrait;
use Pimcore\Bundle\StudioApiBundle\Request\Query\Version as VersionQuery;
use Pimcore\Bundle\StudioApiBundle\Service\Filter\FilterServiceInterface;
use Pimcore\Model\Element\Service;
use Pimcore\Model\Exception\NotFoundException;
use Pimcore\Model\Version\Listing;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

final class CollectionController extends AbstractApiController
{
use PaginatedResponseTrait;

public function __construct(
SerializerInterface $serializer,
private readonly FilterServiceInterface $filterService

Check failure on line 45 in src/Controller/Api/Versions/CollectionController.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Property Pimcore\Bundle\StudioApiBundle\Controller\Api\Versions\CollectionController::$filterService is never read, only written.

Check failure on line 45 in src/Controller/Api/Versions/CollectionController.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Property Pimcore\Bundle\StudioApiBundle\Controller\Api\Versions\CollectionController::$filterService is never read, only written.
) {
parent::__construct($serializer);
}

/**
* @throws Exception
*/
#[Route('/versions', name: 'pimcore_studio_api_element_versions', methods: ['GET'])]
//#[IsGranted('STUDIO_API')]
#[GET(path: self::API_PATH
.'/versions', description: 'Get versions of an element', summary: 'Get all versions of an specific element (Asset, DataObject, Document) by its ID.', security: self::SECURITY_SCHEME, tags: [Tags::Versions->name],)]
#[IdParameter]
#[ElementTypeParameter]
#[SuccessResponse(description: 'Paginated versions with total count as header param', content: new JsonContent(
ref: Version::class

Check failure on line 60 in src/Controller/Api/Versions/CollectionController.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, false)

Class Pimcore\Bundle\StudioApiBundle\Controller\Api\Versions\Version not found.

Check failure on line 60 in src/Controller/Api/Versions/CollectionController.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2, highest, 11.x-dev as 11.99.9, true)

Class Pimcore\Bundle\StudioApiBundle\Controller\Api\Versions\Version not found.
))]
#[UnauthorizedResponse]
public function getVersions(
#[MapQueryString] VersionQuery $versionQuery
): JsonResponse {
$type = $versionQuery->getType();
$id = $versionQuery->getId();
$element = Service::getElementById($type, $id);

if (!$element) {
throw new NotFoundException('Element not found');
}

if (!$element->isAllowed('versions')) {
throw $this->createAccessDeniedException('You are not allowed to view versions of this element');
}

$schedule = $element->getScheduledTasks();
$schedules = [];
foreach ($schedule as $task) {
if ($task->getActive()) {
$schedules[$task->getVersion()] = $task->getDate();
}
}

//only load auto-save versions from current user
$list = new Listing();
$list->setLoadAutoSave(true);

// TODO: Implement the filter of the user as it was on admin-ui-classic-bundle
$list->setCondition('cid = ? AND ctype = ? AND (autoSave=0 OR (autoSave=1)) ', [
$element->getId(),
Service::getElementType($element),
])->setOrderKey('date')->setOrder('ASC');

$versions = $list->load();

$versions = Service::getSafeVersionInfo($versions);
$versions = array_reverse($versions); //reverse array to sort by ID DESC
foreach ($versions as &$version) {
$version['scheduled'] = null;
if (array_key_exists($version['id'], $schedules)) {
$version['scheduled'] = $schedules[$version['id']];
}
}

// TODO: This was just copied from other endpoint without customizing it for this endpoint.
return $this->getPaginatedCollection($this->serializer, ['versions' => $versions]);
}
}
49 changes: 49 additions & 0 deletions src/Request/Query/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\StudioApiBundle\Request\Query;

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

/**
* @internal
*/
#[Schema(
title: 'Version',
description: 'Version request for specific element (dataObject, asset, document).',
type: 'object'
)]
final readonly class Version
{
public function __construct(
#[Property(description: 'Id', type: 'integer', example: 'Element ID.')]
private int $id,
#[Property(description: 'Type', type: 'string', example: 'Element type (asset, dataObject, document).')]
private string $type
) {
}

public function getId(): int
{
return $this->id;
}

public function getType(): string
{
return $this->type;
}
}

0 comments on commit 5f75c81

Please sign in to comment.