1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Elastica Search Form |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Query; |
8
|
|
|
use \SilverStripe\Elastica\ResultList; |
9
|
|
|
use Elastica\Query\QueryString; |
10
|
|
|
|
11
|
|
|
class ElasticSearchForm extends Form { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* List of types to search for, default (blank) returns all |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $types = ''; |
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @param Controller $controller |
23
|
|
|
* @param string $name The name of the form (used in URL addressing) |
24
|
|
|
* @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized |
25
|
|
|
* if fields are added to the form. |
26
|
|
|
* @param FieldList $actions Optional, defaults to a single field named "Go". |
27
|
|
|
*/ |
28
|
|
|
public function __construct($controller, $name, $fields = null, $actions = null) { |
29
|
|
|
$searchText = isset($this->Query) ? $this->Query : ''; |
|
|
|
|
30
|
|
|
$fields = new FieldList( |
31
|
|
|
$tf = new TextField("q", "", $searchText) |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
$buttonText = _t('SearchPage.SEARCH', 'Search'); |
36
|
|
|
$actions = new FieldList( |
37
|
|
|
$fa = new FormAction('submit', $buttonText) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->SubmitButton = $fa; |
|
|
|
|
41
|
|
|
|
42
|
|
|
if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable')) { |
43
|
|
|
$fields->push(new HiddenField('searchlocale', 'searchlocale', Translatable::get_current_locale())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
47
|
|
|
$this->setFormMethod('post'); |
48
|
|
|
$this->disableSecurityToken(); |
49
|
|
|
$this->enableAutocomplete(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function enableAutocomplete() { |
54
|
|
|
$q = $this->Fields()->fieldByName('q'); |
|
|
|
|
55
|
|
|
$q->addExtraClass('autocomplete'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Optionally allow templates to override the text of the submit button, perhaps if using a icon font |
61
|
|
|
* as per the default Simple theme |
62
|
|
|
* |
63
|
|
|
* @param string $newButtonText Alternative search text |
64
|
|
|
*/ |
65
|
|
|
public function setButtonText($newButtonText) { |
66
|
|
|
$this->actions = new FieldList( |
67
|
|
|
$fa = new FormAction('submit', $newButtonText) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.