2007-09-07 00:33:58 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class TagCloudWidget extends Widget {
|
|
|
|
static $db = array(
|
2008-04-22 03:20:59 +02:00
|
|
|
"Title" => "Varchar",
|
|
|
|
"Limit" => "Int",
|
|
|
|
"Sortby" => "Varchar"
|
|
|
|
);
|
2007-09-07 00:33:58 +02:00
|
|
|
|
2009-01-20 05:01:45 +01:00
|
|
|
static $has_one = array();
|
|
|
|
|
|
|
|
static $has_many = array();
|
|
|
|
|
|
|
|
static $many_many = array();
|
|
|
|
|
|
|
|
static $belongs_many_many = array();
|
|
|
|
|
2007-09-07 00:33:58 +02:00
|
|
|
static $defaults = array(
|
2008-04-09 01:22:08 +02:00
|
|
|
"Title" => "Tag Cloud",
|
2007-09-07 00:33:58 +02:00
|
|
|
"Limit" => "0",
|
|
|
|
"Sortby" => "alphabet"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $cmsTitle = "Tag Cloud";
|
|
|
|
static $description = "Shows a tag cloud of tags on your blog.";
|
|
|
|
|
|
|
|
function getCMSFields() {
|
|
|
|
return new FieldSet(
|
2008-04-09 01:22:08 +02:00
|
|
|
new TextField("Title", _t("TagCloudWidget.TILE", "Title")),
|
2007-12-18 02:41:51 +01:00
|
|
|
new TextField("Limit", _t("TagCloudWidget.LIMIT", "Limit number of tags")),
|
|
|
|
new OptionsetField("Sortby",_t("TagCloudWidget.SORTBY","Sort by"),array("alphabet"=>_t("TagCloudWidget.SBAL", "alphabet"),"frequency"=>_t("TagCloudWidget.SBFREQ", "frequency")))
|
2007-09-07 00:33:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-04-09 01:22:08 +02:00
|
|
|
function Title() {
|
|
|
|
return $this->Title ? $this->Title : 'Tag Cloud';
|
|
|
|
}
|
|
|
|
|
2008-07-29 06:17:34 +02:00
|
|
|
function TagsCollection() {
|
2007-09-07 00:33:58 +02:00
|
|
|
Requirements::css("blog/css/tagcloud.css");
|
|
|
|
|
|
|
|
$allTags = array();
|
|
|
|
$max = 0;
|
2009-05-01 00:05:54 +02:00
|
|
|
$container = BlogTree::current();
|
2007-09-07 00:33:58 +02:00
|
|
|
|
2009-05-01 00:05:54 +02:00
|
|
|
$entries = $container->Entries();
|
2007-09-07 00:33:58 +02:00
|
|
|
|
|
|
|
if($entries) {
|
|
|
|
foreach($entries as $entry) {
|
2008-12-04 22:34:49 +01:00
|
|
|
$theseTags = split(" *, *", strtolower(trim($entry->Tags)));
|
2007-09-07 00:33:58 +02:00
|
|
|
foreach($theseTags as $tag) {
|
2008-12-04 22:34:49 +01:00
|
|
|
if($tag != "") {
|
|
|
|
$allTags[$tag] = isset($allTags[$tag]) ? $allTags[$tag] + 1 : 1; //getting the count into key => value map
|
|
|
|
$max = ($allTags[$tag] > $max) ? $allTags[$tag] : $max;
|
|
|
|
}
|
2007-09-07 00:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-22 02:30:40 +02:00
|
|
|
|
2007-09-07 00:33:58 +02:00
|
|
|
if($allTags) {
|
|
|
|
//TODO: move some or all of the sorts to the database for more efficiency
|
|
|
|
if($this->Limit > 0){
|
2008-12-16 05:49:52 +01:00
|
|
|
uasort($allTags, array($this, "column_sort_by_popularity")); //sort by popularity
|
2007-09-07 00:33:58 +02:00
|
|
|
$allTags = array_slice($allTags, 0, $this->Limit);
|
|
|
|
}
|
|
|
|
if($this->Sortby == "alphabet"){
|
2008-12-16 05:49:52 +01:00
|
|
|
$this->natksort($allTags);
|
2007-09-07 00:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$sizes = array();
|
|
|
|
foreach($allTags as $tag => $count){
|
|
|
|
$sizes[$count] = true;
|
|
|
|
}
|
|
|
|
$numsizes = count($sizes)-1; //Work out the number of different sizes
|
|
|
|
if($numsizes > 5){$numsizes = 5;}
|
|
|
|
foreach($allTags as $tag => $count) {
|
2008-04-22 02:30:40 +02:00
|
|
|
|
2007-09-07 00:33:58 +02:00
|
|
|
$popularity = floor($count / $max * $numsizes);
|
|
|
|
|
|
|
|
switch($popularity) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$allTags[$tag] = array(
|
|
|
|
"Tag" => $tag,
|
|
|
|
"Count" => $count,
|
|
|
|
"Class" => $class,
|
2009-05-01 00:05:54 +02:00
|
|
|
"Link" => $container->Link() . 'tag/' . urlencode($tag)
|
2007-09-07 00:33:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = new DataObjectSet();
|
|
|
|
foreach($allTags as $tag => $fields) {
|
|
|
|
$output->push(new ArrayData($fields));
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2008-12-16 05:49:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to compare 2 Vars to work out the results.
|
|
|
|
* @param mixed
|
|
|
|
* @param mixed
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function column_sort_by_popularity($a, $b){
|
|
|
|
if($a == $b) {
|
|
|
|
$result = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$result = $b - $a;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function natksort(&$aToBeSorted) {
|
|
|
|
$aResult = array();
|
|
|
|
$aKeys = array_keys($aToBeSorted);
|
|
|
|
natcasesort($aKeys);
|
|
|
|
foreach ($aKeys as $sKey) {
|
|
|
|
$aResult[$sKey] = $aToBeSorted[$sKey];
|
|
|
|
}
|
|
|
|
$aToBeSorted = $aResult;
|
2007-09-07 00:33:58 +02:00
|
|
|
|
2008-12-16 05:49:52 +01:00
|
|
|
return true;
|
2007-09-07 00:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-18 03:10:44 +01:00
|
|
|
?>
|