Skip to content

Commit

Permalink
[Grid] Add Tag Filter (#422)
Browse files Browse the repository at this point in the history
* Add Tag Filter

* Fix Filter check.

* Apply php-cs-fixer changes

---------

Co-authored-by: martineiber <[email protected]>
  • Loading branch information
martineiber and martineiber authored Sep 13, 2024
1 parent de77f5d commit 96a52a2
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/data_index_filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ services:
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\PathFilter:
tags: [ 'pimcore.studio_backend.open_search.filter' ]

Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\TagFilter:
tags: [ 'pimcore.studio_backend.open_search.filter' ]

# DataObject
Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\DataObject\ClassNameFilter:
tags: [ 'pimcore.studio_backend.open_search.data_object.filter' ]
Expand Down
16 changes: 16 additions & 0 deletions doc/03_Grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Available filters are:
| metadata.asset | integer | ID fo the asset |
| system.string | string | Wildcard search can be used |
| system.datetime | integer | `from`, `to`, or `on` |
| system.tag | object | `considerChildTags`, `tags` |



Expand Down Expand Up @@ -73,3 +74,18 @@ Filter by a date column:
]
...
```

Filter by Tags:
```json
...
"columnFilters" [
{
"type": "system.tag",
"filterValue": {
"considerChildTags": true,
"tags": [1,2,3]
}
}
]
...
```
41 changes: 41 additions & 0 deletions src/DataIndex/Filter/TagFilter.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\StudioBackendBundle\DataIndex\Filter;

use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\TagFilterParameterInterface;

/**
* @internal
*/
final class TagFilter implements FilterInterface
{
public function apply(mixed $parameters, QueryInterface $query): QueryInterface
{
if (!$parameters instanceof TagFilterParameterInterface) {
return $query;
}

$filter = $parameters->getTagFilter();

if (!$filter) {
return $query;
}

return $query->filterTags($filter->getTags(), $filter->considerChildTags());
}
}
11 changes: 11 additions & 0 deletions src/DataIndex/Query/AssetQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\FieldType\DateFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\ParentIdFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\PathFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\TagFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\WildcardSearch;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\OrderByField;
Expand Down Expand Up @@ -145,4 +146,14 @@ public function filterDatetime(

return $this;
}

/**
* @param array<int> $tags
*/
public function filterTags(array $tags, bool $considerChildTags): QueryInterface
{
$this->search->addModifier(new TagFilter($tags, $considerChildTags));

return $this;
}
}
11 changes: 11 additions & 0 deletions src/DataIndex/Query/DataObjectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Basic\IdsFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\ParentIdFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\PathFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Filter\Tree\TagFilter;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\FullTextSearch\ElementKeySearch;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByFullPath;
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Sort\Tree\OrderByIndexField;
Expand Down Expand Up @@ -124,4 +125,14 @@ public function orderByIndex(): self

return $this;
}

/**
* @param array<int> $tags
*/
public function filterTags(array $tags, bool $considerChildTags): QueryInterface
{
$this->search->addModifier(new TagFilter($tags, $considerChildTags));

return $this;
}
}
5 changes: 5 additions & 0 deletions src/DataIndex/Query/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public function getSearch(): SearchInterface;
public function orderByPath(string $direction): self;

public function searchByIds(array $ids): self;

/**
* @param array<int> $tags
*/
public function filterTags(array $tags, bool $considerChildTags): self;
}
30 changes: 29 additions & 1 deletion src/Filter/MappedParameter/FilterParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
namespace Pimcore\Bundle\StudioBackendBundle\Filter\MappedParameter;

use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\CollectionParametersInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ColumnFiltersParameterInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\ExcludeFolderParameterInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\PathParameterInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\SortFilter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\SortFilterParameterInterface;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\TagFilterParameter;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\TagFilterParameterInterface;
use function count;

/**
* @internal
Expand All @@ -33,7 +37,8 @@ final class FilterParameter implements
ExcludeFolderParameterInterface,
PathParameterInterface,
ColumnFiltersParameterInterface,
SortFilterParameterInterface
SortFilterParameterInterface,
TagFilterParameterInterface
{
private ?string $path = null;

Expand Down Expand Up @@ -101,6 +106,29 @@ public function getColumnFilterByType(string $type): iterable
}
}

public function getTagFilter(): ?TagFilterParameter
{
$columns = array_filter(
$this->columnFilters,
static fn ($columnFilter) => $columnFilter['type'] === ColumnType::SYSTEM_TAG->value
);

if (count($columns) > 1) {
throw new InvalidArgumentException('More than one tag filter is not allowed');
}

if (isset($columns[0]['filterValue'])) {
$filterValue = $columns[0]['filterValue'];
if (!isset($filterValue['tags'], $filterValue['considerChildTags'])) {
throw new InvalidArgumentException('Invalid tag filter');
}

return new TagFilterParameter($filterValue['tags'], $filterValue['considerChildTags']);
}

return null;
}

public function getFirstColumnFilterByType(string $type): ?ColumnFilter
{
$columns = iterator_to_array($this->getColumnFilterByType($type));
Expand Down
1 change: 1 addition & 0 deletions src/Grid/Column/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ enum ColumnType: string
case SYSTEM_FILE_SIZE = 'system.fileSize';
case SYSTEM_INTEGER = 'system.integer';
case SYSTEM_DATETIME = 'system.datetime';
case SYSTEM_TAG = 'system.tag';
case METADATA_SELECT = 'metadata.select';
case METADATA_INPUT = 'metadata.input';
case METADATA_DATE = 'metadata.date';
Expand Down
42 changes: 42 additions & 0 deletions src/MappedParameter/Filter/TagFilterParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\MappedParameter\Filter;

/**
* @internal
*/
final readonly class TagFilterParameter
{
public function __construct(
private array $tags,
private bool $considerChildTags
) {
}

/**
* @return array<int>
*/
public function getTags(): array
{
return $this->tags;
}

public function considerChildTags(): bool
{
return $this->considerChildTags;
}
}
25 changes: 25 additions & 0 deletions src/MappedParameter/Filter/TagFilterParameterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\MappedParameter\Filter;

/**
* @internal
*/
interface TagFilterParameterInterface
{
public function getTagFilter(): ?TagFilterParameter;
}

0 comments on commit 96a52a2

Please sign in to comment.