silverstripe-blog/src/Model/BlogFilter.php

119 lines
3.3 KiB
PHP
Raw Normal View History

2015-11-21 07:17:29 +01:00
<?php
namespace SilverStripe\Blog\Model;
use SilverStripe\Blog\Model\Blog;
use SilverStripe\Blog\Model\BlogFilter\BlogFilterGridField;
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;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Versioned\Versioned;
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()
->innerJoin(
DataObject::getSchema()->tableName(BlogPost::class),
sprintf('"BlogPost%s"."ID" = "SiteTree%s"."ID"', $stage, $stage)
)
->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()
{
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()
->innerJoin(
DataObject::getSchema()->tableName(BlogPost::class),
'"BlogPost_Live"."ID" = "SiteTree_Live"."ID"'
)
->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
));
$gridField = BlogFilterGridField::create(
2015-11-21 07:17:29 +01:00
'ChildPages',
$this->getLumberjackTitle(),
$pages,
$this->getLumberjackGridFieldConfig()
);
$tab = Tab::create('ChildPages', $this->getLumberjackTitle(), $gridField);
2015-11-21 07:17:29 +01:00
$fields->insertBefore($tab, 'Main');
}
}
}