getVar('q');
if ($search) {
return $this->doSearch($search);
}
return $this->render();
}
public function ElementalArea()
{
if($this->CurrentElement() || $this->getAction() !== 'index') {
return false;
}
return ElementalArea::get()->byID($this->getField('ElementalAreaID'));
}
public function CurrentElement()
{
$controller_curr = Controller::curr();
if(is_a($controller_curr, ElementFormController::class)) {
return $controller_curr;
}
return false;
}
public function SearchForm()
{
$config = $this->SiteConfig();
return Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('q', 'Search ...')
->setAttribute('placeholder', 'Search '.$config->getField('Title').' Website')
),
FieldList::create(
FormAction::create(
'doSearch',
'Find it!'
)
->setUseButtonTag(true)
->addExtraClass('btn-secondary')
->setButtonContent(
''
.'Search'
)
),
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 = ArrayList::create();
// get pages by title and content
$pages = SiteTree::get()->filterAny([
'Title:PartialMatch' => $term,
'Content:PartialMatch' => $term,
])->sort('Created DESC');
$results->merge($pages);
// get pages by elements
$elements = ElementContent::get()->filterAny([
'Title:PartialMatch' => $term,
'HTML:PartialMatch' => $term,
])->sort('Created DESC');
foreach ($elements as $element) {
$page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first();
if(!$page) {
continue;
}
$results->push($page);
}
$results->removeDuplicates();
return ArrayData::create([
'Title' => 'You searched for: "'.$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();
$request->getSession()->set(
'SiteWideMessage',
ArrayData::create([
'Message' => $message,
'Type' => $type,
])
);
return true;
}
public function getSiteWideMessage()
{
$request = $this->getRequest();
if ($request->isGET() && !$this->site_message) {
$session = $request->getSession();
$this->site_message = $session->get('SiteWideMessage');
$session->clear('SiteWideMessage');
}
return $this->site_message;
}
public function CurrentTime()
{
return DBDatetime::now();
}
}