1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TextFilter.php |
4
|
|
|
* |
5
|
|
|
* @since 19/01/17 |
6
|
|
|
* @author gseidel |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Enhavo\Bundle\AppBundle\Filter\Type; |
10
|
|
|
|
11
|
|
|
use Enhavo\Bundle\AppBundle\Exception\FilterException; |
12
|
|
|
use Enhavo\Bundle\AppBundle\Filter\AbstractFilterType; |
13
|
|
|
use Enhavo\Bundle\AppBundle\Filter\FilterQuery; |
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
15
|
|
|
|
16
|
|
|
class OptionType extends AbstractFilterType |
17
|
|
|
{ |
18
|
|
|
public function createViewData($options, $name) |
19
|
|
|
{ |
20
|
|
|
$data = [ |
21
|
|
|
'type' => $this->getType(), |
22
|
|
|
'choices' => $this->formatChoices($options), |
23
|
|
|
'key' => $name, |
24
|
|
|
'value' => null, |
25
|
|
|
'initialValue' => null, |
26
|
|
|
'component' => $options['component'], |
27
|
|
|
'label' => $this->getLabel($options) |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
return $data; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function formatChoices($options) |
34
|
|
|
{ |
35
|
|
|
$data = []; |
36
|
|
|
foreach($options['options'] as $value => $label) { |
37
|
|
|
$data[] = [ |
38
|
|
|
'label' => $this->translator->trans($label, [], $options['translation_domain']), |
39
|
|
|
'code' => $value, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
return $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function buildQuery(FilterQuery $query, $options, $value) |
46
|
|
|
{ |
47
|
|
|
if($value === null || trim($value) === '') { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$possibleValues = $options['options']; |
52
|
|
|
$possibleValues = array_keys($possibleValues); |
53
|
|
|
$findPossibleValue = false; |
54
|
|
|
foreach($possibleValues as $possibleValue) { |
55
|
|
|
if($possibleValue == $value) { |
56
|
|
|
$findPossibleValue = true; |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if(!$findPossibleValue) { |
62
|
|
|
throw new FilterException('Value does not exists in options'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$property = $options['property']; |
66
|
|
|
$query->addWhere($property, FilterQuery::OPERATOR_EQUALS, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function configureOptions(OptionsResolver $optionsResolver) |
70
|
|
|
{ |
71
|
|
|
parent::configureOptions($optionsResolver); |
72
|
|
|
$optionsResolver->setDefaults([ |
73
|
|
|
'component' => 'filter-option' |
74
|
|
|
]); |
75
|
|
|
$optionsResolver->setRequired(['options']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getType() |
79
|
|
|
{ |
80
|
|
|
return 'option'; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|