]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Form\Type; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
6 | use Lexik\Bundle\FormFilterBundle\Filter\FilterOperands; | |
7 | use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\CheckboxFilterType; | |
8 | use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\ChoiceFilterType; | |
9 | use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\DateRangeFilterType; | |
10 | use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\NumberRangeFilterType; | |
11 | use Lexik\Bundle\FormFilterBundle\Filter\Form\Type\TextFilterType; | |
12 | use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface; | |
13 | use Symfony\Component\Form\AbstractType; | |
14 | use Symfony\Component\Form\FormBuilderInterface; | |
15 | use Symfony\Component\HttpFoundation\Response; | |
16 | use Symfony\Component\OptionsResolver\OptionsResolver; | |
17 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
18 | ||
19 | class EntryFilterType extends AbstractType | |
20 | { | |
21 | private $user; | |
22 | private $repository; | |
23 | ||
24 | /** | |
25 | * Repository & user are used to get a list of language entries for this user. | |
26 | */ | |
27 | public function __construct(EntityRepository $entryRepository, TokenStorageInterface $tokenStorage) | |
28 | { | |
29 | $this->repository = $entryRepository; | |
30 | ||
31 | $this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null; | |
32 | ||
33 | if (null === $this->user || !\is_object($this->user)) { | |
34 | return; | |
35 | } | |
36 | } | |
37 | ||
38 | public function buildForm(FormBuilderInterface $builder, array $options) | |
39 | { | |
40 | $builder | |
41 | ->add('readingTime', NumberRangeFilterType::class, [ | |
42 | 'left_number_options' => [ | |
43 | 'condition_operator' => FilterOperands::OPERATOR_GREATER_THAN_EQUAL, | |
44 | 'attr' => ['min' => 0], | |
45 | ], | |
46 | 'right_number_options' => [ | |
47 | 'condition_operator' => FilterOperands::OPERATOR_LOWER_THAN_EQUAL, | |
48 | 'attr' => ['min' => 0], | |
49 | ], | |
50 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
51 | $lower = $values['value']['left_number'][0]; | |
52 | $upper = $values['value']['right_number'][0]; | |
53 | ||
54 | $min = (int) ($lower * $this->user->getConfig()->getReadingSpeed() / 200); | |
55 | $max = (int) ($upper * $this->user->getConfig()->getReadingSpeed() / 200); | |
56 | ||
57 | if (null === $lower && null === $upper) { | |
58 | // no value? no filter | |
59 | return; | |
60 | } elseif (null === $lower && null !== $upper) { | |
61 | // only lower value is defined: query all entries with reading LOWER THAN this value | |
62 | $expression = $filterQuery->getExpr()->lte($field, $max); | |
63 | } elseif (null !== $lower && null === $upper) { | |
64 | // only upper value is defined: query all entries with reading GREATER THAN this value | |
65 | $expression = $filterQuery->getExpr()->gte($field, $min); | |
66 | } else { | |
67 | // both value are defined, perform a between | |
68 | $expression = $filterQuery->getExpr()->between($field, $min, $max); | |
69 | } | |
70 | ||
71 | return $filterQuery->createCondition($expression); | |
72 | }, | |
73 | 'label' => 'entry.filters.reading_time.label', | |
74 | ]) | |
75 | ->add('createdAt', DateRangeFilterType::class, [ | |
76 | 'left_date_options' => [ | |
77 | 'attr' => [ | |
78 | 'placeholder' => 'yyyy-mm-dd', | |
79 | ], | |
80 | 'format' => 'yyyy-MM-dd', | |
81 | 'widget' => 'single_text', | |
82 | ], | |
83 | 'right_date_options' => [ | |
84 | 'attr' => [ | |
85 | 'placeholder' => 'yyyy-mm-dd', | |
86 | ], | |
87 | 'format' => 'yyyy-MM-dd', | |
88 | 'widget' => 'single_text', | |
89 | ], | |
90 | 'label' => 'entry.filters.created_at.label', | |
91 | ]) | |
92 | ->add('domainName', TextFilterType::class, [ | |
93 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
94 | $value = $values['value']; | |
95 | if (\strlen($value) <= 2 || empty($value)) { | |
96 | return; | |
97 | } | |
98 | $expression = $filterQuery->getExpr()->like($field, $filterQuery->getExpr()->lower($filterQuery->getExpr()->literal('%' . $value . '%'))); | |
99 | ||
100 | return $filterQuery->createCondition($expression); | |
101 | }, | |
102 | 'label' => 'entry.filters.domain_label', | |
103 | ]) | |
104 | ->add('httpStatus', TextFilterType::class, [ | |
105 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
106 | $value = $values['value']; | |
107 | if (false === \array_key_exists($value, Response::$statusTexts)) { | |
108 | return; | |
109 | } | |
110 | ||
111 | $paramName = sprintf('%s', str_replace('.', '_', $field)); | |
112 | $expression = $filterQuery->getExpr()->eq($field, ':' . $paramName); | |
113 | $parameters = [$paramName => $value]; | |
114 | ||
115 | return $filterQuery->createCondition($expression, $parameters); | |
116 | }, | |
117 | 'label' => 'entry.filters.http_status_label', | |
118 | ]) | |
119 | ->add('isArchived', CheckboxFilterType::class, [ | |
120 | 'label' => 'entry.filters.archived_label', | |
121 | ]) | |
122 | ->add('isStarred', CheckboxFilterType::class, [ | |
123 | 'label' => 'entry.filters.starred_label', | |
124 | ]) | |
125 | ->add('isUnread', CheckboxFilterType::class, [ | |
126 | 'label' => 'entry.filters.unread_label', | |
127 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
128 | if (false === $values['value']) { | |
129 | return; | |
130 | } | |
131 | ||
132 | $expression = $filterQuery->getExpr()->eq('e.isArchived', 'false'); | |
133 | ||
134 | return $filterQuery->createCondition($expression); | |
135 | }, | |
136 | ]) | |
137 | ->add('previewPicture', CheckboxFilterType::class, [ | |
138 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
139 | if (false === $values['value']) { | |
140 | return; | |
141 | } | |
142 | ||
143 | $expression = $filterQuery->getExpr()->isNotNull($field); | |
144 | ||
145 | return $filterQuery->createCondition($expression); | |
146 | }, | |
147 | 'label' => 'entry.filters.preview_picture_label', | |
148 | ]) | |
149 | ->add('isPublic', CheckboxFilterType::class, [ | |
150 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
151 | if (false === $values['value']) { | |
152 | return; | |
153 | } | |
154 | ||
155 | // is_public isn't a real field | |
156 | // we should use the "uid" field to determine if the entry has been made public | |
157 | $expression = $filterQuery->getExpr()->isNotNull($values['alias'] . '.uid'); | |
158 | ||
159 | return $filterQuery->createCondition($expression); | |
160 | }, | |
161 | 'label' => 'entry.filters.is_public_label', | |
162 | ]) | |
163 | ->add('language', ChoiceFilterType::class, [ | |
164 | 'choices' => array_flip($this->repository->findDistinctLanguageByUser($this->user->getId())), | |
165 | 'label' => 'entry.filters.language_label', | |
166 | ]) | |
167 | ; | |
168 | } | |
169 | ||
170 | public function getBlockPrefix() | |
171 | { | |
172 | return 'entry_filter'; | |
173 | } | |
174 | ||
175 | public function configureOptions(OptionsResolver $resolver) | |
176 | { | |
177 | $resolver->setDefaults([ | |
178 | 'csrf_protection' => false, | |
179 | 'validation_groups' => ['filtering'], | |
180 | ]); | |
181 | } | |
182 | } |