1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\FilterManagerBundle\Filter\Widget\Search; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Query\TermQuery; |
15
|
|
|
use ONGR\ElasticsearchDSL\Search; |
16
|
|
|
use ONGR\FilterManagerBundle\Filter\FilterState; |
17
|
|
|
use ONGR\FilterManagerBundle\Filter\Relation\RelationAwareInterface; |
18
|
|
|
use ONGR\FilterManagerBundle\Filter\Relation\RelationAwareTrait; |
19
|
|
|
use ONGR\FilterManagerBundle\Search\SearchRequest; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Filter for filtering on exact value in specified field. |
24
|
|
|
*/ |
25
|
|
|
class DocumentValue extends AbstractSingleValue implements RelationAwareInterface |
26
|
|
|
{ |
27
|
|
|
use RelationAwareTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Field name of the document object. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $documentField; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $value; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getDocumentField() |
45
|
|
|
{ |
46
|
|
|
return $this->documentField; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $documentField |
51
|
|
|
*/ |
52
|
|
|
public function setDocumentField($documentField) |
53
|
|
|
{ |
54
|
|
|
$this->documentField = $documentField; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Setter for field value. |
59
|
|
|
* |
60
|
|
|
* @param $value |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setValue($value) |
65
|
|
|
{ |
66
|
|
|
$this->value = $value; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getValue() |
75
|
|
|
{ |
76
|
|
|
return $this->value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function getState(Request $request) |
83
|
|
|
{ |
84
|
|
|
$state = new FilterState(); |
85
|
|
|
$document = $request->get('document'); |
86
|
|
|
|
87
|
|
|
if ($document) { |
88
|
|
|
$this->setValue($document->{$this->getDocumentField()}); |
89
|
|
|
$state->setActive(true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $state; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null) |
99
|
|
|
{ |
100
|
|
|
$search->addPostFilter(new TermQuery($this->getField(), $this->getValue())); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|