Added filters for creationDate and lastModified state

This commit is contained in:
Jan Böhmer 2022-08-20 00:10:41 +02:00
parent 798eb4c1bc
commit d3a42cd989
6 changed files with 137 additions and 9 deletions

View file

@ -0,0 +1,10 @@
<?php
namespace App\DataTables\Filters\Constraints;
/**
* An alias of NumberConstraint to use to filter on a DateTime
*/
class DateTimeConstraint extends NumberConstraint
{
}

View file

@ -12,13 +12,13 @@ class NumberConstraint extends AbstractConstraint
/** /**
* The value1 used for comparison (this is the main one used for all mono-value comparisons) * The value1 used for comparison (this is the main one used for all mono-value comparisons)
* @var float|null * @var float|null|int|\DateTimeInterface
*/ */
protected $value1; protected $value1;
/** /**
* The second value used when operator is RANGE; this is the upper bound of the range * The second value used when operator is RANGE; this is the upper bound of the range
* @var float|null * @var float|null|int|\DateTimeInterface
*/ */
protected $value2; protected $value2;
@ -28,7 +28,7 @@ class NumberConstraint extends AbstractConstraint
protected $operator; protected $operator;
/** /**
* @return float|mixed|null * @return float|int|null|\DateTimeInterface
*/ */
public function getValue1() public function getValue1()
{ {
@ -36,7 +36,7 @@ class NumberConstraint extends AbstractConstraint
} }
/** /**
* @param float|mixed|null $value1 * @param float|int|\DateTimeInterface|null $value1
*/ */
public function setValue1($value1): void public function setValue1($value1): void
{ {
@ -44,7 +44,7 @@ class NumberConstraint extends AbstractConstraint
} }
/** /**
* @return float|mixed|null * @return float|int|null
*/ */
public function getValue2() public function getValue2()
{ {
@ -52,7 +52,7 @@ class NumberConstraint extends AbstractConstraint
} }
/** /**
* @param float|mixed|null $value2 * @param float|int|null $value2
*/ */
public function setValue2($value2): void public function setValue2($value2): void
{ {
@ -60,7 +60,7 @@ class NumberConstraint extends AbstractConstraint
} }
/** /**
* @return mixed|string * @return string
*/ */
public function getOperator(): string public function getOperator(): string
{ {
@ -68,7 +68,7 @@ class NumberConstraint extends AbstractConstraint
} }
/** /**
* @param mixed|string $operator * @param string $operator
*/ */
public function setOperator(?string $operator): void public function setOperator(?string $operator): void
{ {

View file

@ -3,6 +3,7 @@
namespace App\DataTables\Filters; namespace App\DataTables\Filters;
use App\DataTables\Filters\Constraints\BooleanConstraint; use App\DataTables\Filters\Constraints\BooleanConstraint;
use App\DataTables\Filters\Constraints\DateTimeConstraint;
use App\DataTables\Filters\Constraints\NumberConstraint; use App\DataTables\Filters\Constraints\NumberConstraint;
use App\DataTables\Filters\Constraints\TextConstraint; use App\DataTables\Filters\Constraints\TextConstraint;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
@ -27,6 +28,12 @@ class PartFilter implements FilterInterface
/** @var NumberConstraint */ /** @var NumberConstraint */
protected $mass; protected $mass;
/** @var DateTimeConstraint */
protected $lastModified;
/** @var DateTimeConstraint */
protected $addedDate;
/** /**
* @return BooleanConstraint|false * @return BooleanConstraint|false
*/ */
@ -58,13 +65,33 @@ class PartFilter implements FilterInterface
return $this->description; return $this->description;
} }
/**
* @return DateTimeConstraint
*/
public function getLastModified(): DateTimeConstraint
{
return $this->lastModified;
}
/**
* @return DateTimeConstraint
*/
public function getAddedDate(): DateTimeConstraint
{
return $this->addedDate;
}
public function __construct() public function __construct()
{ {
$this->favorite = new BooleanConstraint('part.favorite'); $this->favorite =
$this->needsReview = new BooleanConstraint('part.needs_review'); $this->needsReview = new BooleanConstraint('part.needs_review');
$this->mass = new NumberConstraint('part.mass'); $this->mass = new NumberConstraint('part.mass');
$this->name = new TextConstraint('part.name'); $this->name = new TextConstraint('part.name');
$this->description = new TextConstraint('part.description'); $this->description = new TextConstraint('part.description');
$this->addedDate = new DateTimeConstraint('part.addedDate');
$this->lastModified = new DateTimeConstraint('part.lastModified');
} }
public function apply(QueryBuilder $queryBuilder): void public function apply(QueryBuilder $queryBuilder): void

