IMPR: code linting

This commit is contained in:
Tony Air 2022-01-13 11:54:33 +02:00
parent 6dae2f685d
commit 86a71bbb4e
2 changed files with 67 additions and 63 deletions

View File

@ -4,15 +4,15 @@
// extends global Page class // extends global Page class
//namespace App\Pages; //namespace App\Pages;
use SilverStripe\CMS\Model\SiteTree;
use DNADesign\Elemental\Models\ElementContent; use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\FontAwesome\FontAwesomeField; use SilverStripe\FontAwesome\FontAwesomeField;
use TractorCow\Fluent\Extension\FluentSiteTreeExtension; use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
class Page extends SiteTree class Page extends SiteTree
{ {
private static $default_container_class = 'container';
protected $_cached = []; protected $_cached = [];
private static $default_container_class = 'container';
private static $db = [ private static $db = [
'BlockIcon' => 'Varchar(255)', 'BlockIcon' => 'Varchar(255)',
]; ];
@ -21,7 +21,6 @@ class Page extends SiteTree
'ElementalAreaID', 'ElementalAreaID',
]; ];
public static function DefaultContainer() public static function DefaultContainer()
{ {
return self::config()->get('default_container_class'); return self::config()->get('default_container_class');
@ -44,14 +43,15 @@ class Page extends SiteTree
*/ */
public function Summary($wordsToDisplay = 30) public function Summary($wordsToDisplay = 30)
{ {
if (isset($this->_cached['summary'.$wordsToDisplay])) { if (isset($this->_cached['summary' . $wordsToDisplay])) {
return $this->_cached['summary'.$wordsToDisplay]; return $this->_cached['summary' . $wordsToDisplay];
} }
$summary = $this->getField('Summary'); $summary = $this->getField('Summary');
if ($summary) { if ($summary) {
$this->_cached['summary'.$wordsToDisplay] = $summary; $this->_cached['summary' . $wordsToDisplay] = $summary;
return $this->_cached['summary'.$wordsToDisplay];
return $this->_cached['summary' . $wordsToDisplay];
} }
$element = ElementContent::get()->filter([ $element = ElementContent::get()->filter([
@ -60,18 +60,21 @@ class Page extends SiteTree
])->first(); ])->first();
if ($element) { if ($element) {
$this->_cached['summary'.$wordsToDisplay] = $element->dbObject('HTML')->Summary($wordsToDisplay); $this->_cached['summary' . $wordsToDisplay] = $element->dbObject('HTML')->Summary($wordsToDisplay);
return $this->_cached['summary'.$wordsToDisplay];
return $this->_cached['summary' . $wordsToDisplay];
} }
$content = $this->getField('Content'); $content = $this->getField('Content');
if ($content) { if ($content) {
$this->_cached['summary'.$wordsToDisplay] = $this->dbObject('Content')->Summary($wordsToDisplay); $this->_cached['summary' . $wordsToDisplay] = $this->dbObject('Content')->Summary($wordsToDisplay);
return $this->_cached['summary'.$wordsToDisplay];
return $this->_cached['summary' . $wordsToDisplay];
} }
$this->_cached['summary'.$wordsToDisplay] = false; $this->_cached['summary' . $wordsToDisplay] = false;
return $this->_cached['summary'.$wordsToDisplay];
return $this->_cached['summary' . $wordsToDisplay];
} }
public function CSSClass() public function CSSClass()
@ -83,7 +86,7 @@ class Page extends SiteTree
{ {
parent::onBeforeWrite(); parent::onBeforeWrite();
if (class_exists(FluentSiteTreeExtension::class) && !$this->isDraftedInLocale() && $this->isInDB()) { if (class_exists(FluentSiteTreeExtension::class) && ! $this->isDraftedInLocale() && $this->isInDB()) {
$elementalArea = $this->ElementalArea(); $elementalArea = $this->ElementalArea();
$elementalAreaNew = $elementalArea->duplicate(); $elementalAreaNew = $elementalArea->duplicate();

View File

@ -4,26 +4,26 @@
// extends global PageController class // extends global PageController class
//namespace App\Pages; //namespace App\Pages;
use SilverStripe\Control\Controller; 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;
use SilverStripe\CMS\Controllers\ContentController; use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\View\ArrayData;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\PaginatedList; use SilverStripe\Control\Controller;
use SilverStripe\Forms\Form; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction; use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\RequiredFields;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use DNADesign\Elemental\Models\ElementContent; use SilverStripe\ORM\FieldType\DBDatetime;
use DNADesign\Elemental\Models\ElementalArea; use SilverStripe\ORM\PaginatedList;
use DNADesign\ElementalUserForms\Control\ElementFormController; use SilverStripe\View\ArrayData;
use A2nt\ElementalBasics\Models\TeamMember;
use A2nt\CMSNiceties\Templates\DeferredRequirements;
class PageController extends ContentController class PageController extends ContentController
{ {
@ -42,13 +42,6 @@ class PageController extends ContentController
private $site_message; private $site_message;
private $search_term; private $search_term;
protected function init()
{
DeferredRequirements::Auto();
return parent::init();
}
public function index(HTTPRequest $request) public function index(HTTPRequest $request)
{ {
$search = $request->getVar('q'); $search = $request->getVar('q');
@ -66,7 +59,7 @@ class PageController extends ContentController
public function ElementalArea() public function ElementalArea()
{ {
if (!$this->getAction() || $this->getAction() === 'index') { if (! $this->getAction() || 'index' === $this->getAction()) {
return ElementalArea::get()->byID($this->getField('ElementalAreaID')); return ElementalArea::get()->byID($this->getField('ElementalAreaID'));
} }
@ -87,12 +80,13 @@ class PageController extends ContentController
public function SearchForm(): Form public function SearchForm(): Form
{ {
$config = $this->SiteConfig(); $config = $this->SiteConfig();
return Form::create( return Form::create(
$this, $this,
__FUNCTION__, __FUNCTION__,
FieldList::create( FieldList::create(
TextField::create('q', 'Search ...') TextField::create('q', 'Search ...')
->setAttribute('placeholder', 'Search '.$config->getField('Title').' Website') ->setAttribute('placeholder', 'Search ' . $config->getField('Title') . ' Website')
), ),
FieldList::create( FieldList::create(
FormAction::create( FormAction::create(
@ -103,7 +97,7 @@ class PageController extends ContentController
->addExtraClass('btn-secondary') ->addExtraClass('btn-secondary')
->setButtonContent( ->setButtonContent(
'<i class="fas fa-search"></i>' '<i class="fas fa-search"></i>'
.'<span class="sr-only">Search</span>' . '<span class="sr-only">Search</span>'
) )
), ),
RequiredFields::create(['q']) RequiredFields::create(['q'])
@ -114,29 +108,13 @@ class PageController extends ContentController
{ {
$this->search_term = is_array($data) ? $data['q'] : $data; $this->search_term = is_array($data) ? $data['q'] : $data;
return $this->renderWith([__CLASS__.'_search', 'Page']); return $this->renderWith([__CLASS__ . '_search', 'Page']);
}
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;
} }
public function SearchResults() public function SearchResults()
{ {
$term = $this->search_term; $term = $this->search_term;
if (!$term) { if (! $term) {
return false; return false;
} }
@ -158,7 +136,7 @@ class PageController extends ContentController
foreach ($elements as $element) { foreach ($elements as $element) {
$page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first(); $page = Page::get()->filter('ElementalAreaID', $element->getField('ParentID'))->first();
if (!$page) { if (! $page) {
continue; continue;
} }
@ -173,7 +151,7 @@ class PageController extends ContentController
foreach ($elements as $element) { foreach ($elements as $element) {
$page = $element->Page(); $page = $element->Page();
if (!$page) { if (! $page) {
continue; continue;
} }
@ -183,7 +161,7 @@ class PageController extends ContentController
$results->removeDuplicates(); $results->removeDuplicates();
return ArrayData::create([ return ArrayData::create([
'Title' => 'You searched for: "'.$term.'"', 'Title' => 'You searched for: "' . $term . '"',
'Results' => PaginatedList::create($results), 'Results' => PaginatedList::create($results),
]); ]);
} }
@ -211,7 +189,7 @@ class PageController extends ContentController
{ {
$request = $this->getRequest(); $request = $this->getRequest();
if ($request->isGET() && !$this->site_message) { if ($request->isGET() && ! $this->site_message) {
$session = $request->getSession(); $session = $request->getSession();
$this->site_message = $session->get('SiteWideMessage'); $this->site_message = $session->get('SiteWideMessage');
$session->clear('SiteWideMessage'); $session->clear('SiteWideMessage');
@ -234,4 +212,27 @@ class PageController extends ContentController
{ {
return Director::isDev(); return Director::isDev();
} }
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;
}
} }