Merge pull request #325 from assertchris/add-featured-posts

Added featured posts to CMS
This commit is contained in:
Damian Mooyman 2015-11-13 15:36:45 +13:00
commit bc151e1591
15 changed files with 251 additions and 111 deletions

View File

@ -0,0 +1,41 @@
<?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

@ -0,0 +1,21 @@
<?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

@ -362,7 +362,7 @@ class Blog extends Page implements PermissionProvider {
if($this->config()->grant_user_access) { if($this->config()->grant_user_access) {
$list = Member::get(); $list = Member::get();
$this->extend('updateCandidateUsers', $list); $this->extend('updateCandidateUsers', $list);
return $list; return $list;
} else { } else {
return Permission::get_members_by_permission( return Permission::get_members_by_permission(
$this->config()->grant_user_permission $this->config()->grant_user_permission
@ -471,12 +471,14 @@ class Blog extends Page implements PermissionProvider {
/** /**
* Return blog posts. * Return blog posts.
* *
* @param null|string $context Context for these blog posts (e.g 'rss')
*
* @return DataList of BlogPost objects * @return DataList of BlogPost objects
*/ */
public function getBlogPosts() { public function getBlogPosts($context = null) {
$blogPosts = BlogPost::get()->filter('ParentID', $this->ID); $blogPosts = BlogPost::get()->filter('ParentID', $this->ID);
$this->extend('updateGetBlogPosts', $blogPosts); $this->extend('updateGetBlogPosts', $blogPosts, $context);
return $blogPosts; return $blogPosts;
} }
@ -964,6 +966,7 @@ class Blog_Controller extends Page_Controller {
*/ */
public function PaginatedList() { public function PaginatedList() {
$allPosts = $this->blogPosts ?: new ArrayList(); $allPosts = $this->blogPosts ?: new ArrayList();
$posts = new PaginatedList($allPosts); $posts = new PaginatedList($allPosts);
// Set appropriate page size // Set appropriate page size
@ -994,7 +997,7 @@ class Blog_Controller extends Page_Controller {
*/ */
$dataRecord = $this->dataRecord; $dataRecord = $this->dataRecord;
$this->blogPosts = $dataRecord->getBlogPosts(); $this->blogPosts = $dataRecord->getBlogPosts('rss');
$rss = new RSSFeed($this->blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription); $rss = new RSSFeed($this->blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription);

View File

@ -173,7 +173,7 @@ class BlogPost extends Page {
$self =& $this; $self =& $this;
$this->beforeUpdateCMSFields(function ($fields) use ($self) { $this->beforeUpdateCMSFields(function ($fields) use ($self) {
$uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Featured Image')); $uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Banner Image'));
$uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')); $uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
/** /**
@ -272,6 +272,8 @@ class BlogPost extends Page {
$authorNames $authorNames
)->setTitle('Post Options'); )->setTitle('Post Options');
$options->setName('blog-admin-sidebar');
$fields->insertBefore($options, 'Root'); $fields->insertBefore($options, 'Root');
}); });
@ -658,7 +660,7 @@ class BlogPost extends Page {
public function fieldLabels($includeRelations = true) { public function fieldLabels($includeRelations = true) {
$labels = parent::fieldLabels($includeRelations); $labels = parent::fieldLabels($includeRelations);
$labels['Title'] = _t('BlogPost.PageTitleLabel', "Post Title"); $labels['Title'] = _t('BlogPost.PageTitleLabel', 'Post Title');
return $labels; return $labels;
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

38
docs/en/featured-posts.md Normal file
View File

@ -0,0 +1,38 @@
# Featured posts
You can enable featured posts with the following config:
```yaml
---
Name: featured-posts-blog-config
---
Blog:
extensions:
- BlogFeatureExtension
BlogPost:
extensions:
- BlogPostFeatureExtension
```
This will enable a checkbox in the CMS, with which you can feature blog posts:
![](_images/featured-posts-cms.png)
By default, the template will show the most recent featured post at the top of the
list of posts in a blog. This post will be removed from the normal list of blog posts.
You can increase the number of specially-displayed feature posts by modifying the
template to show more, and by changing the following config setting:
```
<% if $CanHaveFeaturedBlogPosts && $FeaturedBlogPosts %>
<% loop $FeaturedBlogPosts.Limit(10) %>
<% include FeaturedPostSummary %>
<% end_loop %>
<% end_if %>
```
```yaml
BlogFeatureExtension:
excluded_feature_posts: 10
```

View File

@ -69,7 +69,7 @@ en:
CUSTOMSUMMARY: 'Add A Custom Summary' CUSTOMSUMMARY: 'Add A Custom Summary'
Categories: Categories Categories: Categories
DESCRIPTION: 'Generic content page' DESCRIPTION: 'Generic content page'
FeaturedImage: 'Featured Image' FeaturedImage: 'Banner Image'
PLURALNAME: 'Base Pages' PLURALNAME: 'Base Pages'
PageTitleLabel: 'Post Title' PageTitleLabel: 'Post Title'
PublishDate: 'Publish Date' PublishDate: 'Publish Date'

View File

@ -1,20 +1,20 @@
<div class="cms-content-tools east cms-panel cms-panel-layout blog-admin-sidebar<% if $isOpen %> open<% end_if %>" <div class="cms-content-tools east cms-panel cms-panel-layout blog-admin-sidebar<% if $isOpen %> open<% end_if %>"
data-expandOnClick="true" data-expandOnClick="true"
data-layout-type="border" data-layout-type="border"
id="blog-admin-sidebar"> id="blog-admin-sidebar">
<div class="cms-panel-content center"> <div class="cms-panel-content center">
<div class="cms-content-view cms-tree-view-sidebar" id="blog-admin-content"> <div class="cms-content-view cms-tree-view-sidebar" id="blog-admin-content">
<h3 class="cms-panel-header">$Title</h3> <h3 class="cms-panel-header">$Title</h3>
<% loop $Children %> <% loop $Children %>
$FieldHolder $FieldHolder
<% end_loop %> <% end_loop %>
</div> </div>
</div> </div>
<div class="cms-panel-content-collapsed"> <div class="cms-panel-content-collapsed">
<h3 class="cms-panel-header">$Title</h3> <h3 class="cms-panel-header">$Title</h3>
</div> </div>
<div class="cms-panel-toggle south"> <div class="cms-panel-toggle south">
<a class="toggle-expand" href="#"><span>&laquo;</span></a> <a class="toggle-expand" href="#"><span>&laquo;</span></a>
<a class="toggle-collapse" href="#"><span>&raquo;</span></a> <a class="toggle-collapse" href="#"><span>&raquo;</span></a>
</div> </div>
</div> </div>

View File

@ -1,5 +1,5 @@
<% if $SideBarView %> <% if $SideBarView %>
<div class="blog-sidebar typography unit size1of4 lastUnit"> <div class="blog-sidebar typography unit size1of4 lastUnit">
$SideBarView $SideBarView
</div> </div>
<% end_if %> <% end_if %>

View File

@ -0,0 +1,26 @@
<div class="post-summary featured">
<h2>
<a href="$Link" title="<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>">
<% if $MenuTitle %>$MenuTitle
<% else %>$Title<% end_if %>
</a>
</h2>
<p class="post-image">
<a href="$Link" <%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>>
$FeaturedImage.setWidth(795)
</a>
</p>
<% if $Summary %>
<p>$Summary
<% else %>
<p>$Excerpt
<% end_if %>
<a href="$Link">
<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>
</a>
</p>
<% include EntryMeta %>
</div>

View File

@ -1,13 +1,13 @@
<section> <section>
<h1>$CurrentProfile.FirstName $CurrentProfile.Surname</h1> <h1>$CurrentProfile.FirstName $CurrentProfile.Surname</h1>
<div> <div>
<% if $CurrentProfile.BlogProfileImage %> <% if $CurrentProfile.BlogProfileImage %>
<div class="profile-image"> <div class="profile-image">
$CurrentProfile.BlogProfileImage.setWidth(180) $CurrentProfile.BlogProfileImage.setWidth(180)
</div> </div>
<% end_if %> <% end_if %>
<div class="profile-summary"> <div class="profile-summary">
<p>$CurrentProfile.BlogProfileSummary</p> <p>$CurrentProfile.BlogProfileSummary</p>
</div> </div>
</div> </div>
</section> </section>

View File

@ -1,25 +1,25 @@
<%-- NOTE: Before including this, you will need to wrap the include in a with block --%> <%-- NOTE: Before including this, you will need to wrap the include in a with block --%>
<% if $MoreThanOnePage %> <% if $MoreThanOnePage %>
<p class="pagination"> <p class="pagination">
<% if $NotFirstPage %> <% if $NotFirstPage %>
<a class="prev" href="{$PrevLink}">&larr;</a> <a class="prev" href="{$PrevLink}">&larr;</a>
<% end_if %> <% end_if %>
<% loop $Pages %> <% loop $Pages %>
<% if $CurrentBool %> <% if $CurrentBool %>
<span>$PageNum</span> <span>$PageNum</span>
<% else %> <% else %>
<% if $Link %> <% if $Link %>
<a href="$Link">$PageNum</a> <a href="$Link">$PageNum</a>
<% else %> <% else %>
<span>...</span> <span>...</span>
<% end_if %> <% end_if %>
<% end_if %> <% end_if %>
<% end_loop %> <% end_loop %>
<% if $NotLastPage %> <% if $NotLastPage %>
<a class="next" href="{$NextLink}">&rarr;</a> <a class="next" href="{$NextLink}">&rarr;</a>
<% end_if %> <% end_if %>
</p> </p>
<% end_if %> <% end_if %>

View File

@ -2,41 +2,50 @@
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>"> <div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
<article> <article>
<h1> <h1>
<% if $ArchiveYear %> <% if $ArchiveYear %>
<%t Blog.Archive "Archive" %>: <%t Blog.Archive 'Archive' %>:
<% if $ArchiveDay %> <% if $ArchiveDay %>
$ArchiveDate.Nice $ArchiveDate.Nice
<% else_if $ArchiveMonth %> <% else_if $ArchiveMonth %>
$ArchiveDate.format("F, Y") $ArchiveDate.format('F, Y')
<% else %> <% else %>
$ArchiveDate.format("Y") $ArchiveDate.format('Y')
<% end_if %> <% end_if %>
<% else_if $CurrentTag %> <% else_if $CurrentTag %>
<%t Blog.Tag "Tag" %>: $CurrentTag.Title <%t Blog.Tag 'Tag' %>: $CurrentTag.Title
<% else_if $CurrentCategory %> <% else_if $CurrentCategory %>
<%t Blog.Category "Category" %>: $CurrentCategory.Title <%t Blog.Category 'Category' %>: $CurrentCategory.Title
<% else %> <% else %>
$Title $Title
<% end_if %> <% end_if %>
</h1> </h1>
<div class="content">$Content</div> <div class="content">$Content</div>
<% if $PaginatedList.Exists %> <% if $FeaturedBlogPosts.Exists && $FeaturedBlogPosts.First %>
<% loop $PaginatedList %> <% with $FeaturedBlogPosts.First %>
<% include PostSummary %> <% include FeaturedPostSummary %>
<% end_loop %> <% end_with %>
<% else %> <% end_if %>
<p><%t Blog.NoPosts "There are no posts" %></p> <% if $PaginatedList.Exists %>
<% end_if %> <% loop $PaginatedList %>
</article> <% include PostSummary %>
<% end_loop %>
$Form <% else %>
$CommentsForm <% if $FeaturedBlogPosts.Exists %>
<p><%t Blog.NoUnfeaturedPosts 'There are no non-featured posts' %></p>
<% with $PaginatedList %> <% else %>
<p><%t Blog.NoPosts 'There are no posts' %></p>
<% end_if %>
<% end_if %>
</article>
$Form
$CommentsForm
<% with $PaginatedList %>
<% include Pagination %> <% include Pagination %>
<% end_with %> <% end_with %>
</div> </div>

View File

@ -3,17 +3,17 @@
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>"> <div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
<article> <article>
<h1>$Title</h1> <h1>$Title</h1>
<% if $FeaturedImage %> <% if $FeaturedImage %>
<p class="post-image">$FeaturedImage.setWidth(795)</p> <p class="post-image">$FeaturedImage.setWidth(795)</p>
<% end_if %> <% end_if %>
<div class="content">$Content</div> <div class="content">$Content</div>
<% include EntryMeta %> <% include EntryMeta %>
</article> </article>
$Form $Form
$CommentsForm $CommentsForm
</div> </div>

View File

@ -2,22 +2,22 @@
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>"> <div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
<% include MemberDetails %> <% include MemberDetails %>
<% if $PaginatedList.Exists %> <% if $PaginatedList.Exists %>
<h2>Posts by $CurrentProfile.FirstName $CurrentProfile.Surname for $Title:</h2> <h2>Posts by $CurrentProfile.FirstName $CurrentProfile.Surname for $Title:</h2>
<% loop $PaginatedList %> <% loop $PaginatedList %>
<% include PostSummary %> <% include PostSummary %>
<% end_loop %> <% end_loop %>
<% end_if %> <% end_if %>
$Form
$CommentsForm
<% with $PaginatedList %> $Form
<% include Pagination %> $CommentsForm
<% end_with %>
<% with $PaginatedList %>
<% include Pagination %>
<% end_with %>
</div> </div>
<% include BlogSideBar %> <% include BlogSideBar %>