From 4615b7ce8d9e200820724df8817d21ba89098683 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Mon, 6 Nov 2017 16:36:47 +1100 Subject: [PATCH 1/2] Added a Blog controller action to redirect by post URL segment --- src/Pages/BlogController.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Pages/BlogController.php b/src/Pages/BlogController.php index b48e7da..6387a48 100644 --- a/src/Pages/BlogController.php +++ b/src/Pages/BlogController.php @@ -40,7 +40,8 @@ class BlogController extends PageController * @config */ private static $url_handlers = [ - 'tag/$tag!' => 'tag' + 'tag/$Tag!' => 'tag', + '$Post' => 'index' ]; /** @@ -54,6 +55,26 @@ class BlogController extends PageController 'tag' ]; + /** + * Default action for this controller, either render the blog or redirect to an existing post. + * + * @param HTTPRequest $request + * + * @return DBHTMLText|HTTPResponse + */ + public function index(HTTPRequest $request) + { + if ($segment = $request->param('Post')) { + + if ($post = BlogPost::get()->find('URLSegment', $segment)) { + return $this->redirect($post->Link()); + } + + } + + return $this->render(); + } + /** * Renders a list of the latest blog posts as an RSS feed. * @@ -94,7 +115,7 @@ public function tag(HTTPRequest $request) { // Obtain Tag Segment: - if ($segment = $request->param('tag')) { + if ($segment = $request->param('Tag')) { // Obtain Tag Object: From b468d5b9393f9e2e294c8517ee7e0dc3ee4a58e7 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Mon, 6 Nov 2017 17:13:46 +1100 Subject: [PATCH 2/2] Added options for tags/category in blog post list --- client/dist/styles/bundle.css | 2 +- client/src/styles/_styles.scss | 60 ++++++++++++++++++---------------- src/Pages/Blog.php | 21 ++++++++++++ src/Pages/BlogController.php | 10 ++++++ src/Pages/BlogPost.php | 45 ++++++++++++++++++++++++- 5 files changed, 108 insertions(+), 30 deletions(-) diff --git a/client/dist/styles/bundle.css b/client/dist/styles/bundle.css index 0f23701..ebb2434 100644 --- a/client/dist/styles/bundle.css +++ b/client/dist/styles/bundle.css @@ -1 +1 @@ -.blogpost .tags{padding-top:1rem;border-top:1px solid #eee}.blogpost .tags .with{color:#888;display:block;margin-bottom:2px}.blogpost .tags ul{padding:0;font-size:0;list-style:none}.blogpost .tags ul li{margin-left:2px;margin-right:5px;font-size:1rem;display:inline-block}.blogpost .tags ul li:before{color:#ccc;content:"\F02B";font-family:FontAwesome}.blogpost .tags ul li a{margin-left:3px;display:inline-block} \ No newline at end of file +.blogpost article .tags{padding-top:1rem;border-top:1px solid #eee}.blogpost article .tags .with{color:#888;display:block;margin-bottom:2px}.blogpost article .tags ul{padding:0;font-size:0;list-style:none}.blogpost article .tags ul li{margin-left:2px;margin-right:5px;font-size:1rem;display:inline-block}.blogpost article .tags ul li:before{color:#ccc;content:"\F02B";font-family:FontAwesome}.blogpost article .tags ul li a{margin-left:3px;display:inline-block} \ No newline at end of file diff --git a/client/src/styles/_styles.scss b/client/src/styles/_styles.scss index 5eff1e1..0e6dde4 100644 --- a/client/src/styles/_styles.scss +++ b/client/src/styles/_styles.scss @@ -3,40 +3,44 @@ .blogpost { - .tags { + article { - padding-top: 1rem; - border-top: 1px solid #eee; - - .with { - color: #888; - display: block; - margin-bottom: 2px; - } - - ul { + .tags { - padding: 0; - font-size: 0; - list-style: none; + padding-top: 1rem; + border-top: 1px solid #eee; - li { - - margin-left: 2px; - margin-right: 5px; - - font-size: 1rem; - display: inline-block; + .with { + color: #888; + display: block; + margin-bottom: 2px; + } + + ul { - &:before { - color: #ccc; - content: "\f02b"; - font-family: 'FontAwesome'; - } + padding: 0; + font-size: 0; + list-style: none; - a { - margin-left: 3px; + li { + + margin-left: 2px; + margin-right: 5px; + + font-size: 1rem; display: inline-block; + + &:before { + color: #ccc; + content: "\f02b"; + font-family: 'FontAwesome'; + } + + a { + margin-left: 3px; + display: inline-block; + } + } } diff --git a/src/Pages/Blog.php b/src/Pages/Blog.php index 5d9a190..9914c38 100644 --- a/src/Pages/Blog.php +++ b/src/Pages/Blog.php @@ -94,6 +94,8 @@ class Blog extends Page implements ListSource, TagSource * @config */ private static $db = [ + 'ShowCategoryInList' => 'Boolean', + 'ShowTagsInList' => 'Boolean', 'FeedTitle' => 'Varchar(255)', 'FeedDescription' => 'Varchar(255)', 'FeedNumberOfPosts' => 'Int', @@ -107,6 +109,8 @@ class Blog extends Page implements ListSource, TagSource * @config */ private static $defaults = [ + 'ShowCategoryInList' => 1, + 'ShowTagsInList' => 0, 'FeedEnabled' => 1, 'FeedNumberOfPosts' => 10 ]; @@ -159,6 +163,20 @@ public function getCMSFields() $fields->addFieldsToTab( 'Root.Options', [ + FieldSection::create( + 'PostOptions', + $this->fieldLabel('PostOptions'), + [ + CheckboxField::create( + 'ShowCategoryInList', + $this->fieldLabel('ShowCategoryInList') + ), + CheckboxField::create( + 'ShowTagsInList', + $this->fieldLabel('ShowTagsInList') + ) + ] + ), FieldSection::create( 'FeedOptions', $this->fieldLabel('FeedOptions'), @@ -208,9 +226,12 @@ public function fieldLabels($includerelations = true) $labels['FeedTitle'] = _t(__CLASS__ . '.FEEDTITLE', 'Title'); $labels['FeedOptions'] = _t(__CLASS__ . '.FEED', 'Feed'); + $labels['PostOptions'] = _t(__CLASS__ . '.POSTS', 'Posts'); $labels['FeedEnabled'] = _t(__CLASS__ . '.FEEDENABLED', 'Feed enabled'); $labels['FeedDescription'] = _t(__CLASS__ . '.DESCRIPTION', 'Description'); $labels['FeedNumberOfPosts'] = _t(__CLASS__ . '.NUMBEROFPOSTS', 'Number of posts'); + $labels['ShowCategoryInList'] = _t(__CLASS__ . '.SHOWCATEGORYINLIST', 'Show category in list'); + $labels['ShowTagsInList'] = _t(__CLASS__ . '.SHOWTAGSINLIST', 'Show tags in list'); // Answer Field Labels: diff --git a/src/Pages/BlogController.php b/src/Pages/BlogController.php index 6387a48..7ab3d14 100644 --- a/src/Pages/BlogController.php +++ b/src/Pages/BlogController.php @@ -142,6 +142,16 @@ public function tag(HTTPRequest $request) return $this->httpError(404); } + /** + * Answers the associated blog record. + * + * @return Blog + */ + public function getBlog() + { + return $this->data(); + } + /** * Performs initialisation before any action is called on the receiver. * diff --git a/src/Pages/BlogPost.php b/src/Pages/BlogPost.php index 293f490..073f073 100644 --- a/src/Pages/BlogPost.php +++ b/src/Pages/BlogPost.php @@ -119,6 +119,7 @@ class BlogPost extends Page * @config */ private static $casting = [ + 'TagsHTML' => 'HTMLFragment', 'CategoryLink' => 'HTMLFragment' ]; @@ -165,7 +166,13 @@ class BlogPost extends Page private static $list_item_details = [ 'category' => [ 'icon' => 'folder-o', - 'text' => '$CategoryLink' + 'text' => '$CategoryLink', + 'show' => 'ShowCategoryInList' + ], + 'tags' => [ + 'icon' => 'tag', + 'text' => '$TagsHTML', + 'show' => 'ShowTagsInList' ] ]; @@ -320,6 +327,42 @@ public function getCategoryLink() ); } + /** + * Answers a string of HTML containing the tags for the blog post. + * + * @return string + */ + public function getTagsHTML() + { + $output = []; + + foreach ($this->Tags() as $tag) { + $output[] = sprintf('%s', $tag->Link, $tag->Title); + } + + return implode(', ', $output); + } + + /** + * Answers true if the category is to be shown in the list. + * + * @return boolean + */ + public function getShowCategoryInList() + { + return (boolean) $this->getBlog()->ShowCategoryInList; + } + + /** + * Answers true if the tags are to be shown in the list. + * + * @return boolean + */ + public function getShowTagsInList() + { + return (boolean) $this->getBlog()->ShowTagsInList; + } + /** * Answers the parent blog of the receiver. *