diff --git a/_config/base-config.yml b/_config/base-config.yml
index d3c3ee4..ac03930 100755
--- a/_config/base-config.yml
+++ b/_config/base-config.yml
@@ -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'
diff --git a/_config/base-extensions.yml b/_config/base-extensions.yml
index 995e760..da60caa 100755
--- a/_config/base-extensions.yml
+++ b/_config/base-extensions.yml
@@ -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
diff --git a/src/Extensions/PageControllerEx.php b/src/Extensions/PageControllerEx.php
new file mode 100644
index 0000000..4579828
--- /dev/null
+++ b/src/Extensions/PageControllerEx.php
@@ -0,0 +1,255 @@
+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(
+ ''
+ . 'Search'
+ )
+ ),
+ 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;
+ }
+}
diff --git a/src/Extensions/SiteTreeExtension.php b/src/Extensions/SiteTreeExtension.php
index 4b26d2a..7dfbaff 100755
--- a/src/Extensions/SiteTreeExtension.php
+++ b/src/Extensions/SiteTreeExtension.php
@@ -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',
];
@@ -45,15 +48,15 @@ class SiteTreeExtension extends DataExtension
{
$obj = $this->owner;
$area = $obj->ElementalArea();
- if(!$area) {
+ if (!$area) {
return true;
}
$els = $area->Elements();
- if(!$els) {
+ if (!$els) {
return true;
}
$els = $els->find('ClassName', SidebarElement::class);
- if(!$els) {
+ if (!$els) {
return true;
}
@@ -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();