ENHANCEMENT: Addition of tag cloud widget complete with tests

This commit is contained in:
Gordon Anderson 2015-05-19 07:35:35 +07:00
parent 694b41176a
commit 15b197c284
8 changed files with 349 additions and 17 deletions

View File

@ -46,7 +46,7 @@ before_script:
- phpenv rehash
- composer self-update || true
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require "silverstripe/comments" --require "silverstripe/widgets"
- cd ~/builds/ss
#Execute tests with or without coverage

View File

@ -0,0 +1,108 @@
<?php
if (!class_exists('Widget')) {
return;
}
/**
* @method Blog Blog()
*/
class BlogTagsCloudWidget extends Widget
{
/**
* @var string
*/
private static $title = 'Tags Cloud';
/**
* @var string
*/
private static $cmsTitle = 'Blog Tags Cloud';
/**
* @var string
*/
private static $description = 'Displays a tag cloud for this blog.';
/**
* @var array
*/
private static $db = array();
/**
* @var array
*/
private static $has_one = array(
'Blog' => 'Blog',
);
/**
* {@inheritdoc}
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function ($fields) {
/*
* @var FieldList $fields
*/
$fields->push(
DropdownField::create('BlogID', _t('BlogTagsCloudWidget.Blog',
'Blog'), Blog::get()->map())
);
});
return parent::getCMSFields();
}
/**
* @return array
*/
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 = new ArrayList();
foreach ($records as $record) {
$tag = new DataObject();
$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
$tagfactor = 10 / $maxTagCount;
foreach ($tags->getIterator() as $tag) {
$normalized = round($tagfactor * ($tag->TagCount));
$tag->NormalizedTag = $normalized;
}
return $tags;
}
return array();
}
}
class BlogTagsCloudWidget_Controller extends Widget_Controller
{
}

View File

@ -12,3 +12,47 @@
.blog-sidebar .WidgetHolder ul li {
list-style-type: none;
}
ul.blogTagCloud {
list-style-type: none;
clear: both;
}
ul.blogTagCloud li {
float: left;
display: inline;
padding-right: 8px;
}
ul.blogTagCloud li a span {
float: left;
line-height: 30px; text-align: center;
padding: 0px;
}
ul.blogTagCloud .tagCount10 {
font-size: 26pt;
}
ul.blogTagCloud .tagCount9 {
font-size: 24pt;
}
ul.blogTagCloud .tagCount8 {
font-size: 22pt;
}
ul.blogTagCloud .tagCount7 {
font-size: 20pt;
}
ul.blogTagCloud .tagCount6 {
font-size: 18pt;
}
ul.blogTagCloud .tagCount5 {
font-size: 16pt;
}
ul.blogTagCloud .tagCount4 {
font-size: 14pt;
}
ul.blogTagCloud .tagCount3 {
font-size: 12pt;
}
ul.blogTagCloud .tagCount2 {
font-size: 10pt;
}
ul.blogTagCloud .tagCount1 {
font-size: 8pt;
}

View File

@ -1,15 +1,75 @@
.no-sidebar .content-container.size3of4 {
width: 75%;
}
.blog-entry .post-image img {
width: 98.75%;
}
.blog-sidebar .WidgetHolder ul {
margin-left: 0;
li {
list-style-type: none;
}
}
.no-sidebar .content-container.size3of4 {
width: 75%;
}
.blog-entry .post-image img {
width: 98.75%;
}
.blog-sidebar .WidgetHolder ul {
margin-left: 0;
li {
list-style-type: none;
}
}
// tag cloud related
$baseTagFontSize:4pt;
ul.blogTagCloud {
list-style-type: none;
clear: both;
li {
float: left;
display: inline;
padding-right: 8px;
a span {
float: left;
line-height: 30px; text-align: center;
padding: 0px;
}
}
.tagCount10 {
font-size: $baseTagFontSize+22pt;
}
.tagCount9 {
font-size: $baseTagFontSize+20pt;
}
.tagCount8 {
font-size: $baseTagFontSize+18pt;
}
.tagCount7 {
font-size: $baseTagFontSize+16pt;
}
.tagCount6 {
font-size: $baseTagFontSize+14pt;
}
.tagCount5 {
font-size: $baseTagFontSize+12pt;
}
.tagCount4 {
font-size: $baseTagFontSize+10pt;
}
.tagCount3 {
font-size: $baseTagFontSize+8pt;
}
.tagCount2 {
font-size: $baseTagFontSize+6pt;
}
.tagCount1 {
font-size: $baseTagFontSize+4pt;
}
}

View File

@ -0,0 +1,12 @@
<% if $Tags %>
<ul class="blogTagCloud">
<% loop $Tags %>
<li>
<a href="$Link" title="$TagName">
<span class="text tagCount{$NormalizedTag}">$TagName</span>
</a>
</li>
<% end_loop %>
</ul>
<div style="clear: both;"></div>
<% end_if %>

