Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Custom Reports tree endpoint #665

Merged
merged 23 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions config/custom_reports.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\CustomReport\Controller\:
resource: '../src/CustomReport/Controller'
public: true
tags: [ 'controller.service_arguments' ]


#
# Repositories
#

Pimcore\Bundle\StudioBackendBundle\CustomReport\Repository\CustomReportRepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\CustomReport\Repository\CustomReportRepository

#
# Hydrator
#

Pimcore\Bundle\StudioBackendBundle\CustomReport\Hydrator\CustomReportHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\CustomReport\Hydrator\CustomReportHydrator

#
# Services
#

Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CustomReportServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CustomReportService
80 changes: 80 additions & 0 deletions src/CustomReport/Controller/Config/TreeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\CustomReport\Controller\Config;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\CustomReport\Schema\CustomReportConfigTreeNode;
use Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CustomReportServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidQueryTypeException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\PageParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\PageSizeParameter;
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\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;
use function count;

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

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

/**
* @throws InvalidQueryTypeException
*/
#[Route('/custom-reports/config/tree', name: 'pimcore_studio_api_report', methods: ['GET'])]
#[IsGranted(UserPermissions::TAGS_SEARCH->value)]
mcop1 marked this conversation as resolved.
Show resolved Hide resolved
#[Get(
path: self::PREFIX . '/custom-reports/config/tree',
operationId: 'custom_reports_config_get_tree',
description: 'custom_reports_config_get_tree_description',
summary: 'custom_reports_config_get_tree_summary',
tags: [Tags::CustomReports->name]
)]
#[PageParameter]
#[PageSizeParameter]
#[SuccessResponse(
description: 'custom_report_get_tree_success_response',
content: new ItemsJson(CustomReportConfigTreeNode::class)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
])]
public function getCustomReports(): JsonResponse
{
$items = $this->customReportService->getCustomReportConfigTree();

return $this->getPaginatedCollection($this->serializer, $items, count($items));
}
}
80 changes: 80 additions & 0 deletions src/CustomReport/Controller/TreeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\CustomReport\Controller;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\CustomReport\Schema\CustomReportTreeNode;
use Pimcore\Bundle\StudioBackendBundle\CustomReport\Service\CustomReportServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidQueryTypeException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\PageParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\PageSizeParameter;
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\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;
use function count;

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

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

/**
* @throws InvalidQueryTypeException
*/
#[Route('/custom-reports/tree', name: 'pimcore_studio_api_report', methods: ['GET'])]
#[IsGranted(UserPermissions::TAGS_SEARCH->value)]
mcop1 marked this conversation as resolved.
Show resolved Hide resolved
#[Get(
path: self::PREFIX . '/custom-reports/tree',
operationId: 'custom_reports_get_tree',
description: 'custom_reports_get_tree_description',
summary: 'custom_reports_get_tree_summary',
tags: [Tags::CustomReports->name]
)]
#[PageParameter]
#[PageSizeParameter]
#[SuccessResponse(
description: 'custom_report_collection_success_response',
content: new ItemsJson(CustomReportTreeNode::class)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
mcop1 marked this conversation as resolved.
Show resolved Hide resolved
])]
public function getCustomReports(): JsonResponse
{
$items = $this->customReportService->getCustomReportTree();

return $this->getPaginatedCollection($this->serializer, $items, count($items));
}
}
60 changes: 60 additions & 0 deletions src/CustomReport/Hydrator/CustomReportHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\CustomReport\Hydrator;

/**
* @internal
*/
final readonly class CustomReportHydrator implements CustomReportHydratorInterface
{
public function hydrateCustomReportTree(array $reports): array
{
$hydratedReports = [];

foreach ($reports as $report) {
if ($report->getDataSourceConfig() !== null) {
$hydratedReports[] = [
'name' => htmlspecialchars($report->getName()),
'niceName' => htmlspecialchars($report->getNiceName()),
'iconClass' => htmlspecialchars($report->getIconClass()),
'group' => htmlspecialchars($report->getGroup()),
'groupIconClass' => htmlspecialchars($report->getGroupIconClass()),
'menuShortcut' => $report->getMenuShortcut(),
'reportClass' => htmlspecialchars($report->getReportClass()),
];
}
}

return $hydratedReports;
}

public function hydrateCustomReportConfigTree(array $reports): array
{
$hydratedReports = [];

foreach ($reports as $item) {
$hydratedReports[] = [
'id' => $item->getName(),
'text' => $item->getName(),
'cls' => 'pimcore_treenode_disabled',
'writeable' => $item->isWriteable(),
];
}

return $hydratedReports;
}
}
27 changes: 27 additions & 0 deletions src/CustomReport/Hydrator/CustomReportHydratorInterface.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\CustomReport\Hydrator;

/**
* @internal
*/
interface CustomReportHydratorInterface
{
public function hydrateCustomReportTree(array $reports): array;

public function hydrateCustomReportConfigTree(array $reports): array;
}
53 changes: 53 additions & 0 deletions src/CustomReport/Repository/CustomReportRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\CustomReport\Repository;

use Pimcore\Bundle\CustomReportsBundle\Tool\Config\Listing;
use Pimcore\Bundle\CustomReportsBundle\Tool\Config\Listing\Dao;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Model\User;

/**
* @internal
*/
final readonly class CustomReportRepository implements CustomReportRepositoryInterface
{
public function __construct(
private SecurityServiceInterface $securityService
) {
}

public function loadForUser(User $user): array
{
/** @var Dao $dao */
$dao = (new Listing())->getDao();

return $dao->loadForGivenUser(
$user
);
}

public function loadForCurrentUser(): array
{
/** @var User $currentUser */
$currentUser = $this->securityService->getCurrentUser();

return $this->loadForUser(
$currentUser
);
}
}
29 changes: 29 additions & 0 deletions src/CustomReport/Repository/CustomReportRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\CustomReport\Repository;

use Pimcore\Model\User;

/**
* @internal
*/
interface CustomReportRepositoryInterface
{
public function loadForUser(User $user): array;

public function loadForCurrentUser(): array;
}
Loading
Loading