2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Model;
|
|
|
|
|
|
|
|
use SilverStripe\Blog\Model\Blog;
|
2017-01-24 01:45:07 +01:00
|
|
|
use SilverStripe\Blog\Model\BlogFilter\BlogFilterGridField;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Blog\Model\BlogPost;
|
|
|
|
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;
|
2017-04-18 09:02:04 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @package silverstripe
|
|
|
|
* @subpackage blog
|
|
|
|
*/
|
|
|
|
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()
|
|
|
|
{
|
2017-01-24 01:45:07 +01: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)) {
|
|
|
|
$pages = BlogPost::get()->filter(array(
|
|
|
|
'ParentID' => $this->owner->ID,
|
|
|
|
'ClassName' => $excluded
|
|
|
|
));
|
|
|
|
|
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
|
|
|
|
|
|
|
$fields->insertBefore($tab, 'Main');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|