SiteTree search functionality

This commit is contained in:
Tony Air 2018-12-21 01:52:07 +07:00
parent f7faea28a5
commit b376b94038
2 changed files with 122 additions and 0 deletions

View File

@ -4,11 +4,86 @@
// extends global PageController class // extends global PageController class
//namespace Site\Pages; //namespace Site\Pages;
use SilverStripe\Control\Controller;
use SilverStripe\CMS\Controllers\ContentController; use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\View\ArrayData;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Control\HTTPRequest;
class PageController extends ContentController class PageController extends ContentController
{ {
private static $allowed_actions = [
'SearchForm',
];
private $site_message;
private $search_term;
public function index(HTTPRequest $request)
{
$search = $request->getVar('q');
if ($search) {
return $this->doSearch($search);
}
return $this->render();
}
public function SearchForm()
{
return Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('q', 'Search ...')
),
FieldList::create(
FormAction::create(
'doSearch',
'Find it!'
)
),
RequiredFields::create(['q'])
)->setFormMethod('POST');
}
public function doSearch($data)
{
$this->search_term = is_array($data) ? $data['q'] : $data;
return $this->renderWith([__CLASS__.'_search', 'Page']);
}
public function SearchResults()
{
$term = $this->search_term;
if(!$term) {
return false;
}
$results = SiteTree::get()->filterAny([
'Title:PartialMatch' => $term,
'Content:PartialMatch' => $term,
])->sort('Created DESC');
return ArrayData::create([
'Title' => 'Search query "'.$term.'"',
'Results' => PaginatedList::create($results),
]);
}
public function getParentRecursively()
{
return $this->Level(1);
}
public static function setSiteWideMessage($message, $type, $request = null) public static function setSiteWideMessage($message, $type, $request = null)
{ {
$request = $request ? $request : Controller::curr()->getRequest(); $request = $request ? $request : Controller::curr()->getRequest();
@ -19,6 +94,8 @@ class PageController extends ContentController
'Type' => $type, 'Type' => $type,
]) ])
); );
return true;
} }
public function getSiteWideMessage() public function getSiteWideMessage()

View File

@ -0,0 +1,45 @@
<% with $SearchResults %>
<div id="PageContainer" class="page action{$Action}">
<div class="page-content">
<h1 class="page-header container no-elements">$Title</h1>
<div class="page-content">
<% if $Results %>
<div id="SearchAccordion{$ID}">
<% loop $Results %>
<div class="card">
<div class="card-header" id="Heading{$ID}{$Up.ID}">
<h5 class="mb-0">
<strong
data-toggle="collapse"
data-target="#Collapse{$ID}{$Up.ID}"
aria-expanded="false"
aria-controls="Collapse{$ID}{$Up.ID}"
>
$Title
</strong>
</h5>
</div>
<div
id="Collapse{$ID}{$Up.ID}"
class="collapse"
aria-labelledby="Heading{$ID}{$Up.ID}"
data-parent="#SearchAccordion{$Up.ID}"
>
<div class="card-body">
<p>$Summary</p>
<a href="{$Link}">Learn More</a>
</div>
</div>
</div>
<% end_loop %>
</div>
<% else %>
<h2>Nothing was found.</h2>
<% end_if %>
</div>
</div>
</div>
<% end_with %>