From a40ae58623d9189a49cbaa6e39b84ba66b87710a Mon Sep 17 00:00:00 2001 From: Christopher Pitt Date: Wed, 11 Mar 2015 08:01:51 +1300 Subject: [PATCH] Adding authors to posts --- code/model/BlogPost.php | 49 ++++++++++++++++++++++++++++++++++------- css/cms.css | 12 ++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/code/model/BlogPost.php b/code/model/BlogPost.php index 1c9ade1..754882a 100644 --- a/code/model/BlogPost.php +++ b/code/model/BlogPost.php @@ -21,6 +21,7 @@ class BlogPost extends Page { private static $many_many = array( "Categories" => "BlogCategory", "Tags" => "BlogTag", + "Authors" => "Member", ); private static $defaults = array( @@ -100,7 +101,27 @@ class BlogPost extends Page { "Tags", _t("BlogPost.Tags", "Tags"), $self->Parent()->Tags()->map()->toArray() - )->setMultiple(true) + )->setMultiple(true), + ListboxField::create( + "Authors", + _t("BlogPost.Authors", "Authors"), + Member::get()->map()->toArray() + )->setMultiple(true), + FieldGroup::create( + LabelField::create(null, 'Add New Author')->addExtraClass("new-author-label left"), + TextField::create( + 'AuthorFirstName', + '' + ) + ->setAttribute('placeholder', 'First name') + ->addExtraClass("new-author-first-name"), + TextField::create( + 'AuthorLastName', + '' + ) + ->setAttribute('placeholder', 'Last name') + ->addExtraClass("new-author-first-name") + )->addExtraClass("field") )->setTitle('Post Options'); $publishDate->getDateField()->setConfig("showcalendar", true); @@ -123,6 +144,18 @@ class BlogPost extends Page { **/ protected function onBeforeWrite() { parent::onBeforeWrite(); + + if(isset($this->AuthorFirstName) or isset($this->AuthorLastName)) { + $author = new Member(); + + if(isset($this->AuthorFirstName)) $author->FirstName = $this->AuthorFirstName; + if(isset($this->AuthorLastName)) $author->LastName = $this->AuthorLastName; + + $author->write(); + + $this->Authors()->add($author); + } + if(!$this->PublishDate) $this->setCastedField("PublishDate", time()); } @@ -166,7 +199,7 @@ 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); @@ -187,9 +220,9 @@ class BlogPost extends Page { 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") ); } @@ -217,9 +250,9 @@ class BlogPost extends Page { * * @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; } @@ -235,5 +268,5 @@ class BlogPost extends Page { * @author Michael Strong **/ class BlogPost_Controller extends Page_Controller { - + } diff --git a/css/cms.css b/css/cms.css index 89bed44..9f4dcbc 100755 --- a/css/cms.css +++ b/css/cms.css @@ -97,3 +97,15 @@ .blog-admin-sidebar.cms-content-tools .cms-panel-content { width: auto; } + +.new-author-label { + width: 249px !important; +} + +.new-author-first-name { + margin-right: 5px !important; +} + +.new-author-first-name, .new-author-last-name { + width: 115px !important; +}