src/Form/Channel/Filter/ChannelTrainingFilter.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the nellapp package.
  4. *
  5. * (c) Benjamin Georgeault <https://www.drosalys-web.fr/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace App\Form\Channel\Filter;
  11. use App\Entity\Account\Advice;
  12. use Spiriit\Bundle\FormFilterBundle\Filter\Doctrine\ORMQuery;
  13. use Spiriit\Bundle\FormFilterBundle\Filter\Form\Type\ChoiceFilterType;
  14. use Spiriit\Bundle\FormFilterBundle\Filter\Form\Type\TextFilterType;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. /**
  19. * Class ChannelFilter
  20. *
  21. * @author Benjamin Georgeault
  22. */
  23. class ChannelTrainingFilter extends AbstractType
  24. {
  25. public function buildForm(FormBuilderInterface $builder, array $options): void
  26. {
  27. $builder
  28. ->add('order', ChoiceFilterType::class, [
  29. 'label' => false,
  30. 'required' => true,
  31. 'choices' => [
  32. 'Trier par' => null,
  33. 'Les plus récentes' => 'newest',
  34. 'Les plus anciennes' => 'oldest',
  35. 'Les plus populaires' => 'most-popular',
  36. ],
  37. 'attr' => [
  38. 'class' => 'select-order-channel-trainings',
  39. ],
  40. 'choice_attr' => function($key, $val, $index) {
  41. if ($key === null) {
  42. return ['disabled' => 'disabled', 'hidden' => 'hidden'];
  43. }
  44. return [];
  45. },
  46. 'apply_filter' => function (ORMQuery $filterQuery, $field, $values) {
  47. if (empty($values['value'])) {
  48. return null;
  49. }
  50. if ($values['value'] === 'newest') {
  51. return $filterQuery->getQueryBuilder()->orderBy($filterQuery->getRootAlias().'.createdAt', 'DESC');
  52. } elseif ($values['value'] === 'oldest') {
  53. return $filterQuery->getQueryBuilder()->orderBy($filterQuery->getRootAlias().'.createdAt', 'ASC');
  54. } elseif ($values['value'] === 'most-popular') {
  55. return $filterQuery->getQueryBuilder()
  56. ->addSelect('avg(a.note) AS HIDDEN avgmark')
  57. ->leftJoin(Advice::class, 'a', 'WITH', $filterQuery->getRootAlias() . ' = a.training')
  58. ->groupBy($filterQuery->getRootAlias() . '.id')
  59. ->orderBy('avgmark', 'DESC')
  60. ->addOrderBy($filterQuery->getRootAlias() . '.createdAt', 'DESC');
  61. }
  62. return null;
  63. },
  64. ])
  65. ->add('search', TextFilterType::class, [
  66. 'label' => false,
  67. 'required' => false,
  68. 'attr' => [
  69. 'placeholder' => 'Rechercher une formation',
  70. 'autocomplete' => 'off',
  71. ],
  72. 'apply_filter' => function (ORMQuery $filterQuery, $field, $values) {
  73. return null;
  74. },
  75. ])
  76. ;
  77. }
  78. public function configureOptions(OptionsResolver $resolver): void
  79. {
  80. $resolver->setDefaults([
  81. 'method' => 'GET',
  82. 'csrf_protection' => false,
  83. 'validation_groups' => [
  84. 'filtering',
  85. ],
  86. ]);
  87. }
  88. public function getBlockPrefix(): string
  89. {
  90. return 'channel_channel_training_filter';
  91. }
  92. }