mirror of
https://github.com/a2nt/silverstripe-webpack.git
synced 2024-10-22 17:05:31 +02:00
SiteTree search functionality
This commit is contained in:
parent
f7faea28a5
commit
b376b94038
@ -4,11 +4,86 @@
|
||||
// extends global PageController class
|
||||
//namespace Site\Pages;
|
||||
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\CMS\Controllers\ContentController;
|
||||
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
|
||||
{
|
||||
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)
|
||||
{
|
||||
$request = $request ? $request : Controller::curr()->getRequest();
|
||||
@ -19,6 +94,8 @@ class PageController extends ContentController
|
||||
'Type' => $type,
|
||||
])
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getSiteWideMessage()
|
||||
|
45
app/templates/Layout/PageController_search.ss
Normal file
45
app/templates/Layout/PageController_search.ss
Normal 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 %>
|
Loading…
Reference in New Issue
Block a user