ENHANCEMENT: Allow specifying the class buckets to assign tags into based on popularity

This commit is contained in:
Hamish Friedlander 2009-05-06 05:02:01 +00:00
parent 2d1eb07478
commit 58775ea256

View File

@ -24,6 +24,8 @@ class TagCloudWidget extends Widget {
static $cmsTitle = "Tag Cloud"; static $cmsTitle = "Tag Cloud";
static $description = "Shows a tag cloud of tags on your blog."; static $description = "Shows a tag cloud of tags on your blog.";
static $popularities = array( 'not-popular', 'not-very-popular', 'somewhat-popular', 'popular', 'very-popular', 'ultra-popular' );
function getCMSFields() { function getCMSFields() {
return new FieldSet( return new FieldSet(
new TextField("Title", _t("TagCloudWidget.TILE", "Title")), new TextField("Title", _t("TagCloudWidget.TILE", "Title")),
@ -67,38 +69,24 @@ class TagCloudWidget extends Widget {
} }
$sizes = array(); $sizes = array();
foreach($allTags as $tag => $count){ foreach ($allTags as $tag => $count) $sizes[$count] = true;
$sizes[$count] = true;
} $offset = 0;
$numsizes = count($sizes)-1; //Work out the number of different sizes $numsizes = count($sizes)-1; //Work out the number of different sizes
if($numsizes > 5){$numsizes = 5;} $buckets = count(self::$popularities)-1;
foreach($allTags as $tag => $count) {
$popularity = floor($count / $max * $numsizes); // If there are more frequencies then buckets, divide frequencies into buckets
if ($numsizes > $buckets) {
switch($popularity) { $numsizes = $buckets;
case 0:
$class = "not-popular";
break;
case 1:
$class = "not-very-popular";
break;
case 2:
$class = "somewhat-popular";
break;
case 3:
$class = "popular";
break;
case 4:
$class = "very-popular";
break;
case 5:
$class = "ultra-popular";
break;
default:
$class = "broken";
break;
} }
// Otherwise center use central buckets
else {
$offset = round(($buckets-$numsizes)/2);
}
foreach($allTags as $tag => $count) {
$popularity = round($count / $max * $numsizes) + $offset; $popularity=min($buckets,$popularity);
$class = self::$popularities[$popularity];
$allTags[$tag] = array( $allTags[$tag] = array(
"Tag" => $tag, "Tag" => $tag,