2021-06-19 21:30:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace A2nt\CMSNiceties\Extensions;
|
|
|
|
|
2023-09-24 18:51:04 +02:00
|
|
|
use A2nt\ElementalBasics\Elements\SidebarElement;
|
2023-10-25 02:49:42 +02:00
|
|
|
use DNADesign\Elemental\Models\ElementContent;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2021-06-19 21:30:03 +02:00
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
|
2023-10-25 04:26:25 +02:00
|
|
|
use TractorCow\Fluent\Extension\FluentSiteTreeExtension;
|
|
|
|
|
2022-05-10 13:09:06 +02:00
|
|
|
/**
|
|
|
|
* Class \A2nt\CMSNiceties\Extensions\SiteTreeExtension
|
|
|
|
*
|
|
|
|
* @property \A2nt\CMSNiceties\Extensions\SiteTreeExtension $owner
|
|
|
|
* @property string $ExtraCode
|
|
|
|
*/
|
2021-06-19 21:30:03 +02:00
|
|
|
class SiteTreeExtension extends DataExtension
|
|
|
|
{
|
2023-10-25 02:49:42 +02:00
|
|
|
protected $_cached = [];
|
2021-06-19 21:30:03 +02:00
|
|
|
private static $db = [
|
|
|
|
'ExtraCode' => 'Text',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function updateSettingsFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$fields->addFieldsToTab('Root.Settings', [
|
|
|
|
TextareaField::create(
|
|
|
|
'ExtraCode',
|
|
|
|
'Extra page specific HTML code'
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
2023-06-20 18:50:49 +02:00
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$f = $fields->dataFieldByName('MenuTitle');
|
|
|
|
// Elements has own Title field to be used at content (h1 can be hidden),
|
|
|
|
// while Menu Title (h1 page title) and Navigation label should be equal for SEO
|
|
|
|
if ($f) {
|
|
|
|
// name page name as navigation label to be more clear for CMS admin
|
|
|
|
$fields->dataFieldByName('Title')->setTitle($f->Title());
|
|
|
|
$fields->removeByName('MenuTitle');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 18:51:04 +02:00
|
|
|
public function ShowSidebar()
|
|
|
|
{
|
|
|
|
$obj = $this->owner;
|
2023-09-29 20:27:01 +02:00
|
|
|
$area = $obj->ElementalArea();
|
2023-10-25 02:49:42 +02:00
|
|
|
if (!$area) {
|
2023-09-29 20:27:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$els = $area->Elements();
|
2023-10-25 02:49:42 +02:00
|
|
|
if (!$els) {
|
2023-09-29 20:27:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$els = $els->find('ClassName', SidebarElement::class);
|
2023-10-25 02:49:42 +02:00
|
|
|
if (!$els) {
|
2023-09-29 20:27:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-09-24 18:51:04 +02:00
|
|
|
|
2023-09-29 20:27:01 +02:00
|
|
|
if ($els->first()) {
|
2023-09-24 18:51:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($obj->SideBarContent) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (method_exists($obj, 'SideBarView')) {
|
|
|
|
$view = $obj->SideBarView();
|
|
|
|
|
|
|
|
if ($view && $view->Widgets()->count()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-25 02:49:42 +02:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:08:33 +01:00
|
|
|
if(!method_exists($obj, 'ElementalArea')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-25 02:49:42 +02:00
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:50:49 +02:00
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
|
|
|
// h1 page title and navigation label should be equal for SEO
|
|
|
|
$obj = $this->owner;
|
|
|
|
$obj->setField('MenuTitle', $obj->getField('Title'));
|
2023-10-25 04:26:25 +02:00
|
|
|
|
|
|
|
if (class_exists(FluentSiteTreeExtension::class) && ! $obj->isDraftedInLocale() && $obj->isInDB()) {
|
|
|
|
$elementalArea = $obj->ElementalArea();
|
|
|
|
|
|
|
|
$elementalAreaNew = $elementalArea->duplicate();
|
|
|
|
$obj->setField('ElementalAreaID', $elementalAreaNew->ID);
|
|
|
|
}
|
2023-06-20 18:50:49 +02:00
|
|
|
}
|
2021-06-19 21:30:03 +02:00
|
|
|
}
|