silverstripe-blog/code/extensions/BlogFilter.php

107 lines
2.6 KiB
PHP
Raw Normal View History

2013-08-04 18:38:26 +02:00
<?php
/**
2015-05-09 16:33:12 +02:00
* This class is responsible for filtering the SiteTree when necessary and also overlaps into
* filtering only published posts.
2013-08-04 18:38:26 +02:00
*
* @package silverstripe
* @subpackage blog
2015-05-09 16:33:12 +02:00
*/
class BlogFilter extends Lumberjack {
2013-08-04 18:38:26 +02:00
/**
2015-05-09 16:33:12 +02:00
* {@inheritdoc}
*/
2013-08-04 18:38:26 +02:00
public function stageChildren($showAll = false) {
$staged = parent::stageChildren($showAll);
2015-05-09 16:33:12 +02:00
if(!$this->shouldFilter() && $this->subclassForBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) {
2013-08-04 18:38:26 +02:00
$stage = Versioned::current_stage();
2015-05-09 16:33:12 +02:00
if($stage == 'Stage') {
$stage = '';
} elseif($stage) {
$stage = '_' . $stage;
}
2013-08-04 18:38:26 +02:00
$dataQuery = $staged->dataQuery()
2015-05-14 01:11:50 +02:00
->innerJoin('BlogPost', sprintf('"BlogPost%s"."ID" = "SiteTree%s"."ID"', $stage, $stage))
->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(SS_Datetime::now())));
2015-05-09 16:33:12 +02:00
2013-08-04 18:38:26 +02:00
$staged = $staged->setDataQuery($dataQuery);
}
2015-05-09 16:33:12 +02:00
2013-08-04 18:38:26 +02:00
return $staged;
}
2015-05-09 16:33:12 +02:00
/**
* @return bool
*/
protected function subclassForBlog() {
return in_array(get_class($this->owner), ClassInfo::subClassesFor('Blog'));
}
/**
* {@inheritdoc}
*/
2013-08-04 18:38:26 +02:00
public function liveChildren($showAll = false, $onlyDeletedFromStage = false) {
$staged = parent::liveChildren($showAll, $onlyDeletedFromStage);
2015-05-09 16:33:12 +02:00
if(!$this->shouldFilter() && $this->isBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) {
2013-08-04 18:38:26 +02:00
$dataQuery = $staged->dataQuery()
2015-05-14 01:11:50 +02:00
->innerJoin('BlogPost', '"BlogPost_Live"."ID" = "SiteTree_Live"."ID"')
->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(SS_Datetime::now())));
2015-05-09 16:33:12 +02:00
2013-08-04 18:38:26 +02:00
$staged = $staged->setDataQuery($dataQuery);
}
2015-05-09 16:33:12 +02:00
2013-08-04 18:38:26 +02:00
return $staged;
}
2015-05-09 16:33:12 +02:00
/**
* @return bool
*/
protected function isBlog() {
return $this->owner instanceof Blog;
}
/**
* {@inheritdoc}
*/
public function updateCMSFields(FieldList $fields) {
$excluded = $this->owner->getExcludedSiteTreeClassNames();
2015-05-09 16:33:12 +02:00
if(!empty($excluded)) {
$pages = BlogPost::get()->filter(array(
'ParentID' => $this->owner->ID,
'ClassName' => $excluded
));
2015-05-09 16:33:12 +02:00
$gridField = new BlogFilter_GridField(
2015-05-09 16:33:12 +02:00
'ChildPages',
$this->getLumberjackTitle(),
$pages,
$this->getLumberjackGridFieldConfig()
);
$tab = new Tab('ChildPages', $this->getLumberjackTitle(), $gridField);
2015-05-09 16:33:12 +02:00
2015-05-09 10:27:56 +02:00
$fields->insertBefore($tab, 'Main');
}
}
}
/**
2015-05-09 16:33:12 +02:00
* Enables children of non-editable pages to be edited.
*/
class BlogFilter_GridField extends GridField {
2015-05-09 16:33:12 +02:00
/**
* @param FormTransformation $transformation
*
* @return $this
*/
public function transform(FormTransformation $transformation) {
return $this;
}
}