forked from silverstripe/silverstripe-fulltextsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentControllerExtension.php
60 lines (53 loc) · 1.91 KB
/
ContentControllerExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace SilverStripe\FullTextSearch\Solr\Control;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\FullTextSearch\Solr\Forms\SearchForm;
use SilverStripe\ORM\FieldType\DBField;
class ContentControllerExtension extends Extension
{
private static $allowed_actions = array(
'SearchForm',
'results',
);
/**
* Site search form
*
* @return SearchForm
*/
public function SearchForm()
{
$searchText = _t('SilverStripe\\CMS\\Search\\SearchForm.SEARCH', 'Search');
/** @var HTTPRequest $currentRequest */
$currentRequest = $this->owner->getRequest();
if ($currentRequest && $currentRequest->getVar('Search')) {
$searchText = $currentRequest->getVar('Search');
}
$fields = FieldList::create(
TextField::create('Search', false, $searchText)
);
$actions = FieldList::create(
FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go'))
);
return SearchForm::create($this->owner, 'SearchForm', $fields, $actions);
}
/**
* Process and render search results.
*
* @param array $data The raw request data submitted by user
* @param SearchForm $form The form instance that was submitted
* @param HTTPRequest $request Request generated for this action
*/
public function results($data, $form, $request)
{
$data = [
'Results' => $form->getResults(),
'Query' => DBField::create_field('Text', $form->getSearchQuery()),
'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results')
];
return $this->owner->customise($data)->renderWith(['Page_results_solr', 'Page_results', 'Page']);
}
}