From 947f65241f731d5bd918779c0002d02d6c67248f Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Wed, 1 Apr 2015 13:01:54 +1300 Subject: [PATCH] Fixed author credits in blog --- code/model/BlogPost.php | 149 ++++++++++++++++++++++++++------ templates/Includes/EntryMeta.ss | 14 +-- 2 files changed, 123 insertions(+), 40 deletions(-) diff --git a/code/model/BlogPost.php b/code/model/BlogPost.php index dd699d7..c50e86c 100644 --- a/code/model/BlogPost.php +++ b/code/model/BlogPost.php @@ -3,15 +3,15 @@ /** * An indivisual blog post. * - * @package silverstripe + * @package silverstripe * @subpackage blog * * @method ManyManyList Categories() * @method ManyManyList Tags() * @method ManyManyList Authors() * - * @author Michael Strong -**/ + * @author Michael Strong + **/ class BlogPost extends Page { private static $db = array( @@ -62,7 +62,7 @@ class BlogPost extends Page { * variable can be configured using YAML. * * @var boolean - **/ + **/ private static $show_in_sitetree = false; @@ -70,6 +70,7 @@ class BlogPost extends Page { * Determine if the given member is an author of this post * * @param Member $member + * * @return boolean */ protected function isAuthor($member) { @@ -88,7 +89,7 @@ class BlogPost extends Page { Requirements::css(BLOGGER_DIR . '/css/cms.css'); $self =& $this; - $this->beforeUpdateCMSFields(function($fields) use ($self) { + $this->beforeUpdateCMSFields(function ($fields) use ($self) { // Add featured image $fields->insertAfter( @@ -173,26 +174,25 @@ 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() { - if ($this->dbObject('PublishDate')->InPast() && !$this->isPublished()) { + if($this->dbObject('PublishDate')->InPast() && !$this->isPublished()) { $this->PublishDate = SS_Datetime::now()->getValue(); $this->write(); } } - /** * Checks the publish date to see if the blog post has actually been published. * * @param $member Member|null * * @return boolean - **/ + **/ public function canView($member = null) { if(!parent::canView($member)) return false; @@ -230,6 +230,7 @@ class BlogPost extends Page { * Determine if this user can edit the authors list * * @param Member $member + * * @return boolean */ public function canEditAuthors($member = null) { @@ -287,29 +288,28 @@ class BlogPost extends Page { * * @param $wordCount int - number of words to display * - * @return string - **/ + * @return string + **/ public function Excerpt($wordCount = 30) { return $this->dbObject("Content")->LimitWordCount($wordCount); } - /** * Returns a monthly archive link for the current blog post. * * @param $type string day|month|year * * @return string URL - **/ + **/ public function getMonthlyArchiveLink($type = "day") { $date = $this->dbObject("PublishDate"); if($type != "year") { if($type == "day") { return Controller::join_links( - $this->Parent()->Link("archive"), - $date->format("Y"), - $date->format("m"), + $this->Parent()->Link("archive"), + $date->format("Y"), + $date->format("m"), $date->format("d") ); } @@ -319,41 +319,134 @@ class BlogPost extends Page { } - /** * Returns a yearly archive link for the current blog post. * * @return string URL - **/ + **/ public function getYearlyArchiveLink() { $date = $this->dbObject("PublishDate"); return Controller::join_links($this->Parent()->Link("archive"), $date->format("Y")); } - /** * Sets the label for BlogPost.Title to 'Post Title' (Rather than 'Page name') * * @return array - **/ - public function fieldLabels($includerelations = true) { + **/ + public function fieldLabels($includerelations = true) { $labels = parent::fieldLabels($includerelations); - $labels['Title'] = _t('BlogPost.PageTitleLabel', "Post Title"); + $labels['Title'] = _t('BlogPost.PageTitleLabel', "Post Title"); 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; + } } /** * Blog Post controller * - * @package silverstripe + * @package silverstripe * @subpackage blog * - * @author Michael Strong -**/ + * @author Michael Strong + **/ class BlogPost_Controller extends Page_Controller { - + } diff --git a/templates/Includes/EntryMeta.ss b/templates/Includes/EntryMeta.ss index 0569388..93fec06 100644 --- a/templates/Includes/EntryMeta.ss +++ b/templates/Includes/EntryMeta.ss @@ -23,18 +23,8 @@ <%t Blog.Posted "Posted" %> $PublishDate.ago - <% if $Authors || $AuthorNames %> + <% if $Credits %> <%t Blog.By "by" %> - <% if $Authors %><% loop $Authors %> - <% if $URLSegment %> - $Name.XML - <% else %> - $Name.XML - <% end_if %> - <% if not $Last || $Up.AuthorNames %>,<% end_if %> - <% end_loop %><% end_if %> - <% if $AuthorNames %> - $AuthorNames - <% end_if %> + <% loop $Credits %><% if $URL %>$Join$Name.XML<% else %>$Join$Name.XML<% end_if %><% end_loop %> <% end_if %>