2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Model;
|
|
|
|
|
2017-01-24 01:45:07 +01:00
|
|
|
use SilverStripe\Blog\Model\BlogFilter\BlogFilterGridField;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Lumberjack\Model\Lumberjack;
|
2016-12-15 23:57:25 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\Security\Permission;
|
2018-01-29 04:13:19 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-12-15 04:41:49 +01:00
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* This class is responsible for filtering the SiteTree when necessary and also overlaps into
|
|
|
|
* filtering only published posts.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class BlogFilter extends Lumberjack
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function stageChildren($showAll = false)
|
|
|
|
{
|
|
|
|
$staged = parent::stageChildren($showAll);
|
|
|
|
|
|
|
|
if (!$this->shouldFilter() && $this->subclassForBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) {
|
2016-06-01 07:28:59 +02:00
|
|
|
$stage = Versioned::get_stage();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
if ($stage == 'Stage') {
|
|
|
|
$stage = '';
|
|
|
|
} elseif ($stage) {
|
|
|
|
$stage = '_' . $stage;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataQuery = $staged->dataQuery()
|
2016-12-15 23:57:25 +01:00
|
|
|
->innerJoin(
|
|
|
|
DataObject::getSchema()->tableName(BlogPost::class),
|
|
|
|
sprintf('"BlogPost%s"."ID" = "SiteTree%s"."ID"', $stage, $stage)
|
|
|
|
)
|
2016-12-15 04:41:49 +01:00
|
|
|
->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(DBDatetime::now())));
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$staged = $staged->setDataQuery($dataQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $staged;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function subclassForBlog()
|
|
|
|
{
|
2022-04-14 05:16:37 +02:00
|
|
|
return in_array(get_class($this->owner), ClassInfo::subclassesFor(Blog::class) ?? []);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function liveChildren($showAll = false, $onlyDeletedFromStage = false)
|
|
|
|
{
|
|
|
|
$staged = parent::liveChildren($showAll, $onlyDeletedFromStage);
|
|
|
|
|
|
|
|
if (!$this->shouldFilter() && $this->isBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) {
|
|
|
|
$dataQuery = $staged->dataQuery()
|
2016-12-15 23:57:25 +01:00
|
|
|
->innerJoin(
|
|
|
|
DataObject::getSchema()->tableName(BlogPost::class),
|
|
|
|
'"BlogPost_Live"."ID" = "SiteTree_Live"."ID"'
|
|
|
|
)
|
2016-12-15 04:41:49 +01:00
|
|
|
->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(DBDatetime::now())));
|
2015-11-21 07:17:29 +01:00
|
|
|
|
|
|
|
$staged = $staged->setDataQuery($dataQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $staged;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isBlog()
|
|
|
|
{
|
|
|
|
return $this->owner instanceof Blog;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$excluded = $this->owner->getExcludedSiteTreeClassNames();
|
|
|
|
|
|
|
|
if (!empty($excluded)) {
|
2017-09-14 00:59:01 +02:00
|
|
|
$pages = BlogPost::get()->filter([
|
2015-11-21 07:17:29 +01:00
|
|
|
'ParentID' => $this->owner->ID,
|
|
|
|
'ClassName' => $excluded
|
2017-09-14 00:59:01 +02:00
|
|
|
]);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2017-01-24 01:45:07 +01:00
|
|
|
$gridField = BlogFilterGridField::create(
|
2015-11-21 07:17:29 +01:00
|
|
|
'ChildPages',
|
|
|
|
$this->getLumberjackTitle(),
|
|
|
|
$pages,
|
|
|
|
$this->getLumberjackGridFieldConfig()
|
|
|
|
);
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
$tab = Tab::create('ChildPages', $this->getLumberjackTitle(), $gridField);
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2018-04-06 00:21:26 +02:00
|
|
|
$fields->insertBefore('Main', $tab);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|