silverstripe-blog/src/Model/BlogFilter.php

115 lines
3.2 KiB
PHP
Raw Normal View History

2015-11-21 07:17:29 +01:00
<?php
namespace SilverStripe\Blog\Model;
use SilverStripe\Blog\Model\BlogFilter\BlogFilterGridField;
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\Security\Permission;
2018-01-29 04:13:19 +01:00
use SilverStripe\Versioned\Versioned;
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()
->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()
{
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()
->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)) {
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
$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('Main', $tab);
2015-11-21 07:17:29 +01:00
}
}
}