From 344ab7b17a01a4964091a84f9bdc76d429c0ed46 Mon Sep 17 00:00:00 2001 From: micmania1 Date: Fri, 13 Nov 2015 04:32:10 +0000 Subject: [PATCH] Removed sneaky merge featured posts extensions. --- code/extensions/BlogFeatureExtension.php | 41 --------------- code/extensions/BlogPostFeatureExtension.php | 21 -------- code/model/Blog.php | 55 +++++++++++++++++--- code/model/BlogPost.php | 8 +++ docs/en/featured-posts.md | 20 ++----- 5 files changed, 59 insertions(+), 86 deletions(-) delete mode 100644 code/extensions/BlogFeatureExtension.php delete mode 100644 code/extensions/BlogPostFeatureExtension.php diff --git a/code/extensions/BlogFeatureExtension.php b/code/extensions/BlogFeatureExtension.php deleted file mode 100644 index cbd076f..0000000 --- a/code/extensions/BlogFeatureExtension.php +++ /dev/null @@ -1,41 +0,0 @@ -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())); - } - } - } -} diff --git a/code/extensions/BlogPostFeatureExtension.php b/code/extensions/BlogPostFeatureExtension.php deleted file mode 100644 index 99d1593..0000000 --- a/code/extensions/BlogPostFeatureExtension.php +++ /dev/null @@ -1,21 +0,0 @@ - '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')); - } -} diff --git a/code/model/Blog.php b/code/model/Blog.php index e3da896..0ea14d0 100644 --- a/code/model/Blog.php +++ b/code/model/Blog.php @@ -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); diff --git a/code/model/BlogPost.php b/code/model/BlogPost.php index 84ca8f2..38f7960 100644 --- a/code/model/BlogPost.php +++ b/code/model/BlogPost.php @@ -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'); diff --git a/docs/en/featured-posts.md b/docs/en/featured-posts.md index e7bea76..cf7fd49 100644 --- a/docs/en/featured-posts.md +++ b/docs/en/featured-posts.md @@ -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 ```