silverstripe-webpack/app/src/Pages/PageController.php

181 lines
4.7 KiB
PHP
Raw Normal View History

2018-04-21 06:29:32 +02:00
<?php
// vendor/silverstripe/errorpage/src/ErrorPageController.php
// extends global PageController class
//namespace Site\Pages;
2018-12-20 19:52:07 +01:00
use SilverStripe\Control\Controller;
2018-04-21 06:29:32 +02:00
use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\ORM\FieldType\DBDatetime;
2018-12-20 19:52:07 +01:00
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;
2018-12-21 03:56:42 +01:00
use SilverStripe\ORM\ArrayList;
use DNADesign\Elemental\Models\ElementContent;
2019-04-11 00:15:29 +02:00
use DNADesign\Elemental\Models\ElementalArea;
use DNADesign\ElementalUserForms\Control\ElementFormController;
2019-12-09 12:18:05 +01:00
use Site\Templates\DeferredRequirements;
2018-04-21 06:29:32 +02:00
class PageController extends ContentController
{
2018-12-20 19:52:07 +01:00
private static $allowed_actions = [
'SearchForm',
];
private $site_message;
private $search_term;
2019-12-09 12:18:05 +01:00
protected function init()
2019-12-09 12:04:12 +01:00
{
2019-12-09 12:18:05 +01:00
DeferredRequirements::Auto();
2019-12-09 12:04:12 +01:00
return parent::init();
}
2018-12-20 19:52:07 +01:00
public function index(HTTPRequest $request)
{
$search = $request->getVar('q');
if ($search) {
return $this->doSearch($search);
}
return $this->render();
}
2019-04-11 00:15:29 +02:00
public function ElementalArea()
{
2019-12-09 12:04:12 +01:00
if($this->CurrentElement() || $this->getAction() !== 'index') {
2019-04-11 00:15:29 +02:00
return false;
}
return ElementalArea::get()->byID($this->getField('ElementalAreaID'));
}
public function CurrentElement()
{
2019-12-09 12:04:12 +01:00
$controller_curr = Controller::curr();
2019-04-11 00:15:29 +02:00
2019-12-09 12:04:12 +01:00
if(is_a($controller_curr, ElementFormController::class)) {
return $controller_curr;
2019-04-11 00:15:29 +02:00
}
return false;
}
2018-12-20 19:52:07 +01:00
public function SearchForm()
{
2019-12-09 12:04:12 +01:00
$config = $this->SiteConfig();
2018-12-20 19:52:07 +01:00
return Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('q', 'Search ...')
2019-12-09 12:04:12 +01:00
->setAttribute('placeholder', 'Search '.$config->getField('Title').' Website')
2018-12-20 19:52:07 +01:00
),
FieldList::create(
FormAction::create(
'doSearch',
'Find it!'
)
2019-12-09 12:04:12 +01:00
->setUseButtonTag(true)
->addExtraClass('btn-secondary')
->setButtonContent(
'<i class="fas fa-search"></i>'
.'<span class="sr-only">Search</span>'
)
2018-12-20 19:52:07 +01:00
),
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;
2019-12-09 12:04:12 +01:00
if(!$term) {
2018-12-20 19:52:07 +01:00
return false;
}
2018-12-21 03:56:42 +01:00
$results = ArrayList::create();
// get pages by title and content
$pages = SiteTree::get()->filterAny([
2018-12-20 19:52:07 +01:00
'Title:PartialMatch' => $term,
'Content:PartialMatch' => $term,
])->sort('Created DESC');
2018-12-21 03:56:42 +01:00
$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();
2019-12-09 12:04:12 +01:00
if(!$page) {
2018-12-21 03:56:42 +01:00
continue;
}
$results->push($page);
}
$results->removeDuplicates();
2018-12-20 19:52:07 +01:00
return ArrayData::create([
2019-04-11 00:15:29 +02:00
'Title' => 'You searched for: "'.$term.'"',
2018-12-20 19:52:07 +01:00
'Results' => PaginatedList::create($results),
]);
}
public function getParentRecursively()
{
return $this->Level(1);
}
public static function setSiteWideMessage($message, $type, $request = null)
2018-08-06 13:46:54 +02:00
{
$request = $request ? $request : Controller::curr()->getRequest();
$request->getSession()->set(
2018-09-15 00:55:42 +02:00
'SiteWideMessage',
ArrayData::create([
2018-09-15 00:55:42 +02:00
'Message' => $message,
'Type' => $type,
])
2018-09-15 00:55:42 +02:00
);
2018-12-20 19:52:07 +01:00
return true;
2018-08-06 13:46:54 +02:00
}
2018-09-15 00:55:42 +02:00
public function getSiteWideMessage()
2018-08-06 13:46:54 +02:00
{
2019-04-11 00:15:29 +02:00
$request = $this->getRequest();
if ($request->isGET() && !$this->site_message) {
$session = $request->getSession();
$this->site_message = $session->get('SiteWideMessage');
$session->clear('SiteWideMessage');
}
2018-09-15 00:55:42 +02:00
return $this->site_message;
2018-08-06 13:46:54 +02:00
}
public function CurrentTime()
{
return DBDatetime::now();
}
2018-04-21 06:29:32 +02:00
}