View file

@ -0,0 +1,76 @@
<?php
namespace App\Form\Filters\Constraints;
use App\DataTables\Filters\Constraints\DateTimeConstraint;
use App\DataTables\Filters\Constraints\NumberConstraint;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DateTimeConstraintType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'compound' => true,
'data_class' => DateTimeConstraint::class,
'text_suffix' => '', // An suffix which is attached as text-append to the input group. This can for example be used for units
'value1_options' => [], // Options for the first value input
'value2_options' => [], // Options for the second value input
]);
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$choices = [
'' => '',
'=' => '=',
'!=' => '!=',
'<' => '<',
'>' => '>',
'<=' => '<=',
'>=' => '>=',
'BETWEEN' => 'BETWEEN',
];
$builder->add('value1', DateTimeType::class, array_merge_recursive([
'label' => 'filter.datetime_constraint.value1',
'attr' => [
'placeholder' => 'filter.datetime_constraint.value1',
],
'required' => false,
'html5' => true,
'widget' => 'single_text',
], $options['value1_options']));
$builder->add('value2', DateTimeType::class, array_merge_recursive([
'label' => 'filter.datetime_constraint.value2',
'attr' => [
'placeholder' => 'filter.datetime_constraint.value2',
],
'required' => false,
'html5' => true,
'widget' => 'single_text',
], $options['value2_options']));
$builder->add('operator', ChoiceType::class, [
'label' => 'filter.datetime_constraint.operator',
'choices' => $choices,
'required' => false,
]);
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
parent::buildView($view, $form, $options);
$view->vars['text_suffix'] = $options['text_suffix'];
}
}

View file

@ -4,9 +4,11 @@ namespace App\Form\Filters;
use App\DataTables\Filters\PartFilter; use App\DataTables\Filters\PartFilter;
use App\Form\Filters\Constraints\BooleanConstraintType; use App\Form\Filters\Constraints\BooleanConstraintType;
use App\Form\Filters\Constraints\DateTimeConstraintType;
use App\Form\Filters\Constraints\NumberConstraintType; use App\Form\Filters\Constraints\NumberConstraintType;
use App\Form\Filters\Constraints\TextConstraintType; use App\Form\Filters\Constraints\TextConstraintType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\SubmitButton; use Symfony\Component\Form\SubmitButton;
@ -47,6 +49,15 @@ class PartFilterType extends AbstractType
'label' => 'part.edit.description', 'label' => 'part.edit.description',
]); ]);
$builder->add('lastModified', DateTimeConstraintType::class, [
'label' => 'lastModified'
]);
$builder->add('addedDate', DateTimeConstraintType::class, [
'label' => 'createdAt'
]);
$builder->add('submit', SubmitType::class, [ $builder->add('submit', SubmitType::class, [
'label' => 'Update', 'label' => 'Update',
]); ]);

View file

@ -18,4 +18,8 @@
<span class="input-group-text">{{ form.vars["text_suffix"] }}</span> <span class="input-group-text">{{ form.vars["text_suffix"] }}</span>
{% endif %} {% endif %}
</div> </div>
{% endblock %}
{% block date_time_constraint_widget %}
{{ block('number_constraint_widget') }}
{% endblock %} {% endblock %}