]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php
Merge pull request #1804 from wallabag/j0k3r-patch-1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Form / Type / EntryFilterType.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Form\Type;
4
5 use Doctrine\ORM\EntityRepository;
6 use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;
7 use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\NumberRangeFilterType;
8 use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\DateRangeFilterType;
9 use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\TextFilterType;
10 use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\CheckboxFilterType;
11 use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\ChoiceFilterType;
12 use Symfony\Component\Form\AbstractType;
13 use Symfony\Component\Form\FormBuilderInterface;
14 use Symfony\Component\OptionsResolver\OptionsResolver;
15 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
16
17 class EntryFilterType extends AbstractType
18 {
19 private $user;
20 private $repository;
21
22 /**
23 * Repository & user are used to get a list of language entries for this user.
24 *
25 * @param EntityRepository $entryRepository
26 * @param TokenStorage $token
27 */
28 public function __construct(EntityRepository $entryRepository, TokenStorage $token)
29 {
30 $this->repository = $entryRepository;
31 $this->user = $token->getToken()->getUser();
32 }
33
34 public function buildForm(FormBuilderInterface $builder, array $options)
35 {
36 $builder
37 ->add('readingTime', NumberRangeFilterType::class, array(
38 'label' => 'entry.filters.reading_time.label',
39 ))
40 ->add('createdAt', DateRangeFilterType::class, array(
41 'left_date_options' => array(
42 'attr' => array(
43 'placeholder' => 'dd/mm/yyyy',
44 ),
45 'format' => 'dd/MM/yyyy',
46 'widget' => 'single_text',
47 ),
48 'right_date_options' => array(
49 'attr' => array(
50 'placeholder' => 'dd/mm/yyyy',
51 ),
52 'format' => 'dd/MM/yyyy',
53 'widget' => 'single_text',
54 ),
55 'label' => 'entry.filters.created_at.label',
56 )
57 )
58 ->add('domainName', TextFilterType::class, array(
59 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
60 $value = $values['value'];
61 if (strlen($value) <= 2 || empty($value)) {
62 return;
63 }
64 $expression = $filterQuery->getExpr()->like($field, $filterQuery->getExpr()->literal('%'.$value.'%'));
65
66 return $filterQuery->createCondition($expression);
67 },
68 'label' => 'entry.filters.domain_label',
69 ))
70 ->add('isArchived', CheckboxFilterType::class, array(
71 'label' => 'entry.filters.archived_label',
72 ))
73 ->add('isStarred', CheckboxFilterType::class, array(
74 'label' => 'entry.filters.starred_label',
75 ))
76 ->add('previewPicture', CheckboxFilterType::class, array(
77 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) {
78 if (false === $values['value']) {
79 return;
80 }
81
82 $expression = $filterQuery->getExpr()->isNotNull($field);
83
84 return $filterQuery->createCondition($expression);
85 },
86 'label' => 'entry.filters.preview_picture_label',
87 ))
88 ->add('language', ChoiceFilterType::class, array(
89 'choices' => array_flip($this->repository->findDistinctLanguageByUser($this->user->getId())),
90 'choices_as_values' => true,
91 'label' => 'entry.filters.language_label',
92 ))
93 ;
94 }
95
96 public function getBlockPrefix()
97 {
98 return 'entry_filter';
99 }
100
101 public function configureOptions(OptionsResolver $resolver)
102 {
103 $resolver->setDefaults(array(
104 'csrf_protection' => false,
105 'validation_groups' => array('filtering'),
106 ));
107 }
108 }