Skip to content

Commit

Permalink
Added ListFilter handling in PostQueryBuilderSubscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Pascal Devierne committed Dec 12, 2018
1 parent f4f4905 commit c3c788a
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions src/EventListener/PostQueryBuilderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace AlterPHP\EasyAdminExtensionBundle\EventListener;

use AlterPHP\EasyAdminExtensionBundle\Form\Transformer\Operator\GreaterThanModelTransformer;
use AlterPHP\EasyAdminExtensionBundle\Form\Transformer\Operator\GreaterThanOrEqualModelTransformer;
use AlterPHP\EasyAdminExtensionBundle\Form\Transformer\Operator\LowerThanModelTransformer;
use AlterPHP\EasyAdminExtensionBundle\Form\Transformer\Operator\LowerThanOrEqualModelTransformer;
use AlterPHP\EasyAdminExtensionBundle\Model\ListFilter;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\QueryBuilder;
Expand All @@ -18,12 +15,6 @@
*/
class PostQueryBuilderSubscriber implements EventSubscriberInterface
{
protected static $operators = [
GreaterThanModelTransformer::OPERATOR_PREFIX => '>',
GreaterThanOrEqualModelTransformer::OPERATOR_PREFIX => '>=',
LowerThanModelTransformer::OPERATOR_PREFIX => '<',
LowerThanOrEqualModelTransformer::OPERATOR_PREFIX => '<=',
];

/**
* @var \AlterPHP\EasyAdminExtensionBundle\Helper\ListFormFiltersHelper
Expand Down Expand Up @@ -126,21 +117,28 @@ protected function applyRequestFilters(QueryBuilder $queryBuilder, array $filter
protected function applyFormFilters(QueryBuilder $queryBuilder, array $filters = array())
{
foreach ($filters as $field => $value) {

$value = $this->filterEasyadminAutocompleteValue($value);
// Empty string and numeric keys is considered as "not applied filter"
if (null === $value || '' === $value || \is_int($field)) {
continue;
}

// Add root entity alias if none provided
$field = false === \strpos($field, '.') ? $queryBuilder->getRootAlias().'.'.$field : $field;
$property = $field;
if ($value instanceof ListFilter && $value->getProperty()) {
// if a property is specified in the ListFilter, it is on that property that we must filter
$property = $queryBuilder->getRootAlias().'.' .$value->getProperty();
}
// Checks if filter is directly appliable on queryBuilder
if (!$this->isFilterAppliable($queryBuilder, $field)) {
if (!$this->isFilterAppliable($queryBuilder, $property)) {
continue;
}
// Sanitize parameter name
$parameter = 'form_filter_'.\str_replace('.', '_', $field);

$this->filterQueryBuilder($queryBuilder, $field, $parameter, $value);
$this->filterQueryBuilder($queryBuilder, $property, $parameter, $value);
}
}

Expand All @@ -167,7 +165,7 @@ protected function filterQueryBuilder(QueryBuilder $queryBuilder, string $field,
// Multiple values leads to IN statement
case $value instanceof Collection:
case \is_array($value):
if (0 < \count($value)) {
if (0 < count($value)) {
$filterDqlPart = $field.' IN (:'.$parameter.')';
}
break;
Expand All @@ -181,14 +179,15 @@ protected function filterQueryBuilder(QueryBuilder $queryBuilder, string $field,
$parameter = null;
$filterDqlPart = $field.' IS NOT NULL';
break;
// Special case if value has an operatorPrefix
case $operatorPrefix = $this->getOperatorPrefix($value):
// get value without prefix
$value = \substr($value, \strlen($operatorPrefix));

$operator = static::$operators[$operatorPrefix];
// Special case if value is a ListFilter
case $value instanceof ListFilter:
if ($value->getValue() === null || $value->getValue() === '') {
// Break if there is not value
break;
}

$filterDqlPart = $field.' '.$operator.' :'.$parameter;
$filterDqlPart = $field.' ' .$value->getOperator() .' :'.$parameter;
$value = $value->getValue();
break;
// Default is equality
default:
Expand Down Expand Up @@ -228,16 +227,4 @@ protected function isFilterAppliable(QueryBuilder $queryBuilder, string $field):
return true;
}

protected function getOperatorPrefix($value): string
{
$operatorPrefixes = \array_keys(static::$operators);

foreach ($operatorPrefixes as $operatorPrefix) {
if (0 === \strpos($value, $operatorPrefix)) {
return $operatorPrefix;
}
}

return '';
}
}

0 comments on commit c3c788a

Please sign in to comment.