Created
March 27, 2018 09:50
-
-
Save evgeniy1204/bdad0549833fb03c97b3d9b049ff1a45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AdminBundle\Form\Filter; | |
use Doctrine\ORM\Query\Expr; | |
use Doctrine\ORM\QueryBuilder; | |
use Lexik\Bundle\FormFilterBundle\Filter\FilterBuilderExecuterInterface; | |
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class BookingOptionalFilterType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('name', 'filter_text', array( | |
'label' => 'Nome', | |
'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
if (empty($values['value'])) { | |
return null; | |
} | |
$qb = $filterQuery->getQueryBuilder(); | |
$orx = $qb->expr()->orX(); | |
$values = explode(",", $values['value']); | |
foreach ($values as $k => $value) { | |
$orx->add( | |
$qb->expr()->like( | |
sprintf("LOWER(%s)", "e.publicName"), | |
':name_' . $k | |
) | |
); | |
$qb->setParameter('name_' . $k, sprintf("%%%s%%", strtolower(trim($value)))); | |
} | |
return $filterQuery->createCondition($orx); | |
}, | |
)) | |
->add('ride', 'filter_collection_adapter', array( | |
'type' => new BookingOptionalRideToFilterType(), | |
'label' => 'Corsa', | |
'add_shared' => function (FilterBuilderExecuterInterface $qbe) { | |
$closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) { | |
$filterBuilder->leftJoin('booking.bookingTickets', 'bookingTicket'); | |
$filterBuilder->leftJoin('bookingTicket.rideSegment', 'rideSegment'); | |
$filterBuilder->leftJoin('rideSegment.ride', 'ride'); | |
}; | |
// then use the query builder executor to define the join, the join's alias and things to do on the doctrine query builder. | |
$qbe->addOnce($qbe->getAlias().'.ride', 'ride', $closure); | |
}, | |
)) | |
->add('event', 'filter_collection_adapter', array( | |
'type' => new BookingOptionalEventToFilterType(), | |
'label' => 'Evento', | |
'add_shared' => function (FilterBuilderExecuterInterface $qbe) { | |
$closure = function (QueryBuilder $filterBuilder, $alias, $joinAlias, Expr $expr) { | |
$filterBuilder->leftJoin('ride.eventHasRides', 'eventHasRide', 'WITH', 'eventHasRide.ride = ride.id'); | |
$filterBuilder->leftJoin(sprintf('eventHasRide.%s', $joinAlias), 'event'); | |
}; | |
// then use the query builder executor to define the join, the join's alias and things to do on the doctrine query builder. | |
$qbe->addOnce($qbe->getAlias().'.event', 'event', $closure); | |
}, | |
)) | |
; | |
} | |
public function getName() | |
{ | |
return 'filter'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment