]>
Commit | Line | Data |
---|---|---|
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, [ | |
38 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
39 | $value = $values['value']; | |
40 | ||
41 | $min = (int) ($value['left_number'][0] * $this->user->getConfig()->getReadingSpeed()); | |
42 | $max = (int) ($value['right_number'][0] * $this->user->getConfig()->getReadingSpeed()); | |
43 | ||
44 | $expression = $filterQuery->getExpr()->between($field, $min, $max); | |
45 | ||
46 | return $filterQuery->createCondition($expression); | |
47 | }, | |
48 | 'label' => 'entry.filters.reading_time.label', | |
49 | ]) | |
50 | ->add('createdAt', DateRangeFilterType::class, [ | |
51 | 'left_date_options' => [ | |
52 | 'attr' => [ | |
53 | 'placeholder' => 'dd/mm/yyyy', | |
54 | ], | |
55 | 'format' => 'dd/MM/yyyy', | |
56 | 'widget' => 'single_text', | |
57 | ], | |
58 | 'right_date_options' => [ | |
59 | 'attr' => [ | |
60 | 'placeholder' => 'dd/mm/yyyy', | |
61 | ], | |
62 | 'format' => 'dd/MM/yyyy', | |
63 | 'widget' => 'single_text', | |
64 | ], | |
65 | 'label' => 'entry.filters.created_at.label', | |
66 | ] | |
67 | ) | |
68 | ->add('domainName', TextFilterType::class, [ | |
69 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
70 | $value = $values['value']; | |
71 | if (strlen($value) <= 2 || empty($value)) { | |
72 | return; | |
73 | } | |
74 | $expression = $filterQuery->getExpr()->like($field, $filterQuery->getExpr()->literal('%'.$value.'%')); | |
75 | ||
76 | return $filterQuery->createCondition($expression); | |
77 | }, | |
78 | 'label' => 'entry.filters.domain_label', | |
79 | ]) | |
80 | ->add('isArchived', CheckboxFilterType::class, [ | |
81 | 'label' => 'entry.filters.archived_label', | |
82 | ]) | |
83 | ->add('isStarred', CheckboxFilterType::class, [ | |
84 | 'label' => 'entry.filters.starred_label', | |
85 | ]) | |
86 | ->add('previewPicture', CheckboxFilterType::class, [ | |
87 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
88 | if (false === $values['value']) { | |
89 | return; | |
90 | } | |
91 | ||
92 | $expression = $filterQuery->getExpr()->isNotNull($field); | |
93 | ||
94 | return $filterQuery->createCondition($expression); | |
95 | }, | |
96 | 'label' => 'entry.filters.preview_picture_label', | |
97 | ]) | |
98 | ->add('language', ChoiceFilterType::class, [ | |
99 | 'choices' => array_flip($this->repository->findDistinctLanguageByUser($this->user->getId())), | |
100 | 'choices_as_values' => true, | |
101 | 'label' => 'entry.filters.language_label', | |
102 | ]) | |
103 | ; | |
104 | } | |
105 | ||
106 | public function getBlockPrefix() | |
107 | { | |
108 | return 'entry_filter'; | |
109 | } | |
110 | ||
111 | public function configureOptions(OptionsResolver $resolver) | |
112 | { | |
113 | $resolver->setDefaults([ | |
114 | 'csrf_protection' => false, | |
115 | 'validation_groups' => ['filtering'], | |
116 | ]); | |
117 | } | |
118 | } |