silverstripe-cms/code/Search/ContentControllerSearchExtension.php

65 lines
2.1 KiB
PHP
Raw Normal View History

<?php
2016-07-22 01:32:32 +02:00
namespace SilverStripe\CMS\Search;
2016-09-09 01:26:24 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\Search\FulltextSearchable;
/**
* Extension to provide a search interface when applied to ContentController
*/
2017-01-25 21:59:25 +01:00
class ContentControllerSearchExtension extends Extension
{
private static $allowed_actions = array(
'SearchForm',
);
2017-01-25 21:59:25 +01:00
/**
* Site search form
*
* @return SearchForm
2017-01-25 21:59:25 +01:00
*/
public function SearchForm()
{
$searchText = '';
2017-01-25 21:59:25 +01:00
if ($this->owner->getRequest() && $this->owner->getRequest()->getVar('Search')) {
$searchText = $this->owner->getRequest()->getVar('Search');
}
$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
);
$actions = FieldList::create(
FormAction::create('results', _t('SilverStripe\\CMS\\Search\\SearchForm.GO', 'Go'))
2017-01-25 21:59:25 +01:00
);
/** @skipUpgrade */
$form = SearchForm::create($this->owner, 'SearchForm', $fields, $actions);
$form->classesToSearch(FulltextSearchable::get_searchable_classes());
return $form;
}
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)
{
$data = array(
'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')
2017-01-25 21:59:25 +01:00
);
return $this->owner->customise($data)->renderWith(array('Page_results', 'Page'));
}
}