Need to migrate your PHP legacy system to a new framework quickly and without breaking the bank? Get in touch—I'd be happy to help.
#[Route('/some/route', name: 'some_route')]
public function index(Request $request): Response
{
$emailForm->handleRequest($request);
if ($emailForm->isSubmitted() && $emailForm->isValid()) {
$emailAddress = (string) $emailForm->get('email')->getData();
$email = $this->emailRepository->findByEmailAddress($emailAddress);
// return something...
}
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('keyword', TextType::class, [
'label' => $this->translator->trans('event-filter-title-placeholder'),
'attr' => [
],
'required' => false,
'row_attr' => [
'class' => 'form-floating',
],
])
->add('period', CustomEnumType::class, [
'label' => false,
'class' => EventFilterDateRangeEnum::class,
'row_attr' => [
'class' => 'form-floating',
],
'expanded' => true,
'multiple' => false,
])
->add('category', EntityType::class, [
'required' => false,
'class' => Category::class,
'choice_label' => 'title',
'choice_translation_domain' => true,
'autocomplete' => true,
'row_attr' => [
'class' => 'form-floating',
],
])
->add('country', EntityType::class, [
'class' => Country::class,
'choice_label' => 'name',
'row_attr' => [
'class' => 'form-floating',
],
]);
}
TODAY;
private null|Category $category = null;
private null|Country $country = null;
public function getKeyword(): ?string
{
return $this->keyword;
}
public function setKeyword(?string $keyword): void
{
$this->keyword = $keyword;
}
public function getPeriod(): null|EventFilterDateRangeEnum
{
return $this->period;
}
public function setPeriod(null|EventFilterDateRangeEnum $period): void
{
$this->period = $period;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): void
{
$this->category = $category;
}
public function getCountry(): ?Country
{
return $this->country;
}
public function setCountry(?Country $country): void
{
$this->country = $country;
}
}
#[Route(path: '/', name: 'events')]
public function index(Request $request): Response
{
$eventFilterDto = new EventFilterDto();
$eventFilter = $this->createForm(EventFilterType::class, $eventFilterDto);
$events = $this->eventRepository->findByFilter(eventFilterDto: $eventFilterDto, isQuery: true);
$eventFilter->handleRequest($request);
if ($eventFilter->isSubmitted() && $eventFilter->isValid()) {
$events = $this->eventRepository->findByFilter(eventFilterDto: $eventFilterDto, isQuery: true);
return $this->render('events/index.html.twig', [
'eventFilter' => $eventFilter,
'events' => $events
]);
}
return $this->render('events/index.html.twig', [
'eventFilter' => $eventFilter,
'events' => $events
]);
}
/**
* @return Query|array
*/
public function findByFilter(EventFilterDto $eventFilterDto, bool $isQuery = false): Query|array
{
$qb = $this->createQueryBuilder('event');
if ($eventFilterDto->getPeriod() instanceof EventFilterDateRangeEnum) {
$this->findByPeriod(period: $eventFilterDto->getPeriod(), qb: $qb);
}
if ($eventFilterDto->getCategory() instanceof Category) {
$this->findByCategory(category: $eventFilterDto->getCategory(), qb: $qb);
}
if (! empty($eventFilterDto->getKeyword())) {
$this->findByTitle(keyword: $eventFilterDto->getKeyword(), qb: $qb);
}
$qb->andWhere(
$qb->expr()->eq('event.isPrivate', ':false')
)->setParameter('false', false);
$qb->orderBy('event.startAt', Order::Ascending->value);
if ($isQuery) {
return $qb->getQuery();
}
return $qb->getQuery()->getResult();
}
/**
* @return Query|array
*/
public function findByTitle(string $keyword, QueryBuilder $qb = null, bool $isQuery = false): Query|array
{
if (! $qb instanceof QueryBuilder) {
$qb = $this->createQueryBuilder('event');
}
$result = $qb;
$qb->andWhere(
$qb->expr()->like($qb->expr()->lower('event.title'), ':title')
)->setParameter('title', '%' . strtolower($keyword) . '%');
$qb->leftJoin('event.eventGroup', 'event_group');
$qb->orWhere(
$qb->expr()->like($qb->expr()->lower('event_group.name'), ':name')
)->setParameter('name', '%' . strtolower($keyword) . '%');
if ($isQuery) {
return $result->getQuery();
}
return $result->getQuery()->getResult();
}