From 4da2e164eee170308a08a6f8ef5d298049001450 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 15 Oct 2019 17:37:06 +1300 Subject: [PATCH] Fix up tag cloud --- src/Admin/GridFieldCategorisationConfig.php | 11 +-- src/Model/Blog.php | 40 +++++------ src/Model/BlogObject.php | 18 +++++ src/Model/CategorisationObject.php | 7 +- src/Widgets/BlogTagsCloudWidget.php | 80 +++++++++------------ 5 files changed, 78 insertions(+), 78 deletions(-) diff --git a/src/Admin/GridFieldCategorisationConfig.php b/src/Admin/GridFieldCategorisationConfig.php index 6e9de8d..f2c5def 100644 --- a/src/Admin/GridFieldCategorisationConfig.php +++ b/src/Admin/GridFieldCategorisationConfig.php @@ -4,7 +4,6 @@ namespace SilverStripe\Blog\Admin; use SilverStripe\Blog\Forms\GridField\GridFieldAddByDBField; use SilverStripe\Blog\Model\CategorisationObject; -use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; use SilverStripe\Forms\GridField\GridFieldDataColumns; @@ -18,9 +17,8 @@ class GridFieldCategorisationConfig extends GridFieldConfig_RecordEditor * @param string $parentType * @param string $parentMethod * @param string $childMethod - * @param SiteTree $parent */ - public function __construct($itemsPerPage, $mergeRecords, $parentType, $parentMethod, $childMethod, $parent) + public function __construct($itemsPerPage, $mergeRecords, $parentType, $parentMethod, $childMethod) { parent::__construct($itemsPerPage); @@ -41,11 +39,8 @@ class GridFieldCategorisationConfig extends GridFieldConfig_RecordEditor $columns->setFieldFormatting( [ - 'BlogPostsCount' => function ($value, CategorisationObject $item) use ($parent) { - return $item - ->BlogPosts() - ->filter(['ParentID' => $parent->ID]) - ->Count(); + 'BlogPostsCount' => function ($value, CategorisationObject $item) { + return $item->getBlogCount(); }, 'BlogPostsAllCount' => function ($value, CategorisationObject $item) { return $item->BlogPosts()->Count(); diff --git a/src/Model/Blog.php b/src/Model/Blog.php index eaf33a3..9c4cda0 100644 --- a/src/Model/Blog.php +++ b/src/Model/Blog.php @@ -135,17 +135,15 @@ class Blog extends Page implements PermissionProvider */ public function Tags($hideEmpty = true) { - $tags = BlogTag::get(); - if ($this->ID) { - $tags->setDataQueryParam('BlogID', $this->ID); + $tags = BlogTag::get()->setDataQueryParam('BlogID', $this->ID); - // Conditionally hide empty tags - if ($hideEmpty) { - $tags = $tags->filter([ - 'BlogPosts.ParentID' => $this->ID, - ]); - } + // Conditionally hide empty tags + if ($this->ID && $hideEmpty) { + $tags = $tags->filter([ + 'BlogPosts.ParentID' => $this->ID, + ]); } + $this->extend('updateBlogTags', $tags); return $tags; } @@ -158,17 +156,15 @@ class Blog extends Page implements PermissionProvider */ public function Categories($hideEmpty = true) { - $tags = BlogCategory::get(); - if ($this->ID) { - $tags->setDataQueryParam('BlogID', $this->ID); + $tags = BlogCategory::get()->setDataQueryParam('BlogID', $this->ID); - // Conditionally hide empty categories - if ($hideEmpty) { - $tags = $tags->filter([ - 'BlogPosts.ParentID' => $this->ID, - ]); - } + // Conditionally hide empty categories + if ($this->ID && $hideEmpty) { + $tags = $tags->filter([ + 'BlogPosts.ParentID' => $this->ID, + ]); } + $this->extend('updateBlogCategories', $tags); return $tags; } @@ -184,7 +180,7 @@ class Blog extends Page implements PermissionProvider if (!$this->canEdit()) { return; } - + $categories = GridField::create( 'Categories', _t(__CLASS__ . '.Categories', 'Categories'), @@ -194,8 +190,7 @@ class Blog extends Page implements PermissionProvider $this->Categories(false)->sort('Title'), BlogCategory::class, 'Categories', - 'BlogPosts', - $this + 'BlogPosts' ) ); @@ -208,8 +203,7 @@ class Blog extends Page implements PermissionProvider $this->Tags(false)->sort('Title'), BlogTag::class, 'Tags', - 'BlogPosts', - $this + 'BlogPosts' ) ); diff --git a/src/Model/BlogObject.php b/src/Model/BlogObject.php index 866840e..30c9b2a 100644 --- a/src/Model/BlogObject.php +++ b/src/Model/BlogObject.php @@ -67,6 +67,24 @@ trait BlogObject return $fields; } + /** + * Number of times this object has blog posts in the current blog + * + * @return int + */ + public function getBlogCount() + { + $blog = $this->Blog(); + if (!$blog) { + return 0; + } + + return $this + ->BlogPosts() + ->filter(['ParentID' => $blog->ID]) + ->Count(); + } + /** * {@inheritdoc} * @return ValidationResult diff --git a/src/Model/CategorisationObject.php b/src/Model/CategorisationObject.php index 3e29247..ad817b0 100644 --- a/src/Model/CategorisationObject.php +++ b/src/Model/CategorisationObject.php @@ -9,5 +9,10 @@ use SilverStripe\ORM\ManyManyList; */ interface CategorisationObject { - + /** + * Number of times this object has blog posts in the current blog + * + * @return int + */ + public function getBlogCount(); } diff --git a/src/Widgets/BlogTagsCloudWidget.php b/src/Widgets/BlogTagsCloudWidget.php index 4730e7e..c17a580 100644 --- a/src/Widgets/BlogTagsCloudWidget.php +++ b/src/Widgets/BlogTagsCloudWidget.php @@ -3,11 +3,10 @@ namespace SilverStripe\Blog\Widgets; use SilverStripe\Blog\Model\Blog; -use SilverStripe\Core\Convert; +use SilverStripe\Blog\Model\BlogTag; use SilverStripe\Forms\DropdownField; use SilverStripe\ORM\ArrayList; -use SilverStripe\ORM\DataObject; -use SilverStripe\ORM\DB; +use SilverStripe\View\ArrayData; use SilverStripe\Widgets\Model\Widget; if (!class_exists(Widget::class)) { @@ -73,53 +72,42 @@ class BlogTagsCloudWidget extends Widget } /** - * @return array + * @return ArrayList */ public function getTags() { - if ($blog = $this->Blog()) { - $escapedID = Convert::raw2sql($blog->ID); - $sql = 'SELECT DISTINCT "BlogTag"."URLSegment","BlogTag"."Title",Count("BlogTagID") AS "TagCount" - from "BlogPost_Tags" - INNER JOIN "BlogPost" - ON "BlogPost"."ID" = "BlogPost_Tags"."BlogPostID" - INNER JOIN "BlogTag" - ON "BlogTag"."ID" = "BlogPost_Tags"."BlogTagID" - WHERE "BlogID" = ' . $escapedID - . ' GROUP By "BlogTag"."URLSegment","BlogTag"."Title" - ORDER BY "Title"'; - - $records = DB::query($sql); - $bloglink = $blog->Link(); - $maxTagCount = 0; - - // create DataObjects that can be used to render the tag cloud - $tags = ArrayList::create(); - foreach ($records as $record) { - $tag = DataObject::create(); - $tag->TagName = $record['Title']; - $link = $bloglink.'tag/'.$record['URLSegment']; - $tag->Link = $link; - if ($record['TagCount'] > $maxTagCount) { - $maxTagCount = $record['TagCount']; - } - $tag->TagCount = $record['TagCount']; - $tags->push($tag); - } - - // normalize the tag counts from 1 to 10 - if ($maxTagCount) { - $tagfactor = 10 / $maxTagCount; - foreach ($tags->getIterator() as $tag) { - $normalized = round($tagfactor * ($tag->TagCount)); - $tag->NormalizedTag = $normalized; - } - } - - - return $tags; + // Check blog exists + $blog = $this->Blog(); + if (!$blog) { + return ArrayList::create([]); } - return []; + // create ArrayData that can be used to render the tag cloud + $maxTagCount = 0; + $tags = ArrayList::create(); + /** @var BlogTag $record */ + foreach ($blog->Tags() as $record) { + // Remember max count found + $count = $record->getBlogCount(); + $maxTagCount = $maxTagCount > $count ? $maxTagCount : $count; + + // Save + $tags->push(ArrayData::create([ + 'TagName' => $record->Title, + 'Link' => $record->getLink(), + 'TagCount' => $count, + ])); + } + + // normalize the tag counts from 1 to 10 + if ($maxTagCount) { + $tagfactor = 10 / $maxTagCount; + foreach ($tags->getIterator() as $tag) { + $normalized = round($tagfactor * ($tag->TagCount)); + $tag->NormalizedTag = $normalized; + } + } + + return $tags; } }