Fix up tag cloud

This commit is contained in:
Damian Mooyman 2019-10-15 17:37:06 +13:00
parent d815a4589d
commit 4da2e164ee
No known key found for this signature in database
GPG Key ID: 19B1752E86A700BB
5 changed files with 78 additions and 78 deletions

View File

@ -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();

View File

@ -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'
)
);

View File

@ -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

View File

@ -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();
}

View File

@ -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;
}
}