2011-03-18 04:05:23 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Search;
|
|
|
|
|
2024-01-11 01:30:52 +01:00
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\Extension;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\Search\FulltextSearchable;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-18 04:05:23 +01:00
|
|
|
/**
|
|
|
|
* Extension to provide a search interface when applied to ContentController
|
2024-01-11 01:30:52 +01:00
|
|
|
*
|
|
|
|
* @extends Extension<ContentController>
|
2011-03-18 04:05:23 +01:00
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class ContentControllerSearchExtension extends Extension
|
|
|
|
{
|
2020-04-19 06:18:01 +02:00
|
|
|
private static $allowed_actions = [
|
2017-01-25 21:59:25 +01:00
|
|
|
'SearchForm',
|
2020-04-19 06:18:01 +02:00
|
|
|
];
|
2011-03-18 04:05:23 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* Site search form
|
2017-06-21 06:29:40 +02:00
|
|
|
*
|
|
|
|
* @return SearchForm
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
public function SearchForm()
|
|
|
|
{
|
2018-11-16 16:17:49 +01:00
|
|
|
$searchText = '';
|
2017-01-25 21:59:25 +01:00
|
|
|
if ($this->owner->getRequest() && $this->owner->getRequest()->getVar('Search')) {
|
|
|
|
$searchText = $this->owner->getRequest()->getVar('Search');
|
|
|
|
}
|
2011-03-18 04:05:23 +01:00
|
|
|
|
2018-11-16 16:17:49 +01:00
|
|
|
$placeholder = _t('SilverStripe\\CMS\\Search\\SearchForm.SEARCH', 'Search');
|
|
|
|
$fields = FieldList::create(
|
|
|
|
TextField::create('Search', false, $searchText)
|
|
|
|
->setAttribute('placeholder', $placeholder)
|
2017-01-25 21:59:25 +01:00
|
|
|
);
|
2018-11-16 16:17:49 +01:00
|
|
|
$actions = FieldList::create(
|
|
|
|
FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go'))
|
2017-01-25 21:59:25 +01:00
|
|
|
);
|
|
|
|
$form = SearchForm::create($this->owner, 'SearchForm', $fields, $actions);
|
|
|
|
$form->classesToSearch(FulltextSearchable::get_searchable_classes());
|
|
|
|
return $form;
|
|
|
|
}
|
2011-03-18 04:05:23 +01:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2020-04-19 06:18:01 +02:00
|
|
|
$data = [
|
2017-01-25 21:59:25 +01:00
|
|
|
'Results' => $form->getResults(),
|
|
|
|
'Query' => DBField::create_field('Text', $form->getSearchQuery()),
|
2017-04-20 03:15:29 +02:00
|
|
|
'Title' => _t('SilverStripe\\CMS\\Search\\SearchForm.SearchResults', 'Search Results')
|
2020-04-19 06:18:01 +02:00
|
|
|
];
|
|
|
|
return $this->owner->customise($data)->renderWith(['Page_results', 'Page']);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|