Removed sneaky merge featured posts extensions.

This commit is contained in:
micmania1 2015-11-13 04:32:10 +00:00
parent bc151e1591
commit 344ab7b17a
5 changed files with 59 additions and 86 deletions

View File

@ -1,41 +0,0 @@
<?php
class BlogFeatureExtension extends DataExtension {
/**
* @config
*
* @var int
*/
private static $excluded_feature_posts = 1;
/**
* @return DataList
*/
public function getFeaturedBlogPosts() {
return BlogPost::get()
->filter('ParentID', $this->owner->ID)
->filter('IsFeatured', true);
}
/**
* @param DataList $posts
* @param null|string $context Context for these blog posts (e.g 'rss')
*
* @return DataList
*/
public function updateGetBlogPosts(DataList &$posts, $context = null) {
if($context === 'rss') {
return;
}
$excluded = (int) Config::inst()->get('BlogFeatureExtension', 'excluded_feature_posts');
if($excluded > 0) {
$taken = $this->getFeaturedBlogPosts()->limit($excluded);
if ($taken->count()) {
$posts = $posts->exclude(array('ID' => $taken->getIDList()));
}
}
}
}

View File

@ -1,21 +0,0 @@
<?php
class BlogPostFeatureExtension extends DataExtension {
/**
* @var array
*/
private static $db = array(
'IsFeatured' => 'Boolean',
);
/**
* @inheritdoc
*
* @param FieldList
*/
public function updateCMSFields(FieldList $fields) {
$sidebar = $fields->fieldByName('blog-admin-sidebar');
$sidebar->insertBefore('PublishDate', new CheckboxField('IsFeatured', 'Mark as featured post'));
}
}

View File

@ -49,11 +49,19 @@ class Blog extends Page implements PermissionProvider {
*/
private static $grant_user_group = 'blog-users';
/**
* @config
*
* @var int
*/
private static $exclude_featured_posts = 1;
/**
* @var array
*/
private static $db = array(
'PostsPerPage' => 'Int',
'EnableFeaturedPosts' => 'Boolean',
);
/**
@ -288,9 +296,10 @@ class Blog extends Page implements PermissionProvider {
public function getSettingsFields() {
$fields = parent::getSettingsFields();
$fields->addFieldToTab('Root.Settings',
NumericField::create('PostsPerPage', _t('Blog.PostsPerPage', 'Posts Per Page'))
);
$fields->addFieldsToTab('Root.Settings', array(
NumericField::create('PostsPerPage', _t('Blog.PostsPerPage', 'Posts Per Page')),
CheckboxField::create('EnableFeaturedPosts', 'Enable featured posts?'),
));
$members = $this->getCandidateUsers()->map()->toArray();
@ -471,14 +480,22 @@ class Blog extends Page implements PermissionProvider {
/**
* Return blog posts.
*
* @param null|string $context Context for these blog posts (e.g 'rss')
*
* @return DataList of BlogPost objects
*/
public function getBlogPosts($context = null) {
public function getBlogPosts($excludeFeaturedPosts = true) {
$blogPosts = BlogPost::get()->filter('ParentID', $this->ID);
$this->extend('updateGetBlogPosts', $blogPosts, $context);
if($excludeFeaturedPosts
&& $this->isFeaturedPostsEnabled()
&& $this->config()->exclude_featured_posts > 0
) {
$excluded = $this->getFeaturedBlogPosts()
->limit((int) $this->config()->exclude_featured_posts)
->getIDList();
$blogPosts = $blogPosts->exclude(array('ID' => $excluded));
}
$this->extend('updateGetBlogPosts', $blogPosts);
return $blogPosts;
}
@ -586,6 +603,28 @@ class Blog extends Page implements PermissionProvider {
return $group;
}
/**
* @return SS_List
*/
public function getFeaturedBlogPosts() {
if($this->isFeaturedPostsEnabled()) {
return BlogPost::get()
->filter('ParentID', $this->owner->ID)
->filter('IsFeatured', true);
}
return new ArrayList();
}
/**
* Featured posts can be enabled per blog in the CMS settings page.
*
* @return bool
*/
public function isFeaturedPostsEnabled() {
return $this->EnableFeaturedPosts;
}
}
/**
@ -997,7 +1036,7 @@ class Blog_Controller extends Page_Controller {
*/
$dataRecord = $this->dataRecord;
$this->blogPosts = $dataRecord->getBlogPosts('rss');
$this->blogPosts = $dataRecord->getBlogPosts(false);
$rss = new RSSFeed($this->blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription);

View File

@ -31,6 +31,7 @@ class BlogPost extends Page {
'PublishDate' => 'SS_Datetime',
'AuthorNames' => 'Varchar(1024)',
'Summary' => 'HTMLText',
'IsFeatured' => 'Boolean',
);
/**
@ -272,6 +273,13 @@ class BlogPost extends Page {
$authorNames
)->setTitle('Post Options');
// Add featured checkbox to admin sidepanel is enabled on this blog
if($this->Parent() instanceof Blog && $this->Parent()->isFeaturedPostsEnabled()) {
$options->insertBefore('PublishDate',
CheckboxField::create('IsFeatured', 'Mark as featured post')
);
}
$options->setName('blog-admin-sidebar');
$fields->insertBefore($options, 'Root');

View File

@ -1,19 +1,7 @@
# Featured posts
You can enable featured posts with the following config:
```yaml
---
Name: featured-posts-blog-config
---
Blog:
extensions:
- BlogFeatureExtension
BlogPost:
extensions:
- BlogPostFeatureExtension
```
Featured posts can be enabled on a per-blog basis via the 'Settings' tab of each blog
in the CMS.
This will enable a checkbox in the CMS, with which you can feature blog posts:
@ -33,6 +21,6 @@ template to show more, and by changing the following config setting:
```
```yaml
BlogFeatureExtension:
excluded_feature_posts: 10
Blog:
excluded_featured_posts: 10
```