|
- <?php
-
- namespace Lc\SovBundle\Field\Filter;
-
- use Doctrine\ORM\QueryBuilder;
- use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\FormType;
- use Symfony\Component\Form\FormBuilderInterface;
-
- /**
- * @author La clic ! <contact@laclic.fr>
- */
- class DateFilter
- {
- use FilterTrait;
-
- public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
- {
- $builder->add(
- $builder->create(
- str_replace('.', '_', $fieldDto->getProperty()),
- FormType::class,
- array('inherit_data' => true)
- )
- ->add(
- 'dateStart',
- DateType::class,
- array(
- 'widget' => 'single_text',
- 'required' => false,
- )
- )
- ->add(
- 'dateEnd',
- DateType::class,
- array(
- 'widget' => 'single_text',
- 'required' => false,
-
- )
- )
- );
- }
-
- public function applyFilter(
- QueryBuilder $queryBuilder,
- string $fieldProperty,
- \DateTime $dateStart = null,
- \DateTime $dateEnd = null
- ) {
- if ($dateStart) {
- $queryBuilder->andWhere(
- 'entity.'.$fieldProperty.' >= :dateStart'
- )->setParameter('dateStart', $dateStart);
- }
- if ($dateEnd) {
- $dateEnd->setTime(23, 59, 59);
- $queryBuilder->andWhere(
- 'entity.'.$fieldProperty.' <= :dateEnd'
- )->setParameter('dateEnd', $dateEnd);
- }
- }
- }
|