add establishment filters

This commit is contained in:
Simon Vieille 2022-04-18 16:02:47 +02:00
parent 0f790db136
commit f8db332058
Signed by: deblan
GPG key ID: 579388D585F70417
3 changed files with 67 additions and 2 deletions

View file

@ -10,6 +10,7 @@ use App\Core\Manager\EntityManager;
use App\Entity\Establishment as Entity;
use App\Factory\EstablishmentFactory as Factory;
use App\Form\EstablishmentType as Type;
use App\Form\EstablishmentFilterType as FilterType;
use App\Repository\EstablishmentRepositoryQuery as RepositoryQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -118,7 +119,7 @@ class EstablishmentAdminController extends CrudController
->setForm('edit', Type::class, [])
->setForm('new', Type::class)
// ->setForm('filter', Type::class)
->setForm('filter', FilterType::class)
->setView('form', 'admin/establishment/_form.html.twig')

View file

@ -0,0 +1,40 @@
<?php
namespace App\Form;
use App\Core\Form\Type\CollectionType;
use App\Entity\Establishment;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Project;
use Doctrine\ORM\EntityRepository;
class EstablishmentFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', null, [
'required' => false,
])
->add('project', EntityType::class, [
'class' => Project::class,
'required' => false,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.label', 'ASC');
},
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => null,
'csrf_protection' => false,
]);
}
}

View file

@ -3,8 +3,9 @@
namespace App\Repository;
use App\Core\Repository\RepositoryQuery;
use Knp\Component\Pager\PaginatorInterface;
use App\Entity\Project;
use App\Repository\EstablishmentRepository as Repository;
use Knp\Component\Pager\PaginatorInterface;
class EstablishmentRepositoryQuery extends RepositoryQuery
{
@ -12,4 +13,27 @@ class EstablishmentRepositoryQuery extends RepositoryQuery
{
parent::__construct($repository, 'e', $paginator);
}
public function withProject(?Project $project): self
{
if (!$project) {
return $this;
}
$keyProject = 'k'.mt_rand();
$keyId = 'id'.mt_rand();
return $this
->innerJoin('.projects', $keyProject)
->andWhere($keyProject.'.id = :'.$keyId)
->setParameter($keyId, $project->getId())
;
}
protected function filterHandler(string $name, $value)
{
if ('project' === $name) {
return $this->withProject($value);
}
}
}