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\Model; |
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
|
|
|
|
23
|
|
|
class QueryType extends AbstractType { |
24
|
|
|
|
25
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) { |
26
|
|
|
$builder->add('term', SearchType::class, array( |
27
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.term.label' |
28
|
|
|
)); |
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @var ResultSet $result |
32
|
|
|
*/ |
33
|
|
|
$result = $options['results']; |
34
|
|
|
$this->createFacets($builder, $result->getFacets()); |
35
|
|
|
$builder->add('filter', SubmitType::class, array( |
36
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.filter.label' |
37
|
|
|
)); |
38
|
|
|
$builder->add('clear', SubmitType::class, array( |
39
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.clear.label' |
40
|
|
|
)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @param FormBuilderInterface $builder |
46
|
|
|
* @param FacetSet $facets |
47
|
|
|
*/ |
48
|
|
|
protected function createFacets(FormBuilderInterface $builder, FacetSet $facets) { |
49
|
|
|
foreach($facets->getFacets() as $facetType => $facetValues) { |
50
|
|
|
$builder->add('facet_' . $facetType, ChoiceType::class, array( |
51
|
|
|
'label' => 'stinger_soft_entity_search.forms.query.' . $facetType . '.label', |
52
|
|
|
'multiple' => true, |
53
|
|
|
'expanded' => true, |
54
|
|
|
'choices_as_values' => true, |
55
|
|
|
'choice' => $this->generateFacetChoices($facetType, $facetValues) |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @param string $facetType |
63
|
|
|
* @param array $facets |
64
|
|
|
*/ |
65
|
|
|
protected function generateFacetChoices($facetType, array $facets) { |
|
|
|
|
66
|
|
|
$choices = array(); |
67
|
|
|
|
68
|
|
|
foreach($facets as $facet => $count) { |
69
|
|
|
if($count == 0) |
70
|
|
|
break; |
71
|
|
|
$choices[$facet] = $facet . ' (' . $count . ')'; |
72
|
|
|
} |
73
|
|
|
return $choices; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
|
public function configureOptions(OptionsResolver $resolver) { |
82
|
|
|
$resolver->setDefault('data_class', Query::class); |
83
|
|
|
$resolver->setDefault('translation_domain', 'StingerSoftEntitySearchBundle'); |
84
|
|
|
|
85
|
|
|
$resolver->setRequired('result', null); |
|
|
|
|
86
|
|
|
$resolver->setAllowedTypes('result', ResultSet::class); |
87
|
|
|
|
88
|
|
|
$resolver->setRequired('translator'); |
89
|
|
|
$resolver->setAllowedTypes('translator', TranslatorInterface::class); |
90
|
|
|
} |
91
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.