Add user profiles to Blog

This commit is contained in:
David Craig 2015-03-23 11:18:02 +13:00
parent 47369dc9b6
commit 49bee390a3
8 changed files with 224 additions and 44 deletions

View File

@ -0,0 +1,3 @@
Member:
extensions:
- BlogMemberExtension

View File

@ -0,0 +1,76 @@
<?php
/**
* This class is responsible for add Blog specific behaviour to Members.
*
* @package silverstripe
* @subpackage blog
*
**/
class BlogMemberExtension extends DataExtension {
private static $db = array(
'URLSegment' => 'Varchar',
'BlogProfileSummary' => 'Text'
);
private static $has_one = array(
'BlogProfileImage' => 'Image'
);
private static $belongs_many_many = array(
'AuthoredPosts' => 'BlogPost'
);
public function onBeforeWrite() {
// Generate a unique URL segment for the Member.
$count = 1;
$this->owner->URLSegment = $this->generateURLSegment();
while(!$this->validURLSegment()) {
$this->owner->URLSegment = preg_replace('/-[0-9]+$/', null, $this->owner->URLSegment) . '-' . $count;
$count++;
}
}
public function updateCMSFields(FieldList $fields) {
$fields->removeByName('URLSegment');
return $fields;
}
/**
* Generate a unique URL segment based on the Member's name.
*
* @return string Generated URL segment.
*/
public function generateURLSegment() {
$filter = URLSegmentFilter::create();
$name = $this->owner->FirstName . ' ' . $this->owner->Surname;
$urlSegment = $filter->filter($name);
// Fallback to generic profile name if path is empty (= no valid, convertable characters)
if(!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') $urlSegment = "profile-$this->ID";
return $urlSegment;
}
/**
* Returns TRUE if this object has a URL segment value that does not conflict with any other objects.
*
* @return bool
*/
public function validURLSegment() {
$conflict = Member::get()->filter('URLSegment', $this->owner->URLSegment);
// If the Member we're checking against is saved, exclude them from the check.
// i.e. don't conflict against yourself.
if ($this->owner->ID) {
$conflict = $conflict->exclude('ID', $this->owner->ID);
}
return $conflict->count() == 0;
}
}

View File

@ -330,6 +330,16 @@ class Blog extends Page implements PermissionProvider {
} }
/**
* Get a link to a Member profile.
*
* @param urlSegment
* @return String
*/
public function ProfileLink($urlSegment) {
return Controller::join_links($this->Link(), 'profile', $urlSegment);
}
/** /**
* This sets the title for our gridfield * This sets the title for our gridfield
@ -450,12 +460,14 @@ class Blog_Controller extends Page_Controller {
'tag', 'tag',
'category', 'category',
'rss', 'rss',
'profile'
); );
private static $url_handlers = array( private static $url_handlers = array(
'tag/$Tag!' => 'tag', 'tag/$Tag!' => 'tag',
'category/$Category!' => 'category', 'category/$Category!' => 'category',
'archive/$Year!/$Month/$Day' => 'archive', 'archive/$Year!/$Month/$Day' => 'archive',
'profile/$URLSegment!' => 'profile'
); );
@ -473,7 +485,54 @@ class Blog_Controller extends Page_Controller {
return $this->render(); return $this->render();
} }
/**
* Renders a Blog Member's profile.
*
* @return SS_HTTPResponse
**/
public function profile() {
$profile = $this->getCurrentProfile();
if(!$profile) {
return $this->httpError(404, 'Not Found');
}
$this->blogPosts = $this->getCurrentProfilePosts();
return $this->render();
}
/**
* Get the Member associated with the current URL segment.
*
* @return Member|null
**/
public function getCurrentProfile() {
$urlSegment = $this->request->param('URLSegment');
if($urlSegment) {
return Member::get()
->filter('URLSegment', $urlSegment)
->first();
}
return null;
}
/**
* Get posts related to the current Member profile
*
* @return DataList|null
**/
public function getCurrentProfilePosts() {
$profile = $this->getCurrentProfile();
if($profile) {
return $profile->AuthoredPosts()->filter('ParentID', $this->ID);
}
return null;
}
/** /**
* Renders an archive for a specificed date. This can be by year or year/month * Renders an archive for a specificed date. This can be by year or year/month

View File

@ -1,32 +1,37 @@
<p class="blog-post-meta"> <p class="blog-post-meta">
<% if $Categories.exists %> <% if $Categories.exists %>
<%t Blog.PostedIn "Posted in" %> <%t Blog.PostedIn "Posted in" %>
<% loop $Categories %> <% loop $Categories %>
<a href="$Link" title="$Title">$Title</a><% if not Last %>, <% else %>;<% end_if %> <a href="$Link" title="$Title">$Title</a><% if not Last %>, <% else %>;<% end_if %>
<% end_loop %> <% end_loop %>
<% end_if %> <% end_if %>
<% if $Tags.exists %> <% if $Tags.exists %>
<%t Blog.Tagged "Tagged" %> <%t Blog.Tagged "Tagged" %>
<% loop $Tags %> <% loop $Tags %>
<a href="$Link" title="$Title">$Title</a><% if not Last %>, <% else %>;<% end_if %> <a href="$Link" title="$Title">$Title</a><% if not Last %>, <% else %>;<% end_if %>
<% end_loop %> <% end_loop %>
<% end_if %> <% end_if %>
<% if $Comments.exists %> <% if $Comments.exists %>
<a href="{$Link}#comments-holder"> <a href="{$Link}#comments-holder">
<%t Blog.Comments "Comments" %> <%t Blog.Comments "Comments" %>
$Comments.count $Comments.count
</a>; </a>;
<% end_if %> <% end_if %>
<%t Blog.Posted "Posted" %> <%t Blog.Posted "Posted" %>
<a href="$MonthlyArchiveLink">$PublishDate.ago</a> <a href="$MonthlyArchiveLink">$PublishDate.ago</a>
<% if $Authors || $AuthorNames %> <% if $Authors || $AuthorNames %>
<%t Blog.By "by" %> <%t Blog.By "by" %>
<% if $Authors %><% loop $Authors %> <% if $Authors %><% loop $Authors %>
$Name.XML<% if not $Last || $Up.AuthorNames %>,<% end_if %> <% if $URLSegment %>
<a href="{$Up.Parent.ProfileLink($URLSegment)}">$Name.XML</a>
<% else %>
$Name.XML
<% end_if %>
<% if not $Last || $Up.AuthorNames %>,<% end_if %>
<% end_loop %><% end_if %> <% end_loop %><% end_if %>
<% if $AuthorNames %> <% if $AuthorNames %>
$AuthorNames $AuthorNames

View File

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

View File

@ -0,0 +1,29 @@
<div class="post-summary">
<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 $Excerpt %>
<p>
$Excerpt
<a href="$Link">
<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>
</a>
</p>
<% else %>
<p><a href="$Link">
<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>
</a></p>
<% end_if %>
<% include EntryMeta %>
</div>

View File

@ -26,35 +26,7 @@
<% if $PaginatedList.Exists %> <% if $PaginatedList.Exists %>
<% loop $PaginatedList %> <% loop $PaginatedList %>
<div class="post-summary"> <% include PostSummary %>
<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 $Excerpt %>
<p>
$Excerpt
<a href="$Link">
<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>
</a>
</p>
<% else %>
<p><a href="$Link">
<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>
</a></p>
<% end_if %>
<% include EntryMeta %>
</div>
<% end_loop %> <% end_loop %>
<% else %> <% else %>
<p><%t Blog.NoPosts "There are no posts" %></p> <p><%t Blog.NoPosts "There are no posts" %></p>

View File

@ -0,0 +1,23 @@
<% require themedCSS('blog', 'blog') %>
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
<% include MemberDetails %>
<% if $PaginatedList.Exists %>
<h2>Posts by $CurrentProfile.FirstName $CurrentProfile.Surname for $Title:</h2>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% end_if %>
$Form
$PageComments
<% with $PaginatedList %>
<% include Pagination %>
<% end_with %>
</div>
<% include BlogSideBar %>