mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
Fix up tag cloud
This commit is contained in:
parent
d815a4589d
commit
4da2e164ee
@ -4,7 +4,6 @@ namespace SilverStripe\Blog\Admin;
|
|||||||
|
|
||||||
use SilverStripe\Blog\Forms\GridField\GridFieldAddByDBField;
|
use SilverStripe\Blog\Forms\GridField\GridFieldAddByDBField;
|
||||||
use SilverStripe\Blog\Model\CategorisationObject;
|
use SilverStripe\Blog\Model\CategorisationObject;
|
||||||
use SilverStripe\CMS\Model\SiteTree;
|
|
||||||
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
|
||||||
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
|
||||||
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
||||||
@ -18,9 +17,8 @@ class GridFieldCategorisationConfig extends GridFieldConfig_RecordEditor
|
|||||||
* @param string $parentType
|
* @param string $parentType
|
||||||
* @param string $parentMethod
|
* @param string $parentMethod
|
||||||
* @param string $childMethod
|
* @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);
|
parent::__construct($itemsPerPage);
|
||||||
|
|
||||||
@ -41,11 +39,8 @@ class GridFieldCategorisationConfig extends GridFieldConfig_RecordEditor
|
|||||||
|
|
||||||
$columns->setFieldFormatting(
|
$columns->setFieldFormatting(
|
||||||
[
|
[
|
||||||
'BlogPostsCount' => function ($value, CategorisationObject $item) use ($parent) {
|
'BlogPostsCount' => function ($value, CategorisationObject $item) {
|
||||||
return $item
|
return $item->getBlogCount();
|
||||||
->BlogPosts()
|
|
||||||
->filter(['ParentID' => $parent->ID])
|
|
||||||
->Count();
|
|
||||||
},
|
},
|
||||||
'BlogPostsAllCount' => function ($value, CategorisationObject $item) {
|
'BlogPostsAllCount' => function ($value, CategorisationObject $item) {
|
||||||
return $item->BlogPosts()->Count();
|
return $item->BlogPosts()->Count();
|
||||||
|
@ -135,17 +135,15 @@ class Blog extends Page implements PermissionProvider
|
|||||||
*/
|
*/
|
||||||
public function Tags($hideEmpty = true)
|
public function Tags($hideEmpty = true)
|
||||||
{
|
{
|
||||||
$tags = BlogTag::get();
|
$tags = BlogTag::get()->setDataQueryParam('BlogID', $this->ID);
|
||||||
if ($this->ID) {
|
|
||||||
$tags->setDataQueryParam('BlogID', $this->ID);
|
|
||||||
|
|
||||||
// Conditionally hide empty tags
|
// Conditionally hide empty tags
|
||||||
if ($hideEmpty) {
|
if ($this->ID && $hideEmpty) {
|
||||||
$tags = $tags->filter([
|
$tags = $tags->filter([
|
||||||
'BlogPosts.ParentID' => $this->ID,
|
'BlogPosts.ParentID' => $this->ID,
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->extend('updateBlogTags', $tags);
|
$this->extend('updateBlogTags', $tags);
|
||||||
return $tags;
|
return $tags;
|
||||||
}
|
}
|
||||||
@ -158,17 +156,15 @@ class Blog extends Page implements PermissionProvider
|
|||||||
*/
|
*/
|
||||||
public function Categories($hideEmpty = true)
|
public function Categories($hideEmpty = true)
|
||||||
{
|
{
|
||||||
$tags = BlogCategory::get();
|
$tags = BlogCategory::get()->setDataQueryParam('BlogID', $this->ID);
|
||||||
if ($this->ID) {
|
|
||||||
$tags->setDataQueryParam('BlogID', $this->ID);
|
|
||||||
|
|
||||||
// Conditionally hide empty categories
|
// Conditionally hide empty categories
|
||||||
if ($hideEmpty) {
|
if ($this->ID && $hideEmpty) {
|
||||||
$tags = $tags->filter([
|
$tags = $tags->filter([
|
||||||
'BlogPosts.ParentID' => $this->ID,
|
'BlogPosts.ParentID' => $this->ID,
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->extend('updateBlogCategories', $tags);
|
$this->extend('updateBlogCategories', $tags);
|
||||||
return $tags;
|
return $tags;
|
||||||
}
|
}
|
||||||
@ -194,8 +190,7 @@ class Blog extends Page implements PermissionProvider
|
|||||||
$this->Categories(false)->sort('Title'),
|
$this->Categories(false)->sort('Title'),
|
||||||
BlogCategory::class,
|
BlogCategory::class,
|
||||||
'Categories',
|
'Categories',
|
||||||
'BlogPosts',
|
'BlogPosts'
|
||||||
$this
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -208,8 +203,7 @@ class Blog extends Page implements PermissionProvider
|
|||||||
$this->Tags(false)->sort('Title'),
|
$this->Tags(false)->sort('Title'),
|
||||||
BlogTag::class,
|
BlogTag::class,
|
||||||
'Tags',
|
'Tags',
|
||||||
'BlogPosts',
|
'BlogPosts'
|
||||||
$this
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -67,6 +67,24 @@ trait BlogObject
|
|||||||
return $fields;
|
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}
|
* {@inheritdoc}
|
||||||
* @return ValidationResult
|
* @return ValidationResult
|
||||||
|
@ -9,5 +9,10 @@ use SilverStripe\ORM\ManyManyList;
|
|||||||
*/
|
*/
|
||||||
interface CategorisationObject
|
interface CategorisationObject
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Number of times this object has blog posts in the current blog
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getBlogCount();
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,10 @@
|
|||||||
namespace SilverStripe\Blog\Widgets;
|
namespace SilverStripe\Blog\Widgets;
|
||||||
|
|
||||||
use SilverStripe\Blog\Model\Blog;
|
use SilverStripe\Blog\Model\Blog;
|
||||||
use SilverStripe\Core\Convert;
|
use SilverStripe\Blog\Model\BlogTag;
|
||||||
use SilverStripe\Forms\DropdownField;
|
use SilverStripe\Forms\DropdownField;
|
||||||
use SilverStripe\ORM\ArrayList;
|
use SilverStripe\ORM\ArrayList;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\View\ArrayData;
|
||||||
use SilverStripe\ORM\DB;
|
|
||||||
use SilverStripe\Widgets\Model\Widget;
|
use SilverStripe\Widgets\Model\Widget;
|
||||||
|
|
||||||
if (!class_exists(Widget::class)) {
|
if (!class_exists(Widget::class)) {
|
||||||
@ -73,53 +72,42 @@ class BlogTagsCloudWidget extends Widget
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return ArrayList
|
||||||
*/
|
*/
|
||||||
public function getTags()
|
public function getTags()
|
||||||
{
|
{
|
||||||
if ($blog = $this->Blog()) {
|
// Check blog exists
|
||||||
$escapedID = Convert::raw2sql($blog->ID);
|
$blog = $this->Blog();
|
||||||
$sql = 'SELECT DISTINCT "BlogTag"."URLSegment","BlogTag"."Title",Count("BlogTagID") AS "TagCount"
|
if (!$blog) {
|
||||||
from "BlogPost_Tags"
|
return ArrayList::create([]);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user