Skip to content

Commit

Permalink
Added filter to filter parts by storage location
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Oct 3, 2023
1 parent 0070860 commit 852624a
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 63 deletions.
66 changes: 3 additions & 63 deletions src/ApiPlatform/Filter/EntityFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class EntityFilter extends AbstractFilter

public function __construct(
ManagerRegistry $managerRegistry,
private NodesListBuilder $nodesListBuilder,
private EntityManagerInterface $entityManager,
private readonly EntityFilterHelper $filter_helper,
LoggerInterface $logger = null,
?array $properties = null,
?NameConverterInterface $nameConverter = null
Expand Down Expand Up @@ -72,77 +71,18 @@ protected function filterProperty(
return;
}

$elements = $this->valueToEntityArray($value, $target_class);
$elements = $this->filter_helper->valueToEntityArray($value, $target_class);

$parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters
$queryBuilder
->andWhere(sprintf('o.%s IN (:%s)', $property, $parameterName))
->setParameter($parameterName, $elements);
}

private function valueToEntityArray(string $value, string $target_class): array
{
//Convert value to IDs:
$elements = [];

//Split the given value by comm
foreach (explode(',', $value) as $id) {
if (trim($id) === '') {
continue;
}

//Check if the given value ends with a plus, then we want to include all direct children
$include_children = false;
$include_recursive = false;
if (str_ends_with($id, '++')) { //Plus Plus means include all children recursively
$id = substr($id, 0, -2);
$include_recursive = true;
} elseif (str_ends_with($id, '+')) {
$id = substr($id, 0, -1);
$include_children = true;
}

//Get a (shallow) reference to the entitity
$element = $this->entityManager->getReference($target_class, (int) $id);
$elements[] = $element;

//If $element is not structural we are done
if (!is_a($element, AbstractStructuralDBElement::class)) {
continue;
}

//Get the recursive list of children
if ($include_recursive) {
$elements = array_merge($elements, $this->nodesListBuilder->getChildrenFlatList($element));
} elseif ($include_children) {
$elements = array_merge($elements, $element->getChildren()->toArray());
}
}

return $elements;
}

public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}

$description = [];
foreach ($this->properties as $property => $strategy) {
$description["$property"] = [
'property' => $property,
'type' => Type::BUILTIN_TYPE_STRING,
'required' => false,
'description' => 'Filter using a comma seperated list of element IDs. Use + to include all direct children and ++ to include all children recursively.',
'openapi' => [
'example' => '',
'allowReserved' => false,// if true, query parameters will be not percent-encoded
'allowEmptyValue' => true,
'explode' => false, // to be true, the type must be Type::BUILTIN_TYPE_ARRAY, ?product=blue,green will be ?product=blue&product=green
],
];
}
return $description;
return $this->filter_helper->getDescription($this->properties);
}
}
104 changes: 104 additions & 0 deletions src/ApiPlatform/Filter/EntityFilterHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);


namespace App\ApiPlatform\Filter;

use App\Entity\Base\AbstractStructuralDBElement;
use App\Services\Trees\NodesListBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PropertyInfo\Type;

class EntityFilterHelper
{
public function __construct(private NodesListBuilder $nodesListBuilder,
private EntityManagerInterface $entityManager)
{

}

public function valueToEntityArray(string $value, string $target_class): array
{
//Convert value to IDs:
$elements = [];

//Split the given value by comm
foreach (explode(',', $value) as $id) {
if (trim($id) === '') {
continue;
}

//Check if the given value ends with a plus, then we want to include all direct children
$include_children = false;
$include_recursive = false;
if (str_ends_with($id, '++')) { //Plus Plus means include all children recursively
$id = substr($id, 0, -2);
$include_recursive = true;
} elseif (str_ends_with($id, '+')) {
$id = substr($id, 0, -1);
$include_children = true;
}

//Get a (shallow) reference to the entitity
$element = $this->entityManager->getReference($target_class, (int) $id);
$elements[] = $element;

//If $element is not structural we are done
if (!is_a($element, AbstractStructuralDBElement::class)) {
continue;
}

//Get the recursive list of children
if ($include_recursive) {
$elements = array_merge($elements, $this->nodesListBuilder->getChildrenFlatList($element));
} elseif ($include_children) {
$elements = array_merge($elements, $element->getChildren()->toArray());
}
}

return $elements;
}

public function getDescription(array $properties): array
{
if (!$properties) {
return [];
}

$description = [];
foreach ($properties as $property => $strategy) {
$description["$property"] = [
'property' => $property,
'type' => Type::BUILTIN_TYPE_STRING,
'required' => false,
'description' => 'Filter using a comma seperated list of element IDs. Use + to include all direct children and ++ to include all children recursively.',
'openapi' => [
'example' => '',
'allowReserved' => false,// if true, query parameters will be not percent-encoded
'allowEmptyValue' => true,
'explode' => false, // to be true, the type must be Type::BUILTIN_TYPE_ARRAY, ?product=blue,green will be ?product=blue&product=green
],
];
}
return $description;
}
}
83 changes: 83 additions & 0 deletions src/ApiPlatform/Filter/PartStoragelocationFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);


namespace App\ApiPlatform\Filter;

use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parts\StorageLocation;
use App\Services\Trees\NodesListBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

class PartStoragelocationFilter extends AbstractFilter
{

public function __construct(
ManagerRegistry $managerRegistry,
private readonly EntityFilterHelper $filter_helper,
LoggerInterface $logger = null,
?array $properties = null,
?NameConverterInterface $nameConverter = null
) {
parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
}

protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
Operation $operation = null,
array $context = []
): void {
//Do not check for mapping here, as we are using a virtual property
if (
!$this->isPropertyEnabled($property, $resourceClass)
) {
return;
}

$elements = $this->filter_helper->valueToEntityArray($value, StorageLocation::class);

$parameterName = $queryNameGenerator->generateParameterName($property); // Generate a unique parameter name to avoid collisions with other filters
$queryBuilder
->leftJoin('o.partLots', 'partLots')
->andWhere(sprintf('partLots.storage_location IN (:%s)', $parameterName))
->setParameter($parameterName, $elements);
}



public function getDescription(string $resourceClass): array
{
return $this->filter_helper->getDescription($this->properties);
}
}
2 changes: 2 additions & 0 deletions src/Entity/Parts/Part.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use App\ApiPlatform\DocumentedAPIProperty;
use App\ApiPlatform\Filter\EntityFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\ApiPlatform\Filter\PartStoragelocationFilter;
use App\Entity\Attachments\AttachmentTypeAttachment;
use App\Repository\PartRepository;
use Doctrine\DBAL\Types\Types;
Expand Down Expand Up @@ -93,6 +94,7 @@
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(EntityFilter::class, properties: ["category", "footprint", "manufacturer", "partUnit"])]
#[ApiFilter(PartStoragelocationFilter::class, properties: ["storage_location"])]
#[ApiFilter(LikeFilter::class, properties: ["name", "comment", "description", "ipn", "tags", "manufacturer_product_number"])]
#[ApiFilter(BooleanFilter::class, properties: ["favorite" , "needs_review"])]
#[ApiFilter(RangeFilter::class, properties: ["mass", "minamount"])]
Expand Down

0 comments on commit 852624a

Please sign in to comment.