Fixed author credits in blog

This commit is contained in:
Christopher Pitt 2015-04-01 13:01:54 +13:00
parent 56f9d0c906
commit 947f65241f
2 changed files with 123 additions and 40 deletions

View File

@ -11,7 +11,7 @@
* @method ManyManyList Authors() * @method ManyManyList Authors()
* *
* @author Michael Strong <github@michaelstrong.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class BlogPost extends Page { class BlogPost extends Page {
private static $db = array( private static $db = array(
@ -70,6 +70,7 @@ class BlogPost extends Page {
* Determine if the given member is an author of this post * Determine if the given member is an author of this post
* *
* @param Member $member * @param Member $member
*
* @return boolean * @return boolean
*/ */
protected function isAuthor($member) { protected function isAuthor($member) {
@ -88,7 +89,7 @@ class BlogPost extends Page {
Requirements::css(BLOGGER_DIR . '/css/cms.css'); Requirements::css(BLOGGER_DIR . '/css/cms.css');
$self =& $this; $self =& $this;
$this->beforeUpdateCMSFields(function($fields) use ($self) { $this->beforeUpdateCMSFields(function ($fields) use ($self) {
// Add featured image // Add featured image
$fields->insertAfter( $fields->insertAfter(
@ -173,19 +174,18 @@ class BlogPost extends Page {
} }
/** /**
* Update the PublishDate to now, if being published for the first time, and the date hasn't been set to the future. * Update the PublishDate to now, if being published for the first time, and the date hasn't
* been set to the future.
**/ **/
public function onBeforePublish() { public function onBeforePublish() {
if ($this->dbObject('PublishDate')->InPast() && !$this->isPublished()) { if($this->dbObject('PublishDate')->InPast() && !$this->isPublished()) {
$this->PublishDate = SS_Datetime::now()->getValue(); $this->PublishDate = SS_Datetime::now()->getValue();
$this->write(); $this->write();
} }
} }
/** /**
* Checks the publish date to see if the blog post has actually been published. * Checks the publish date to see if the blog post has actually been published.
* *
@ -230,6 +230,7 @@ class BlogPost extends Page {
* Determine if this user can edit the authors list * Determine if this user can edit the authors list
* *
* @param Member $member * @param Member $member
*
* @return boolean * @return boolean
*/ */
public function canEditAuthors($member = null) { public function canEditAuthors($member = null) {
@ -294,7 +295,6 @@ class BlogPost extends Page {
} }
/** /**
* Returns a monthly archive link for the current blog post. * Returns a monthly archive link for the current blog post.
* *
@ -319,7 +319,6 @@ class BlogPost extends Page {
} }
/** /**
* Returns a yearly archive link for the current blog post. * Returns a yearly archive link for the current blog post.
* *
@ -331,7 +330,6 @@ class BlogPost extends Page {
} }
/** /**
* Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name') * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name')
* *
@ -343,6 +341,101 @@ class BlogPost extends Page {
return $labels; return $labels;
} }
/**
* Resolves dynamic and static authors with appropriate join data.
*
* @return ArrayList
*/
public function getCredits() {
$dynamic = $this->getDynamicAuthors();
$static = $this->getStaticAuthors();
return new ArrayList(
$this->itemsWithJoins(
array_merge($dynamic, $static)
)
);
}
/**
* Resolves all dynamic authors linked to this post.
*
* @return array
*/
protected function getDynamicAuthors() {
$items = [];
$authors = $this->Authors()->toArray();
foreach($authors as $author) {
$item = new ArrayData(array(
'Name' => $author->Name,
'Join' => '',
'URL' => '',
));
if($author->URLSegment) {
$item->URL = $this->Parent->ProfileLink($author->URLSegment);
}
$items[] = $item;
}
return $items;
}
/**
* Resolves all static authors linked to this post.
*
* @return array
*/
protected function getStaticAuthors() {
$items = [];
$authors = array_filter(explode(',', $this->AuthorNames));
foreach($authors as $author) {
$item = new ArrayData(array(
'Name' => $author,
'Join' => '',
'URL' => ''
));
$items[] = $item;
}
return $items;
}
/**
* Returns a new array with the appropriate join data.
*
* @param array $items
*
* @return array
*/
protected function itemsWithJoins(array $items) {
$count = count($items);
for($i = 0; $i < $count; $i++) {
if($count === 2 && $i > 0) {
$items[$i]->Join = ' and ';
}
if($count > 2) {
if($i > 0) {
$items[$i]->Join = ', ';
}
if($i + 1 === $count) {
$items[$i]->Join = ' and ';
}
}
}
return $items;
}
} }
@ -353,7 +446,7 @@ class BlogPost extends Page {
* @subpackage blog * @subpackage blog
* *
* @author Michael Strong <github@michaelstrong.co.uk> * @author Michael Strong <github@michaelstrong.co.uk>
**/ **/
class BlogPost_Controller extends Page_Controller { class BlogPost_Controller extends Page_Controller {
} }

View File

@ -23,18 +23,8 @@
<%t Blog.Posted "Posted" %> <%t Blog.Posted "Posted" %>
<a href="$MonthlyArchiveLink">$PublishDate.ago</a> <a href="$MonthlyArchiveLink">$PublishDate.ago</a>
<% if $Authors || $AuthorNames %> <% if $Credits %>
<%t Blog.By "by" %> <%t Blog.By "by" %>
<% if $Authors %><% loop $Authors %> <% loop $Credits %><% if $URL %>$Join<a href="$URL">$Name.XML</a><% else %>$Join$Name.XML<% end_if %><% end_loop %>
<% 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
<% end_if %>
<% end_if %> <% end_if %>
</p> </p>