1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stinger Entity Search package. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
7
|
|
|
* (c) Florian Meyer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace StingerSoft\EntitySearchBundle\Form; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractType; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SearchType; |
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
20
|
|
|
use StingerSoft\EntitySearchBundle\Model\Result\FacetSet; |
21
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
22
|
|
|
use StingerSoft\EntitySearchBundle\Model\Query; |
23
|
|
|
use StingerSoft\EntitySearchBundle\Model\ResultSet; |
24
|
|
|
|
25
|
|
|
class QueryType extends AbstractType { |
26
|
|
|
|
27
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) { |
28
|
|
|
$builder->add('term', SearchType::class, array( |
29
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.term.label' |
30
|
|
|
)); |
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @var ResultSet $result |
34
|
|
|
*/ |
35
|
|
|
$result = $options['result']; |
36
|
|
|
$this->createFacets($builder, $result->getFacets()); |
37
|
|
|
$builder->add('filter', SubmitType::class, array( |
38
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.filter.label' |
39
|
|
|
)); |
40
|
|
|
$builder->add('clear', SubmitType::class, array( |
41
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.clear.label' |
42
|
|
|
)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* @param FormBuilderInterface $builder |
48
|
|
|
* @param FacetSet $facets |
49
|
|
|
*/ |
50
|
|
|
protected function createFacets(FormBuilderInterface $builder, FacetSet $facets) { |
51
|
|
|
foreach($facets->getFacets() as $facetType => $facetValues) { |
52
|
|
|
$builder->add('facet_' . $facetType, ChoiceType::class, array( |
53
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.' . $facetType . '.label', |
54
|
|
|
'multiple' => true, |
55
|
|
|
'expanded' => true, |
56
|
|
|
'choices_as_values' => true, |
57
|
|
|
'choices' => $this->generateFacetChoices($facetType, $facetValues) |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @param string $facetType |
65
|
|
|
* @param array $facets |
66
|
|
|
*/ |
67
|
|
|
protected function generateFacetChoices($facetType, array $facets) { |
|
|
|
|
68
|
|
|
$choices = array(); |
69
|
|
|
|
70
|
|
|
foreach($facets as $facet => $count) { |
71
|
|
|
if($count == 0) |
72
|
|
|
break; |
73
|
|
|
$choices[$facet . ' (' . $count . ')'] = $facet; |
74
|
|
|
} |
75
|
|
|
return $choices; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
|
|
public function configureOptions(OptionsResolver $resolver) { |
84
|
|
|
$resolver->setDefault('data_class', Query::class); |
85
|
|
|
$resolver->setDefault('translation_domain', 'StingerSoftEntitySearchBundle'); |
86
|
|
|
|
87
|
|
|
$resolver->setRequired('result', null); |
|
|
|
|
88
|
|
|
$resolver->setAllowedTypes('result', ResultSet::class); |
89
|
|
|
|
90
|
|
|
$resolver->setRequired('translator'); |
91
|
|
|
$resolver->setAllowedTypes('translator', TranslatorInterface::class); |
92
|
|
|
} |
93
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.