mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
Add user profiles to Blog
This commit is contained in:
parent
47369dc9b6
commit
49bee390a3
@ -0,0 +1,3 @@
|
||||
Member:
|
||||
extensions:
|
||||
- BlogMemberExtension
|
76
code/extensions/BlogMemberExtension.php
Normal file
76
code/extensions/BlogMemberExtension.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
@ -450,12 +460,14 @@ class Blog_Controller extends Page_Controller {
|
||||
'tag',
|
||||
'category',
|
||||
'rss',
|
||||
'profile'
|
||||
);
|
||||
|
||||
private static $url_handlers = array(
|
||||
'tag/$Tag!' => 'tag',
|
||||
'category/$Category!' => 'category',
|
||||
'archive/$Year!/$Month/$Day' => 'archive',
|
||||
'profile/$URLSegment!' => 'profile'
|
||||
);
|
||||
|
||||
|
||||
@ -473,7 +485,54 @@ class Blog_Controller extends Page_Controller {
|
||||
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
|
||||
|
@ -1,32 +1,37 @@
|
||||
<p class="blog-post-meta">
|
||||
<% if $Categories.exists %>
|
||||
<%t Blog.PostedIn "Posted in" %>
|
||||
<% if $Categories.exists %>
|
||||
<%t Blog.PostedIn "Posted in" %>
|
||||
<% loop $Categories %>
|
||||
<a href="$Link" title="$Title">$Title</a><% if not Last %>, <% else %>;<% end_if %>
|
||||
<% end_loop %>
|
||||
<% end_if %>
|
||||
|
||||
<% if $Tags.exists %>
|
||||
<%t Blog.Tagged "Tagged" %>
|
||||
|
||||
<% if $Tags.exists %>
|
||||
<%t Blog.Tagged "Tagged" %>
|
||||
<% loop $Tags %>
|
||||
<a href="$Link" title="$Title">$Title</a><% if not Last %>, <% else %>;<% end_if %>
|
||||
<% end_loop %>
|
||||
<% end_if %>
|
||||
|
||||
<% if $Comments.exists %>
|
||||
<a href="{$Link}#comments-holder">
|
||||
<%t Blog.Comments "Comments" %>
|
||||
$Comments.count
|
||||
</a>;
|
||||
|
||||
<% if $Comments.exists %>
|
||||
<a href="{$Link}#comments-holder">
|
||||
<%t Blog.Comments "Comments" %>
|
||||
$Comments.count
|
||||
</a>;
|
||||
<% end_if %>
|
||||
|
||||
|
||||
<%t Blog.Posted "Posted" %>
|
||||
<a href="$MonthlyArchiveLink">$PublishDate.ago</a>
|
||||
|
||||
<a href="$MonthlyArchiveLink">$PublishDate.ago</a>
|
||||
|
||||
<% if $Authors || $AuthorNames %>
|
||||
<%t Blog.By "by" %>
|
||||
<% 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 %>
|
||||
<% if $AuthorNames %>
|
||||
$AuthorNames
|
||||
|
13
templates/Includes/MemberDetails.ss
Normal file
13
templates/Includes/MemberDetails.ss
Normal 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>
|
29
templates/Includes/PostSummary.ss
Normal file
29
templates/Includes/PostSummary.ss
Normal 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>
|
@ -26,35 +26,7 @@
|
||||
|
||||
<% if $PaginatedList.Exists %>
|
||||
<% loop $PaginatedList %>
|
||||
<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>
|
||||
<% include PostSummary %>
|
||||
<% end_loop %>
|
||||
<% else %>
|
||||
<p><%t Blog.NoPosts "There are no posts" %></p>
|
||||
|
23
templates/Layout/Blog_profile.ss
Normal file
23
templates/Layout/Blog_profile.ss
Normal 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 %>
|
Loading…
Reference in New Issue
Block a user