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

#261 [Grid] Pagination and basic Filter. #273

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
9 changes: 9 additions & 0 deletions doc/03_Grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ A column configuration represents how the column should behave, for example if i

## ColumnData
A column data is the actual data for a column. It has a reference to the column and the actual data.


## Filter
A filter is a way to filter the data in the grid. One Property of getting the grid data is the `filter` property.
Here you can define `page`, `pageSize` and `includeDescendants`.

`page` is the page number of the data you want to get.
`pageSize` is the number of items you want to get. `
includeDescendants` is a boolean value to include the descendants of the current item.
martineiber marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions src/Asset/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ public function getAsset(int $id): Asset|Archive|Audio|Document|Folder|Image|Tex
return $asset;
}

/**
* @throws SearchException|NotFoundException
*/
public function getAssetFolder(int $id): Folder
{
$asset = $this->assetSearchService->getAssetById($id);

if (!$asset instanceof Folder) {
throw new NotFoundException('folder', $id);
martineiber marked this conversation as resolved.
Show resolved Hide resolved
}

$this->eventDispatcher->dispatch(
new AssetEvent($asset),
AssetEvent::EVENT_NAME
);

return $asset;
}

/**
* @throws AccessDeniedException|NotFoundException
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Asset/Service/AssetServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function getAssets(ElementParameters $parameters): Collection;
*/
public function getAsset(int $id): Asset|Archive|Audio|Document|Folder|Image|Text|Unknown|Video;

/**
* @throws SearchException|NotFoundException
*/
public function getAssetFolder(int $id): Folder;

/**
* @throws AccessDeniedException|NotFoundException
*/
Expand Down
41 changes: 26 additions & 15 deletions src/DataIndex/Grid/GridSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,45 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Grid;

use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResult;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\ParentIdFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\AssetServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\OpenSearchFilterInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Filter\Service\FilterServiceProviderInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\GridParameter;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Model\User;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\ElementTypes;

/**
* @internal
*/
final readonly class GridSearch implements GridSearchInterface
{
public function __construct(
private SecurityServiceInterface $securityService,
private FilterServiceProviderInterface $filterServiceProvider,
private AssetSearchServiceInterface $assetSearchService,
private AssetServiceInterface $assetService
) {
}

/**
* @throws NotFoundException
martineiber marked this conversation as resolved.
Show resolved Hide resolved
*/
public function searchAssets(GridParameter $gridParameter): AssetSearchResult
{
// TODO Try to repurpose filter concept from data-index
$search = new AssetSearch();
/** @var User $user */
$user = $this->securityService->getCurrentUser();
$search->setUser($user);
$search->addModifier(new ParentIdFilter($gridParameter->getFolderId()));

return $this->assetSearchService->search($search);
/** @var OpenSearchFilterInterface $filterService */
$filterService = $this->filterServiceProvider->create(OpenSearchFilterInterface::SERVICE_TYPE);
$filter = $gridParameter->getFilters();

$asset = $this->assetService->getAssetFolder($gridParameter->getFolderId());

$filter->setPath($asset->getFullPath());

$assetQuery = $filterService->applyFilters(
$filter,
ElementTypes::TYPE_ASSET
);

return $this->assetSearchService->searchAssets($assetQuery);
}
}
5 changes: 4 additions & 1 deletion src/DataIndex/Grid/GridSearchInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Grid;

use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\AssetSearchResult;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\AssetSearchResult;
use Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter\GridParameter;

/**
* @internal
*/
interface GridSearchInterface
{
/**
* @throw NotFoundException
*/
public function searchAssets(GridParameter $gridParameter): AssetSearchResult;
}
6 changes: 6 additions & 0 deletions src/Grid/Attributes/Request/GridRequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\Column;
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\Filter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Property\SingleInteger;

