]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Filter; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
6 | use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface; | |
7 | use Symfony\Component\Form\AbstractType; | |
8 | use Symfony\Component\Form\FormBuilderInterface; | |
9 | use Symfony\Component\OptionsResolver\OptionsResolver; | |
10 | use Wallabag\UserBundle\Entity\User; | |
11 | ||
12 | class EntryFilterType extends AbstractType | |
13 | { | |
14 | private $user; | |
15 | private $repository; | |
16 | ||
17 | /** | |
18 | * Repository & user are used to get a list of language entries for this user. | |
19 | * | |
20 | * @param EntityRepository $entryRepository | |
21 | * @param User $user | |
22 | */ | |
23 | public function __construct(EntityRepository $entryRepository, User $user) | |
24 | { | |
25 | $this->repository = $entryRepository; | |
26 | $this->user = $user; | |
27 | } | |
28 | ||
29 | public function buildForm(FormBuilderInterface $builder, array $options) | |
30 | { | |
31 | $builder | |
32 | ->add('readingTime', 'filter_number_range') | |
33 | ->add('createdAt', 'filter_date_range', array( | |
34 | 'left_date_options' => array( | |
35 | 'attr' => array( | |
36 | 'placeholder' => 'dd/mm/yyyy', | |
37 | ), | |
38 | 'format' => 'dd/MM/yyyy', | |
39 | 'widget' => 'single_text', | |
40 | ), | |
41 | 'right_date_options' => array( | |
42 | 'attr' => array( | |
43 | 'placeholder' => 'dd/mm/yyyy', | |
44 | ), | |
45 | 'format' => 'dd/MM/yyyy', | |
46 | 'widget' => 'single_text', | |
47 | ), | |
48 | ) | |
49 | ) | |
50 | ->add('domainName', 'filter_text', array( | |
51 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
52 | $value = $values['value']; | |
53 | if (strlen($value) <= 2 || empty($value)) { | |
54 | return; | |
55 | } | |
56 | $expression = $filterQuery->getExpr()->like($field, $filterQuery->getExpr()->literal('%'.$value.'%')); | |
57 | ||
58 | return $filterQuery->createCondition($expression); | |
59 | }, | |
60 | )) | |
61 | ->add('isArchived', 'filter_checkbox') | |
62 | ->add('isStarred', 'filter_checkbox') | |
63 | ->add('previewPicture', 'filter_checkbox', array( | |
64 | 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { | |
65 | if (false === $values['value']) { | |
66 | return; | |
67 | } | |
68 | ||
69 | $expression = $filterQuery->getExpr()->isNotNull($field); | |
70 | ||
71 | return $filterQuery->createCondition($expression); | |
72 | }, | |
73 | )) | |
74 | ->add('language', 'filter_choice', array( | |
75 | 'choices' => $this->repository->findDistinctLanguageByUser($this->user->getId()), | |
76 | )) | |
77 | ; | |
78 | } | |
79 | ||
80 | public function getBlockPrefix() | |
81 | { | |
82 | return 'entry_filter'; | |
83 | } | |
84 | ||
85 | public function configureOptions(OptionsResolver $resolver) | |
86 | { | |
87 | $resolver->setDefaults(array( | |
88 | 'csrf_protection' => false, | |
89 | 'validation_groups' => array('filtering'), | |
90 | )); | |
91 | } | |
92 | } |