View File

@ -49,7 +49,7 @@ class BlogCategoryTest extends FunctionalTest
*/
$category = $this->objFromFixture('BlogCategory', 'FirstCategory');
$this->assertEquals(1, $category->BlogPosts()->count(), 'Category blog post count');
$this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count');
}
public function testCanView()

View File

@ -0,0 +1,60 @@
<?php
class BlogTagsCloudWidgetTest extends SapphireTest {
/**
* @var string
*/
public static $fixture_file = 'blog.yml';
public function testGetCMSFields() {
if (!class_exists('Widget')) {
$this->markTestSkipped('Widgets module not installed');
}
$widget = new BlogTagsCloudWidget();
$fields = $widget->getCMSFields();
$names = array();
foreach ($fields as $field) {
array_push($names, $field->getName());
}
$expected = array('Title', 'Enabled', 'BlogID');
$this->assertEquals($expected, $names);
}
public function testGetTags() {
if (!class_exists('Widget')) {
$this->markTestSkipped('Widgets module not installed');
}
$widget = new BlogTagsCloudWidget();
$blog = $this->objFromFixture('Blog', 'FourthBlog');
$widget->BlogID = $blog->ID;
$widget->write();
$tags = $widget->getTags()->toArray();
$tag = $tags[0];
$this->assertEquals('Cat', $tag->TagName);
$this->assertEquals('/fourth-blog/tag/cat', $tag->Link);
$this->assertEquals(2, $tag->TagCount);
$this->assertEquals(5, $tag->NormalizedTag);
$tag = $tags[1];
$this->assertEquals('Cool', $tag->TagName);
$this->assertEquals('/fourth-blog/tag/cool', $tag->Link);
$this->assertEquals(3, $tag->TagCount);
$this->assertEquals(8, $tag->NormalizedTag);
$tag = $tags[2];
$this->assertEquals('Kiwi', $tag->TagName);
$this->assertEquals('/fourth-blog/tag/kiwi', $tag->Link);
$this->assertEquals(1, $tag->TagCount);
$this->assertEquals(3, $tag->NormalizedTag);
$tag = $tags[3];
$this->assertEquals('Popular', $tag->TagName);
$this->assertEquals('/fourth-blog/tag/popular', $tag->Link);
$this->assertEquals(4, $tag->TagCount);
$this->assertEquals(10, $tag->NormalizedTag);
}
}

View File

@ -85,6 +85,24 @@ BlogTag:
URLSegment: 'third-tag'
Blog: =>Blog.ThirdBlog
#Tags for Tag Cloud widget
PopularTag:
Title: 'Popular'
URLSegment: 'popular-tag'
Blog: =>Blog.FourthBlog
CoolTag:
Title: 'Cool'
URLSegment: 'cool-tag'
Blog: =>Blog.FourthBlog
CatTag:
Title: 'Cat'
URLSegment: 'cat-tag'
Blog: =>Blog.FourthBlog
KiwiTag:
Title: 'Kiwi'
URLSegment: 'kiwi-tag'
Blog: =>Blog.FourthBlog
BlogCategory:
FirstCategory:
Title: 'First Category'
@ -146,3 +164,33 @@ BlogPost:
PublishDate: '2012-01-09 15:00:00'
Parent: =>Blog.FourthBlog
Authors: =>Member.BlogEditor,=>Member.Writer,=>Member.Contributor
#Posts for the tag cloud widget test
TaggedPost1:
Title: 'Tagged Post 1'
URLSegment: tagged-post-1
PublishDate: '2012-01-09 15:00:00'
Tags: =>BlogTag.PopularTag,=>BlogTag.CoolTag
Categories: =>BlogCategory.FirstCategory
Parent: =>Blog.FourthBlog
TaggedPost2:
Title: 'Tagged Post 2'
URLSegment: tagged-post-2
PublishDate: '2012-01-09 15:00:00'
Tags: =>BlogTag.PopularTag,=>BlogTag.CoolTag,=>BlogTag.CatTag
Categories: =>BlogCategory.FirstCategory
Parent: =>Blog.FourthBlog
TaggedPost3:
Title: 'Tagged Post 3'
URLSegment: tagged-post-3
PublishDate: '2012-01-09 17:20:00'
Tags: =>BlogTag.PopularTag,=>BlogTag.CoolTag,=>BlogTag.CatTag,=>BlogTag.KiwiTag
Categories: =>BlogCategory.FirstCategory
Parent: =>Blog.FourthBlog
TaggedPost4:
Title: 'Tagged Post 4'
URLSegment: tagged-post-4
PublishDate: '2012-04-09 15:00:00'
Tags: =>BlogTag.PopularTag
Categories: =>BlogCategory.FirstCategory
Parent: =>Blog.FourthBlog