/**
Expand All @@ -43,6 +44,11 @@ public function __construct()
type: 'array',
items: new Items(ref: Column::class)
),
new Property(
property: 'filters',
ref: Filter::class,
type: 'object'
),
],
type: 'object',
),
Expand Down
39 changes: 36 additions & 3 deletions src/Grid/MappedParameter/FilterParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@
namespace Pimcore\Bundle\StudioBackendBundle\Grid\MappedParameter;

use Pimcore\Bundle\StudioBackendBundle\MappedParameter\CollectionParametersInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ExcludeFolderParameterInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\PathParameterInterface;

/**
* @internal
*/
final readonly class FilterParameter implements CollectionParametersInterface
final class FilterParameter implements
CollectionParametersInterface,
ExcludeFolderParameterInterface,
PathParameterInterface
{
private ?string $path = null;

public function __construct(
private int $page = 1,
private int $pageSize = 50
private readonly int $page = 1,
private readonly int $pageSize = 50,
private readonly bool $includeDescendants = true
) {
}

Expand All @@ -38,4 +46,29 @@ public function getPageSize(): int
{
return $this->pageSize;
}

public function getExcludeFolders(): bool
{
return true;
}

public function getPath(): ?string
{
return $this->path;
}

public function setPath(?string $path): void
{
$this->path = $path;
}

public function getPathIncludeParent(): bool
{
return false;
}

public function getPathIncludeDescendants(): bool
{
return $this->includeDescendants;
}
}
10 changes: 10 additions & 0 deletions src/Grid/MappedParameter/GridParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public function __construct(
private int $folderId,
private array $columns,
private ?FilterParameter $filters
) {
}

Expand All @@ -36,4 +37,13 @@ public function getColumns(): array
{
return $this->columns;
}

public function getFilters(): FilterParameter
{
if ($this->filters === null) {
return new FilterParameter();
}

return $this->filters;
}
}
58 changes: 58 additions & 0 deletions src/Grid/Schema/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\Grid\Schema;

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

/**
* Contains all data that is needed to get all the data for the column.
*
* @internal
*/
#[Schema(
title: 'Grid Filter',
required: ['page', 'pageSize', 'includeDescendants'],
type: 'object'
)]
final readonly class Filter
{
public function __construct(
#[Property(description: 'Page', type: 'integer', example: 1)]
private int $page,
#[Property(description: 'Page Size', type: 'integer', example: 50)]
private int $pageSize,
#[Property(description: 'Include Descendant Items', type: 'boolean', example: false)]
private string $includeDescendants,
) {
}

public function getPage(): int
{
return $this->page;
}

public function getPageSize(): int
{
return $this->pageSize;
}

public function getIncludeDescendants(): string
{
return $this->includeDescendants;
}
}
16 changes: 7 additions & 9 deletions src/Grid/Service/GridService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __construct(
private readonly ColumnCollectorLoaderInterface $columnCollectorLoader,
private readonly GridSearchInterface $gridSearch,
private readonly ServiceResolverInterface $serviceResolver,
private readonly EventDispatcherInterface $eventDispatcher
private readonly EventDispatcherInterface $eventDispatcher,
) {
}

Expand All @@ -77,26 +77,24 @@ public function __construct(
public function getAssetGrid(GridParameter $gridParameter): Collection
{
$result = $this->gridSearch->searchAssets($gridParameter);
$items = $result->getItems();

if (empty($result->getIds())) {
if (empty($items)) {
return new Collection(totalItems: 0, items: []);
}

$data = [];

foreach ($result->getItems() as $item) {
$type = $item->getElementType()->value;
$asset = $this->getElement($this->serviceResolver, $type, $item->getId());

foreach ($items as $item) {
$asset = $this->getElement($this->serviceResolver, 'asset', $item->getId());
$data[] = $this->getGridDataForElement(
$this->getConfigurationFromArray($gridParameter->getColumns()),
$asset,
$type
'asset'
martineiber marked this conversation as resolved.
Show resolved Hide resolved
);
}

return new Collection(
totalItems: $result->getPagination()->getTotalItems(),
totalItems: $result->getTotalItems(),
items: $data
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/MappedParameter/Filter/PathParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface PathParameterInterface
{
public function getPath(): ?string;

public function getPathIncludeParent(): ?bool;
public function getPathIncludeParent(): bool;

public function getPathIncludeDescendants(): ?bool;
public function getPathIncludeDescendants(): bool;
}
Loading