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

250 lines
6.3 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
2021-03-14 21:45:35 +01:00
//namespace App\Pages;
2018-04-21 06:29:32 +02:00
2022-01-13 10:54:33 +01:00
use A2nt\CMSNiceties\Templates\DeferredRequirements;
use A2nt\ElementalBasics\Models\TeamMember;
use DNADesign\Elemental\Models\ElementalArea;
use DNADesign\Elemental\Models\ElementContent;
use DNADesign\ElementalUserForms\Control\ElementFormController;
2018-04-21 06:29:32 +02:00
use SilverStripe\CMS\Controllers\ContentController;
2022-01-13 10:54:33 +01:00
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
2020-04-24 12:31:16 +02:00
use SilverStripe\Control\Director;
2022-01-13 10:54:33 +01:00
use SilverStripe\Control\HTTPRequest;
2020-05-24 09:04:07 +02:00
use SilverStripe\Core\Config\Config;
2018-12-20 19:52:07 +01:00
use SilverStripe\Forms\FieldList;
2022-01-13 10:54:33 +01:00
use SilverStripe\Forms\Form;
2018-12-20 19:52:07 +01:00
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
2022-01-13 10:54:33 +01:00
use SilverStripe\Forms\TextField;
2018-12-21 03:56:42 +01:00
use SilverStripe\ORM\ArrayList;
2022-01-13 10:54:33 +01:00
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\View\ArrayData;
2018-04-21 06:29:32 +02:00
2022-05-10 12:36:40 +02:00
/**
* Class \PageController
*
* @property \Page dataRecord
* @method \Page data()
* @mixin \Page
*/
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',
];
2020-05-24 09:04:07 +02:00
private static $searchable_elements = [
ElementContent::class,
];
private static $searchable_objects = [
TeamMember::class,
];
2018-12-20 19:52:07 +01:00
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 setAction($action)
{
$this->action = $action;
}
2019-04-11 00:15:29 +02:00
public function ElementalArea()
{
2022-01-13 10:54:33 +01:00
if (! $this->getAction() || 'index' === $this->getAction()) {
return ElementalArea::get()->byID($this->getField('ElementalAreaID'));
}
2019-04-11 00:15:29 +02:00
return false;
2019-04-11 00:15:29 +02:00
}
public function CurrentElement()
{
2019-12-09 12:04:12 +01:00
$controller_curr = Controller::curr();
2019-04-11 00:15:29 +02:00
if (is_a($controller_curr, ElementFormController::class)) {
2019-12-09 12:04:12 +01:00
return $controller_curr;
2019-04-11 00:15:29 +02:00
}
return false;
}
public function SearchForm(): Form
2018-12-20 19:52:07 +01:00
{
2019-12-09 12:04:12 +01:00
$config = $this->SiteConfig();
2022-01-13 10:54:33 +01:00
2022-03-15 19:48:57 +01:00
$form = Form::create(
2018-12-20 19:52:07 +01:00
$this,
__FUNCTION__,
FieldList::create(
TextField::create('q', 'Search ...')
2022-01-13 10:54:33 +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>'
2022-01-13 10:54:33 +01:00
. '<span class="sr-only">Search</span>'
2019-12-09 12:04:12 +01:00
)
2018-12-20 19:52:07 +01:00
),
RequiredFields::create(['q'])
)->setFormMethod('POST');
2022-03-15 19:48:57 +01:00
$form->setLegend('Search ' . $config->getField('Title') . ' Website');
return $form;
2018-12-20 19:52:07 +01:00
}
public function doSearch($data)
{
$this->search_term = is_array($data) ? $data['q'] : $data;
2022-01-13 10:54:33 +01:00
return $this->renderWith([__CLASS__ . '_search', 'Page']);
2020-05-24 09:04:07 +02:00
}
2018-12-20 19:52:07 +01:00
public function SearchResults()
{
$term = $this->search_term;
2022-01-13 10:54:33 +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([
2022-01-13 10:54:33 +01:00
'Title:PartialMatch' => $term,
'Content:PartialMatch' => $term,
])->sort('Created DESC');
2018-12-20 19:52:07 +01:00
2018-12-21 03:56:42 +01:00
$results->merge($pages);
// get pages by elements
2020-05-24 09:04:07 +02:00
$elements = self::getSearchObjects(
self::config()->get('searchable_elements'),
$term
);
2018-12-21 03:56:42 +01:00
foreach ($elements as $element) {
$page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first();
2022-01-13 10:54:33 +01:00
if (! $page) {
2018-12-21 03:56:42 +01:00
continue;
}
$results->push($page);
}
// get pages by objects
2020-05-24 09:04:07 +02:00
$elements = self::getSearchObjects(
self::config()->get('searchable_objects'),
$term
);
foreach ($elements as $element) {
$page = $element->Page();
2022-01-13 10:54:33 +01:00
if (! $page) {
2020-05-24 09:04:07 +02:00
continue;
}
$results->push($page);
}
2018-12-21 03:56:42 +01:00
$results->removeDuplicates();
2018-12-20 19:52:07 +01:00
return ArrayData::create([
2022-01-13 10:54:33 +01: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();
2022-01-13 10:54:33 +01:00
if ($request->isGET() && ! $this->site_message) {
2019-04-11 00:15:29 +02:00
$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 static function DefaultContainer()
{
return \Page::DefaultContainer();
}
public function CurrentTime()
{
return DBDatetime::now();
}
public function isDev()
{
return Director::isDev();
}
2022-01-13 10:54:33 +01:00
protected function init()
{
DeferredRequirements::Auto();
return parent::init();
}
private static function getSearchObjects($classNames, $term): ArrayList
{
$elements = ArrayList::create();
foreach ($classNames as $class) {
$fields = Config::inst()->get($class, 'frontend_searchable_fields');
$find = array_combine($fields, $fields);
$find = array_map(static function () use ($term) {
return $term;
}, $find);
$elements->merge($class::get()->filterAny($find)->sort('Created DESC'));
}
return $elements;
}
2018-04-21 06:29:32 +02:00
}