Skip to content

Commit

Permalink
Merge 2a5d537 into 765dfb3
Browse files Browse the repository at this point in the history
  • Loading branch information
CheapHasz committed Jan 17, 2019
2 parents 765dfb3 + 2a5d537 commit b7ceded
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Configuration/ShortFormTypeConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AlterPHP\EasyAdminExtensionBundle\Configuration;

use AlterPHP\EasyAdminExtensionBundle\Form\Type\EasyAdminEmbeddedListType;
use AlterPHP\EasyAdminExtensionBundle\Form\Type\ListFilterType;
use AlterPHP\EasyAdminExtensionBundle\Form\Type\Security\AdminRolesType;
use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface;
use EasyCorp\Bundle\EasyAdminBundle\Form\Util\LegacyFormHelper;
Expand All @@ -23,6 +24,7 @@ class ShortFormTypeConfigPass implements ConfigPassInterface
private static $nativeShortFormTypes = array(
'embedded_list' => EasyAdminEmbeddedListType::class,
'admin_roles' => AdminRolesType::class,
'list_filter' => ListFilterType::class
);

public function __construct(array $customFormTypes = array())
Expand Down
26 changes: 23 additions & 3 deletions src/EventListener/PostQueryBuilderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace AlterPHP\EasyAdminExtensionBundle\EventListener;

use AlterPHP\EasyAdminExtensionBundle\Model\ListFilter;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand All @@ -14,6 +15,7 @@
*/
class PostQueryBuilderSubscriber implements EventSubscriberInterface
{

/**
* @var \AlterPHP\EasyAdminExtensionBundle\Helper\ListFormFiltersHelper
*/
Expand Down Expand Up @@ -115,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 Down Expand Up @@ -170,6 +179,16 @@ protected function filterQueryBuilder(QueryBuilder $queryBuilder, string $field,
$parameter = null;
$filterDqlPart = $field.' IS NOT NULL';
break;
// 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.' ' .$value->getOperator() .' :'.$parameter;
$value = $value->getValue();
break;
// Default is equality
default:
$filterDqlPart = $field.' = :'.$parameter;
Expand Down Expand Up @@ -207,4 +226,5 @@ protected function isFilterAppliable(QueryBuilder $queryBuilder, string $field):

return true;
}

}
58 changes: 58 additions & 0 deletions src/Form/Type/ListFilterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


namespace AlterPHP\EasyAdminExtensionBundle\Form\Type;


use AlterPHP\EasyAdminExtensionBundle\Form\Transformer\Operator\GreaterThanOrEqualModelTransformer;
use AlterPHP\EasyAdminExtensionBundle\Model\ListFilter;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ListFilterType extends AbstractType
{
public static $operators = [
'equals' => '=',
'gt' => '>',
'gte' => '>=',
'lt' => '<',
'lte' => '<=',
];

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('value', $options['input_type'], $options['input_type_options'] + [
'label' => false,
'required' => false
])
->add('operator', HiddenType::class, [
'data' => self::$operators[$options['operator']]
]);
if ($options['property'] !== null) {
$builder
->add('property', HiddenType::class, [
'data' => $options['property']
]);
}

}


public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'operator' => 'equals',
'data_class' => ListFilter::class,
'input_type' => TextType::class,
'input_type_options' => [],
));
$resolver->setDefined([
'property',
]);
$resolver->setAllowedValues('operator', static::$operators);
}
}
51 changes: 51 additions & 0 deletions src/Model/ListFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php


namespace AlterPHP\EasyAdminExtensionBundle\Model;


class ListFilter
{
/** @var string */
protected $operator;
/** @var mixed */
protected $value;
/**
* @var string
*
* Used to override the property, allowing multiple list filter on the same property
*/
protected $property;

public function getOperator()
{
return $this->operator;
}

public function setOperator(string $operator)
{
$this->operator = $operator;
}

public function getValue()
{
return $this->value;
}

public function setValue($value)
{
$this->value = $value;
}

public function getProperty()
{
return $this->property;
}

public function setProperty($property)
{
$this->property = $property;
}


}

0 comments on commit b7ceded

Please sign in to comment.