|
1 <?php |
|
2 |
|
3 /* |
|
4 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
5 * Version 1.0.1 (Loch Ness) |
|
6 * Copyright (C) 2006-2007 Dan Fuhry |
|
7 * |
|
8 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 */ |
|
14 |
|
15 /** |
|
16 * Class for formatting and displaying tag clouds. Loosely based the reference cloud engine from <http://www.lotsofcode.com/php/tutorials/tag-cloud>. |
|
17 * @package Enano |
|
18 * @subpackage Presentation/UI |
|
19 * @copyright (C) 2007 Dan Fuhry |
|
20 * @license GNU General Public License, version 2 or at your option any later versionc |
|
21 */ |
|
22 |
|
23 class TagCloud |
|
24 { |
|
25 |
|
26 /** |
|
27 * The list of words in the cloud. |
|
28 * @var array |
|
29 */ |
|
30 |
|
31 var $words = array(); |
|
32 |
|
33 /** |
|
34 * Constructor. |
|
35 * @param array Optional. An initial list of words, just a plain old array. |
|
36 */ |
|
37 |
|
38 function __construct($words) |
|
39 { |
|
40 if ( count($words) > 0 ) |
|
41 { |
|
42 foreach ( $words as $word ) |
|
43 $this->add_word($word); |
|
44 } |
|
45 } |
|
46 |
|
47 /** |
|
48 * Adds a word into the word list. |
|
49 * @param string The word to add |
|
50 */ |
|
51 |
|
52 function add_word($word) |
|
53 { |
|
54 $word = strtolower($word); |
|
55 |
|
56 if ( isset($this->words[$word]) ) |
|
57 $this->words[$word] += 1; |
|
58 else |
|
59 $this->words[$word] = 1; |
|
60 } |
|
61 |
|
62 /** |
|
63 * Returns the total size of the cloud. |
|
64 * @return int |
|
65 */ |
|
66 |
|
67 function get_cloud_size() |
|
68 { |
|
69 return array_sum($this->words); |
|
70 } |
|
71 |
|
72 /** |
|
73 * Shuffles the cloud. |
|
74 */ |
|
75 |
|
76 function shuffle_cloud() |
|
77 { |
|
78 $keys = array_keys($this->words); |
|
79 if ( !$keys || empty($keys) || !is_array($keys) ) |
|
80 return null; |
|
81 |
|
82 shuffle($keys); |
|
83 if ( !$keys || empty($keys) || !is_array($keys) ) |
|
84 return null; |
|
85 |
|
86 $temp = $this->words; |
|
87 $this->words = array(); |
|
88 foreach ( $keys as $word ) |
|
89 { |
|
90 $this->words[$word] = $temp[$word]; |
|
91 } |
|
92 |
|
93 unset($temp); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Returns the popularity index (scale class) for a 1-100 number. |
|
98 * @param int |
|
99 * @return int |
|
100 */ |
|
101 |
|
102 function get_scale_class($val) |
|
103 { |
|
104 $ret = 0; |
|
105 if ( $val >= 99 ) |
|
106 $ret = 1; |
|
107 else if ( $val >= 70 ) |
|
108 $ret = 2; |
|
109 else if ( $val >= 60 ) |
|
110 $ret = 3; |
|
111 else if ( $val >= 50 ) |
|
112 $ret = 4; |
|
113 else if ( $val >= 40 ) |
|
114 $ret = 5; |
|
115 else if ( $val >= 30 ) |
|
116 $ret = 6; |
|
117 else if ( $val >= 20 ) |
|
118 $ret = 7; |
|
119 else if ( $val >= 10 ) |
|
120 $ret = 8; |
|
121 else if ( $val >= 5 ) |
|
122 $ret = 9; |
|
123 return $ret; |
|
124 } |
|
125 |
|
126 /** |
|
127 * Generates and returns HTML for the cloud. |
|
128 * @return string |
|
129 */ |
|
130 |
|
131 function make_html() |
|
132 { |
|
133 $html = array(); |
|
134 $max = max($this->words); |
|
135 $size = $this->get_cloud_size(); |
|
136 if ( count($this->words) > 0 ) |
|
137 { |
|
138 foreach ( $this->words as $word => $popularity ) |
|
139 { |
|
140 $word = htmlspecialchars($word); |
|
141 $percent = ( $popularity / $max ) * 100; |
|
142 $index = $this->get_scale_class($percent); |
|
143 $html[] = "<span class='tc_word tc_index_$index'>$word</span>"; |
|
144 } |
|
145 } |
|
146 $html = implode("\n", $html); |
|
147 return $html; |
|
148 } |
|
149 |
|
150 |
|
151 } |
|
152 |
|
153 ?> |