-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagcloud.module
More file actions
62 lines (49 loc) · 1.71 KB
/
Copy pathTagcloud.module
File metadata and controls
62 lines (49 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class Tagcloud extends WireData implements Module {
public static $defaultOptions = array (
'minSize' => 0.8,
'maxSize' => 1.8,
'unit' => 'em',
);
public static function getModuleInfo () {
return array(
'title' => 'Tagcloud',
'version' => 001,
'summary' => 'Renders a tagcloud',
'singular' => false,
'autoload' => false
);
}
public function init () {
/* Maybe we could cache the tag cloud between page edits
* Need to look at hooks for that.
*/
}
public function render(Array $options = null, $tagroot = "/tags") {
if ($options != null && is_array($options)) {
$options = array_merge(self::$defaultOptions, $options);
} else {
$options = self::$defaultOptions;
}
$diffFontSize = $options['maxSize'] - $options['minSize'];
$tags = $this->pages->get($tagroot)->children;
$items = array();
$min = 1e9;
$max = -1e9;
foreach ($tags as $tag) {
$count = $this->pages->count('tags=' . $tag);
$min = min ($min, $count);
$max = max ($max, $count);
$items[$tag->title] = array("count" => $count, "url" => $tag->url);
}
$diff = log($max) - log($min);
foreach ($items as $tag => $values) {
$count = $values["count"];
$url = $values["url"];
$relative = (log($count) - log($min)) / $diff;
$size = $options['minSize'] + $relative * $diffFontSize;
echo "<a class=\"tag\" href=\"{$url}\" style=\"font-size: {$size}{$options['unit']}\">{$tag}</a> ";
}
}
}
?>