IMPR: Move features from silverstripe-webpack repo

This commit is contained in:
Tony Air 2023-10-25 02:49:42 +02:00
parent 8b13df9397
commit 0efa3b0580
4 changed files with 318 additions and 6 deletions

View File

@ -4,9 +4,6 @@ Name: a2nt-cms-niceties-base-config
SilverStripe\Core\Manifest\ModuleManifest:
project: app
Page:
default_container_class: 'container'
#SilverStripe\Admin\LeftAndMain:
# extra_requirements_javascript:
# - 'colymba/gridfield-bulk-editing-tools:client/dist/js/main.js'

View File

@ -12,7 +12,12 @@ SilverStripe\SiteConfig\SiteConfig:
- A2nt\CMSNiceties\Extensions\SiteConfigExtension
- A2nt\CMSNiceties\Extensions\NotificationsExtension
PageController:
extensions:
- A2nt\CMSNiceties\Extensions\PageControllerEx
SilverStripe\CMS\Model\SiteTree:
default_container_class: 'container'
extensions:
- A2nt\CMSNiceties\Extensions\SiteTreeExtension

View File

@ -0,0 +1,255 @@
<?php
namespace A2nt\CMSNiceties\Extensions;
use DNADesign\Elemental\Models\ElementalArea;
use Page;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\View\ArrayData;
class PageControllerEx extends Extension
{
private static $allowed_actions = [
'SearchForm',
];
private $site_message;
public static function DefaultContainer()
{
return SiteTreeExtension::DefaultContainer();
}
public function CurrentTime()
{
return DBDatetime::now();
}
public function isDev()
{
return Director::isDev();
}
public function getSiteWideMessage()
{
$obj = $this->owner;
$request = $obj->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 getParentRecursively()
{
$obj = $this->owner;
return $obj->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;
}
private static $searchable_elements = [
ElementContent::class,
];
private static $searchable_objects = [
TeamMember::class,
];
private $search_term;
public function index(HTTPRequest $request)
{
$obj = $this->owner;
$search = $request->getVar('q');
if ($search) {
return $this->doSearch($search);
}
return $obj->render();
}
public function setAction($action)
{
$obj = $this->owner;
$obj->action = $action;
}
public function ElementalArea()
{
$obj = $this->owner;
if (!$obj->getAction() || 'index' === $obj->getAction()) {
return ElementalArea::get()->byID($obj->getField('ElementalAreaID'));
}
return false;
}
public function CurrentElement()
{
$controller_curr = Controller::curr();
if (is_a($controller_curr, ElementFormController::class)) {
return $controller_curr;
}
return false;
}
public function SearchForm(): Form
{
$obj = $this->owner;
$config = $obj->SiteConfig();
$form = Form::create(
$obj,
__FUNCTION__,
FieldList::create(
TextField::create('q', 'Search ...')
->setAttribute('placeholder', 'What are you looking for?')
),
FieldList::create(
FormAction::create(
'doSearch',
'Find it!'
)
->setUseButtonTag(true)
->addExtraClass('btn-secondary')
->setButtonContent(
'<i class="fas fa-search"></i>'
. '<span class="sr-only">Search</span>'
)
),
RequiredFields::create(['q'])
)->setFormMethod('GET');
$form->setLegend('Search ' . $config->getField('Title') . ' Website');
return $form;
}
public function doSearch($data)
{
$obj = $this->owner;
$this->search_term = is_array($data) ? $data['q'] : $data;
return $obj->renderWith(['PageController_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,
])->exclude([
'ClassName' => ErrorPage::class,
])->sort('Created DESC');
$results->merge($pages);
// get pages by elements
$elements = self::getSearchObjects(
Page::config()->get('searchable_elements'),
$term
);
foreach ($elements as $element) {
if (!is_a($element, \DNADesign\Elemental\Models\BaseElement::class)
&& !$element->hasMethod('getPage')) {
continue;
}
$page = $element->getPage();
if (! $page) {
continue;
}
$results->push($page);
}
// get pages by objects
$elements = self::getSearchObjects(
Page::config()->get('searchable_objects'),
$term
);
foreach ($elements as $element) {
$page = $element->getPage();
if (!$element->hasMethod('getPage')) {
continue;
}
if (! $page) {
continue;
}
$results->push($page);
}
$results->removeDuplicates();
return ArrayData::create([
'Title' => 'You searched for: "' . $term . '"',
'Results' => PaginatedList::create($results),
]);
}
private static function getSearchObjects($classNames, $term): ArrayList
{
$elements = ArrayList::create();
foreach ($classNames as $class) {
$fields = Config::inst()->get($class, 'frontend_searchable_fields');
if (!$fields) {
continue;
}
$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;
}
}

View File

@ -3,6 +3,8 @@
namespace A2nt\CMSNiceties\Extensions;
use A2nt\ElementalBasics\Elements\SidebarElement;
use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\TextareaField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
@ -15,6 +17,7 @@ use SilverStripe\Forms\FieldList;
*/
class SiteTreeExtension extends DataExtension
{
protected $_cached = [];
private static $db = [
'ExtraCode' => 'Text',
];
@ -75,6 +78,58 @@ class SiteTreeExtension extends DataExtension
return false;
}
public static function DefaultContainer()
{
return SiteTree::config()->get('default_container_class');
}
/*
* Shows custom summary of the post, otherwise
* Displays summary of the first content element
*/
public function Summary($wordsToDisplay = 30)
{
$obj = $this->owner;
if (isset($this->_cached['summary' . $wordsToDisplay])) {
return $this->_cached['summary' . $wordsToDisplay];
}
$summary = $obj->getField('Summary');
if ($summary) {
$this->_cached['summary' . $wordsToDisplay] = $summary;
return $this->_cached['summary' . $wordsToDisplay];
}
$element = ElementContent::get()->filter([
'ParentID' => $obj->ElementalArea()->ID,
'HTML:not' => [null],
])->first();
if ($element) {
$this->_cached['summary' . $wordsToDisplay] = $element->dbObject('HTML')->Summary($wordsToDisplay);
return $this->_cached['summary' . $wordsToDisplay];
}
$content = $obj->getField('Content');
if ($content) {
$this->_cached['summary' . $wordsToDisplay] = $obj->dbObject('Content')->Summary($wordsToDisplay);
return $this->_cached['summary' . $wordsToDisplay];
}
$this->_cached['summary' . $wordsToDisplay] = false;
return $this->_cached['summary' . $wordsToDisplay];
}
public function CSSClass()
{
$obj = $this->owner;
return str_replace(['\\'], '-', $obj->getField('ClassName'));
}
public function onBeforeWrite()
{
parent::onBeforeWrite();