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] Add MetaData Filter #277

Merged
merged 8 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 26 additions & 1 deletion config/data_index_filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,29 @@ services:

# DataObject
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\DataObject\ClassNameFilter:
tags: [ 'pimcore.studio_backend.open_search.data_object.filter' ]
tags: [ 'pimcore.studio_backend.open_search.data_object.filter' ]

# Asset MetaData
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\SelectFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\InputFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\TextAreaFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\CheckboxFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\DateFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\AssetFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\DocumentFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\Asset\MetaData\ObjectFilter:
tags: [ 'pimcore.studio_backend.open_search.asset.filter' ]
50 changes: 50 additions & 0 deletions doc/03_Grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,53 @@ 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.

### ColumnFilter
It is also possible to filter the data by a column. This is done by adding a `columnFilter` to the `filter` property.
A `columnFilter` has a reference to the column and the value you want to filter by.

Available filters are:

| Type | filterValue | Options |
|:-----------------:|:-------------------:|:---------------------:|
| metadata.select | string | |
| metadata.date | object of timestamp | `from`, `to`, or `on` |
| metadata.input | string | |
| metadata.checkbox | boolean | |
| metadata.textarea | string | |
| metadata.object | integer | ID of the object |
| metadata.document | integer | ID fo the document |
| metadata.asset | integer | ID fo the asset |



### Examples:

Filter by a select column:
```json
...
"columnFilters" [
{
"key": "selectKey",
"type": "metadata.select",
"filterValue": "selectValue"
}
]
...
```

Filter by a date column:
```json
...
"columnFilters" [
{
"key": "selectKey",
"type": "metadata.select",
"filterValue": {
"from": 1719792000,
"to": 1718792000
}
}
]
...
```
60 changes: 60 additions & 0 deletions src/DataIndex/Filter/Asset/MetaData/AssetFilter.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\DataIndex\Filter\Asset\MetaData;

use Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\FilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQuery;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFiltersParameterInterface;
use function is_int;

/**
* @internal
*/
final class AssetFilter implements FilterInterface
{
use IsAssetMetaDataTrait;

public function apply(mixed $parameters, QueryInterface $query): QueryInterface
{
/** @var ColumnFiltersParameterInterface $parameters */
/** @var AssetQuery $query */
if (!$this->isAssetMetaData($parameters, $query)) {
return $query;
}

foreach ($parameters->getColumnFilterByType(ColumnType::METADATA_ASSET->value) as $column) {
$query = $this->applyAssetFilter($column, $query);
}

return $query;
}

private function applyAssetFilter(ColumnFilter $column, AssetQuery $query): AssetQuery
{
if (!is_int($column->getFilterValue())) {
throw new InvalidArgumentException('Filter value for asset must be a integer (ID of the asset)');
}

$query->filterMetaDate($column->getKey(), FilterType::ASSET->value, $column->getFilterValue());

return $query;
}
}
60 changes: 60 additions & 0 deletions src/DataIndex/Filter/Asset/MetaData/CheckboxFilter.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\DataIndex\Filter\Asset\MetaData;

use Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\FilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQuery;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFiltersParameterInterface;
use function is_bool;

/**
* @internal
*/
final class CheckboxFilter implements FilterInterface
{
use IsAssetMetaDataTrait;

public function apply(mixed $parameters, QueryInterface $query): QueryInterface
{
/** @var ColumnFiltersParameterInterface $parameters */
/** @var AssetQuery $query */
if (!$this->isAssetMetaData($parameters, $query)) {
return $query;
}

foreach ($parameters->getColumnFilterByType(ColumnType::METADATA_CHECKBOX->value) as $column) {
$query = $this->applyCheckboxFilter($column, $query);
}

return $query;
}

private function applyCheckboxFilter(ColumnFilter $column, AssetQuery $query): AssetQuery
{
if (!is_bool($column->getFilterValue())) {
throw new InvalidArgumentException('Filter value for checkbox must be a boolean');
}

$query->filterMetaDate($column->getKey(), FilterType::CHECKBOX->value, $column->getFilterValue());

return $query;
}
}
85 changes: 85 additions & 0 deletions src/DataIndex/Filter/Asset/MetaData/DateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\DataIndex\Filter\Asset\MetaData;

use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Query\DateFilter as GenericDateFilter;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\FilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQuery;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFiltersParameterInterface;
use function is_array;

/**
* @internal
*/
final class DateFilter implements FilterInterface
{
use IsAssetMetaDataTrait;

public function apply(mixed $parameters, QueryInterface $query): QueryInterface
{
/** @var ColumnFiltersParameterInterface $parameters */
/** @var AssetQuery $query */
if (!$this->isAssetMetaData($parameters, $query)) {
return $query;
}

foreach ($parameters->getColumnFilterByType(ColumnType::METADATA_DATE->value) as $column) {
$query = $this->applyDateFilter($column, $query);
}

return $query;
}

private function applyDateFilter(ColumnFilter $column, AssetQuery $query): AssetQuery
{
if (!is_array($column->getFilterValue())) {
throw new InvalidArgumentException('Filter value for date must be an array');
}

$filterValue = $column->getFilterValue();

if (isset($filterValue['on'])) {
$query->filterMetaDate(
$column->getKey(),
FilterType::DATE->value,
[GenericDateFilter::PARAM_ON => $filterValue['on']]
);
}

if (isset($filterValue['to'])) {
$query->filterMetaDate(
$column->getKey(),
FilterType::DATE->value,
[GenericDateFilter::PARAM_END => $filterValue['to']]
);
}

if (isset($filterValue['from'])) {
$query->filterMetaDate(
$column->getKey(),
FilterType::DATE->value,
[GenericDateFilter::PARAM_START => $filterValue['from']]
);
}

return $query;
}
}
60 changes: 60 additions & 0 deletions src/DataIndex/Filter/Asset/MetaData/DocumentFilter.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\DataIndex\Filter\Asset\MetaData;

use Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\FilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\AssetQuery;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFiltersParameterInterface;
use function is_int;

/**
* @internal
*/
final class DocumentFilter implements FilterInterface
{
use IsAssetMetaDataTrait;

public function apply(mixed $parameters, QueryInterface $query): QueryInterface
{
/** @var ColumnFiltersParameterInterface $parameters */
/** @var AssetQuery $query */
if (!$this->isAssetMetaData($parameters, $query)) {
return $query;
}

foreach ($parameters->getColumnFilterByType(ColumnType::METADATA_DOCUMENT->value) as $column) {
$query = $this->applyDocumentFilter($column, $query);
}

return $query;
}

private function applyDocumentFilter(ColumnFilter $column, AssetQuery $query): AssetQuery
{
if (!is_int($column->getFilterValue())) {
throw new InvalidArgumentException('Filter value for document must be a integer (ID of the document)');
}

$query->filterMetaDate($column->getKey(), FilterType::DOCUMENT->value, $column->getFilterValue());

return $query;
}
}
30 changes: 30 additions & 0 deletions src/DataIndex/Filter/Asset/MetaData/FilterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\DataIndex\Filter\Asset\MetaData;

enum FilterType: string
{
case SELECT = 'select';
case INPUT = 'input';
case TEXTAREA = 'textarea';
case CHECKBOX = 'checkbox';
case DATE = 'date';
case ASSET = 'asset';
case DOCUMENT = 'document';
case OBJECT = 'object';
}
Loading
Loading